<script setup>
import { ref, computed, onMounted, onBeforeUnmount, nextTick, markRaw, watch } from 'vue'
import { NPopover, NIcon } from 'naive-ui'
import {
  SmilingFace,
  Pic,
  FolderUpload
} from '@icon-park/vue-next'
import { bus } from '@/utils/event-bus'
import { EditorConst } from '@/constant/event-bus'
import { emitCall } from '@/utils/common'
import { deltaToMessage, deltaToString, isEmptyDelta } from './util'
import { useDialogueStore, useEditorDraftStore ,useUserStore} from '@/store'
import { uploadImg } from '@/api/upload'
import { defAvatar } from '@/constant/default'
import { getImageInfo } from '@/utils/functions'
import MeEditorEmoticon from './MeEditorEmoticon.vue'
import {IosSend} from '@vicons/ionicons4'
const props = defineProps({
  vote: {
    type: Boolean,
    default: false
  },
  members: {
    type: Array,
    default: () => []
  },
  placeholder: {
    type: String,
    default: 'Enter-发送消息  [Ctrl+Enter/Shift+Enter]-换行'
  }
})

const emit = defineEmits(['editor-event'])
const userStore = useUserStore()
const editorRef = ref(null)
const content = ref('')
const isFocused = ref(false)
const showMention = ref(false)
const mentionQuery = ref('')
const mentionPosition = ref({ top: 0, left: 0 })
const selectedMentionIndex = ref(0)
const quoteData = ref(null)
const editingMessage = ref(null)
const isShowEmoticon = ref(false)

const fileImageRef = ref(null)
const uploadFileRef = ref(null)
const emoticonRef = ref(null)

const dialogueStore = useDialogueStore()
const editorDraftStore = useEditorDraftStore()


const indexName = computed(() => dialogueStore.index_name)


const navs = ref([
  {
    title: '图片',
    icon: markRaw(Pic),
    show: true,
    click: () => {
      fileImageRef.value.click()
    }
  },
  {
    title: '文件',
    icon: markRaw(FolderUpload),
    show: true,
    click: () => {
      uploadFileRef.value.click()
    }
  }
])

const mentionList = ref([])
const currentMentionQuery = ref('')
setTimeout(() => {
  console.log('props.members',props.members)
}, 1000)

const editorContent = ref('')
const editorHtml = ref('')


const toolbarConfig = computed(() => {
  const config = [
    { type: 'emoticon', icon: 'icon-biaoqing', title: '表情' },
    { type: 'image', icon: 'icon-image', title: '图片' },
    { type: 'file', icon: 'icon-folder-plus', title: '文件' }
  ]
  
  return config
})


const handleInput = (event) => {
  const target = event.target
  
  
  const editorClone = target.cloneNode(true)
  const quoteElements = editorClone.querySelectorAll('.editor-quote')
  quoteElements.forEach(quote => quote.remove())
  quoteElements.forEach(quote => quote.remove())
  
  
  const emojiImages = editorClone.querySelectorAll('img.editor-emoji')
  let textContent = editorClone.textContent || ''
  
  
  if (emojiImages.length > 0) {
    emojiImages.forEach(emoji => {
      const altText = emoji.getAttribute('alt')
      if (altText) {
        textContent += altText
      }
    })
  }
  
  
  editorContent.value = textContent

  
  
  const editorNode = target;
  const currentNormalizedHtml = editorNode.innerHTML.trim().toLowerCase().replace(/\s+/g, '');

  const hasTextContent = editorNode.textContent.trim() !== '';
  const hasSpecialElements = editorNode.querySelector('img, .editor-file, .mention');

  if (!hasTextContent && !hasSpecialElements) {
    if (currentNormalizedHtml !== '' && currentNormalizedHtml !== '<br>') {
      editorNode.innerHTML = '';
    }
  }

  
  editorHtml.value = target.innerHTML || ''
  const currentEditor= parseEditorContent().items
  
  checkMention(target)
  saveDraft()

  emit('editor-event', {
    event: 'input_event',
    data: currentEditor.reduce((result, x) => {
  if (x.type === 3) return result + '[图片]'
  if (x.type === 1) return result + x.content
  return result
}, '')
  })
}


const checkMention = (target) => {
  const selection = window.getSelection()
  if (!selection.rangeCount) return
  
  const range = selection.getRangeAt(0)
  const textBeforeCursor = range.startContainer.textContent?.substring(0, range.startOffset) || ''
  const mentionMatch = textBeforeCursor.match(/@([^@\s]*)$/)
  
  if (mentionMatch) {
    currentMentionQuery.value = mentionMatch[1]
    showMentionList()
    updateMentionPosition(range)
  } else {
    hideMentionList()
  }
}


const showMentionList = () => {
  const query = currentMentionQuery.value.toLowerCase()
  mentionList.value = props.members.filter(member => {
    return member.value.toLowerCase().startsWith(query)&& member.id !== userStore.uid
  })
  console.log('userStore',userStore.uid)
  if(dialogueStore.groupInfo.is_manager){
    mentionList.value.unshift({ id: 0, nickname: '全体成员', avatar: defAvatar, value: '全体成员' })
}
  showMention.value = mentionList.value.length > 0
  selectedMentionIndex.value = 0
}


const handleMentionSelectByMouse = (member) => {
  const selection = window.getSelection();
  if (selection && selection.rangeCount > 0) {
    insertMention(member, selection.getRangeAt(0).cloneRange());
  } else {
    
    editorRef.value?.focus();
    nextTick(() => {
      const newSelection = window.getSelection();
      if (newSelection && newSelection.rangeCount > 0) {
        insertMention(member, newSelection.getRangeAt(0).cloneRange());
      }
    });
  }
};


const hideMentionList = () => {
  showMention.value = false
  mentionList.value = []
  currentMentionQuery.value = ''
}


const updateMentionPosition = (range) => {
  const rect = range.getBoundingClientRect()
  const editorRect = editorRef.value.getBoundingClientRect()
  
  mentionPosition.value = {
    top: rect.bottom - editorRect.top + 5,
    left: rect.left - editorRect.left
  }
}


