Toast UI Editor富文本编辑器
安装npm
$ npm install @toast-ui/editor 存在一定的被墙风险
$ npm install @toast-ui/editor @<版本> 存在一定的被墙风险
如何使用
首先在html中加入需要tui-editor绑定的元素节点
<div id="editorSection"></div>
编辑器模式
yarn/npm install
方式引入
import '@toast-ui/editor/dist/toastui-editor.css';
import '@toast-ui/editor/dist/toastui-editor-viewer.css';
import Editor from '@toast-ui/editor';const editor = new Editor({el: document.querySelector('#editorSection'),initialEditType: 'markdown', // 默认编辑模式,还支持富文本编辑模式previewStyle: 'vertical', / 编辑样式,还支持tab切换的形式height: '300px'
});
cdn方式
let editor = new toastui.Editor({el: document.querySelector('#editorSection'),initialEditType: 'markdown', // 编辑器markdown 还是 其他形式previewStyle: 'vertical', // 是否是切一半的页面,另外参数 tabheight: window.innerHeight, // 高度hideModeSwitch: true, // 不展示底部tab切换placeholder: '请输入正文...',
});
展示模式
this.viewer = new toastui.Editor.factory({el: document.querySelector('#viewerSection'),height: window.innerHeight + 'px',viewer: true,initialValue: '初始化值'
});
以上是cdn
格式使用方式,只展示正文,使用 npm 包引入时只需把 toastui 去掉即可。
编辑器自带的插件
需要单独npm或cdn引入
npm包名称 | 描述 | cdn css | cdn 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… |
如何引入这些插件
const { Editor } = toastui;
const { chart, codeSyntaxHighlight, colorSyntax, tableMergedCell, uml } = Editor.plugin;const chartOptions = {minWidth: 100,maxWidth: 600,minHeight: 100,maxHeight: 300
};// 编辑器
const editor = new Editor({el: document.querySelector('#editor'),previewStyle: 'vertical',height: '500px',initialValue: '',plugins: [[chart, chartOptions], codeSyntaxHighlight, colorSyntax, tableMergedCell, uml]
});// 不可编辑的视图
const viewer = Editor.factory({el: document.querySelector('#viewer'),viewer: true,height: '500px',initialValue: allPluginsContent,plugins: [[chart, chartOptions], codeSyntaxHighlight, tableMergedCell, uml]
});
一些重要的api讲解
钩子函数
addImageBlobHook
初始化时定义 ,用于监听编辑器中文件的变化,从而自定义方法,如:图片上传服务器
this.editor = new tui.Editor({el: document.querySelector('#editorSection'),height: window.innerHeight, // 高度hooks: { // 钩子函数addImageBlobHook: (fileOrBlob, callback) => {this.uploadImgApi(fileOrBlob).then(path => {callback(path, 'T_T,出错了');});},},
});
fileOrBlob
返回一个文件对象 callback
回调函数。
监听聚焦事件
this.editor = new Editor({el: document.querySelector('#editor'),height: 'calc(100vh - 400px)',initialEditType: 'markdown',initialValue: this.contentForm.content,previewStyle: 'vertical',hooks: {// 聚焦事件focus: () => {//业务逻辑...}}})
编辑器对象api ToastUIEditor
getCodeMirror
由于编辑器闪烁光标 是 自定义的 div,该编辑器提供获取光标位置对象的方法,在后续源码的挖掘中发现,CodeMirror
是tui-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)
该方法并非文档中暴露方法,而是阅读源码后知晓的方法。该方法可以更好地控制 光标的位置
let getCodeMirror = this.editor.getCodeMirror();
this.editor.insertText('```\n\n```');
getCodeMirror.setCursor(getCodeMirror.getCursor().line - 1, 0);
上面代码先获取光标对象,在指定位置插入代码段,由于插入后 ,光标会移动到代码段末尾,影响用户体验,于是这里提供了一个 setCursor ,设置光标的位置,达到效果。
Toolbar 顶部快捷菜单 api
获取 顶部 ui 实例的方法
const toolbarArr = [{name: 'uploadQiniu',tooltip: '选择图片',el: () => {const button = document.createElement('button');button.className = 'tui-image tui-toolbar-icons';return button;},index: 14,callback: (_this, callback) => {_this.uploadImg();if (callback) {callback();}}},{name: 'code',tooltip: '代码段',el: () => {const button = document.createElement('button');button.className = 'tui-codeblock tui-toolbar-icons';return button;},index: 15,callback: (_this, callback) => {let getCodeMirror = _this.editor.getCodeMirror();_this.editor.insertText('```\n\n```');getCodeMirror.setCursor(getCodeMirror.getCursor().line - 1, 0);if (callback) {callback();}}},
]this.toolbar = this.editor.getUI().getToolbar();toolbarArr.forEach(toolbar => {this.editor.eventManager.addEventType(toolbar.name);this.editor.eventManager.listen(toolbar.name, () => {toolbar.callback(this);});this.toolbar.insertItem(toolbar.index, {type: 'button',options: {name: toolbar.name,className: '',event: toolbar.name,tooltip: toolbar.tooltip,el: toolbar.el()}});
});
这是推荐的写法,因为项目到后期,新增的toolbar会很多,这里直接把它抽出去,通过数组循环的形式简化了代码。
官网: Editor | TOAST UI :: Make Your Web Delicious!
Toast UI Editor富文本编辑器相关推荐
- 小程序editor 富文本编辑器组件
效果: 关于editor 富文本编辑器组件 复制到组件common 里面.json 引用即可 链接:https://pan.baidu.com/s/1_QIp28IOVuyVFfKfFZwxKQ 提取 ...
- 小程序的四次元口袋:editor富文本编辑器的使用、渲染,以及rich-text进行解析
editor富文本 1.介绍 富文本编辑器,可以对图片.文字进行编辑.极大地丰富了我们文章的结构样式,之前小程序还没有自己开发富文本插件,而在2019年微信小程序正式发布了富文本api. 支持的类型 ...
- froala editor富文本编辑器出现验证失败的解决方法
froala editor富文本编辑器出现验证失败的解决方法 出现这种情况,首先要下载源码包地址 https://www.froala.com/wysiwyg-editor 之后引用本地的 froal ...
- uniapp 微信小程序 editor富文本编辑器 api 使用记录
文章目录 1.查看官方示例 2.关于富文本编辑器的工具栏 3.自己实践一下 效果 这里记录一下自己研究学习的结果 之前一直使用textarea 来进行内容的编辑.但是局限性还是太多,最近发现了edit ...
- 云开发小程序editor富文本编辑器上传图片实现增删改
在做一个博客小程序,用的是云开发,用editor编辑器上传图片,一开始直接传图片会出现真机不显示的情况,排查原因后发现是因为传入的是本地路径,而小程序要求的是https或者云id,于是做了以下修改:先 ...
- 【uniapp-内置组件editor富文本编辑器】
在b站跟着咸虾米老师学的 editor组件 <template><view class="edit"><view class="title& ...
- uniapp editor富文本编辑器,h5富文本编辑器封装成插件
重点提示:工具栏的图标请自行在iconfont(https://www.iconfont.cn/)去添加,否则图标无法显示 公司业务需求,需要在h5实现富文本编辑功能.uniapp的editor可以实 ...
- 使用微信小程序editor富文本编辑器(爬坑要点)
编辑器wxml <view class="container" style="height:{{editorHeight}}px;"><edi ...
- uni-app editor富文本编辑器
<form @submit="formCompany" @reset="formReset"> <view class="uni-f ...
最新文章
- 怎么让BarTender对象等间距分布
- 解决No version of NDK matched the requested version问题
- java 工程新建ivy文件_Hadoop学习之路(八)在eclispe上搭建Hadoop开发环境
- python open函数 创建变量文件_python的open函数怎么用
- abp mysql .net core_ABP .Net Core Entity Framework迁移使用MySql数据库
- python土味情话_有哪些比较新的土味情话?
- 车牌检测和识别(转)
- python sys模块详解_python中os和sys模块的区别与常用方法总结
- 手动安装ipa,通过XCode手动安装包iOS App, ipa Devices and Simulators
- python微服务开发pdf_微服务架构实战 中文pdf完整版[207MB]
- 栈的主要特点及实例应用
- Word2016任意页开始设置页码
- Java中文英文数字混合掩码_Java8 中文教程
- Arcmap特殊注记
- PS(简单操作) 单张图片制作九宫格/证件照排版
- snprintf() 函数
- Android studio软件编译生成APK
- 双(三氟甲基磺酰基)酰亚胺钠 cas91742-21-1白色-类白色晶体-粉末 分子量:303.1358892
- 自媒体“人工智能小技巧”文章与视频合集(持续更新)
- SEO教程:如何优化长尾关键词达到快速排名(干货)
热门文章
- Professor Hendryx vs. Big Coal
- 北京内推 | 微软亚洲研究院智能多媒体算法组招聘算法实习生
- JavaWeb入门到精通-搭建javaweb环境并运行javaweb项目-下
- 技术管理之如何协调加班问题
- 大规模医学图像中的计算与学习问题【VALSE Webinar】Panel实录
- Androidstudio的 制作微信导航栏 的 微信素材库
- java计算机毕业设计理发预约系统源码+系统+数据库+lw文档+mybatis+运行部署
- ​人工智能创新:方向比努力更重要
- Mysql MHA搭建
- idea断点有个叉_idea 断点上面有x