首页 > 其他分享 >wangeditor——cdn引入的形式创建一个简易版编辑器——js技能提升

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

时间:2024-09-11 10:49:45浏览次数:3  
标签:const textarea wangeditor cdn js wangEditor html editor group

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

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

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

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

解决步骤1:cdn引入

head头部标签引入css

<link
      href="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 HTML
      document.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>
    <link
      href="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 HTML
      document.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>

标签:const,textarea,wangeditor,cdn,js,wangEditor,html,editor,group
From: https://blog.csdn.net/yehaocheng520/article/details/142132588

相关文章

  • 与 D3.js 的对比:ECharts 在前端可视化中的优势与劣势
    在前端数据可视化的领域,ECharts和D3.js是两个非常流行且强大的工具。尽管它们都可以用来创建图表和数据可视化,但它们的设计理念、使用方式和适用场景有所不同。本文将对这两个库进行比较,帮助你理解ECharts在前端可视化中的优势与劣势。ECharts的优势易于上手ECharts......
  • mysql 8.0数据类型 json
    mysql8.0新增数据类型json。5.7通过blob等类型来保存json格式的数据,为什么还要专门增加这一数据格式的支持呢?  1.保证了JSON数据类型的强校验:JSON数据列会自动校验存入此列的内容是否符合JSON格式,    非正常格式则报错,而varchar类型和text等类型本身是不存在这......
  • js中【微任务】和【宏任务】长篇解读
    在JavaScript中,理解微任务(microtasks)和宏任务(macrotasks)是掌握异步编程和事件循环(EventLoop)机制的关键。这两个概念影响了代码的执行顺序,特别是在涉及异步操作(如setTimeout、Promise、async/await等)时。为了深刻理解它们的差异及其在事件循环中的表现,我们将从最基......
  • jsp餐厅服务人员评价系统o8pf9
    jsp餐厅服务人员评价系统o8pf9本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能员工,用户,工作签到,请假信息,考勤信息,服务评价,员工考核技术要求:   开发语言:JSP前端使用:HTML5,CSS,JSP动态网......
  • jsp超市管理系统设计与实现5ojjs本系统(程序+源码+数据库+调试部署+开发环境)带论文文档
    jsp超市管理系统设计与实现5ojjs本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能会员,商品分类,员工信息,热卖商品,订单信息,商品采购开题报告内容JSP超市管理系统设计与实现(5OJJS)开题内容报告一......
  • jsp超市管理系统的设计与实现103v4 本系统(程序+源码+数据库+调试部署+开发环境)
    jsp超市管理系统的设计与实现10本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能超市公告,商品分类,用户,商品信息,商品入库,用户订单,商品出库,商品盘点技术要求:   开发语言:JSP前端使用:HTML......
  • jsp超市管理系统的设计s6kuk
    jsp超市管理系统的设计s6kuk本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能员工,商品信息,商品分类,仓库信息,供应商,进货信息,退货信息,销售信息,进货结算,退货结算,销售结算技术要求:   开......
  • HTML/CSS/JS学习笔记 Day4(HTML--C3 表格)
    跟着该视频学习,记录笔记:【黑马程序员pink老师前端入门教程,零基础必看的h5(html5)+css3+移动端前端视频教程】https://www.bilibili.com/video/BV14J4114768?p=12&vd_source=04ee94ad3f2168d7d5252c857a2bf358Day4 内容梳理:目录HTML3.0表格3.1表格标签(1)语法基本标签......
  • jsp超市管理系统06c97 本系统(程序+源码+数据库+调试部署+开发环境)
    jsp超市管理系统06c97本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能商品分类,员工,库存信息,进货信息,销售信息技术要求:   开发语言:JSP前端使用:HTML5,CSS,JSP动态网页技术后端使用SpringBo......
  • jsp超市产品保质期管理系统fscqq
    jsp超市产品保质期管理系统fscq本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能用户,产品种类,产品信息,生产厂家,产品入库,产品出库,保质期技术要求:   开发语言:JSP前端使用:HTML5,CSS,JSP动态......