const insertMention = (member, clonedRange) => {
  console.log('插入mention', member);
  const selection = window.getSelection();
  if (!clonedRange || !selection) return;

  const range = clonedRange; 

  const textNode = range.startContainer;
  const offset = range.startOffset;
  const textContent = textNode.nodeType === Node.TEXT_NODE ? textNode.textContent || '' : '';
  
  const atIndex = (textNode.nodeType === Node.TEXT_NODE && offset > 0) ? textContent.lastIndexOf('@', offset - 1) : -1;

  const mentionSpan = document.createElement('span');
  mentionSpan.className = 'mention';
  mentionSpan.setAttribute('data-user-id', String(member.id));
  mentionSpan.textContent = `@${member.value || member.nickname}`;
  mentionSpan.contentEditable = 'false';

  if (atIndex !== -1 && textNode.nodeType === Node.TEXT_NODE) {
    const parent = textNode.parentNode;
    if (!parent) return; 

    
    range.setStart(textNode, atIndex);
    range.setEnd(textNode, offset);
    range.deleteContents();

    
    range.insertNode(mentionSpan);
  } else {
    
    if (!range.collapsed) {
      range.deleteContents();
    }
    range.insertNode(mentionSpan);
  }

  
  const spaceNode = document.createTextNode('\u00A0'); 
  const currentParent = mentionSpan.parentNode;
  if (currentParent) {
    
    if (mentionSpan.nextSibling) {
      currentParent.insertBefore(spaceNode, mentionSpan.nextSibling);
    } else {
      currentParent.appendChild(spaceNode);
    }
    
    range.setStartAfter(spaceNode);
    range.collapse(true);
  } else {
    
    range.setStartAfter(mentionSpan);
    range.collapse(true);
  }

  selection.removeAllRanges();
  selection.addRange(range);

  editorRef.value?.focus(); 

  nextTick(() => {
    handleInput({ target: editorRef.value });
    hideMentionList();
  });
};


const handlePaste = (event) => {
  event.preventDefault()
  
  
  const items = event.clipboardData?.items
  if (items) {
    for (let i = 0; i < items.length; i++) {
      if (items[i].type.indexOf('image') !== -1) {
        
        const file = items[i].getAsFile()
        if (file) {
          const tempUrl = URL.createObjectURL(file);
    const image = new Image();
    image.src = tempUrl;
    image.onload = () => {
      const form = new FormData();
      form.append('file', file);
      form.append("source", "fonchain-chat");  
      form.append("urlParam", `width=${image.width}&height=${image.height}`);
      insertImage(tempUrl, image.width, image.height);
      uploadImg(form).then(({ code, data, message }) => {
        if (code == 0) {
          const editorImages = editorRef.value.querySelectorAll('img.editor-image');
          const lastImage = editorImages[editorImages.length - 1];
          if (lastImage && lastImage.src === tempUrl) {
            lastImage.src = data.ori_url;
            handleInput({ target: editorRef.value });
          }
        } else {
          window['$message'].error(message);
        }
      });
    };
    
          return
        }
      }
    }
  }
  
  
  const text = event.clipboardData?.getData('text/plain') || ''
  
  if (text) {
    
    const selection = window.getSelection()
    if (selection && selection.rangeCount > 0) {
      const range = selection.getRangeAt(0)
      range.deleteContents()
      range.insertNode(document.createTextNode(text))
      range.collapse(false)
      selection.removeAllRanges()
      selection.addRange(range)
      
      
      handleInput({ target: editorRef.value })
    }
  }
}
const insertLineBreak = (range) => {
  const editor = editorRef.value;
  if (!editor) return;

  const br = document.createElement('br');
  range.deleteContents();
  range.insertNode(br);

  const nbsp = document.createTextNode('\u200B'); 
  range.setStartAfter(br);
  range.insertNode(nbsp);
  range.setStartAfter(nbsp);
  range.collapse(true);

  const selection = window.getSelection();
  if (selection) {
    selection.removeAllRanges();
    selection.addRange(range);
  }

  editor.focus();
  nextTick(() => {
    handleInput({ target: editor });
  });
};

