wangEditor增加源码模式,添加查看源码功能
wangEditor是一款轻量级的富文本编辑器。使用还比较方便,但是缺少查看源码模式,需要我们自定义一个menu给增加查看源码模式
下面是wangEditor增加源码模式的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>wangEditor增加源码模式</title> </head> <body> <div id= "editor" ></div> <script type= "text/javascript" src= "https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js" ></script> <script type= "text/javascript" > const E = window.wangEditor; const editor = new E( "#editor" ); let isHTML = false ; const { BtnMenu } = E class htmlMenu extends BtnMenu { constructor(editor) { const $elem = E.$( `<div class = "w-e-menu" > <button>html</button> </div>` ) super($elem, editor) } clickHandler() { let source = editor.txt.html(); if (source){ isHTML = !isHTML; } if (isHTML){ source = source.replace(/</g, "<" ).replace(/>/g, ">" ).replace(/ /g, " " ); } else { source = editor.txt.text().replace(/</ig, "<" ).replace(/>/ig, ">" ).replace(/ /ig, " " ) } editor.txt.html(source); this .tryChangeActive(); // console.log(source); } tryChangeActive() { if (isHTML){ this .active() } else { this .unActive() } } } // 注册菜单 const menuKey = 'htmlMenuKey' // 菜单 key ,各个菜单不能重复 editor.menus.extend( 'htmlMenuKey' , htmlMenu) // 将菜单加入到 editor.config.menus 中 // 也可以通过配置 menus 调整菜单的顺序,参考【配置菜单】部分的文档 editor.config.menus = editor.config.menus.concat(menuKey) editor.create() </script> </body> </html> 此代码亲测可用,直接复制使用即可 |
本文来自博客园,作者:飞龙在生,转载请注明原文链接:https://www.cnblogs.com/flzs/p/17674033.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2020-09-02 Mysql存储日期类型用int、timestamp还是datetime?