Procházet zdrojové kódy

/可以,但初始化不行了

CzRger před 1 měsícem
rodič
revize
5363ea0256

+ 83 - 46
src/views/workflow/instance/component/params-textarea/index.vue

@@ -174,6 +174,12 @@ watch(
 )
 
 const handleInput = (e) => {
+  if (e.target.innerText) {
+    const brTags = e.target.querySelectorAll('br')
+    brTags.forEach((br) => {
+      br.parentNode.removeChild(br)
+    })
+  }
   let lastReplacedSpan: any = null
 
   const regex = /\{\{#([^#]+)#\}\}/g
@@ -219,56 +225,87 @@ const handleInput = (e) => {
     newSelection.addRange(newRange)
   }
   state.textCount = e.target.innerText?.trim().length || 0
-  const position = getCaretPosition()
-  if (position) {
-    const startContainer = position.range.startContainer
-    const startOffset = position.range.startOffset
-    // 获取光标所在的父元素
-    let currentNode = startContainer
-    if (currentNode.nodeType === Node.TEXT_NODE) {
-      currentNode = currentNode.parentNode
+  isSlashBeforeCursorEnhanced()
+
+  emitValue()
+}
+function isSlashBeforeCursorEnhanced() {
+  const selection = window.getSelection()
+  if (selection.rangeCount === 0) return false
+
+  const range = selection.getRangeAt(0)
+  let node = range.startContainer
+  let offset = range.startOffset
+
+  // 如果当前节点不是文本节点,尝试找到前一个文本节点
+  if (node.nodeType !== Node.TEXT_NODE) {
+    // 如果光标在元素开始处,需要查找前一个兄弟节点
+    if (offset === 0) {
+      let prevNode = getPreviousTextNode(node)
+      if (!prevNode) return false
+      node = prevNode
+      offset = node.textContent.length
+    } else {
+      // 光标在元素中间,需要检查子节点
+      const child = node.childNodes[offset - 1]
+      const lastTextNode = getLastTextNode(child)
+      if (!lastTextNode) return false
+      node = lastTextNode
+      offset = node.textContent.length
     }
-    // 获取所有同级元素
-    const siblings = currentNode.childNodes
-    let count = 0
-    // 遍历光标前的元素
-    for (let i = 0; i < siblings.length; i++) {
-      const sibling = siblings[i]
-      // 如果遇到当前元素,检查光标位置(如果是文本节点)
-      if (sibling === currentNode) {
-        if (currentNode.nodeType === Node.TEXT_NODE) {
-          // 对于文本节点,检查是否在开始位置
-          if (startOffset > 0) break
-        } else {
-          // 对于元素节点,检查是否在元素前面
-          if (i > 0 && siblings[i - 1] === currentNode) break
-        }
-      }
-      // 检查是否是 div 且有 vars-dom 类
-      if (
-        sibling.nodeType === Node.ELEMENT_NODE &&
-        sibling.tagName === 'DIV' &&
-        sibling.classList.contains('vars-dom')
-      ) {
-        count++
-      }
-      if (sibling === currentNode) break
+  }
+
+  // 检查前一个字符是否是/
+  if (offset > 0) {
+    if (node.textContent[offset - 1] === '/') {
+      showVariablePopup(range)
+      return
     }
-    console.log(count)
-    // 检查是否输入了 /
-    const textBeforeCaret = e.target.innerText.substring(
-      0,
-      position.offset + count,
-    )
-    console.log(textBeforeCaret)
-    if (textBeforeCaret.endsWith('/')) {
-      state.lastSlashPosition = position.offset - 1 // 保存 / 的位置
-      showVariablePopup(position.range)
-    } else {
-      state.showVariableList = false
+  }
+
+  // 如果当前文本节点开头,需要检查前一个文本节点
+  const prevTextNode = getPreviousTextNode(node)
+  if (prevTextNode && prevTextNode.textContent.length > 0) {
+    if (prevTextNode.textContent[prevTextNode.textContent.length - 1] === '/') {
+      showVariablePopup(range)
+      return
     }
   }
-  emitValue()
+
+  state.showVariableList = false
+}
+// 辅助函数:获取前一个文本节点
+function getPreviousTextNode(node) {
+  let sibling = node.previousSibling
+  while (sibling) {
+    if (
+      sibling.nodeType === Node.TEXT_NODE &&
+      sibling.textContent.trim() !== ''
+    ) {
+      return sibling
+    }
+    if (
+      sibling.nodeType === Node.ELEMENT_NODE &&
+      sibling.childNodes.length > 0
+    ) {
+      const lastChild = getLastTextNode(sibling)
+      if (lastChild) return lastChild
+    }
+    sibling = sibling.previousSibling
+  }
+  return node.parentNode ? getPreviousTextNode(node.parentNode) : null
+}
+
+// 辅助函数:获取最后一个文本节点
+function getLastTextNode(node) {
+  if (node.nodeType === Node.TEXT_NODE) return node
+  if (node.nodeType === Node.ELEMENT_NODE && node.childNodes.length > 0) {
+    for (let i = node.childNodes.length - 1; i >= 0; i--) {
+      const result = getLastTextNode(node.childNodes[i])
+      if (result) return result
+    }
+  }
+  return null
 }
 const handlePaste = (e) => {
   e.preventDefault()