const handleKeydown = (event) => {
  
  if (showMention.value) {
    switch (event.key) {
      case 'ArrowUp':
        event.preventDefault()
        selectedMentionIndex.value = Math.max(0, selectedMentionIndex.value - 1)
        
        nextTick(() => {
          const mentionList = document.querySelector('.mention-list ul')
          const selectedItem = mentionList?.children[selectedMentionIndex.value]
          if (mentionList && selectedItem && selectedItem.offsetTop < mentionList.scrollTop) {
            mentionList.scrollTop = selectedItem.offsetTop
          }
        })
        break
      case 'ArrowDown':
        event.preventDefault()
        selectedMentionIndex.value = Math.min(mentionList.value.length - 1, selectedMentionIndex.value + 1)
        
        nextTick(() => {
          const mentionList = document.querySelector('.mention-list ul')
          const selectedItem = mentionList?.children[selectedMentionIndex.value]
          if (mentionList && selectedItem) {
            const itemBottom = selectedItem.offsetTop + selectedItem.offsetHeight
            const listBottom = mentionList.scrollTop + mentionList.clientHeight
            if (itemBottom > listBottom) {
              mentionList.scrollTop = itemBottom - mentionList.clientHeight
            }
          }
        })
        break
      case 'Enter':
      case 'Tab':
        event.preventDefault();
        const selectedMember = mentionList.value[selectedMentionIndex.value];
        if (selectedMember) {
          const selection = window.getSelection();
          if (selection && selection.rangeCount > 0) {
            insertMention(selectedMember, selection.getRangeAt(0).cloneRange());
          }
        }
        break;
      case 'Escape':
        hideMentionList()
        break
    }
    return
  }
  
  
  if (event.key === 'Backspace' || event.key === 'Delete') {
    const selection = window.getSelection()
    if (!selection.rangeCount) return
    
    const range = selection.getRangeAt(0)
    const editor = editorRef.value
    
    
    if (range.collapsed) {
      let targetMention = null
      
      
      const container = range.startContainer
      const offset = range.startOffset
      
      if (event.key === 'Backspace') {
        
        if (container.nodeType === Node.TEXT_NODE) {
          
          if (offset === 0) {
            let prevSibling = container.previousSibling
            while (prevSibling) {
              if (prevSibling.nodeType === Node.ELEMENT_NODE && 
                  prevSibling.classList && 
                  prevSibling.classList.contains('mention')) {
                targetMention = prevSibling
                break
              }
              
              if (prevSibling.nodeType === Node.TEXT_NODE && prevSibling.textContent.trim()) {
                break
              }
              prevSibling = prevSibling.previousSibling
            }
          } else {
            
            
            if (!container.textContent.trim()) {
              let prevSibling = container.previousSibling
              while (prevSibling) {
                if (prevSibling.nodeType === Node.ELEMENT_NODE && 
                    prevSibling.classList && 
                    prevSibling.classList.contains('mention')) {
                  targetMention = prevSibling
                  break
                }
                
                if (prevSibling.nodeType === Node.TEXT_NODE && prevSibling.textContent.trim()) {
                  break
                }
                prevSibling = prevSibling.previousSibling
              }
            }
          }
        } else if (container.nodeType === Node.ELEMENT_NODE) {
          
          if (offset > 0) {
            let prevChild = container.childNodes[offset - 1]
            
            if (prevChild && prevChild.nodeType === Node.ELEMENT_NODE && 
                prevChild.classList && prevChild.classList.contains('mention')) {
              targetMention = prevChild
            }
            
            else if (prevChild && prevChild.nodeType === Node.TEXT_NODE && !prevChild.textContent.trim()) {
              let prevSibling = prevChild.previousSibling
              while (prevSibling) {
                if (prevSibling.nodeType === Node.ELEMENT_NODE && 
                    prevSibling.classList && 
                    prevSibling.classList.contains('mention')) {
                  targetMention = prevSibling
                  break
                }
                if (prevSibling.nodeType === Node.TEXT_NODE && prevSibling.textContent.trim()) {
                  break
                }
                prevSibling = prevSibling.previousSibling
              }
            }
          } else if (offset === 0 && container === editor) {
            
            const firstChild = container.firstChild
            if (firstChild && firstChild.nodeType === Node.ELEMENT_NODE && 
                firstChild.classList && firstChild.classList.contains('mention')) {
              targetMention = firstChild
            }
          }
        }
      } else if (event.key === 'Delete') {
        
        if (container.nodeType === Node.TEXT_NODE) {
          
          if (offset === container.textContent.length) {
            let nextSibling = container.nextSibling
            while (nextSibling) {
              if (nextSibling.nodeType === Node.ELEMENT_NODE && 
                  nextSibling.classList && 
                  nextSibling.classList.contains('mention')) {
                targetMention = nextSibling
                break
              }
              
              if (nextSibling.nodeType === Node.TEXT_NODE && nextSibling.textContent.trim()) {
                break
              }
              nextSibling = nextSibling.nextSibling
            }
          }
        } else if (container.nodeType === Node.ELEMENT_NODE) {
          
          if (offset < container.childNodes.length) {
            const nextChild = container.childNodes[offset]
            if (nextChild && nextChild.nodeType === Node.ELEMENT_NODE && 
                nextChild.classList && nextChild.classList.contains('mention')) {
              targetMention = nextChild
            }
          }
        }
      }
      
      
      if (targetMention) {
        event.preventDefault()
        
        
        targetMention.remove()
        
        
        handleInput({ target: editor })
        return
      }
    }
  }
  
  
  if (event.key === 'Enter' && (event.ctrlKey || event.metaKey || event.shiftKey)) {
    event.preventDefault();

    const editor = editorRef.value;
    if (!editor) return;

    const selection = window.getSelection();
    if (!selection || selection.rangeCount === 0) {
      // 如果没有选区，尝试聚焦并创建选区
      editor.focus();
      // 等待DOM更新
      nextTick(() => {
        const newSelection = window.getSelection();
        if (newSelection && newSelection.rangeCount > 0) {
          insertLineBreak(newSelection.getRangeAt(0));
        }
      });
      return;
    }

    insertLineBreak(selection.getRangeAt(0));
    return;
  }

  

  
  
  if (event.key === 'Enter' && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
    event.preventDefault(); 

    const editor = editorRef.value;
    if (!editor) return;

    const messageData = parseEditorContent(); 
    const isEmptyMessage = messageData.items.length === 0 ||
                    (messageData.items.length === 1 &&
                     messageData.items[0].type === 1 &&
                     !messageData.items[0].content.trimEnd());

    if (isEmptyMessage) {
      if (editor.innerHTML !== '') { 
        clearEditor(); 
      }
      return; 
    }
    const quoteElement = editor.querySelector('.editor-quote');
    if (!quoteElement && quoteData.value) {
      quoteData.value = null;
    }

    sendMessage(); 
  }
}


const sendMessage = () => {
  
  const messageData = parseEditorContent()
  
  
  if (messageData.items.length === 0 || 
     (messageData.items.length === 1 && 
      messageData.items[0].type === 1 && 
      !messageData.items[0].content.trimEnd())) {
    return 
  }
  
  
  function cleanInvisibleChars(text) {
  return text.replace(/[\u200B-\u200D\uFEFF]/g, '');
}
  messageData.items.forEach(item => {
    
     if (item.type === 1 && cleanInvisibleChars(item.content).trimEnd()) { 
      const finalContent = cleanInvisibleChars(item.content).replace(/<br\s*\/?>/gi, '\n').trimEnd();
      if (!finalContent && !messageData.mentionUids.length && !messageData.quoteId) {
  
        return;
      }
      const data = {
        items: [{
          content: finalContent, 
          type: 1
        }],
        mentionUids: messageData.mentionUids,
        mentions: messageData.mentionUids.map(uid => {
          return {
            atid: uid,
            name: mentionList.value.find(member => member.id === uid)?.nickname || ''
          }
        }),
        quoteId: messageData.quoteId,
      }
      console.log('data',data)
      emit(
        'editor-event',
        emitCall('text_event', data)
      )
    } else if (item.type === 3) { 
      const data = {
        height: 0,
        width: 0,
        size: 10000,
        url: item.content,
      }
      console.log('图片消息data',data)
      emit(
        'editor-event',
        emitCall('image_event', data)
      )
    } else if (item.type === 4) { 
      
    }
  })
  
  
  clearEditor()
}


