当前位置: 首页 > news >正文

wangeditor——cdn引入的形式创建一个简易版编辑器——js技能提升

昨天同事那边有个需求,就是要实现聊天功能,需要用到一个富文本编辑器,参考如下:
在这里插入图片描述
上面的这个效果图是博客园的评论输入框

最终使用wangEditor编辑器实现的效果如下:

在这里插入图片描述
只保留了个别的菜单:

默认模式的wangEditor编辑器如下:
在这里插入图片描述
下面直接上代码:

解决步骤1:cdn引入

head头部标签引入css

<linkhref="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css"rel="stylesheet"/>

script引入js

 <script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>

解决步骤2:其余css配置

<style>#editor—wrapper {border: 1px solid #ccc;z-index: 100; /* 按需定义 */}#toolbar-container {border-bottom: 1px solid #ccc;}#editor-container {height: 500px;}</style>

解决步骤3:html代码

 <div id="editor-content-textarea"></div><button id="btn-set-html">设置html</button><div id="editor—wrapper" style="width: 900px"><div id="toolbar-container"><!-- 工具栏 --></div><div id="editor-container"><!-- 编辑器 --></div></div>

解决步骤4:script代码

<script>const { createEditor, createToolbar } = window.wangEditor;const editorConfig = {placeholder: '请输入内容...',// maxLength:2000,//设置最大长度MENU_CONF: {},onChange(editor) {const html = editor.getHtml();console.log('editor content', html);// 也可以同步到 <textarea>},};const editor = createEditor({selector: '#editor-container',html: '<p><br></p>',config: editorConfig,mode: 'simple', // or 'simple'});const toolbarConfig = {excludeKeys: ['blockquote','bgColor',// 'headerSelect','italic','group-more-style', // 排除菜单组,写菜单组 key 的值即可'bulletedList', //无序列表'numberedList', //有序列表'todo', //待办'emotion', //表情'insertTable', //表格'codeBlock', //代码块'group-video', //视频'divider', //分割线'fullScreen', //全屏// 'insertLink',//插入链接'group-justify', //对齐方式'group-indent', //缩进'fontSize', //字体大小'fontFamily', //字体'lineHeight', //行高'underline', //下划线'color', //颜色'undo', //撤销'redo', //重做],};toolbarConfig.modalAppendToBody = true;// 创建 toolbar 和 editor// 可监听 `modalOrPanelShow` 和 `modalOrPanelHide` 自定义事件来设置样式、蒙层editor.on('modalOrPanelShow', (modalOrPanel) => {if (modalOrPanel.type !== 'modal') return;const { $elem } = modalOrPanel; // modal element// 设置 modal 样式(定位、z-index)// 显示蒙层});editor.on('modalOrPanelHide', () => {// 隐藏蒙层});const toolbar = createToolbar({editor,selector: '#toolbar-container',config: toolbarConfig,mode: 'default', // or 'simple'});editorConfig.MENU_CONF['uploadImage'] = {server: '/api/upload-image',fieldName: 'custom-field-name',// 继续写其他配置...customInsert(res, insertFn) {console.log(res);// JS 语法// res 即服务端的返回结果// 从 res 中找到 url alt href ,然后插入图片//   "url": "xxx", // 图片 src ,必须// "alt": "yyy", // 图片描述文字,非必须// "href": "zzz" // 图片的链接,非必须insertFn(url, alt, href);},//【注意】不需要修改的不用写,wangEditor 会去 merge 当前其他配置};
});

如果要实现回显,则需要通过下面的代码

// textarea 初始化值const textarea = document.getElementById('editor-content-textarea');textarea.value ='<p>wangEditor 只识别 editor.getHtml() 生成的 html 格式,不可以随意自定义 html 代码(html 格式太灵活了,不会全部兼容)</p>\n<p>wangEditor can only understand the HTML format from editor.getHtml() , but not all HTML formats.</p>\n<p><br></p>';// Set HTMLdocument.getElementById('btn-set-html').addEventListener('click', () => {if (editor.isDisabled()) editor.enable();if (!editor.isFocused()) editor.focus();editor.select([]);editor.deleteFragment();window.wangEditor.SlateTransforms.setNodes(editor,{ type: 'paragraph' },{ mode: 'highest' });editor.dangerouslyInsertHtml(textarea.value);

