金山在线文档编辑器

官方文档地址:快速开始-WebOffice 知识库

首先按照文档写的方式将包引入项目了

util.js

import WebOfficeSDK from "../../public/JSEditor/open-jssdk-v0.0.13.umd"
export function WordSDK(url, isEdit, mountDom, isShowTopArea, isShowHeader) {
  const el = document.querySelector(mountDom)
  if (!el) return null
  const instance = WebOfficeSDK.config({
    url,
    mount: el,
    commandBars: [
      {
        cmbId: 'HeaderMiddle', // 组件 ID
        attributes: {
          enable: isEdit, // 禁用组件(组件显示但不响应点击事件)
        },
      },
    ],
    wordOptions: {
      enableReadOnlyComment: isEdit,
      isBestScale: false, // 打开文档时,默认以最佳比例显示
    },
    commonOptions: {
      isShowTopArea, // 隐藏顶部区域(头部和工具栏)
      isShowHeader, // 隐藏头部区域
      // isBrowserViewFullscreen: false, // 是否在浏览器区域全屏
      // isIframeViewFullscreen: false, // 是否在 iframe 区域内全屏
      // acceptVisualViewportResizeEvent: true, // 控制 WebOffice 是否接受外部的 VisualViewport
    },
  });
  instance.setToken({
    // token: `bearer ${getToken('token')}`,
    token: '',
    timeout: 10 * 60 * 1000,
  });
  return instance;
}

封装编辑器组件:KSEditor.vue

<template>
  <div ref="editorContainer" class="editor-container"></div>
</template>

<script>
  import {WordSDK} from "@/utils/util"

  export default {
    name: 'JsEditorComponent',
    props: {
      KSfile: {
        type: String,
        default: ''
      },
      isShowHeader: {
        type: Boolean,
        default: true
      },
      isShowTopArea: {
        type: Boolean,
        default: true
      },
      isEditable: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        editorInstance: null,
        ksConfig: {
          // 在这里配置金山在线文档编辑器的初始化参数
          // 比如 token、文档ID 等
        }
      };
    },
    mounted() {
      // 在组件加载完成后初始化编辑器
      this.initEditor()
    },
    methods: {
      initEditor() {
        let KSInstance = WordSDK(
          this.KSfile,
          this.isEditable,
          '.editor-container',
          this.isShowTopArea,
          this.isShowHeader,
        )
        this.$emit('setInstance', KSInstance)
      },
      createDocument() {
        // 在点击按钮时触发新建文档操作
        // 假设SDK提供了相应的方法来新建文档
        if (this.editor) {
          this.editor.createDocument(); // 假设SDK方法名为createDocument
        } else {
          console.error('编辑器尚未初始化');
        }
      }
    }
  };
</script>

<style scoped>
  /* 可能需要一些样式来控制编辑器容器的宽度和高度 */
  .editor-container {
    width: 100%;
    height: 600px;
  }
</style>

页面testKSEditor.vue

<template>
  <div style="width: 100%">
    <el-upload
      class="compare-uploader"
      ref="upload"
      :with-credentials="true"
      :show-file-list="false"
      withCredentials
      :http-request="httpRequest"
      :on-progress="onProgress"
      :multiple="false"
    >
      <!-- 文件上传-->
      <el-button
        type="ghost"
        slot="trigger"
        size="mini"
      >
        导入文件
      </el-button>
    </el-upload>
    id:<el-input v-model="ksFileInfoCopy.id"/><br/>
    文件类型:<el-input v-model="ksFileInfoCopy.ext"/><br/>
    文件名称:<el-input v-model="ksFileInfoCopy.name"/><br/>
    <el-button
      @click="()=>getFileUrl(this.ksFileInfoCopy.id, this.ksFileInfoCopy.ext)"
    >
      刷新
    </el-button>
    <el-button
      @click="()=>destroyKS()"
    >
      销毁
    </el-button>
    <el-button
      @click="()=>insertBookmark('TEXT')"
    >
      插入书签
    </el-button>
    <el-button
      @click="()=>insertBookmark('TABLE')"
    >
      插入表格类型书签
    </el-button>
    <el-button
      @click="() => this.bookmarks = []"
    >
      清空
    </el-button>
    <el-button
      @click="()=>changeMode()"
    >
      {{mode === 'edit'?'预览':'编辑'}}
    </el-button>
    <el-button
      @click="()=>replaceBookmarkValue()"
    >
      替换书签值
    </el-button>
    <JSEditor
      v-if="showKS"
      @setInstance="setInstance"
      :KSfile="KSfile"
      :isShowTopArea="isShowTopArea"
      :isShowHeader="isShowHeader"
      :isEditable="isEditableFile"
    />
  </div>
