左键选择v-html绑定的文本内容,松开鼠标后出现复制弹窗

HTML

html 复制代码
<template>
  <div class="report_generate_wrap" id="parentElement">
    <div ref="textContainer" @mouseup="mouseUp" v-html="textContent"></div>
    <div v-if="showCopyPopup" :style="copyPopupStyle" class="copy-popup">
      <p>已选中文本,点击复制:</p>
      <button @click="copyText" style="margin-right: 10px">复制</button>
      <button @click="hideCopyPopup">取消</button>
    </div>
  </div>
</template>

JS

javascript 复制代码
data() {
    return {
      textContent: "这是一段示例文本,你可以尝试选中它。",
      showCopyPopup: false,
      selectedText: "",
      lastMousePosition: { x: 0, y: 0 },
      previousSelectedText: "",
    };
  },

computed: {
  copyPopupStyle() {
    return {
      position: "absolute",
      left: `${this.lastMousePosition.x + 20}px`, // 假设弹窗距离鼠标右侧20px
      top: `${this.lastMousePosition.y}px`,
      transform: "translateY(-50%)", // 垂直居中
    };
  },
},

mounted() {
  window.addEventListener("click", this.handleGlobalClick);
},

beforeDestroy() {
  window.removeEventListener("click", this.handleGlobalClick);
},

methods: {
  handleGlobalClick(event) {
    // 如果点击事件不是发生在textContainer元素内,则隐藏复制弹窗
    if (!this.$refs.textContainer.contains(event.target)) {
      this.showCopyPopup = false;
    }
  },
  mouseUp(event) {
    console.log("鼠标松开");
    this.updateMousePosition(event);
    const selection = window.getSelection();
    if (selection.rangeCount > 0) {
      const range = selection.getRangeAt(0);
      this.selectedText = range.toString().trim();

      // 检查当前选中的文本是否与之前选中的文本不同
      if (this.selectedText !== this.previousSelectedText) {
        if (this.selectedText !== "") {
          this.showCopyPopup = true;
        } else {
          this.showCopyPopup = false;
        }
        // 更新之前选中的文本
        this.previousSelectedText = this.selectedText;
      } else {
        // 如果相同,则不显示复制弹出框
        this.showCopyPopup = false;
      }
    } else {
      this.showCopyPopup = false;
      this.selectedText = "";
      this.previousSelectedText = ""; // 清除之前选中的文本
    }
  },
  updateMousePosition(event) {
    const parentElement = document.getElementById("parentElement");
    this.lastMousePosition = {
      x: event.clientX - parentElement.getBoundingClientRect().left,//鼠标相对于父元素的位置
      y: event.clientY,
    };
  },

  copyText() {
    const textarea = document.createElement("textarea");
    textarea.value = this.selectedText;
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand("copy");
    document.body.removeChild(textarea);
    this.hideCopyPopup();
  },
  hideCopyPopup() {
    this.showCopyPopup = false;
  },
}

CSS

javascript 复制代码
<style scoped lang="less">
.copy-popup {
  background-color: white;
  border: 1px solid #ccc;
  padding: 10px;
  z-index: 1000;
}
</style>
相关推荐
来恩10034 分钟前
jQuery选择器
前端·javascript·jquery
前端繁华如梦6 分钟前
树上挂苹果还是挂玻璃球?Three.js 程序化果实的完整实现指南
前端·javascript
墨痕诉清风13 分钟前
Web浏览器客户端检测网站网络健康(代码)
前端·网络·测试工具
IMPYLH16 分钟前
Linux 的 wc 命令
linux·运维·服务器·前端·bash
happybasic32 分钟前
Python库升级标准流程~
linux·前端·python
川冰ICE37 分钟前
前端工程化深度实战:从Webpack5到Vite5的构建工具演进与选型决策
前端
CDwenhuohuo39 分钟前
优惠券组件直接用 uview plus
前端·javascript·vue.js
用户74090472362751 小时前
我用 curl 排查了一次 OpenAI-compatible API 连接失败:401、403、404 分别怎么定位
前端
kft13141 小时前
XSS深度剖析:从弹窗到持久化窃取Cookie
前端·web安全·xss·安全测试
烬羽1 小时前
《前端三权分立:HTML、CSS、JS为什么不能“乱搞”》
前端