完整代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>富文本编辑器</title><linkhref="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css"rel="stylesheet"/><style>#editor—wrapper {border: 1px solid #ccc;z-index: 100; /* 按需定义 */}#toolbar-container {border-bottom: 1px solid #ccc;}#editor-container {height: 500px;}</style></head><body><div id="editor-content-textarea"></div><button id="btn-set-html">设置html</button><div id="editor—wrapper" style="width: 900px"><div id="toolbar-container"><!-- 工具栏 --></div><div id="editor-container"><!-- 编辑器 --></div></div><script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script><script>const { createEditor, createToolbar } = window.wangEditor;const editorConfig = {placeholder: '请输入内容...',// maxLength:2000,//设置最大长度MENU_CONF: {},onChange(editor) {const html = editor.getHtml();console.log('editor content', html);// 也可以同步到 <textarea>},};const editor = createEditor({selector: '#editor-container',html: '<p><br></p>',config: editorConfig,mode: 'simple', // or 'simple'});const toolbarConfig = {excludeKeys: ['blockquote','bgColor',// 'headerSelect','italic','group-more-style', // 排除菜单组,写菜单组 key 的值即可'bulletedList', //无序列表'numberedList', //有序列表'todo', //待办'emotion', //表情'insertTable', //表格'codeBlock', //代码块'group-video', //视频'divider', //分割线'fullScreen', //全屏// 'insertLink',//插入链接'group-justify', //对齐方式'group-indent', //缩进'fontSize', //字体大小'fontFamily', //字体'lineHeight', //行高'underline', //下划线'color', //颜色'undo', //撤销'redo', //重做],};toolbarConfig.modalAppendToBody = true;// 创建 toolbar 和 editor// 可监听 `modalOrPanelShow` 和 `modalOrPanelHide` 自定义事件来设置样式、蒙层editor.on('modalOrPanelShow', (modalOrPanel) => {if (modalOrPanel.type !== 'modal') return;const { $elem } = modalOrPanel; // modal element// 设置 modal 样式(定位、z-index)// 显示蒙层});editor.on('modalOrPanelHide', () => {// 隐藏蒙层});const toolbar = createToolbar({editor,selector: '#toolbar-container',config: toolbarConfig,mode: 'default', // or 'simple'});editorConfig.MENU_CONF['uploadImage'] = {server: '/api/upload-image',fieldName: 'custom-field-name',// 继续写其他配置...customInsert(res, insertFn) {console.log(res);// JS 语法// res 即服务端的返回结果// 从 res 中找到 url alt href ,然后插入图片//   "url": "xxx", // 图片 src ,必须// "alt": "yyy", // 图片描述文字,非必须// "href": "zzz" // 图片的链接,非必须insertFn(url, alt, href);},//【注意】不需要修改的不用写,wangEditor 会去 merge 当前其他配置};// textarea 初始化值const textarea = document.getElementById('editor-content-textarea');textarea.value ='<p>wangEditor 只识别 editor.getHtml() 生成的 html 格式,不可以随意自定义 html 代码(html 格式太灵活了,不会全部兼容)</p>\n<p>wangEditor can only understand the HTML format from editor.getHtml() , but not all HTML formats.</p>\n<p><br></p>';// Set HTMLdocument.getElementById('btn-set-html').addEventListener('click', () => {if (editor.isDisabled()) editor.enable();if (!editor.isFocused()) editor.focus();editor.select([]);editor.deleteFragment();window.wangEditor.SlateTransforms.setNodes(editor,{ type: 'paragraph' },{ mode: 'highest' });editor.dangerouslyInsertHtml(textarea.value);});</script></body>
</html>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 从状态管理到性能优化:全面解析 Android Compose
  • 【 前端优化】Vue 3 性能优化技巧
  • 二叉树(上)
  • 2.大语言模型LLM的涌现能力和关键技术
  • JVM面试(七)G1垃圾收集器剖析
  • css问题:display:flex布局+justify-content: space-between; 最后一行不能左对齐
  • 2024年重磅报告!国内AI大模型产业飞速发展!
  • APO使用场景之:统一的指标采集展示
  • Android SPN/PLMN 显示逻辑简介
  • 图算法 | 图算法的分类有哪些?(下)
  • HTML 基础知识详解与代码示例
  • Vue3流程图插件-Vue Flow
  • 黑神话 Java,Solon v2.9.2 发布
  • 鸿蒙(API 12 Beta6版)【ArkGraphics 3D资源创建以及使用】方舟3D图形
  • pytest 生成allure测试报告
  • 30秒的PHP代码片段(1)数组 - Array
  • CSS盒模型深入
  • gcc介绍及安装
  • JS实现简单的MVC模式开发小游戏
  • magento 货币换算
  • mysql 5.6 原生Online DDL解析
  • mysql中InnoDB引擎中页的概念
  • SpiderData 2019年2月13日 DApp数据排行榜
  • Terraform入门 - 1. 安装Terraform
  • web标准化(下)
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 警报:线上事故之CountDownLatch的威力
  • 聚类分析——Kmeans
  • 悄悄地说一个bug
  • 使用agvtool更改app version/build
  • 听说你叫Java(二)–Servlet请求
  • 新版博客前端前瞻
  • 怎么将电脑中的声音录制成WAV格式
  • ​linux启动进程的方式
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • #pragma 指令
  • (1)(1.13) SiK无线电高级配置(五)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第6节 (嵌套的Finally代码块)
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (顶刊)一个基于分类代理模型的超多目标优化算法
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (附源码)计算机毕业设计大学生兼职系统
  • (转)visual stdio 书签功能介绍
  • . NET自动找可写目录
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .Net 垃圾回收机制原理(二)
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args
  • .NET/C#⾯试题汇总系列:集合、异常、泛型、LINQ、委托、EF!(完整版)
  • .net实现客户区延伸至至非客户区
  • .pyc文件是什么?
  • .考试倒计时43天!来提分啦!
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [Android]常见的数据传递方式