• 基于语雀编辑器的在线文档编辑与查看


    概述

    语雀是一个非常优秀的文档和知识库工具,其编辑器更是非常好用,虽无开源版本,但有编译好的可以使用。本文基于语雀编辑器实现在线文档的编辑与文章的预览。

    实现效果

    image.png

    image.png

    实现

    参考语雀编辑器官方文档,其实现需要引入以下文件:

    <link rel="stylesheet" type="text/css" href="https://gw.alipayobjects.com/render/p/yuyan_npm/@alipay_lakex-doc/1.1.0-beta.1/umd/doc.css"/>
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/antd@4.24.13/dist/antd.css"/>
    <script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js">script>
    <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js">script>
    <script src="https://gw.alipayobjects.com/render/p/yuyan_npm/@alipay_lakex-doc/1.1.0-beta.1/umd/doc.umd.js">script>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1. 文档编辑

    const { createOpenEditor, toolbarItems } = window.Doc;
    docEditor = createOpenEditor(this.$refs.editor, {
      toolbar: {
        agentConfig: {
          default: {
            items: [
              toolbarItems.cardSelect,
              '|',
              toolbarItems.undo,
              toolbarItems.redo,
              toolbarItems.formatPainter,
              toolbarItems.clearFormat,
              '|',
              toolbarItems.style,
              toolbarItems.fontsize,
              toolbarItems.bold,
              toolbarItems.italic,
              toolbarItems.strikethrough,
              toolbarItems.underline,
              toolbarItems.mixedTextStyle,
              '|',
              toolbarItems.color,
              toolbarItems.bgColor,
              '|',
              toolbarItems.alignment,
              toolbarItems.unorderedList,
              toolbarItems.orderedList,
              toolbarItems.indent,
              toolbarItems.lineHeight,
              '|',
              toolbarItems.taskList,
              toolbarItems.link,
              toolbarItems.quote,
              toolbarItems.hr,
            ]
          },
          // table选区工具栏
          table: {
            items: [
              toolbarItems.cardSelect,
              '|',
              toolbarItems.undo,
              toolbarItems.redo,
              toolbarItems.formatPainter,
              toolbarItems.clearFormat,
              '|',
              toolbarItems.style,
              toolbarItems.fontsize,
              toolbarItems.bold,
              toolbarItems.italic,
              toolbarItems.strikethrough,
              toolbarItems.underline,
              toolbarItems.mixedTextStyle,
              '|',
              toolbarItems.color,
              toolbarItems.bgColor,
              toolbarItems.tableCellBgColor,
              toolbarItems.tableBorderVisible,
              '|',
              toolbarItems.alignment,
              toolbarItems.tableVerticalAlign,
              toolbarItems.tableMergeCell,
              '|',
              toolbarItems.unorderedList,
              toolbarItems.orderedList,
              toolbarItems.indent,
              toolbarItems.lineHeight,
              '|',
              toolbarItems.taskList,
              toolbarItems.link,
              toolbarItems.quote,
              toolbarItems.hr,
            ],
          }
        }
      },
      image: {
        isCaptureImageURL:(url) => {
          // return false表示需要转存,会调用createUploadPromise
          return false;
        },
        // 配置上传接口,要返回一个promise对象
        createUploadPromise: (request) => {
          const {type, data} = request;
          if(type === 'url') {
            return Promise.resolve({
              url: url,
              filename: '上传图片'
            });
          } else if(type === 'file') {
            return new Promise(resolve => {
              let formData = new FormData();
              formData.set("file", data);
              //这里使用封装的上传文件的接口
              upload('file/upload/img', formData)
                .then(res => {
                  if(res.code === 200) {
                    const {fileName, url} = res.data
                    resolve({
                      url: url,
                      filename: fileName
                    });
                  } else {
                    ElMessage({
                      message: '图片上传失败!',
                      type: 'warning',
                    })
                  }
                })
            })
          }
        },
      }
    });
    window.docEditor = docEditor
    
    // 获取文档内容
    docEditor.getDocument('text/lake')
    
    // 设置文档内容
    docEditor.setDocument('text/lake', docContent);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121

    2. 文章预览

    const { createOpenViewer } = window.Doc;
    // 创建阅读器
     const viewer = createOpenViewer(this.$refs.editor, {});
    viewer.setDocument('text/lake', docContent);
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    稀疏矩阵的压缩存储
    【MySQL进阶】多表连接的原理
    Javascript的同步与异步,异步的宏任务与微任务——事件循环
    Unity3D PRO 3D游戏制作系列教程第四课:认识菜单二
    mybatis驼峰映射
    分类预测 | MATLAB实现基于GRU-AdaBoost门控循环单元结合AdaBoost多输入分类预测
    基于JAVA+SpringBoot+Mybatis+MYSQL的课程在线学习系统
    涂鸦智能物联网平台初探
    【2-3个月左右录用】物联网、无线通信类、人工智能、传感器、人机交互等领域必投快刊,进展顺利,12月截稿
    数组及其应用
  • 原文地址:https://blog.csdn.net/GISShiXiSheng/article/details/132724644