jerry 3 years ago
parent
commit
9c24651997

+ 2 - 2
packages/ot-core/src/basics/Command.ts

@@ -87,8 +87,8 @@ export class Command {
 
   public apply(resource: unknown): void {
     const operations = this._operations;
-    const sizeof = operations.length;
-    for (let i = 0; i < sizeof; i++) {
+    const sizeof = operations.length - 1;
+    for (let i = sizeof; i >= 0; i--) {
       const action = operations[i];
       action.apply(resource);
     }

+ 0 - 1
packages/ot-core/src/client/UndoManager.ts

@@ -12,7 +12,6 @@ export class UndoManager {
   protected _observer: UndoManagerObserver;
 
   protected _transformStack(stack: Array<UndoManager.Wrapped>, command: Command): void {
-    debugger;
     const transform = this._transform;
     const sizeof = stack.length;
     let redoCommand = command;

+ 86 - 88
packages/ot-demo/src/basics/CreateConflict.ts

@@ -10,92 +10,35 @@ import {
 
 export function CreateConflict(): Conflict {
   const conflict = new Conflict();
-  conflict.add(RemoveRow, InsertRow, (targetOperation, savedOperation) => {
-    // 服务端插入的行在当前行的下方
-    if (targetOperation.getHeight() <= savedOperation.getIndex()) {
-      const offset = targetOperation.getCount();
-      savedOperation.setIndex(savedOperation.getIndex() + offset);
-      return;
-    }
+  conflict.add(RemoveRow, RemoveRow, (targetOperation, savedOperation) => {
     // 服务端插入的行在当前行的上方
     if (targetOperation.getIndex() >= savedOperation.getHeight()) {
       const offset = savedOperation.getCount();
-      targetOperation.setIndex(targetOperation.getIndex() + offset);
-      return;
-    }
-    // 服务端插入的行被当前行包含
-    if (targetOperation.getIndex() <= savedOperation.getIndex() && targetOperation.getHeight() >= savedOperation.getHeight()) {
-      // 前操作
-      const prevIndex = targetOperation.getIndex();
-      const prevCount = savedOperation.getIndex() - targetOperation.getIndex();
-      // 后操作
-      const nextIndex = savedOperation.getHeight() - prevCount;
-      const nextCount = targetOperation.getHeight() - savedOperation.getIndex();
-      // 拆分操作
-      const prevOperation = new RemoveRow({
-        describe: 'RemoveRow',
-        index: prevIndex,
-        count: prevCount,
-      });
-      const nextOperation = new RemoveRow({
-        describe: 'RemoveRow',
-        index: nextIndex,
-        count: nextCount,
-      });
-      // 保存命令
-      targetOperation.replaceOperations(prevOperation, nextOperation);
-      return;
-    }
-    // 服务端插入的行包含当前行
-    if (targetOperation.getIndex() >= savedOperation.getIndex() && targetOperation.getHeight() <= savedOperation.getHeight()) {
-      const offset = targetOperation.getIndex() - savedOperation.getIndex() + savedOperation.getCount();
-      targetOperation.setIndex(targetOperation.getIndex() + offset);
-      return;
-    }
-    // 服务端插入的行和当前行上交叉
-    if (targetOperation.getIndex() > savedOperation.getIndex() && targetOperation.getIndex() < savedOperation.getHeight() && targetOperation.getHeight() >= savedOperation.getHeight()) {
-      const offset = targetOperation.getIndex() - savedOperation.getIndex() + savedOperation.getCount();
-      targetOperation.setIndex(targetOperation.getIndex() + offset);
-      return;
+      targetOperation.setIndex(targetOperation.getIndex() - offset);
     }
-    // 服务端插入的行和当前行下交叉
-    if (targetOperation.getIndex() <= savedOperation.getIndex() && targetOperation.getHeight() > savedOperation.getIndex() && targetOperation.getHeight() < savedOperation.getHeight()) {
-      // 前操作
-      const prevIndex = targetOperation.getIndex();
-      const prevCount = savedOperation.getIndex() - targetOperation.getIndex();
-      // 后操作
-      const nextIndex = savedOperation.getHeight() - prevCount;
-      const nextCount = targetOperation.getHeight() - savedOperation.getIndex();
-      // 拆分操作
-      const prevOperation = new RemoveRow({
-        describe: 'RemoveRow',
-        index: prevIndex,
-        count: prevCount,
-      });
-      const nextOperation = new RemoveRow({
-        describe: 'RemoveRow',
-        index: nextIndex,
-        count: nextCount,
-      });
-      // 保存命令
-      targetOperation.replaceOperations(prevOperation, nextOperation);
+  });
+  conflict.add(InsertRow, RemoveRow, (targetOperation, savedOperation) => {
+    // 服务端插入的行在当前行的上方
+    if (targetOperation.getIndex() >= savedOperation.getHeight()) {
+      const offset = savedOperation.getCount();
+      targetOperation.setIndex(targetOperation.getIndex() - offset);
     }
   });
   conflict.add(InsertRow, InsertRow, (targetOperation, savedOperation) => {
-    // 服务端插入的行在当前行的
-    if (targetOperation.getHeight() <= savedOperation.getIndex()) {
-      const offset = targetOperation.getCount();
-      savedOperation.setIndex(savedOperation.getIndex() + offset);
+    // 服务端插入的行在当前行的上方
+    if (targetOperation.getIndex() > savedOperation.getHeight()) {
+      const offset = savedOperation.getCount();
+      targetOperation.setIndex(targetOperation.getIndex() + offset);
       return;
     }
-    // 服务端插入的行在当前行的上方
-    if (targetOperation.getIndex() >= savedOperation.getHeight()) {
+    // 服务端插入的行和当前行的起始位置对齐
+    if (targetOperation.getIndex() === savedOperation.getIndex()) {
       const offset = savedOperation.getCount();
       targetOperation.setIndex(targetOperation.getIndex() + offset);
       return;
     }
     // 服务端插入的行被当前行包含
-    if (targetOperation.getIndex() <= savedOperation.getIndex() && targetOperation.getHeight() >= savedOperation.getHeight()) {
+    if (targetOperation.getIndex() < savedOperation.getIndex() && targetOperation.getHeight() > savedOperation.getHeight()) {
       const prevSwap = new SwapData();
       const nextSwap = new SwapData();
       const currSwap = targetOperation.getBuffer();
@@ -130,19 +73,13 @@ export function CreateConflict(): Conflict {
       return;
     }
     // 服务端插入的行包含当前行
-    if (targetOperation.getIndex() >= savedOperation.getIndex() && targetOperation.getHeight() <= savedOperation.getHeight()) {
-      const offset = targetOperation.getIndex() - savedOperation.getIndex() + savedOperation.getCount();
-      targetOperation.setIndex(targetOperation.getIndex() + offset);
-      return;
-    }
-    // 服务端插入的行和当前行上交叉
-    if (targetOperation.getIndex() > savedOperation.getIndex() && targetOperation.getIndex() < savedOperation.getHeight() && targetOperation.getHeight() >= savedOperation.getHeight()) {
-      const offset = targetOperation.getIndex() - savedOperation.getIndex() + savedOperation.getCount();
-      targetOperation.setIndex(targetOperation.getIndex() + offset);
+    if (targetOperation.getIndex() > savedOperation.getIndex() && targetOperation.getHeight() < savedOperation.getHeight()) {
+      const offset = savedOperation.getIndex() + savedOperation.getCount();
+      targetOperation.setIndex(targetOperation.getIndex() - offset);
       return;
     }
     // 服务端插入的行和当前行下交叉
-    if (targetOperation.getIndex() <= savedOperation.getIndex() && targetOperation.getHeight() > savedOperation.getIndex() && targetOperation.getHeight() < savedOperation.getHeight()) {
+    if (targetOperation.getIndex() < savedOperation.getIndex() && targetOperation.getHeight() > savedOperation.getIndex() && targetOperation.getHeight() <= savedOperation.getHeight()) {
       const prevSwap = new SwapData();
       const nextSwap = new SwapData();
       const currSwap = targetOperation.getBuffer();
@@ -175,18 +112,79 @@ export function CreateConflict(): Conflict {
       // 保存命令
       targetOperation.replaceOperations(prevOperation, nextOperation);
     }
-  });
-  conflict.add(RemoveRow, RemoveRow, (targetOperation, savedOperation) => {
-    // 服务端插入的行在当前行的上方
-    if (targetOperation.getIndex() >= savedOperation.getHeight()) {
-      const offset = savedOperation.getCount();
+    // 服务端插入的行和当前行上交叉
+    if (targetOperation.getIndex() > savedOperation.getIndex() && targetOperation.getIndex() < savedOperation.getHeight() && targetOperation.getHeight() >= savedOperation.getHeight()) {
+      const offset = savedOperation.getIndex() + savedOperation.getCount();
       targetOperation.setIndex(targetOperation.getIndex() - offset);
     }
   });
-  conflict.add(InsertRow, RemoveRow, (targetOperation, savedOperation) => {
+  conflict.add(RemoveRow, InsertRow, (targetOperation, savedOperation) => {
     // 服务端插入的行在当前行的上方
     if (targetOperation.getIndex() >= savedOperation.getHeight()) {
       const offset = savedOperation.getCount();
+      targetOperation.setIndex(targetOperation.getIndex() + offset);
+      return;
+    }
+    // 服务端插入的行和当前行的起始位置对齐
+    if (targetOperation.getIndex() === savedOperation.getIndex()) {
+      const offset = savedOperation.getCount();
+      targetOperation.setIndex(targetOperation.getIndex() + offset);
+      return;
+    }
+    // 服务端插入的行被当前行包含
+    if (targetOperation.getIndex() < savedOperation.getIndex() && targetOperation.getHeight() > savedOperation.getHeight()) {
+      // 前操作
+      const prevIndex = targetOperation.getIndex();
+      const prevCount = savedOperation.getIndex() - targetOperation.getIndex();
+      // 后操作
+      const nextIndex = savedOperation.getHeight();
+      const nextCount = targetOperation.getHeight() - savedOperation.getIndex();
+      // 拆分操作
+      const prevOperation = new RemoveRow({
+        describe: 'RemoveRow',
+        index: prevIndex,
+        count: prevCount,
+      });
+      const nextOperation = new RemoveRow({
+        describe: 'RemoveRow',
+        index: nextIndex,
+        count: nextCount,
+      });
+      // 保存命令
+      targetOperation.replaceOperations(prevOperation, nextOperation);
+      return;
+    }
+    // 服务端插入的行包含当前行
+    if (targetOperation.getIndex() > savedOperation.getIndex() && targetOperation.getHeight() < savedOperation.getHeight()) {
+      const offset = savedOperation.getIndex() + savedOperation.getCount();
+      targetOperation.setIndex(targetOperation.getIndex() - offset);
+      return;
+    }
+    // 服务端插入的行和当前行下交叉
+    if (targetOperation.getIndex() < savedOperation.getIndex() && targetOperation.getHeight() > savedOperation.getIndex() && targetOperation.getHeight() <= savedOperation.getHeight()) {
+      // 前操作
+      const prevIndex = targetOperation.getIndex();
+      const prevCount = savedOperation.getIndex() - targetOperation.getIndex();
+      // 后操作
+      const nextIndex = savedOperation.getHeight();
+      const nextCount = targetOperation.getHeight() - savedOperation.getIndex();
+      // 拆分操作
+      const prevOperation = new RemoveRow({
+        describe: 'RemoveRow',
+        index: prevIndex,
+        count: prevCount,
+      });
+      const nextOperation = new RemoveRow({
+        describe: 'RemoveRow',
+        index: nextIndex,
+        count: nextCount,
+      });
+      // 保存命令
+      targetOperation.replaceOperations(prevOperation, nextOperation);
+    }
+    // 服务端插入的行和当前行上交叉
+    if (targetOperation.getIndex() > savedOperation.getIndex() && targetOperation.getIndex() < savedOperation.getHeight() && targetOperation.getHeight() >= savedOperation.getHeight()) {
+      const offset = savedOperation.getIndex() + savedOperation.getCount();
       targetOperation.setIndex(targetOperation.getIndex() - offset);
     }
   });

+ 2 - 0
packages/ot-demo/src/client.ts

@@ -79,3 +79,5 @@ undo.addEventListener('click', () => {
   const undoManager = webClient.getUndoManager();
   undoManager.performUndo();
 });
+
+(window as any).webClient = webClient;