社区 API Demo 成为赞助者
相关 API: options.comment,Vditor.getCommentIds,Vditor.hlCommentIds,Vditor.unHlCommentIds,Vditor.removeCommentIds
⚠️ 注意事项:

    const bindCommentEvent = (cmtElement) => {
      const inputElement = cmtElement.querySelector('input')
      const id = cmtElement.getAttribute('data-id')
      inputElement.addEventListener('blur', () => {
        if (inputElement.value.trim() === '') {
          vditor.removeCommentIds([id])
          cmtElement.remove()
        }
      })
      cmtElement.querySelector('button').addEventListener('click', () => {
        vditor.removeCommentIds([id])
        removeComment(cmtElement, id)
      })

      cmtElement.addEventListener('mouseover', () => {
        vditor.hlCommentIds([id])
      })

      cmtElement.addEventListener('mouseout', () => {
        vditor.unHlCommentIds([id])
      })
    }

    const removeComment = (cmtElement, id) => {
      cmtElement.remove()

      let cmts = localStorage.getItem('cmts')
      if (!cmts) {
        return
      } else {
        cmts = JSON.parse(cmts)
      }
      cmts.find((item, index) => {
        if (item.id === id) {
          cmts.splice(index, 1)
          return true
        }
      })
      localStorage.setItem('cmts', JSON.stringify(cmts))
    }

    const renderComments = (ids) => {
      let cmts = localStorage.getItem('cmts')
      if (!cmts) {
        localStorage.setItem('cmts', '[]')
        cmts = []
      } else {
        cmts = JSON.parse(cmts)
      }

      ids.forEach(id => {
        let text = ''
        cmts.find((item) => {
          if (item.id === id) {
            text = item.text
            return true
          }
        })

        const cmtElement = document.createElement('div')
        cmtElement.setAttribute('data-id', id)
        cmtElement.innerHTML = `<div>
${text}<br>
<button>删除</button><br>
<input class="vcomment__input">
</div>`
        cmtElement.value = text
        document.getElementById('comments').
          insertAdjacentElement('beforeend', cmtElement)
        bindCommentEvent(cmtElement)
      })
    }

    vditor = new Vditor('vditor', {
      mode: 'wysiwyg',
      height: 360,
      cache: false,
      value: '选中文字后即可进行评论',
      comment: {
        enable: true,
        add (id, text) {
          const cmtElement = document.createElement('div')
          cmtElement.setAttribute('data-id', id)
          cmtElement.innerHTML = `<div>
${text}<br>
<button>删除</button><br>
<input class="vcomment__input">
</div>`
          cmtElement.value = text
          document.getElementById('comments').
            insertAdjacentElement('beforeend', cmtElement)
          bindCommentEvent(cmtElement)
          cmtElement.querySelector('input').focus()
          let cmts = localStorage.getItem('cmts')
          if (!cmts) {
            localStorage.setItem('cmts', '[]')
            cmts = []
          } else {
            cmts = JSON.parse(cmts)
          }
          cmts.push({id, text})
          localStorage.setItem('cmts', JSON.stringify(cmts))
        },
        remove (ids) {
          ids.forEach((id) => {
            removeComment(document.querySelector(`#comments div[data-id="${id}"]`),
              id)
          })
        },
      },
      after () {
        renderComments(vditor.getCommentIds())
      },
    })
        

参与讨论