</template>

<script>
    import axios from "axios";
    import JSEditor from 'lib@/components/KSEditor'
    import {userTableMap} from "@/modules/testKSeditor/fieldMap";

    const userTable = userTableMap
    export default {
      name: "index",
      components: {
        JSEditor
      },
      data () {
        // supplementRules:
        return {
          userId: this.$store.getters.userId,
          fileLoading: false,
          app: null,
          bookmark: null,
          KSfile: '',
          KSurl: '',
          showKS: false,
          isShowHeader: true,
          isShowTopArea: true,
          isEditableFile: true,
          ksFileInfo: {
            ext: 'docx',
            id: '0af2cc2e1da145f48667a6bd09d30e77',
            // id: '6170f21b9fe94db78bc2e56d8f543c1e',
            name: '工治具报修管理.docx',
            url: ''
          },
          ksFileInfoCopy: {
            ext: 'docx',
            id: '0af2cc2e1da145f48667a6bd09d30e77',
            // id: '6170f21b9fe94db78bc2e56d8f543c1e',
            name: '工治具报修管理.docx',
            url: ''
          },
          tableData: [
            {
              number: '1',
              name: 'yy',
              sex: 'b',
              age: '11'
            },
            {
              number: '2',
              name: 'ee',
              sex: 'g',
              age: '22'
            },
            {
              number: '3',
              name: 'ss',
              sex: 'b',
              age: '33'
            },
            {
              index: '4',
              name: 'ff',
              sex: 'g',
              age: '44'
            },
          ],
          mode: 'edit',
          bookmarks: [],
          KSInstance: null
        }
      },
      methods: {
        async setInstance(instance){
          // console.log(instance, '1111')
          this.KSInstance = instance
          await instance.ready()
          if(this.mode === 'preview'){
            const app = instance.Application
            await app.ActiveDocument.SetReadOnly({
              Value: true
            })
          }
          // console.log(app, 'this.KSInstance.Application')
        },
        httpRequest(fileObj) {
          const formData = new FormData();
          formData.append("file", fileObj.file);
          axios({
            url: 'https://sdap-dev.sunwoda.com/api/platform-oss/public/upload?bucket=sdap',
            method: "post",
            data: formData,
          })
            .then(res => {
              this.ksFileInfo = res.data.datas
              this.ksFileInfoCopy = res.data.datas
              this.getFileUrl(res.data.datas.id, res.data.datas.ext)
            })
            .catch(error => {
              console.log(error);
            });
        },
        getFileUrl(fileId, ext, isPreview) {
          this.showKS = false
          this.fileLoading = true
          axios({
            url: 'https://sdap-dev.sunwoda.com/api/platform-system/public/wps-api/edit-url',
            method: "get",
            params: { fileId, ext, previewMode: 'high_definition' }
          })
            .then(res => {
              this.fileLoading = false
              this.KSfile = res.data.datas
              this.KSurl = res.data.datas
              this.ksFileInfoCopy.url = isPreview ? this.ksFileInfo.url : res.data.datas
              this.ksFileInfo.url = res.data.datas
              this.showKS = true
            })
            .catch(error => {
              console.log(error);
            });
        },
        destroyKS(){
          this.KSInstance.destroy()
        },
        async insertBookmark(type){
          const bookmark = await this.KSInstance.Application.ActiveDocument.Bookmarks;
          const selection = await this.KSInstance.Application.ActiveDocument.ActiveWindow.Selection
          // 区域对象
          let mark = 'mark' + Date.now()
          const range = await selection.Range
          const start = await range.Start
          let content = type + '书签'+ mark
          const end = start + content.length
          // 在选区后面插入内容
          await selection.InsertAfter(content)
          await bookmark.Add({
            Name: mark,
            Range: {
              Start: start,
              End: end
            }
          })
          this.bookmarks.push({
            markName: mark,
            type: type,
            value: mark + '的值'
          })
        },
        changeMode(){
          if(this.mode === 'edit'){
            this.mode = 'preview'
            this.isShowHeader = false
            this.isShowTopArea = false
            this.isEditableFile = false
            let replaceData = []
            let modelLines = this.bookmarks // 合同元素相关信息
            // let allMarks = await this.KSInstance.Application.ActiveDocument.Bookmarks.Json()
            for (const i of modelLines) {
              replaceData.push({
                bookmark: i.markName,
                type: i.type,
                text: i.value
              })
            }

            let template_url = 'https://sdap-dev.sunwoda.com/api/platform-oss/public/download/' + this.ksFileInfo.id
            let data = {
              task_id: this.userId,
              template_url: template_url,
              template_filename: this.ksFileInfo.name,
              use_template_section_property:true,
              flatten_source_style: true,
              sample_list: replaceData
            }
            axios({
              url: 'https://sdap-dev.sunwoda.com/api/platform-system/public/wps-api/wrap-header',
              method: 'post',
              data
            }).then(res => {
              if(res.data.resp_code === 200){
                this.getReviewFile(res.data.datas.download_id)
              }
            })
          } else {
            this.mode = 'edit'
            this.ksFileInfoCopy = this.ksFileInfo
            this.isShowHeader = true
            this.isShowTopArea = true
            this.isEditableFile = true
            this.getFileUrl(this.ksFileInfo.id, this.ksFileInfo.ext)
          }
        },
        getReviewFile(id){
          let data = {
            bucket: 'sdap',
            downloadId: id,
            fileName: id + '测试文档.docx'
          }
          axios({
            url: ' https://sdap-dev.sunwoda.com/api/platform-system/public/wps-api/upload-file',
            method: 'post',
            params: data
          }).then(res => {
            this.ksFileInfoCopy = res.data.datas
            this.getFileUrl(res.data.datas.id, res.data.datas.ext, true)
          })
        },
        async replaceBookmarkValue(){
          let modelLines = this.bookmarks // 合同元素相关信息
          let allMarks = await this.KSInstance.Application.ActiveDocument.Bookmarks.Json()
          let replaceData = []
          for (const i of modelLines) {
            if (i.type === 'TABLE') {
              let tableData = JSON.parse(JSON.stringify(this.tableData))
              let header = ["序号", "姓名", "性别", "年龄"]
              await this.insertTable(header, tableData, userTable, i)
            } else {
              replaceData.push({
                name: i.markName,
                type: 'text',
                value: i.value
              })
            }
          }
          this.KSInstance.Application.ActiveDocument.Bookmarks.ReplaceBookmark(replaceData)
        },
        async insertTable(tableHead, tableBodyData, fieldMap, eleInfo){
          const tables = await this.KSInstance.Application.ActiveDocument.Tables // 编辑器的表格对象
          let tableData = JSON.parse(JSON.stringify(tableBodyData))
          tableData.map((i, ind) => {
            i.number = ind + 1 + ''
          })
          let headerData = {}
          let fields = tableHead.map((elmKey, index) => {
            if (!!elmKey) {
              headerData[fieldMap.get(elmKey)] = elmKey
              return {
                name: elmKey,
                field: fieldMap.get(elmKey)
              }
            }
          })
          tableData.unshift(headerData)
          let rowCount = tableData.length, colCount = tableHead.length
          // 插入表格
          await tables.Add(
            this.KSInstance.Application.ActiveDocument.Bookmarks.Item(eleInfo.markName).Range, // 位置信息
            rowCount, // 新增表格的行数
            colCount, // 新增表格的列数
            1, // 启用自动调整功能
            1 // 根据表格中包含的内容自动调整表格的大小
          )
          const count = await tables.Count  // 表格数量
          const curTable = await tables.Item(count)
          console.log(count, 'count')
          // console.log(tableData, 'tableData')
          // console.log(headerData, 'headerData')
          // console.log(fields, 'fields')
          for (let j = 1; j < rowCount + 1; j++) { // 遍历行
            let row = tableData[j-1]
            for (let f = 1; f < colCount + 1; f++) {  // 遍历列
              let fieldsInfo = fields[f-1]
              // console.log(fieldsInfo, 'fieldsInfo')
              let field = row[fieldsInfo.field] || ''
              const cell = await curTable.Rows.Item(j).Cells.Item(f).Range
              cell.Text = field + ''
            }
          }
          this.KSInstance.Application.ActiveDocument.ReplaceText([ // 将书签的内容替换成空字符串
            {
              key: 'TABLE书签' + eleInfo.markName,
              value: ''
            }
          ])
        }
      }
    }