const parseEditorContent = () => {
  const items = []
  const mentionUids = []
  
  
  const tempDiv = document.createElement('div')
  tempDiv.innerHTML = editorHtml.value
  
  
  const quoteElements = tempDiv.querySelectorAll('.editor-quote')
  const hasQuote = quoteElements.length > 0 && quoteData.value
  const quoteId = hasQuote ? quoteData.value.id || '' : ''
  
  
  quoteElements.forEach(quote => quote.remove())
  
  let textContent = ''
  
  
  const processNode = (node) => {
    if (node.nodeType === Node.TEXT_NODE) {
      textContent += node.textContent;
      return;
    }

    if (node.nodeType !== Node.ELEMENT_NODE) return;

    if (node.tagName === 'BR') {
      textContent += '\n';
    } else if (node.classList.contains('mention')) {
      const userId = node.getAttribute('data-user-id');
      if (userId) {
        mentionUids.push(Number(userId));
      }
      textContent += node.textContent;
    } else if (node.tagName === 'IMG') {
      processImage(node);
    } else if (node.classList.contains('emoji')) {
      textContent += node.getAttribute('alt') || node.textContent;
    } else if (node.classList.contains('editor-file')) {
      processFile(node);
    } else if (node.childNodes.length) {
      Array.from(node.childNodes).forEach(processNode);
    } else {
      textContent += node.textContent;
    }
  }
  
  
  const processImage = (node) => {
    const src = node.getAttribute('src')
    const width = node.getAttribute('data-original-width') || node.getAttribute('width') || ''
    const height = node.getAttribute('data-original-height') || node.getAttribute('height') || ''
    const isEmoji = node.classList.contains('editor-emoji')
    
    
    if (textContent.trim()) {
      items.push({
        type: 1,
        content: textContent.trimEnd()
      })
      textContent = ''
    }
    
    if (isEmoji) {
      
      const altText = node.getAttribute('alt') || ''
      if (altText) {
        
        textContent += altText
      } else {
        
        items.push({
          type: 3,
          content: src + (width && height ? `?width=${width}&height=${height}` : ''),
          isEmoji: true
        })
      }
    } else {
      
      items.push({
        type: 3,
        content: src + (width && height ? `?width=${width}&height=${height}` : ''),
        width: width,
        height: height
      })
    }
  }
  
  
  const processFile = (node) => {
    const fileUrl = node.getAttribute('data-url')
    const fileName = node.getAttribute('data-name')
    const fileSize = node.getAttribute('data-size')
    
    
    if (textContent.trim()) {
      items.push({
        type: 1,
        content: textContent.trimEnd()
      })
      textContent = ''
    }
    
    if (fileUrl && fileName) {
      items.push({
        type: 4, 
        content: fileUrl,
        name: fileName,
        size: node.getAttribute('data-size-raw') || fileSize || 0
      })
    }
  }
  
  
  Array.from(tempDiv.childNodes).forEach(processNode)
  
  
  if (textContent) {
    items.push({
      type: 1,
      content: textContent
    });
  }
  
  
  return {
    items: items.length > 0 ? items : [{ type: 1, content: '' }],
    mentionUids,
    quoteId
  }
}


const clearEditor = () => {
  
  editorContent.value = ''
  editorHtml.value = ''
  quoteData.value = null
  
  
  if (editorRef.value) {
    editorRef.value.innerHTML = ''
    
    nextTick(() => editorRef.value.focus())
  }
  
  
  hideMentionList()
  
  
  saveDraft()
  
  
  emit('editor-event', {
    event: 'input_event',
    data: ''
  })
}




const insertImage = (src, width, height) => {
  const selection = window.getSelection()
  if (!selection.rangeCount) return
  
  const range = selection.getRangeAt(0)
  
  
  const img = document.createElement('img')
  img.src = src
  img.className = 'editor-image'
  img.alt = '图片'
  img.style.maxHeight = '150px'
  img.style.maxWidth = '150px'
  img.style.objectFit = 'contain' 
  
  
  if (width) img.setAttribute('data-original-width', width)
  if (height) img.setAttribute('data-original-height', height)
  
  range.deleteContents()
  range.insertNode(img)
  range.setStartAfter(img)
  range.collapse(true)
  selection.removeAllRanges()
  selection.addRange(range)
  
  editorRef.value.focus()
  handleInput({ target: editorRef.value })
}


const formatFileSize = (size) => {
  if (size < 1024) {
    return size + ' B'
  } else if (size < 1024 * 1024) {
    return (size / 1024).toFixed(2) + ' KB'
  } else if (size < 1024 * 1024 * 1024) {
    return (size / (1024 * 1024)).toFixed(2) + ' MB'
  } else {
    return (size / (1024 * 1024 * 1024)).toFixed(2) + ' GB'
  }
}

const onUploadSendImg=async (eventFile)=>{
  for (const file of eventFile.target.files) {
    const form = new FormData();
    form.append('file', file);
    form.append("source", "fonchain-chat");

   const res=await uploadImg(form)
   if(res.status===0){
    const data={
          height:0,
          width:0,
          size:10000,
          url:res.data.ori_url,
        }
        emit(
        'editor-event',
        emitCall(
          'image_event',
          data
        )
      )
   }
  }
}
async function onUploadFile(e) {
  const file = e.target.files[0]
  if (!file) return
  
  
  e.target.value = null
  
  if (file.type.indexOf('image/') === 0) {
    
    emit('editor-event', emitCall('image_event', file))

    return
  }

  if (file.type.indexOf('video/') === 0) {
    
    emit('editor-event', emitCall('video_event', file))
  } else {
    
    emit('editor-event', emitCall('file_event', file))
  }
}


const onEmoticonEvent = (emoji) => {
  
  emoticonRef.value?.setShow(false)
  
  
  switch (emoji.type) {
    case 'text':
    case 'emoji':
      
      insertTextEmoji(emoji.value)
      break
      
    case 'image':
      
      insertImageEmoji(emoji.img, emoji.value)
      break
      
    case 1: 
      emoji.img ? insertImageEmoji(emoji.img, emoji.value) : insertTextEmoji(emoji.value)
      break
      
    default:
      
      emit('editor-event', {
        event: 'emoticon_event',
        data: emoji.value || emoji.id
      })
      break
  }
}


const insertTextEmoji = (emojiText) => {
  const editor = editorRef.value
  if (!editor) return

  editor.focus()
  const selection = window.getSelection()
  if (!selection.rangeCount) return

  const range = selection.getRangeAt(0)
  range.deleteContents()

  const textNode = document.createTextNode(emojiText)
  range.insertNode(textNode)

  
  range.setStartAfter(textNode)
  range.collapse(true)
  selection.removeAllRanges()
  selection.addRange(range)

  
  handleInput({ target: editor })
}


const insertImageEmoji = (imgSrc, altText) => {
  const editor = editorRef.value
  if (!editor) return

  editor.focus()
  const selection = window.getSelection()
  if (!selection.rangeCount) return

  const range = selection.getRangeAt(0)
  range.deleteContents()

  const img = document.createElement('img')
  img.src = imgSrc
  img.alt = altText
  img.className = 'editor-emoji' 
  
  
  

  range.insertNode(img)

  
  range.setStartAfter(img)
  range.collapse(true)
  selection.removeAllRanges()
  selection.addRange(range)

  
  handleInput({ target: editor })
}


