js 自定义粘贴内容 ,粘贴之后用快捷键ctrl+z无法撤回,请问有没有办法解决?

有个需求要修改粘贴里边的内容。但是无法做到快捷键撤回,但是原生的是可以撤回的。代码如下

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="wrap">
      <div
        id="editableDiv"
        contenteditable="true"
        suppressContentEditableWarning="true"
        style="width: 500px; min-height: 50px; border: 1px solid #ccc"
      >
        text1
      </div>
      <script>
        editableDiv.onpaste = (e) => {
          let paste = e.clipboardData.getData("text");

          const selection = window.getSelection() || {};
          if (!selection.rangeCount) return false;
          selection.deleteFromDocument();

          //对要粘贴的内容做处理 代码省略
          let newPaste = paste;
          let range = selection.getRangeAt(0);
          let newDom = document.createTextNode(newPaste);
          range.insertNode(newDom);
          range.setStartAfter(newDom);
          e.preventDefault();
        };
      </script>
    </div>
  </body>
</html>
阅读 2.3k
2 个回答

1、在复制时修改剪切板的内容。

editableDiv.addEventListener("copy", (event) => {
  const selection = document.getSelection();
  const originData = selection.toString();
  event.clipboardData.setData(
    "text/html",
    `<span style="color:red;">${originData}</span>`
  );
  event.preventDefault();
});

2、拦截粘贴事件。

editableDiv.addEventListener("paste", (event) => {
  console.log("paste handler");
  const data = event.clipboardData.getData("text");

  const value = `<span style="color:red;">${data}</span>`;

  document.execCommand("insertHTML", false, value);
  event.preventDefault();
});
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <div id="wrap">
    <div
      id="editableDiv"
      contenteditable="true"
      suppressContentEditableWarning="true"
      style="width: 500px; min-height: 50px; border: 1px solid #ccc"
    >
      text1
    </div>
    <script>
      editableDiv.onpaste = (e) => {
        let paste = e.clipboardData.getData("text");

        const selection = window.getSelection() || {};
        if (!selection.rangeCount) return false;
        // selection.deleteFromDocument();

        if (selection.isCollapsed) {
          return true
        }



        //对要粘贴的内容做处理 代码省略
        let newPaste = paste;
        let range = selection.getRangeAt(0);

        let { anchorOffset, focusOffset } = selection
        console.log(anchorOffset, focusOffset);
        let offset = Math.min(anchorOffset, focusOffset)

        range.setStart(editableDiv.firstChild, offset)
        range.setEnd(editableDiv.firstChild, offset)

        // let newDom = document.createTextNode(newPaste);
        // range.insertNode(newDom);
        // range.setStartAfter(newDom);
        // e.preventDefault();
      };
    </script>
  </div>
</body>

</html>

完美解决问题

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