</script>

<style scoped>

</style>
fieldMap.js
export const userTableMap = new Map();
userTableMap.set('序号', "number");
userTableMap.set('姓名', "name");
userTableMap.set("性别", "sex");
userTableMap.set("年龄", "age");

前端代码实现书签替换页面效果:

两张截图是分开截的,效果差不多是这样。是有bug的,给表格赋值不太稳定。书签替换也不稳定,没截到连续的图就是因为这个,老有问题,不想截了,不稳定的原因不明。

还可以调用在线编辑的接口实现书签替换,这种方式不支持替换成表格,但支持替换成在线文档,就是可以建一个在线文档,里面加表格,然后将这个在线文档引入进来。我用的接口是我们的后端封装过的,具体的找金山的询问,毕竟要用这个肯定得买,他们肯定会派人支持。

调用接口的方式,可以在“编辑”“预览”之前多次跳转,因为这个方法替换书签不是在原文档上修改,是后台会新建一个文件来替换书签,并且这个新文档会缓存24小时,在这时间内可以下载。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 金山文档在线预览编辑demo是一种方便的在线文档编辑工具,可以在任何时间和任何地方创建和编辑文档。这个工具提供了一个简单易用的界面,可以直接在网页上创建、编辑和共享文档,而不需要下载或安装任何软件,非常方便。 金山文档在线预览编辑demo还具有实时协作的功能,多个用户可以同时编辑同一份文档,而且所有修改都可以实时同步到其他用户的文档中。这个功能可以大大提高团队的协作效率,减少沟通成本,提高工作效率。 此外,金山文档在线预览编辑demo还支持多种文档格式,包括Word、Excel、PPT等,用户可以根据自己的需求选择合适的格式进行编辑和保存。同时,这个工具还提供了丰富的格式化选项,如字体、颜色、排版等,可以满足用户不同的排版需求。 总的来说,金山文档在线预览编辑demo是一款非常实用的在线文档编辑工具,可以大大提高团队的协作效率,降低文档编辑的成本,应该得到广泛的应用。 ### 回答2: 金山文档在线预览编辑demo是一个非常强大的在线文档编辑工具。首先,它允许用户在线预览和编辑各种格式的文档,如Microsoft Office文档、PDF文件和图片等。这意味着用户可以在一个统一的平台上完成不同类型文档编辑,而不必安装各种软件或导入或导出文档。其次,金山文档在线预览编辑demo的界面非常简洁易懂,用户可以很快熟悉并掌握它的使用方法。此外,该工具的编辑功能非常强大,可以实现字体格式、段落、列表、表格、超链接、插入图片、批注等多种编辑功能,同时还能自动保存,避免编辑时因疏忽或其他原因导致的丢失。此外,金山文档在线预览编辑demo还支持实时协作,即多人同时编辑同一份文档,实现团队协作,增强办公效率,非常适合团队合作和远程办公。总的来说,金山文档在线预览编辑demo是一款高效、实用、易用的在线文档编辑工具,是办公自动化时代不可缺少的办公工具之一。 ### 回答3: 金山文档在线预览编辑demo是一款非常方便实用的文档预览和编辑工具。 用户可以通过这款工具在网页中编辑文档,修改文字的大小、颜色、字体等格式,也可以插入图片、表格、图表等内容,帮助用户更好地表达自己的思想和观点。 除了编辑功能之外,金山文档在线预览编辑demo还提供了文档预览功能,用户可以在不离开网页的情况下浏览和查阅文章。文档的格式不会发生改变,用户可以随时进行修改、保存、分享。 为了进一步提高用户的使用体验,金山文档在线预览编辑demo还具有自动保存功能,用户在编辑过程中不用担心意外断电或者系统故障导致的数据丢失,所有数据都可以自动保存在云端中,用户可以随时随地访问。 总体来说,金山文档在线预览编辑demo是一款功能强大、易用性高的在线文档管理工具。在工作和学习中,用户可以随时使用它,轻松创建和编辑文档,提高工作效率,为个人和企业带来优异的成果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值