const onSubscribeMention = async (data) => {
  const editorNode = editorRef.value;
  if (!editorNode) return;

  editorNode.focus();
  await nextTick(); 

  let selection = window.getSelection();
  if (!selection || selection.rangeCount === 0) {
    const range = document.createRange();
    if (editorNode.lastChild) {
      range.setStartAfter(editorNode.lastChild);
    } else {
      range.setStart(editorNode, 0);
    }
    range.collapse(true);
    if (selection) selection.removeAllRanges();
    selection?.addRange(range);
    await nextTick(); 
    selection = window.getSelection(); 
  } else if (!editorNode.contains(selection.anchorNode)) {
    const range = document.createRange();
    if (editorNode.lastChild) {
      range.setStartAfter(editorNode.lastChild);
    } else {
      range.setStart(editorNode, 0);
    }
    range.collapse(true);
    selection.removeAllRanges();
    selection.addRange(range);
    await nextTick();
    selection = window.getSelection();
  }

  if (selection && selection.rangeCount > 0) {
    insertMention(data, selection.getRangeAt(0).cloneRange());
  }
};

// 在组件顶层作用域定义handleDeleteQuote函数
const handleDeleteQuote = function(e) {
  // 如果不是删除键或退格键，直接返回
  if (e.key !== 'Backspace' && e.key !== 'Delete') return;
  
  const selection = window.getSelection();
  if (selection.rangeCount === 0) return;
  
  const range = selection.getRangeAt(0);
  const editor = editorRef.value;
  if (!editor) return;
  
  const quoteElement = editor.querySelector('.editor-quote');
  
  if (!quoteElement) {
    // 如果没有引用元素，移除事件监听器
    editor.removeEventListener('keydown', handleDeleteQuote);
    return;
  }
  
  // 获取引用元素在编辑器子节点中的索引
  const quoteIndex = Array.from(editor.childNodes).indexOf(quoteElement);
  
  // 检查是否在引用元素前按下退格键
  const isBeforeQuote = e.key === 'Backspace' && 
                     range.collapsed && 
                     range.startContainer === editor && 
                     quoteIndex === range.startOffset;
  
  // 检查是否在引用元素后按下删除键
  const isAfterQuote = e.key === 'Delete' && 
                    range.collapsed && 
                    range.startContainer === editor && 
                    quoteIndex === range.startOffset - 1;
  
  if (isBeforeQuote || isAfterQuote) {
    // 阻止默认行为
    e.preventDefault();
    
    // 移除引用元素
    quoteElement.remove();
    quoteData.value = null;
    
    // 触发输入事件更新编辑器内容
    handleInput({ target: editor });
  }
};

const onSubscribeQuote = (data) => {
  
  quoteData.value = data
  
  
  const editor = editorRef.value
  if (!editor) return
  
  
  const existingQuotes = editor.querySelectorAll('.editor-quote')
  existingQuotes.forEach(quote => quote.remove())
  
  
  const selection = window.getSelection()
  const savedRange = selection.rangeCount > 0 ? selection.getRangeAt(0).cloneRange() : null
  const hasContent = editor.textContent.trim().length > 0
  
  
  const quoteElement = document.createElement('div')
  quoteElement.className = 'editor-quote'
  quoteElement.contentEditable = 'false' 
  
  
  quoteElement.innerHTML = `
    <div class="quote-content-wrapper">
      <div class="quote-title">${data.title}</div>
      ${data.image ? `<div class="quote-image"><img src="${data.image}" alt="引用图片" /></div>` : ''}
      ${data.describe ? `<div class="quote-content">${data.describe}</div>` : ''}
    </div>
    <div class="quote-close">×</div>
  `
  
  
  if (editor.firstChild) {
    editor.insertBefore(quoteElement, editor.firstChild)
  } else {
    editor.appendChild(quoteElement)
  }
  
  
  quoteElement.addEventListener('click', (e) => {
    
    const closeButton = e.target.classList?.contains('quote-close') ? e.target : e.target.closest('.quote-close')
    
    
    e.stopPropagation()
    
    if (closeButton) {
      
      quoteElement.remove()
      quoteData.value = null
      
      
      editorContent.value = editor.textContent || ''
      editorHtml.value = editor.innerHTML || ''
      
      
      nextTick(() => editor.focus())
    } else {
      
      const selection = window.getSelection()
      const range = document.createRange()
      range.setStartAfter(quoteElement)
      range.collapse(true)
      selection.removeAllRanges()
      selection.addRange(range)
      
      
      editor.focus()
    }
  })
  
  // 使用顶层作用域定义的handleDeleteQuote函数
  editor.addEventListener('keydown', handleDeleteQuote);
  
  
setTimeout(() => {
  
  if (!editor.childNodes.length || (editor.childNodes.length === 1 && editor.childNodes[0] === quoteElement)) {
    
    const textNode = document.createTextNode('\u200B'); 
    editor.appendChild(textNode);
  }
  
  
  const currentHtml = editor.innerHTML;
  editor.innerHTML = currentHtml;
  
  
  const newQuoteElement = editor.querySelector('.editor-quote');
  
  editor.focus();
  const newSelection = window.getSelection();
  const newRange = document.createRange();
  
  
  if (newQuoteElement) {
    
    let nextNode = newQuoteElement.nextSibling;
    if (nextNode && nextNode.nodeType === 3) { 
      newRange.setStart(nextNode, 0);
    } else if (nextNode) { 
      newRange.setStartBefore(nextNode);
    } else { 
      newRange.setStartAfter(newQuoteElement);
    }
  } else {
    
    if (editor.firstChild) {
      newRange.setStartBefore(editor.firstChild);
    } else {
      newRange.setStart(editor, 0);
    }
  }
  
  newRange.collapse(true);
  newSelection.removeAllRanges();
  newSelection.addRange(newRange);
  
  
  editor.scrollTop = editor.scrollHeight;
  
  
  handleInput({ target: editor });
  
  
  setTimeout(() => {
    editor.focus();
    
    if (newSelection.rangeCount === 0) {
      const finalRange = document.createRange();
      if (newQuoteElement) {
        finalRange.setStartAfter(newQuoteElement);
      } else {
        finalRange.setStart(editor, 0);
      }
      finalRange.collapse(true);
      newSelection.removeAllRanges();
      newSelection.addRange(finalRange);
    }
  }, 50);
}, 200); 
}

const onSubscribeEdit = (data) => {
  editingMessage.value = data
  clearEditor()
  
  
  if (data.content) {
    editorRef.value.innerHTML = data.content
    editorContent.value = data.content
    editorHtml.value = data.content
  }
}


