当前位置:   article > 正文

Toast UI Editor富文本编辑器_@toast-ui/editor

@toast-ui/editor

安装npm

  1. $ npm install @toast-ui/editor 存在一定的被墙风险
  2. $ npm install @toast-ui/editor @<版本> 存在一定的被墙风险

如何使用

首先在html中加入需要tui-editor绑定的元素节点

<div id="editorSection"></div>

编辑器模式

yarn/npm install方式引入

  1. import '@toast-ui/editor/dist/toastui-editor.css';
  2. import '@toast-ui/editor/dist/toastui-editor-viewer.css';
  3. import Editor from '@toast-ui/editor';
  4. const editor = new Editor({
  5. el: document.querySelector('#editorSection'),
  6. initialEditType: 'markdown', // 默认编辑模式,还支持富文本编辑模式
  7. previewStyle: 'vertical', / 编辑样式,还支持tab切换的形式
  8. height: '300px'
  9. });

cdn方式

  1. let editor = new toastui.Editor({
  2. el: document.querySelector('#editorSection'),
  3. initialEditType: 'markdown', // 编辑器markdown 还是 其他形式
  4. previewStyle: 'vertical', // 是否是切一半的页面,另外参数 tab
  5. height: window.innerHeight, // 高度
  6. hideModeSwitch: true, // 不展示底部tab切换
  7. placeholder: '请输入正文...',
  8. });

展示模式

  1. this.viewer = new toastui.Editor.factory({
  2. el: document.querySelector('#viewerSection'),
  3. height: window.innerHeight + 'px',
  4. viewer: true,
  5. initialValue: '初始化值'
  6. });

以上是cdn格式使用方式,只展示正文,使用 npm 包引入时只需把 toastui 去掉即可。

编辑器自带的插件

需要单独npm或cdn引入

npm包名称描述cdn csscdn js
@toast-ui/editor-plugin-chart插件呈现图表uicdn.toast.com/tui.chart/v…uicdn.toast.com/editor-plug…
@toast-ui/editor-plugin-code-syntax-highlight插件突出显示代码语法cdnjs.cloudflare.com/ajax/libs/h…uicdn.toast.com/editor-plug…
@toast-ui/editor-plugin-color-syntax插件颜色编辑文本uicdn.toast.com/tui-color-p…uicdn.toast.com/editor-plug…
@toast-ui/editor-plugin-table-merged-cell合并表列的插件--uicdn.toast.com/editor-plug…
@toast-ui/editor-plugin-uml呈现UML的插件--uicdn.toast.com/editor-plug…

如何引入这些插件

  1. const { Editor } = toastui;
  2. const { chart, codeSyntaxHighlight, colorSyntax, tableMergedCell, uml } = Editor.plugin;
  3. const chartOptions = {
  4. minWidth: 100,
  5. maxWidth: 600,
  6. minHeight: 100,
  7. maxHeight: 300
  8. };
  9. // 编辑器
  10. const editor = new Editor({
  11. el: document.querySelector('#editor'),
  12. previewStyle: 'vertical',
  13. height: '500px',
  14. initialValue: '',
  15. plugins: [[chart, chartOptions], codeSyntaxHighlight, colorSyntax, tableMergedCell, uml]
  16. });
  17. // 不可编辑的视图
  18. const viewer = Editor.factory({
  19. el: document.querySelector('#viewer'),
  20. viewer: true,
  21. height: '500px',
  22. initialValue: allPluginsContent,
  23. plugins: [[chart, chartOptions], codeSyntaxHighlight, tableMergedCell, uml]
  24. });

 一些重要的api讲解

钩子函数

addImageBlobHook

初始化时定义 ,用于监听编辑器中文件的变化,从而自定义方法,如:图片上传服务器

  1. this.editor = new tui.Editor({
  2. el: document.querySelector('#editorSection'),
  3. height: window.innerHeight, // 高度
  4. hooks: { // 钩子函数
  5. addImageBlobHook: (fileOrBlob, callback) => {
  6. this.uploadImgApi(fileOrBlob).then(path => {
  7. callback(path, 'T_T,出错了');
  8. });
  9. },
  10. },
  11. });

