Browse Source

修复了replace方法只计算delete不计算insert的bug

Jiang Pengyu 5 months ago
parent
commit
a272361bad
2 changed files with 53 additions and 49 deletions
  1. 38 34
      src/components/QuillEditor/Editor.vue
  2. 15 15
      src/router/index.ts

+ 38 - 34
src/components/QuillEditor/Editor.vue

@@ -87,44 +87,48 @@ import {ElMessage} from "element-plus";
     // 文本变化监控
     quill.on(Quill.events.TEXT_CHANGE, (...args) => {
       const [delta, oldDelta, source] = args;
-      const deleteOps = delta.ops.filter(op => 'delete' in op);
-      // 直接将 ops 数组中的属性合并到一个对象
-      const opsObj = {};
+      const oldContent = processOps(args["1"].ops);
+      const docLength = quill.getLength() - 1;
+      const letterCount = (quill.getText().match(/[a-zA-Z\d\u4e00-\u9fa5]/g) || []).length;
+      let retain = 0;
+
       delta.ops.forEach(op => {
-        Object.assign(opsObj, op);
+        if ('retain' in op) {
+          retain += op.retain;
+          return;
+        }
+
+        if ('delete' in op) {
+          const deleteLength = op.delete;
+          const deletedContent = oldContent.slice(retain, retain + deleteLength);
+
+          emitter.emit('change-record', {
+            type: "delete",
+            retain,
+            delete: deleteLength,
+            oldContent,
+            deletedContent,
+            docLength,
+            letterCount
+          })
+          return;
+        }
+
+        if ('insert' in op) {
+          const insertedContent = typeof op.insert === 'string' ? op.insert : '';
+
+          emitter.emit('change-record', {
+            type: "insert",
+            retain,
+            insert: op.insert,
+            docLength,
+            letterCount,
+            key: insertedContent || null
+          })
+        }
       });
-      delete opsObj.attributes;
-      //有bug,当replace时,既有delete又有insert,此时会被误判为删除操作
-      if(deleteOps.length > 0){ //删除操作
-        // 获取删除操作的位置和长度
-        const deleteStart = delta.ops.find(op => 'retain' in op).retain || 0;   // 删除起始位置
-        const deleteLength = delta.ops.find(op => 'delete' in op).delete;       // 删除长度
-        const oldContent = processOps(args["1"].ops)// 原内容
-        // 提取被删除的具体内容
-        const deletedContent = oldContent.slice(deleteStart, deleteStart + deleteLength);
-
-        emitter.emit('change-record', {
-          type: "delete",
-          retain: 0,
-          ...opsObj,
-          oldContent,
-          deletedContent,
-          docLength: quill.getLength() - 1,
-          letterCount: (quill.getText().match(/[a-zA-Z\d\u4e00-\u9fa5]/g) || []).length
-        })
-      } else {
-        emitter.emit('change-record', {
-          type: "insert",
-          retain: 0,
-          ...opsObj,
-          docLength: quill.getLength() - 1,
-          letterCount: (quill.getText().match(/[a-zA-Z\d\u4e00-\u9fa5]/g) || []).length,
-          key: args["0"].ops.find(op => 'insert' in op).insert || null
-        })
-      }
     });
 
-    // 光标变化监控
     quill.on(Quill.events.SELECTION_CHANGE, (...args) => {
       emitter.emit('change-record', {
         type: "cursor_move",

+ 15 - 15
src/router/index.ts

@@ -16,24 +16,24 @@ const router = createRouter({
     },
 
     // 登录与注册
-    {
-      path: "/",
-      redirect: () => {
-        window.location.href = "https://p-nju.seec.seecoder.cn/login?from=https://air-nju.seec.seecoder.cn";
-        // window.location.href = "http://localhost:8000/login?from=http://localhost:4000";
-        // {本地门户首页地址}?from={本地eai首页地址}
-        return "/"; // 占位返回值,实际不会执行
-    },
-    },
     // {
     //   path: "/",
-    //   redirect: "/login"
+    //   redirect: () => {
+    //     window.location.href = "https://p-nju.seec.seecoder.cn/login?from=https://air-nju.seec.seecoder.cn";
+    //     // window.location.href = "http://localhost:8000/login?from=http://localhost:4000";
+    //     // {本地门户首页地址}?from={本地eai首页地址}
+    //     return "/"; // 占位返回值,实际不会执行
     // },
-    // {
-    //   path: '/login',
-    //   name: 'login',
-    //   component: () => import('../views/LoginPage/LoginPage.vue')
     // },
+    {
+      path: "/",
+      redirect: "/login"
+    },
+    {
+      path: '/login',
+      name: 'login',
+      component: () => import('../views/LoginPage/LoginPage.vue')
+    },
     {
       path: '/register',
       name: 'register',
@@ -260,7 +260,7 @@ const router = createRouter({
  */
   router.beforeEach(async (to) => {
     const store = useStore();
-    const publicPaths = [ '/register', '/passwordChange', '/admin/login'];
+    const publicPaths = [ '/register', '/passwordChange', '/admin/login','/login' ];
     // 1. 检查URL中的token
     const urlToken = new URLSearchParams(window.location.search).get('token');
     if (urlToken) {