123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- import { HistoryController } from "./history";
- import {BehaviorSubject} from "rxjs";
- import { reactive, toRaw } from "vue";
- export class ValueSnap {
- Id:string;
- Value: any;
- OldValue: any;
- Rx: BehaviorSubject<any>;
- constructor(id:string, value:any, oldValue:any, rx: BehaviorSubject<any>) {
- this.Id = id;
- this.Value = value;
- this.OldValue = oldValue;
- this.Rx = rx
- }
- redo() {
- this.Rx.next({value: this.Value, _hstry:false});
- }
- undo() {
- this.Rx.next({value: this.OldValue, _hstry:false});
- }
- clone() {
- return new ValueSnap(this.Id, this.Value,this.OldValue, this.Rx);
- }
- }
- export type RxValueType<T> = {
- value: T,
- _hstry?: boolean
- }
- function createRxValue<T>(value: T, histry:boolean) {
- return new BehaviorSubject< RxValueType<T> >({value:value, _hstry: histry})
- }
- let _valueIndex = 0;
- export function createValueSnap(value:any, oldValue:any, rx:BehaviorSubject<any>) {
- let i = _valueIndex + 1;
- _valueIndex +=1;
- return new ValueSnap(i+"", value, oldValue, rx);
- }
- class RxValue {
- static create<T extends {[key:string]: any}>(_fields:T, histroy?: HistoryController ) {
- let obj = {} as any;
-
- obj._historySnap = {} as any;
- obj._historySub = {} as any;
- obj._rxs = {} as any;
- obj._fields = _fields;
- obj._history = histroy;
- obj._refs = {} as any;
- const names = Object.keys(_fields);
-
- names.forEach(name=>{
- const currName = name;
- const initValue = _fields[currName]
- const f = createRxValue(initValue, !!histroy);
- obj._rxs[name] = f;
- const snap = createValueSnap(initValue, initValue, f);
- obj._historySnap[name] = snap;
- const rxc = reactive({value: initValue});
- Object.defineProperty(obj, currName, {
- get: function(){
- return rxc.value;
- },
- set: function(v) {
- f.next({value: v});
- },
- configurable: true,
- enumerable: true
- })
- const CamName = currName.slice(0,1).toUpperCase() +currName.slice(1);
-
- obj["set"+CamName] = function(value:T, nohistory = false){
- f.next({value, _hstry: !nohistory});
- }
-
- obj["on"+CamName + "Changed"] = function(subscribe: (value:T, oldValue:T)=>void){
- return f.subscribe((v:any)=>{
- if (CamName == "Transform") console.log("history 2222222222222222222222222222")
- subscribe(v.value, snap.OldValue)
- }
- )
- }
-
- obj._historySub[name] = f.subscribe((v)=>{
- if (CamName == "Transform") console.log("history 11111111111111111111111111")
- snap.OldValue = rxc.value;
- rxc.value = v.value;
- if (obj._history && obj._history.enable) {
- if (!v._hstry) return;
- const s = snap.clone();
- s.Value = v.value;
- obj._history.record(s);
- }
- })
- });
- obj["setHistory"] = function(h: HistoryController){
- obj._history = h;
- }
- obj["toJson"] = function() {
- const out:any = {};
- const names = Object.keys(_fields);
- names.forEach(name=>{
- out[name] = obj._rxs[name].getValue().value;
- })
- return out;
- }
- obj["fromJson"] = function(json:any) {
- const out:any = {};
- const names = Object.keys(_fields);
- names.forEach(name=>{
- obj._rxs[name].next({value: json[name], _hstry: false})
- })
- return out;
- }
-
- return obj as typeof _fields & {
- [K in keyof typeof _fields as `set${Capitalize<string & K>}`]: (value: typeof _fields[K], nohistory?:boolean) => void;
- } & {
- [K in keyof typeof _fields as `on${Capitalize<string & K>}Changed`]: (subscribe: (value: typeof _fields[K], oldValue:typeof _fields[K])=>void) => void;
- } &
- // {
- // [K in keyof typeof _fields as `ref${Capitalize<string & K>}`]: () => typeof _fields[K];
- // } &
- {
- setHistory: (history: HistoryController)=>void
- toJson:()=>typeof _fields
- fromJson:(json:typeof _fields)=>void
- }
- }
- }
- export {RxValue};
|