fileOrBlob 返回一个文件对象 callback 回调函数。

监听聚焦事件

  1. this.editor = new Editor({
  2. el: document.querySelector('#editor'),
  3. height: 'calc(100vh - 400px)',
  4. initialEditType: 'markdown',
  5. initialValue: this.contentForm.content,
  6. previewStyle: 'vertical',
  7. hooks: {
  8. // 聚焦事件
  9. focus: () => {
  10. //业务逻辑...
  11. }
  12. }
  13. })

编辑器对象api ToastUIEditor

getCodeMirror

由于编辑器闪烁光标 是 自定义的 div,该编辑器提供获取光标位置对象的方法,在后续源码的挖掘中发现,CodeMirrortui-editor内部核心解析库,它融入了tui-editor中。

let getCodeMirror = this.editor.getCodeMirror();
 

insertText(text)

插入文本,注意,这里他会记录上一次的光标位置插入。

this.editor.insertText('```\n\n```');
 

CodeMirrorExt 光标对象

通过getCodeMirror获取

重点强调两个

getCursor(start)

获取光标位置 start : 'from'|'to'|'head'|'anchor'

setCursor(line,ch)

该方法并非文档中暴露方法,而是阅读源码后知晓的方法。该方法可以更好地控制 光标的位置

  1. let getCodeMirror = this.editor.getCodeMirror();
  2. this.editor.insertText('```\n\n```');
  3. getCodeMirror.setCursor(getCodeMirror.getCursor().line - 1, 0);

上面代码先获取光标对象,在指定位置插入代码段,由于插入后 ,光标会移动到代码段末尾,影响用户体验,于是这里提供了一个 setCursor ,设置光标的位置,达到效果。

Toolbar 顶部快捷菜单 api

获取 顶部 ui 实例的方法

  1. const toolbarArr = [
  2. {
  3. name: 'uploadQiniu',
  4. tooltip: '选择图片',
  5. el: () => {
  6. const button = document.createElement('button');
  7. button.className = 'tui-image tui-toolbar-icons';
  8. return button;
  9. },
  10. index: 14,
  11. callback: (_this, callback) => {
  12. _this.uploadImg();
  13. if (callback) {
  14. callback();
  15. }
  16. }
  17. },
  18. {
  19. name: 'code',
  20. tooltip: '代码段',
  21. el: () => {
  22. const button = document.createElement('button');
  23. button.className = 'tui-codeblock tui-toolbar-icons';
  24. return button;
  25. },
  26. index: 15,
  27. callback: (_this, callback) => {
  28. let getCodeMirror = _this.editor.getCodeMirror();
  29. _this.editor.insertText('```\n\n```');
  30. getCodeMirror.setCursor(getCodeMirror.getCursor().line - 1, 0);
  31. if (callback) {
  32. callback();
  33. }
  34. }
  35. },
  36. ]
  37. this.toolbar = this.editor.getUI().getToolbar();
  38. toolbarArr.forEach(toolbar => {
  39. this.editor.eventManager.addEventType(toolbar.name);
  40. this.editor.eventManager.listen(toolbar.name, () => {
  41. toolbar.callback(this);
  42. });
  43. this.toolbar.insertItem(toolbar.index, {
  44. type: 'button',
  45. options: {
  46. name: toolbar.name,
  47. className: '',
  48. event: toolbar.name,
  49. tooltip: toolbar.tooltip,
  50. el: toolbar.el()
  51. }
  52. });
  53. });

这是推荐的写法,因为项目到后期,新增的toolbar会很多,这里直接把它抽出去,通过数组循环的形式简化了代码。

官网:  Editor | TOAST UI :: Make Your Web Delicious!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/54402
推荐阅读
相关标签
  

闽ICP备14008679号