const onSubscribeClear = () => {
  clearEditor()
}


const saveDraft = () => {
  if (!indexName.value || !editorRef.value) return
  
  
  
  const fragment = document.createDocumentFragment()
  const tempDiv = document.createElement('div')
  tempDiv.innerHTML = editorRef.value.innerHTML
  fragment.appendChild(tempDiv)
  
  
  const quoteElements = tempDiv.querySelectorAll('.editor-quote')
  quoteElements.forEach(quote => quote.remove())
  
  
  const contentToSave = tempDiv.textContent || ''
  const htmlToSave = tempDiv.innerHTML || ''
  const currentEditor= parseEditorContent().items
  
  const hasContent = contentToSave.trim().length > 0 || 
                    htmlToSave.includes('<img') || 
                    htmlToSave.includes('editor-file')
  
  
  if (currentEditor.length>0) {
    
    editorDraftStore.items[indexName.value] = JSON.stringify({
      content: currentEditor.reduce((result, x) => {
  if (x.type === 3) return result + '[图片]'
  if (x.type === 1) return result + x.content
  return result
}, ''),
      html: htmlToSave
    })
  } else {
    
    delete editorDraftStore.items[indexName.value]
  }
}


const loadDraft = () => {
  if (!indexName.value) return
  
  
  nextTick(() => {
    
    const currentQuoteData = quoteData.value
    
    
    quoteData.value = null
    
    
    if (!editorRef.value) return
    
    
    editorRef.value.innerHTML = ''
    editorContent.value = ''
    editorHtml.value = ''
    
    
    const draft = editorDraftStore.items[indexName.value]
    
    
    if (draft) {
      try {
        const draftData = JSON.parse(draft)
        
        
        editorRef.value.innerHTML = draftData.html || ''
        editorContent.value = draftData.content || ''
        editorHtml.value = draftData.html || ''
      } catch (error) {
        console.warn('加载草稿失败，使用空内容', error)
      }
    }
    
    
    if (currentQuoteData) {
      onSubscribeQuote(currentQuoteData)
    }
  })
}


watch(indexName, loadDraft, { immediate: true })


const handleDocumentClick = (event) => {
  if (!editorRef.value?.contains(event.target)) {
    hideMentionList()
  }
}


onMounted(() => {
  
  const subscriptions = [
    [EditorConst.Mention, onSubscribeMention],
    [EditorConst.Quote, onSubscribeQuote],
    [EditorConst.Edit, onSubscribeEdit],
    [EditorConst.Clear, onSubscribeClear]
  ]
  
  
  subscriptions.forEach(([event, handler]) => {
    bus.subscribe(event, handler)
  })

  
  editorRef.value?.addEventListener('click', handleEditorClick)
  
  
  document.addEventListener('click', handleDocumentClick)
  
  
  loadDraft()
})

/**
 * 组件生命周期钩子 - 组件卸载前
 * 清理所有事件订阅和监听器，防止内存泄漏
 */
onBeforeUnmount(() => {
  
  const subscriptions = [
    [EditorConst.Mention, onSubscribeMention],
    [EditorConst.Quote, onSubscribeQuote],
    [EditorConst.Edit, onSubscribeEdit],
    [EditorConst.Clear, onSubscribeClear]
  ]
  
  
  subscriptions.forEach(([event, handler]) => {
    bus.unsubscribe(event, handler)
  })

  
  editorRef.value?.removeEventListener('click', handleEditorClick)
  
  
  document.removeEventListener('click', handleDocumentClick)
  const editor = editorRef.value
  if (editor && handleDeleteQuote) {
    editor.removeEventListener('keydown', handleDeleteQuote)
  }
})

/**
 * 表情选择事件处理函数
 * 
 * @param {Object} emoji - 选中的表情对象
 * 
 * 当用户从表情选择器中选择表情时触发
 * 调用onEmoticonEvent插入表情到编辑器
 * 然后关闭表情选择器面板
 */
const onEmoticonSelect = (emoji) => {
  
  onEmoticonEvent(emoji)
  isShowEmoticon.value = false
}

/**
 * 代码块提交事件处理函数
 * 
 * @param {Object} data - 代码块数据
 * 
 * 当用户提交代码块时触发
 * 通过emit向父组件发送code_event事件
 * 传递代码块数据并关闭代码块面板
 */
const onCodeSubmit = (data) => {
  emit('editor-event', {
    event: 'code_event',
    data,
    callBack: () => {}
  })
  isShowCode.value = false
}

/**
 * 投票提交事件处理函数
 * 
 * @param {Object} data - 投票数据
 * 
 * 当用户提交投票时触发
 * 通过emit向父组件发送vote_event事件
 * 传递投票数据并关闭投票面板
 */
const onVoteSubmit = (data) => {
  emit('editor-event', {
    event: 'vote_event',
    data,
    callBack: () => {}
  })
  isShowVote.value = false
}


const handleEditorClick = (event) => {
  const closeButton = event.target.closest('.quote-close');

  if (closeButton) {
    const quoteElement = event.target.closest('.editor-quote');
    if (quoteElement) {
      quoteElement.remove();
      quoteData.value = null;

      handleInput({ target: editorRef.value });

      event.preventDefault();
      event.stopPropagation();
    }
  }
};
</script>

