董国庆
昨天 fe6509e66e6852f6b5d740385bdae4fd0a11da77
laboratory/src/components/AiEditor/index.vue
@@ -57,7 +57,8 @@
    value: {
      handler(newVal) {
        if (this.editor && newVal !== this.editor.getHtml()) {
          this.editor.setContent(newVal)
          const contentWithIp = this.addIpToContent(newVal);
          this.editor.setContent(contentWithIp)
        }
      },
      immediate: true
@@ -78,18 +79,91 @@
    this.destroyEditor()
  },
  methods: {
    // 去除内容中的IP地址,只保留相对路径
    removeIpFromContent(content) {
      if (!content || typeof content !== 'string') return content;
      // 获取当前IP地址
      const currentIp = getAllocateIp();
      // 使用正则表达式匹配并替换所有包含当前IP的URL
      const ipRegex = new RegExp(currentIp.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
      return content.replace(ipRegex, '');
    },
    // 为内容中的相对路径添加IP地址,并处理已包含IP的绝对路径
    addIpToContent(content) {
      if (!content || typeof content !== 'string') return content;
      // 获取当前IP地址
      const currentIp = getAllocateIp();
      // 匹配所有src、href、poster属性
      const pathRegex = /(src|href|poster)="([^"]+)"/g;
      return content.replace(pathRegex, (match, attr, path) => {
        // 如果路径是相对路径(不以http开头),则添加当前IP地址
        if (!path.startsWith('http')) {
          // 检查路径是否已经以/images/开头,避免重复添加
          if (path.startsWith('/images/')) {
            // 如果currentIp已经以/images/结尾,则去掉路径开头的/
            if (currentIp.endsWith('/images/')) {
              return `${attr}="${currentIp}${path.substring(1)}"`;
            } else {
              return `${attr}="${currentIp}${path}"`;
            }
          } else {
            return `${attr}="${currentIp}${path}"`;
          }
        }
        // 如果路径已经包含当前IP地址,直接返回
        if (path.startsWith(currentIp)) {
          return match;
        }
        // 如果路径是绝对路径,提取路径部分并替换为当前IP地址
        // 匹配形如 http://192.168.1.100:8080/xxx 的路径
        const absolutePathRegex = /^https?:\/\/[^\/]+(\/.*)$/;
        const matchResult = path.match(absolutePathRegex);
        if (matchResult) {
          // 提取路径部分,替换为当前IP地址
          let pathPart = matchResult[1];
          // 如果currentIp已经以/images/结尾,且pathPart以/images/开头,则去掉pathPart开头的/images/
          if (currentIp.endsWith('/images/') && pathPart.startsWith('/images/')) {
            pathPart = pathPart.substring(7); // 去掉开头的 '/images/'
            // 如果pathPart以/开头,则去掉开头的/
            if (pathPart.startsWith('/')) {
              pathPart = pathPart.substring(1);
            }
          }
          return `${attr}="${currentIp}${pathPart}"`;
        }
        // 其他情况(如外部链接)保持不变
        return match;
      });
    },
    initEditor() {
      const options = {
        element: this.$refs.editorRef,
        placeholder: this.placeholder,
        content: this.value,
        content: this.addIpToContent(this.value),
        editable: !this.readOnly,
        toolbar: this.toolbar,
        toolbarExcludeKeys: ["ai"],
        draggable: false,
        onChange: (content) => {
          this.$emit('input', content)
          this.$emit('change', content)
          // 确保content是字符串类型
          const contentStr = typeof content === 'string' ? content : String(content || '');
          const contentWithoutIp = this.removeIpFromContent(contentStr);
          this.$emit('input', contentWithoutIp)
          this.$emit('change', contentWithoutIp)
        },
        onFocus: () => {
          this.$emit('focus')
@@ -205,6 +279,7 @@
      }
      console.log('this.addIpToContent(this.value)',this.addIpToContent(this.value))
      try {
        this.editor = new AiEditor(options)
      } catch (error) {
@@ -217,14 +292,17 @@
        this.editor = null
      }
    },
    // 获取编辑器内容
    // 获取编辑器内容(去除IP地址,只保留相对路径)
    getContent() {
      return this.editor ? this.editor.getHtml() : ''
      if (!this.editor) return '';
      const content = this.editor.getHtml();
      return this.removeIpFromContent(content);
    },
    // 设置编辑器内容
    // 设置编辑器内容(为相对路径添加IP地址)
    setContent(content) {
      if (this.editor) {
        this.editor.setContent(content)
        const contentWithIp = this.addIpToContent(content);
        this.editor.setContent(contentWithIp);
      }
    },
    // 清空编辑器内容