Sfoglia il codice sorgente

调整复制功能

yangg 2 settimane fa
parent
commit
2fd1e09e75
1 ha cambiato i file con 45 aggiunte e 0 eliminazioni
  1. 45 0
      src/components/CanvasEditor/index.vue

+ 45 - 0
src/components/CanvasEditor/index.vue

@@ -857,6 +857,21 @@ export default {
         
         // 如果成功获取到内容,则执行插入操作
         if (text || htmlContent) {
+          // 检查是否在表格单元格内
+          const isInTableCell = this.isCursorInTableCell();
+          
+          if (isInTableCell && text) {
+            // 在表格单元格内,检查是否包含制表符分隔的数据
+            if (text.includes('\t') || (text.includes('\n') && text.split('\n').some(line => line.includes('\t') || line.includes('  ')))) {
+              console.log('Detected table data in table cell, using special paste');
+              // 直接插入原始文本,让编辑器处理制表符分隔
+              this.editorRef.command.executeInsertText(text);
+              return;
+            } else {
+              console.log('Single cell data, using normal paste');
+            }
+          }
+          
           // 统一按纯文本在光标处插入,避免覆盖整篇文档
           let toInsert = text && text.trim() ? text : '';
           if (!toInsert && htmlContent && htmlContent.trim()) {
@@ -892,6 +907,36 @@ export default {
       }
     },
     
+    // 检测当前光标是否在表格单元格内
+    isCursorInTableCell() {
+      try {
+        if (!this.editorRef) return false;
+        
+        // 获取当前选区
+        const range = this.editorRef.command.getRange();
+        if (!range) return false;
+        
+        // 获取选区起始位置的DOM元素
+        const startElement = range.startContainer;
+        if (!startElement) return false;
+        
+        // 向上查找是否在表格单元格内
+        let currentElement = startElement.nodeType === Node.TEXT_NODE ? startElement.parentElement : startElement;
+        while (currentElement && currentElement !== document.body) {
+          if (currentElement.tagName === 'TD' || currentElement.tagName === 'TH') {
+            return true;
+          }
+          currentElement = currentElement.parentElement;
+        }
+        
+        return false;
+      } catch (error) {
+        console.error('Error detecting table cell context:', error);
+        return false;
+      }
+    },
+
+
     // 处理粘贴的HTML内容,清理超链接样式
     processPastedHtml(htmlContent) {
       try {