Pārlūkot izejas kodu

fix: synchronize undo & redo actions with worker model

田丰 10 mēneši atpakaļ
vecāks
revīzija
bdb41afdfa

+ 32 - 3
packages/semi-json-viewer-core/src/model/jsonModel.ts

@@ -186,7 +186,22 @@ export class JSONModel {
         const command = this._undoStack.pop()!;
         command.undo();
         this._redoStack.push(command);
-        this.emitter?.emit('contentChanged', command.operation);
+        if (!isInWorkerThread()) {
+            this.emitter?.emit('contentChanged', command.operation);
+        }
+        if (this._jsonWorkerManager) {
+            this._jsonWorkerManager
+                .undo()
+                .then(res => {
+                    return this._jsonWorkerManager?.validate();
+                })
+                .then(result => {
+                    this.emitter?.emit('problemsChanged', {
+                        problems: result.problems,
+                        root: result.root,
+                    });
+                });
+        }
     }
 
     redo() {
@@ -195,10 +210,24 @@ export class JSONModel {
         const command = this._redoStack.pop()!;
         command.execute();
         this._undoStack.push(command);
-        this.emitter?.emit('contentChanged', command.operation);
+        if (!isInWorkerThread()) {
+            this.emitter?.emit('contentChanged', command.operation);
+        }
+        if (this._jsonWorkerManager) {
+            this._jsonWorkerManager
+                .redo()
+                .then(res => {
+                    return this._jsonWorkerManager?.validate();
+                })
+                .then(result => {
+                    this.emitter?.emit('problemsChanged', {
+                        problems: result.problems,
+                        root: result.root,
+                    });
+                });
+        }
     }
 
-
     /**
      * 获取值
      * @returns 值

+ 6 - 1
packages/semi-json-viewer-core/src/worker/json.worker.ts

@@ -21,7 +21,12 @@ self.onmessage = (e: MessageEvent) => {
     switch (method) {
         case 'updateModel':
             jsonWorker.updateModel(params.op);
-            result = jsonWorker.getModel()?.getValue();
+            break;
+        case 'undo':
+            jsonWorker.undo();
+            break;
+        case 'redo':
+            jsonWorker.redo();
             break;
         case 'format':
             result = jsonWorker.format(params.options as FormattingOptions);

+ 8 - 0
packages/semi-json-viewer-core/src/worker/jsonWorker.ts

@@ -35,6 +35,14 @@ export class JsonWorker {
         return op;
     }
 
+    undo() {
+        this._model?.undo();
+    }
+
+    redo() {
+        this._model?.redo();
+    }
+
     parse() {
         if (!this._model) throw new Error('Model not initialized');
         return parseJsonAst(this._model);

+ 21 - 7
packages/semi-json-viewer-core/src/worker/jsonWorkerManager.ts

@@ -7,11 +7,17 @@ import { getCurrentNameSpaceId } from '../common/nameSpace';
 /**
  * JsonWorkerManager 类用于管理 JSON Worker
  */
-type WorkerMethod = 'init' | 'updateModel' | 'format' | 'foldRange' | 'validate';
-type WorkerParams = {
-    value?: string;
-    options?: FormattingOptions;
-    op?: IModelContentChangeEvent | IModelContentChangeEvent[]
+
+type WorkerService = {
+    init: { value: string };
+    updateModel: {
+        op: IModelContentChangeEvent | IModelContentChangeEvent[]
+    };
+    format: { options: FormattingOptions };
+    foldRange: Record<string, never>;
+    validate: Record<string, never>;
+    undo: Record<string, never>;
+    redo: Record<string, never>
 };
 
 const workerManagerMap = new Map<string, JsonWorkerManager>();
@@ -21,7 +27,7 @@ export class JsonWorkerManager {
     private _callbacks: Map<number, (result: any) => void>;
 
     constructor() {
-        const workerRaw = decodeURIComponent('%WORKER_RAW%');
+        const workerRaw = decodeURIComponent(`%WORKER_RAW%`);
         const blob = new Blob([workerRaw], { type: 'application/javascript' });
         const workerURL = URL.createObjectURL(blob);
         this._worker = new Worker(workerURL);
@@ -38,6 +44,14 @@ export class JsonWorkerManager {
         return this._sendRequest('updateModel', { op });
     }
 
+    undo() {
+        return this._sendRequest('undo', {});
+    }
+
+    redo() {
+        return this._sendRequest('redo', {});
+    }
+
     formatJson(options: FormattingOptions) {
         return this._sendRequest('format', { options });
     }
@@ -50,7 +64,7 @@ export class JsonWorkerManager {
         return this._sendRequest('validate', {});
     }
 
-    private _sendRequest(method: WorkerMethod, params: WorkerParams): Promise<any> {
+    private _sendRequest<T extends keyof WorkerService>(method: T, params: WorkerService[T]): Promise<any> {
         return new Promise((resolve, reject) => {
             const messageId = Date.now() + Math.random();
             this._callbacks.set(messageId, resolve);