<template>
  <!-- 
    编辑器容器组件
    使用el-container布局组件创建垂直布局的编辑器
    包含工具栏和编辑区域两个主要部分
  -->
  <section class="el-container editor">
    <section class="el-container is-vertical">
      <!-- 
        工具栏区域 
        使用el-header组件作为工具栏容器
        包含各种编辑工具按钮
      -->
      <header class="el-header toolbar bdr-t">
        <div class="tools pr-30px">
          <!-- 
            表情选择器弹出框
            使用n-popover组件创建悬浮的表情选择面板
            通过trigger="click"设置点击触发
            raw属性表示内容不会被包装在额外的div中
          -->
          <n-popover
            placement="top-start"
            trigger="click"
            raw
            :show-arrow="false"
            :width="300"
            ref="emoticonRef"
            style="width: 500px; height: 250px; border-radius: 10px; overflow: hidden"
          >
            <!-- 
              触发器模板
              点击此区域会显示表情选择器
            -->
            <template #trigger>
              <div class="item pointer">
                <n-icon size="18" class="icon" :component="SmilingFace" />
                <p class="tip-title">表情符号</p>
              </div>
            </template>

            <!-- 
              表情选择器组件
              通过on-select事件将选中的表情传递给onEmoticonEvent处理函数
            -->
            <MeEditorEmoticon @on-select="onEmoticonEvent" />
          </n-popover>

          <!-- 
            工具栏其他功能按钮
            通过v-for循环渲染navs数组中定义的工具按钮
            每个按钮包含图标和提示文本
            通过v-show="nav.show"控制按钮的显示/隐藏
            点击时执行nav.click定义的函数
          -->
          <div
            class="item pointer"
            v-for="nav in navs"
            :key="nav.title"
            v-show="nav.show"
            @click="nav.click"
          >
            <n-icon size="18" class="icon" :component="nav.icon" />
            <p class="tip-title">{{ nav.title }}</p>
          </div>
          <n-button class="w-80px h-30px ml-auto" type="primary" @click="sendMessage">
            <template #icon>
        <n-icon>
          <IosSend />
        </n-icon>
      </template>
      发送
    </n-button>
        </div>
      </header>

      <!-- 
        编辑器主体区域
        使用el-main组件作为主要内容区域
      -->
      <main class="el-main height100">
        <!-- 
          可编辑区域
          使用contenteditable="true"使div可编辑，这是HTML5的属性
          ref="editorRef"用于获取DOM引用
          placeholder属性用于显示占位文本
          绑定多个事件处理函数处理用户交互
        -->
        <div
          ref="editorRef"
          class="custom-editor"
          contenteditable="true"
          :placeholder="placeholder"
          @input="handleInput"
          @keydown="handleKeydown"
          @paste="handlePaste"
          @focus="handleFocus"
          @blur="handleBlur"
        ></div>

        <!-- 
          @提及列表
          当用户输入@时显示的用户列表
          通过v-if="showMention"控制显示/隐藏
          使用动态样式定位列表位置
        -->
        <div
          v-if="showMention"
          class="mention-list py-5px"
          :style="{ top: mentionPosition.top + 'px', left: mentionPosition.left + 'px' }"
        >
          <ul class="max-h-140px w-163px overflow-auto hide-scrollbar">
            <li
              v-for="(member, index) in mentionList"
              :key="member.user_id || member.id"
              class="cursor-pointer px-14px h-42px"
              :class="{ 'bg-#EEE9F9': index === selectedMentionIndex }"
              @mousedown.prevent="handleMentionSelectByMouse(member)"
              @mouseover="selectedMentionIndex = index"
            >
              <div class="flex items-center border-b-1px border-b-solid border-b-#F8F8F8 h-full">
                <img class="w-26px h-26px rounded-50% mr-11px" :src="member.avatar" alt="">
                <span>{{ member.nickname }}</span>
              </div>
            </li>
          </ul>
        </div>
      </main>
    </section>
  </section>

  <!-- 
    隐藏的文件上传表单
    使用display: none隐藏表单
    包含两个文件输入框，分别用于上传图片和其他文件
    通过ref获取DOM引用
  -->
  <form enctype="multipart/form-data" style="display: none">
    <input type="file" ref="fileImageRef" accept="image/*" @change="onUploadSendImg" />
    <input type="file" ref="uploadFileRef" @change="onUploadFile" />
  </form>


</template>

<style lang="less" scoped>
/**
 * 编辑器组件样式
 * 使用CSS变量定义可复用的颜色值
 * --tip-bg-color: 提示背景色
 */
