Quellcode durchsuchen

支持复制粘贴

yangkaixiang vor 5 Monaten
Ursprung
Commit
545c691269

+ 46 - 14
src/pages/Alarm/components/components/Formular.jsx

@@ -302,15 +302,31 @@ const Formular = (props, ref) => {
   const setValue = () => {
     let formula = "";
     const vars = {};
-    formulaRef.current.childNodes.forEach((item) => {
-      if (item.dataset?.point_id) {
-        formula += `[${item.dataset?.point_id}]`;
-        vars[item.dataset?.point_id] = item.innerText;
-      } else if (item.localName === "br") {
-      } else {
-        formula += item.data;
+    
+    const processNode = (node) => {
+      if (node.dataset?.point_id) {
+        // 处理点位节点
+        formula += `[${node.dataset?.point_id}]`;
+        vars[node.dataset?.point_id] = node.innerText || node.textContent;
+      } else if (node.localName === "br") {
+        // 忽略br标签
+      } else if (node.nodeType === Node.TEXT_NODE) {
+        // 处理文本节点
+        formula += node.data || node.textContent;
+      } else if (node.nodeType === Node.ELEMENT_NODE) {
+        // 处理其他HTML元素(如span),提取其文本内容
+        if (node.children && node.children.length > 0) {
+          // 如果有子元素,递归处理
+          Array.from(node.children).forEach(processNode);
+        } else {
+          // 如果没有子元素,直接提取文本内容
+          formula += node.textContent || node.innerText || "";
+        }
       }
-    });
+    };
+    
+    formulaRef.current.childNodes.forEach(processNode);
+    
     const res = {
       formula,
       vars,
@@ -319,9 +335,25 @@ const Formular = (props, ref) => {
     console.log(outStrRef.current);
   };
 
-  const stopNativeEvent = useCallback((e) => {
-    e.preventDefault();
-    return false;
+  const handlePaste = useCallback((e) => {
+    // 允许粘贴,但在粘贴后更新内容
+    setTimeout(() => {
+      setValue();
+      updateCursorLocation();
+    }, 0);
+  }, []);
+
+  const handleCopy = useCallback((e) => {
+    // 允许复制操作
+    updateCursorLocation();
+  }, []);
+
+  const handleCut = useCallback((e) => {
+    // 允许剪切,但在剪切后更新内容
+    setTimeout(() => {
+      setValue();
+      updateCursorLocation();
+    }, 0);
   }, []);
 
   return (
@@ -334,10 +366,10 @@ const Formular = (props, ref) => {
         onClick={handleClick}
         onKeyUp={handleKeyUp}
         onKeyDown={handleKeyDown}
-        onCut={stopNativeEvent}
+        onCut={handleCut}
         onBlur={divBlur}
-        onPaste={stopNativeEvent}
-        onCopy={stopNativeEvent}
+        onPaste={handlePaste}
+        onCopy={handleCopy}
       ></div>
       <div>
         <HintBox

+ 1 - 1
src/pages/Alarm/components/components/fomular.module.less

@@ -47,7 +47,7 @@
 
       img {
         margin: 0 1px;
-        cursor: pointer;
+        // cursor: pointer;
       }
     }
   }

+ 0 - 4
src/pages/Alarm/components/components/oneNode.js

@@ -6,10 +6,6 @@ export default function oneNode(point_id, name) {
   // window.navigator.clipboard.writeText("${point_id}");
   return `<img 
   title='${point_id}' 
-  onclick='
-  window.copyToClipboard("${point_id}");
-  window.dtmessage.success("已复制");
-  ' 
   data-point_id='${point_id}' 
   src=${drawPoint(name)}
     >`