jerry před 3 roky
rodič
revize
a60ad4a1d4
1 změnil soubory, kde provedl 64 přidání a 69 odebrání
  1. 64 69
      packages/ot-demo/src/basics/CreateConflict.ts

+ 64 - 69
packages/ot-demo/src/basics/CreateConflict.ts

@@ -10,51 +10,35 @@ import {
 
 export function CreateConflict(): Conflict {
   const conflict = new Conflict();
-  conflict.add(InsertRow, InsertRow, (targetOperation, savedOperation) => {
+  conflict.add(RemoveRow, InsertRow, (targetOperation, savedOperation) => {
     // 服务端插入的行在当前行的下方
     if (targetOperation.getHeight() <= savedOperation.getIndex()) {
-      // 将服务端插入的数据行向下转移
-      const offset = savedOperation.getIndex() + targetOperation.getCount();
+      const offset = targetOperation.getCount();
       savedOperation.setIndex(savedOperation.getIndex() + offset);
       return;
     }
-
     // 服务端插入的行在当前行的上方
     if (targetOperation.getIndex() >= savedOperation.getHeight()) {
-      // 将当前插入的数据行向下转移
-      const offset = targetOperation.getIndex() + savedOperation.getCount();
+      const offset = savedOperation.getCount();
       targetOperation.setIndex(targetOperation.getIndex() + offset);
       return;
     }
-
     // 服务端插入的行被当前行包含
     if (targetOperation.getIndex() <= savedOperation.getIndex() && targetOperation.getHeight() >= savedOperation.getHeight()) {
-      const prevSwap = new SwapData();
-      const nextSwap = new SwapData();
-      const currSwap = targetOperation.getBuffer();
-      // 拆分点
-      const splicePoint = savedOperation.getIndex() - targetOperation.getIndex();
       // 前操作
       const prevIndex = targetOperation.getIndex();
       const prevCount = savedOperation.getIndex() - targetOperation.getIndex();
       // 后操作
-      const nextIndex = savedOperation.getHeight();
+      const nextIndex = savedOperation.getHeight() - prevCount;
       const nextCount = targetOperation.getHeight() - savedOperation.getIndex();
-      // 拆分数据
-      if (currSwap.hasData()) {
-        prevSwap.setData(currSwap.getData().slice(0, splicePoint));
-        nextSwap.setData(currSwap.getData().slice(splicePoint));
-      }
       // 拆分操作
-      const prevOperation = new InsertRow({
-        describe: 'InsertRow',
-        buffer: prevSwap,
+      const prevOperation = new RemoveRow({
+        describe: 'RemoveRow',
         index: prevIndex,
         count: prevCount,
       });
-      const nextOperation = new InsertRow({
-        describe: 'InsertRow',
-        buffer: nextSwap,
+      const nextOperation = new RemoveRow({
+        describe: 'RemoveRow',
         index: nextIndex,
         count: nextCount,
       });
@@ -62,51 +46,34 @@ export function CreateConflict(): Conflict {
       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;
     }
-
     // 服务端插入的行和当前行下交叉
     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();
-      // 拆分点
-      const splicePoint = savedOperation.getIndex() - targetOperation.getIndex();
       // 前操作
       const prevIndex = targetOperation.getIndex();
       const prevCount = savedOperation.getIndex() - targetOperation.getIndex();
       // 后操作
-      const nextIndex = savedOperation.getHeight();
+      const nextIndex = savedOperation.getHeight() - prevCount;
       const nextCount = targetOperation.getHeight() - savedOperation.getIndex();
-      // 拆分数据
-      if (currSwap.hasData()) {
-        prevSwap.setData(currSwap.getData().slice(0, splicePoint));
-        nextSwap.setData(currSwap.getData().slice(splicePoint));
-      }
       // 拆分操作
-      const prevOperation = new InsertRow({
-        describe: 'InsertRow',
-        buffer: prevSwap,
+      const prevOperation = new RemoveRow({
+        describe: 'RemoveRow',
         index: prevIndex,
         count: prevCount,
       });
-      const nextOperation = new InsertRow({
-        describe: 'InsertRow',
-        buffer: nextSwap,
+      const nextOperation = new RemoveRow({
+        describe: 'RemoveRow',
         index: nextIndex,
         count: nextCount,
       });
@@ -114,40 +81,47 @@ export function CreateConflict(): Conflict {
       targetOperation.replaceOperations(prevOperation, nextOperation);
     }
   });
-
-  conflict.add(RemoveRow, InsertRow, (targetOperation, savedOperation) => {
+  conflict.add(InsertRow, InsertRow, (targetOperation, savedOperation) => {
     // 服务端插入的行在当前行的下方
     if (targetOperation.getHeight() <= savedOperation.getIndex()) {
-      // 将服务端插入的数据行向下转移
-      const offset = savedOperation.getIndex() + targetOperation.getCount();
+      const offset = targetOperation.getCount();
       savedOperation.setIndex(savedOperation.getIndex() + offset);
       return;
     }
-
     // 服务端插入的行在当前行的上方
     if (targetOperation.getIndex() >= savedOperation.getHeight()) {
-      // 将当前插入的数据行向下转移
-      const offset = targetOperation.getIndex() + savedOperation.getCount();
+      const offset = savedOperation.getCount();
       targetOperation.setIndex(targetOperation.getIndex() + offset);
       return;
     }
-
     // 服务端插入的行被当前行包含
     if (targetOperation.getIndex() <= savedOperation.getIndex() && targetOperation.getHeight() >= savedOperation.getHeight()) {
+      const prevSwap = new SwapData();
+      const nextSwap = new SwapData();
+      const currSwap = targetOperation.getBuffer();
+      // 拆分点
+      const splicePoint = savedOperation.getIndex() - targetOperation.getIndex();
       // 前操作
       const prevIndex = targetOperation.getIndex();
       const prevCount = savedOperation.getIndex() - targetOperation.getIndex();
       // 后操作
-      const nextIndex = savedOperation.getHeight() - prevCount;
+      const nextIndex = savedOperation.getHeight();
       const nextCount = targetOperation.getHeight() - savedOperation.getIndex();
+      // 拆分数据
+      if (currSwap.hasData()) {
+        prevSwap.setData(currSwap.getData().slice(0, splicePoint));
+        nextSwap.setData(currSwap.getData().slice(splicePoint));
+      }
       // 拆分操作
-      const prevOperation = new RemoveRow({
-        describe: 'RemoveRow',
+      const prevOperation = new InsertRow({
+        describe: 'InsertRow',
+        buffer: prevSwap,
         index: prevIndex,
         count: prevCount,
       });
-      const nextOperation = new RemoveRow({
-        describe: 'RemoveRow',
+      const nextOperation = new InsertRow({
+        describe: 'InsertRow',
+        buffer: nextSwap,
         index: nextIndex,
         count: nextCount,
       });
@@ -155,39 +129,46 @@ export function CreateConflict(): Conflict {
       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;
     }
-
     // 服务端插入的行和当前行下交叉
     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();
+      // 拆分点
+      const splicePoint = savedOperation.getIndex() - targetOperation.getIndex();
       // 前操作
       const prevIndex = targetOperation.getIndex();
       const prevCount = savedOperation.getIndex() - targetOperation.getIndex();
       // 后操作
-      const nextIndex = savedOperation.getHeight() - prevCount;
+      const nextIndex = savedOperation.getHeight();
       const nextCount = targetOperation.getHeight() - savedOperation.getIndex();
+      // 拆分数据
+      if (currSwap.hasData()) {
+        prevSwap.setData(currSwap.getData().slice(0, splicePoint));
+        nextSwap.setData(currSwap.getData().slice(splicePoint));
+      }
       // 拆分操作
-      const prevOperation = new RemoveRow({
-        describe: 'RemoveRow',
+      const prevOperation = new InsertRow({
+        describe: 'InsertRow',
+        buffer: prevSwap,
         index: prevIndex,
         count: prevCount,
       });
-      const nextOperation = new RemoveRow({
-        describe: 'RemoveRow',
+      const nextOperation = new InsertRow({
+        describe: 'InsertRow',
+        buffer: nextSwap,
         index: nextIndex,
         count: nextCount,
       });
@@ -195,5 +176,19 @@ export function CreateConflict(): Conflict {
       targetOperation.replaceOperations(prevOperation, nextOperation);
     }
   });
+  conflict.add(RemoveRow, RemoveRow, (targetOperation, savedOperation) => {
+    // 服务端插入的行在当前行的上方
+    if (targetOperation.getIndex() >= savedOperation.getHeight()) {
+      const offset = savedOperation.getCount();
+      targetOperation.setIndex(targetOperation.getIndex() - offset);
+    }
+  });
+  conflict.add(InsertRow, RemoveRow, (targetOperation, savedOperation) => {
+    // 服务端插入的行在当前行的上方
+    if (targetOperation.getIndex() >= savedOperation.getHeight()) {
+      const offset = savedOperation.getCount();
+      targetOperation.setIndex(targetOperation.getIndex() - offset);
+    }
+  });
   return conflict;
 }