VUE 实现划词 问AI 翻译等功能

实现原理: 通过监听鼠标 mouseup mousedown等事件 定位选中的词语 然后计算浮窗位置

javascript 复制代码
    handleMouseUp(e) {
      // 排除拖动图标、no-word-select类的元素
      if (e.target.closest('.drag-icon') || e.target.closest(".no-word-select")) return;

      let selectedText = '';
      let positionData = null; // 存储定位所需的数据(Range或鼠标坐标+元素)
      const target = e.target;
      const targetTag = target.tagName;

      // 处理textarea/input的选中文本
      if (targetTag === 'TEXTAREA' || (targetTag === 'INPUT' && target.type === 'text')) {
        const el = target;
        const start = el.selectionStart;
        const end = el.selectionEnd;
        if (start < end) {
          selectedText = el.value.substring(start, end).trim();
          // 存储鼠标坐标和元素本身(用于输入框定位)
          positionData = {
            type: 'input',
            clientX: e.clientX,
            clientY: e.clientY,
            element: el
          };
        }
      } else {
        // 普通元素的选中文本
        const selection = window.getSelection();
        selectedText = selection.toString().trim();
        if (selectedText) {
          positionData = {
            type: 'normal',
            range: selection.getRangeAt(0)
          };
        }
      }

      // 赋值选中文本
      this.selectedText = selectedText;

      // 有选中文本且有定位数据时,显示浮窗并计算位置
      if (this.selectedText && positionData) {
        this.calculatePosition(positionData);
        this.visible = true;
      } else {
        // 无选中文本时隐藏浮窗
        this.visible = false;
        this.explainVisible = false;
      }
    },
    // 计算浮窗位置
    calculatePosition(positionData) {
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
      const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
      const floatWidth = 240;
      const floatHeight = 34;

      if (positionData.type === 'input') {
        // 输入框的定位逻辑:基于鼠标松开的坐标
        const { clientX, clientY, element } = positionData;
        // 浮窗位置:鼠标位置下方8px,水平居中于鼠标位置
        this.top = clientY + scrollTop + 8;
        this.left = clientX + scrollLeft - floatWidth / 2;

        // 额外处理:防止浮窗超出输入框的右侧/左侧
        const elRect = element.getBoundingClientRect();
        const elRight = elRect.right + scrollLeft;
        const elLeft = elRect.left + scrollLeft;
        // 浮窗右边界超出输入框右边界时,左移
        if (this.left + floatWidth > elRight) {
          this.left = elRight - floatWidth - 8;
        }
        // 浮窗左边界超出输入框左边界时,右移
        if (this.left < elLeft) {
          this.left = elLeft + 8;
        }
      } else {
        // 普通元素的定位逻辑:基于Range的坐标
        const rect = positionData.range.getBoundingClientRect();
        this.top = rect.bottom + scrollTop + 8;
        this.left = rect.left + scrollLeft + (rect.width - floatWidth) / 2;
      }

      // 全局边界处理:防止浮窗超出屏幕
      const screenWidth = document.documentElement.clientWidth;
      const screenHeight = document.documentElement.clientHeight;

      if (this.left + floatWidth > screenWidth) this.left = screenWidth - floatWidth - 8;
      if (this.left < 0) this.left = 8;
      if (this.top + floatHeight > screenHeight) this.top = this.top - floatHeight - 16; // 向上偏移
    },

获取到所选词语后,调用相应的大模型,我这边用的是Dify的API,另外一个文章有写实现方法

相关推荐
海石2 小时前
📱随时随地大小编:TraeSolo 移动端初体验
前端·ai编程·trae
爱滑雪的码农3 小时前
详细说说React大型项目结构以及日常开发核心语法
前端·javascript·react.js
七牛开发者4 小时前
HTML is the new Markdown:来自 Claude Code 团队的实践
前端·人工智能·语言模型·html
@大迁世界4 小时前
43.HTML 事件处理和 React 事件处理有什么区别?
前端·javascript·react.js·html·ecmascript
CloneCello4 小时前
AI时代程序员认知调整指南
前端
ZC跨境爬虫5 小时前
跟着 MDN 学 HTML day_38:(DocumentFragment 文档片段接口详解)
前端·javascript·ui·html·音视频
@大迁世界6 小时前
41.ShadCN 是什么?它如何和 Tailwind CSS 集成,从而更容易构建可访问且可自定义的 React 组件?
前端·javascript·css·react.js·前端框架
千叶风行6 小时前
Text-to-SQL 技术设计与注意事项
前端·人工智能·后端
软件开发技术深度爱好者6 小时前
HTML5+JavaScript读取DOCX 文档完整内容
前端·html5
幽络源小助理7 小时前
苹果CMS V10 MXPro V4.5模版下载, 自适应视频主题源码, 幽络源源码
前端·开源·源码·php源码