.editor {
  --tip-bg-color: rgb(241 241 241 / 90%);
  height: 100%;

  /**
   * 工具栏样式
   * 固定高度的顶部工具栏
   */
  .toolbar {
    height: 38px;
    display: flex;

    /**
     * 工具按钮容器
     * 使用flex布局排列工具按钮
     */
    .tools {
      height: 40px;
      flex: auto;
      display: flex;
      align-items: center;
      
      /**
       * 单个工具按钮样式
       * 使用flex布局居中对齐内容
       * 相对定位用于放置提示文本
       */
      .item {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 35px;
        margin: 0 2px;
        position: relative;
        user-select: none; 

        /**
         * 提示文本样式
         * 默认隐藏，悬停时显示
         * 使用绝对定位在按钮下方显示
         */
        .tip-title {
          display: none;
          position: absolute;
          top: 40px;
          left: 0px;
          line-height: 26px;
          background-color: var(--tip-bg-color);
          color: var(--im-text-color);
          min-width: 20px;
          font-size: 12px;
          padding: 0 5px;
          border-radius: 2px;
          white-space: pre; 
          user-select: none;
          z-index: 999999999999; 
        }

        /**
         * 悬停效果
         * 当鼠标悬停在按钮上时显示提示文本
         */
        &:hover {
          .tip-title {
            display: block;
          }
        }
      }
    }
  }
  :deep(.editor-file) {
    display: inline-block;
    padding: 5px 10px;
    margin: 5px 0;
    background-color: #f5f5f5;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    color: #462AA0;
    text-decoration: none;
    position: relative;
    padding-right: 60px; 
    max-width: 100%;
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis; 
    white-space: nowrap; 
    
    &::after {
      content: attr(data-size);
      position: absolute;
      right: 10px;
      color: #757575;
      font-size: 12px;
    }
    
    &:hover {
      background-color: #e3f2fd;
    }
  }

  :deep(.editor-emoji) {
    display: inline-block;
    width: 24px;
    height: 24px;
    vertical-align: middle; 
    margin: 0 2px;
  }
  :deep(.editor-quote) {
    margin-bottom: 8px;
    padding: 8px 12px;
    background-color: var(--im-message-left-bg-color, #f5f5f5);
    border-left: 3px solid var(--im-primary-color, #409eff);
    border-radius: 4px;
    font-size: 13px;
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); 
    cursor: pointer; 
    user-select: none; 
    transition: background-color 0.2s ease; 
    
    /**
     * 引用卡片悬停效果
     * 改变背景色提供视觉反馈
     */
    &:hover {
      background-color: var(--im-message-left-bg-hover-color, #eaeaea);
    }
    
    /**
     * 引用内容容器
     * 使用flex: 1占据剩余空间
     * 处理内容溢出
     */
    .quote-content-wrapper {
      flex: 1;
      overflow: hidden;
    }
    
    /**
     * 引用标题样式
     * 使用主题色显示引用来源
     */
    .quote-title {
      color: var(--im-primary-color, #409eff);
      margin-bottom: 4px;
      font-weight: 500;
    }
    
    /**
     * 引用内容样式
     * 限制显示行数，超出部分显示省略号
     * 使用-webkit-line-clamp实现多行文本截断
     */
    .quote-content {
      color: var(--im-text-color, #333);
      word-break: break-all; 
      white-space: normal; 
      overflow: hidden;
      text-overflow: ellipsis; 
      display: -webkit-box;
      -webkit-line-clamp: 2; 
      -webkit-box-orient: vertical;
    }
    
    /**
     * 引用图片样式
     * 限制图片大小，添加圆角
     */
    .quote-image img {
      max-width: 100px;
      max-height: 60px;
      border-radius: 3px;
      pointer-events: none; 
    }
    
    /**
     * 引用关闭按钮样式
     * 圆形按钮，悬停时改变背景色和文字颜色
     */
    .quote-close {
      width: 18px;
      height: 18px;
      line-height: 16px;
      text-align: center;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, 0.1);
      color: #666;
      cursor: pointer;
      font-size: 16px;
      margin-left: 8px;
      user-select: none;
      
      &:hover {
        background-color: rgba(0, 0, 0, 0.2);
        color: #333;
      }
    }
  }

  .custom-editor {
    width: 100%;
    height: 100%;
    padding: 8px;
    border: none;
    outline: none;
    resize: none;
    font-size: 14px;
    line-height: 1.5;
    color: #333;
    background: transparent;
    overflow-y: auto;

    
    &:empty:before {
      content: attr(placeholder);
      color: #999;
      pointer-events: none;
    }

    /**
     * 自定义滚动条样式 - 轨道
     * 使用::-webkit-scrollbar伪元素自定义滚动条轨道
     * 设置宽度和高度为3px，背景色为透明
     */
    &::-webkit-scrollbar {
      width: 3px;
      height: 3px;
      background-color: unset;
    }

    /**
     * 自定义滚动条样式 - 滑块
     * 使用::-webkit-scrollbar-thumb伪元素自定义滚动条滑块
     * 设置圆角和透明背景色
     */
    &::-webkit-scrollbar-thumb {
      border-radius: 3px;
      background-color: transparent;
    }

    /**
     * 滚动条悬停效果
     * 当鼠标悬停在编辑器上时，显示滚动条滑块
     * 使用CSS变量适配不同主题的滚动条颜色
     */
    &:hover {
      &::-webkit-scrollbar-thumb {
        background-color: var(--im-scrollbar-thumb);
      }
    }
  }

  /**
   * 编辑器占位符样式
   * 使用:empty::before伪元素在编辑器为空时显示占位文本
   * content: attr(placeholder)获取placeholder属性的值作为显示内容
   */
  .custom-editor:empty::before {
    content: attr(placeholder);
    color: #999;
    pointer-events: none; 
    font-family: PingFang SC, Microsoft YaHei, 'Alibaba PuHuiTi 2.0 45' !important;
  }

  /**
   * 编辑器聚焦样式
   * 移除默认的聚焦轮廓
   */
  .custom-editor:focus {
    outline: none;
  }

  /**
   * @提及样式
   * 为@提及的用户名添加特殊样式
   * 使用蓝色背景和文字颜色突出显示
   */
  .mention {
    color: #1890ff;
    background-color: #e6f7ff;
    padding: 2px 4px;
    border-radius: 3px;
    text-decoration: none;
    cursor: pointer;
  }

  /**
   * @提及悬停效果
   * 当鼠标悬停在@提及上时改变背景色
   */
  .mention:hover {
    background-color: #bae7ff;
  }

  /**
   * 图片样式
   * 限制编辑器中插入的图片大小
   * 添加圆角和鼠标指针样式
   */
  
  
  
  
  
  
  
  
  

  /**
   * 表情样式
   * 设置表情图片的大小和对齐方式
   */
  .editor-emoji {
    width: 20px;
    height: 20px;
    vertical-align: middle;
    margin: 0 2px;
  }

  /**
   * @提及列表样式
   * 定位和样式化@提及的下拉列表
   * 使用绝对定位、白色背景和阴影效果
   */
  .mention-list {
    position: absolute;
    background-color: white;
    border: 1px solid #eee;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    z-index: 1000;

  }

  
  .quote-card {
    background: #f5f5f5;
    border-left: 3px solid #1890ff;
    padding: 8px 12px;
    margin: 8px 0;
    border-radius: 4px;
    position: relative;
  }

  /**
   * 引用内容样式
   * 设置较小的字体大小
   */
  .quote-content {
    font-size: 12px;
  }

  /**
   * 引用标题样式
   * 使用粗体和主题蓝色
   * 添加底部间距
   */
  .quote-title {
    font-weight: bold;
    color: #1890ff;
    margin-bottom: 4px;
  }

  /**
   * 引用文本样式
   * 使用灰色文本和适当的行高
   */
  .quote-text {
    color: #666;
    line-height: 1.4;
  }

  /**
   * 引用关闭按钮样式
   * 绝对定位在右上角
   * 移除默认按钮样式，添加鼠标指针
   */
  .quote-close {
    position: absolute;
    top: 4px;
    right: 4px;
    background: none;
    border: none;
    cursor: pointer;
    color: #999;
    font-size: 12px;
  }

  /**
   * 编辑提示样式
   * 使用橙色背景和边框创建警示效果
   * 添加内边距、圆角和适当的字体大小
   * 使用flex布局对齐内容
   */
  .edit-tip {
    background: #fff7e6;
    border: 1px solid #ffd591;
    padding: 6px 12px;
    margin-bottom: 8px;
    border-radius: 4px;
    font-size: 12px;
    color: #d46b08;
    display: flex;
    align-items: center;
    gap: 8px;
  }

  /**
   * 编辑提示按钮样式
   * 移除默认按钮样式，添加鼠标指针
   * 使用margin-left: auto将按钮推到右侧
   */
  .edit-tip button {
    background: none;
    border: none;
    cursor: pointer;
    color: #d46b08;
    margin-left: auto;
  }
}

/**
 * 暗色模式样式调整
 * 使用HTML属性选择器检测暗色主题
 * 重新定义暗色模式下的CSS变量
 */
html[theme-mode='dark'] {
  .editor {
    --tip-bg-color: #48484d;
  }
}

/**
 * 隐藏滚动条样式
 * 保留滚动功能但隐藏滚动条的视觉显示
 */
.hide-scrollbar {
  
  &::-webkit-scrollbar {
    width: 0;
    display: none;
  }
  
  
  scrollbar-width: none;
  
  
  -ms-overflow-style: none;
}
</style>