/** * Created by W.X. on 16/7/29. * 历史管理器, 负责undo和redo */ import {IObservableArray, observable, observe, action} from "mobx/lib/mobx"; import {DesignerHost} from './DesignerHost'; import {Report} from "../items/Report"; import {JSONSchemaWriter} from "../schema/JSONSchemaWriter"; import {JSONSchemaLoader} from "../schema/loader/JSONSchemaLoader"; class HistoryService { private _history = []; private _items: IObservableArray = observable([]); private _host: DesignerHost; private _index: number = 0; private _status: Object = observable({ undo: false, redo: false }); constructor() { this._host = DesignerHost.instance(); } private get report():Report { return this._host.designReport.report; } private initItems() { this._items.length = 0; const getItem = (items) => { items.map( item => { this._items.push(item); if(item.items){ getItem(item.items); } } ); }; getItem(this.report.items); } private observeReportChange() { this._items.map( item => { // observe(item, (change) => { // console.log(change); // }); // observe(item.items, (change) => { // console.log('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&'); // console.log(change); // }); // observe(item.propertys, (change) => { // console.log("------------------------"); // console.log(change); // console.log(item); // console.log("------------------------"); // }); }); } get status() { return this._status; } /* * 记录一次操作 */ snap() { const writer = new JSONSchemaWriter(); const json = writer.write(this.report, false); let newPoint = { snapData : json, undoPoint : null, redoPoint : null }; if(this._history.length === 0 ){ this._history.push(newPoint); this._index=0; } else { let lastPoint = this._history[this._index]; if (JSON.stringify(lastPoint.snapData) === JSON.stringify(newPoint.snapData)) { return; } /* console.log('~~~~~~~~~~~~~~~~~~~~'); try{ console.log(JSON.parse(newPoint.snapData.cspReport.detail.band.elementGroup[0].dataTable.data)); console.log(JSON.stringify(newPoint.snapData.cspReport.detail.band.elementGroup[0])); }catch(e) {}*/ lastPoint.redoPoint = newPoint; newPoint.undoPoint = lastPoint; //如果现在是在历史最顶端 if (this._index === this._history.length-1) { this._history.push(newPoint); } else { this._history.splice(this._index+1); this._history.push(newPoint); } this._index++; this._host.onChange(json, writer.write(this.report, true)); } this.refreshStatus(); } /* * 撤销一次操作 */ undo() { const writer = new JSONSchemaWriter(); this._index--; this.setSnapPoint(); this.refreshStatus(); this._host.onChange(this._history[this._index].snapData, writer.write(this.report, true)); } /* * 恢复一次操作 */ redo() { const writer = new JSONSchemaWriter(); this._index++; this.setSnapPoint(); this.refreshStatus(); this._host.onChange(this._history[this._index].snapData, writer.write(this.report, true)); } stayLastPoint() { this.setSnapPoint(); } /* * 确定当前是否可以undo和redo */ private refreshStatus(){ const len = this._history.length; if(len<=1){ this._status['undo'] = false; this._status['redo'] = false; } else if(this._index<=0){ this._status['undo'] = false; this._status['redo'] = true; } else if(this._index>=len-1){ this._status['undo'] = true; this._status['redo'] = false; } else{ this._status['undo'] = true; this._status['redo'] = true; } } private setSnapPoint(){ const snap = this._history[this._index]; //console.log(snap.snapData); this.restoreReport(snap.snapData); } private restoreReport(data){ const report = JSONSchemaLoader.load(data); DesignerHost.instance().setDesignReport(report); report.setNeedDisplay(); } } export default HistoryService;