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

概述

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

实现效果

实现

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

html 复制代码
<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. 文档编辑

javascript 复制代码
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);

2. 文章预览

javascript 复制代码
const { createOpenViewer } = window.Doc;
// 创建阅读器
 const viewer = createOpenViewer(this.$refs.editor, {});
viewer.setDocument('text/lake', docContent);
相关推荐
冬奇Lab2 小时前
一天一个开源项目(第14篇):CC Workflow Studio - 可视化AI工作流编辑器,让AI自动化更简单
人工智能·开源·编辑器
承渊政道5 小时前
Linux系统学习【Linux基础开发工具】
linux·运维·笔记·学习·centos·编辑器
徐小夕@趣谈前端19 小时前
Web文档的“Office时刻“:jitword共建版2.0发布!让浏览器变成本地生产力
前端·数据结构·vue.js·算法·开源·编辑器·es6
zhanglianzhao1 天前
Win 11 WSL 配置Claude code 并在VsCode中使用
ide·vscode·编辑器·claude·cladue code
Keying,,,,1 天前
VScode中终端闪退问题
ide·vscode·编辑器
热爱生活的五柒1 天前
如何在vscode中使用Claude code以及插件中配置setting.json无效的解决方法
ide·vscode·编辑器
ONLYOFFICE1 天前
ONLYOFFICE 自动化工具:宏和 AI 函数如何选择?
运维·自动化·编辑器·onlyoffice
JQLvopkk1 天前
VSCode基础使用
ide·vscode·编辑器
一路向北North2 天前
vscode 安装插件非常慢
ide·vscode·编辑器
民国二十三画生2 天前
Trae.cn 编辑器四大功能(builder/chat/builder with MCP/SOLO coder)大白话区别
编辑器