{"id":1590,"date":"2021-08-25T23:48:54","date_gmt":"2021-08-25T15:48:54","guid":{"rendered":"https:\/\/www.strongd.net\/?p=1590"},"modified":"2021-08-26T11:11:16","modified_gmt":"2021-08-26T03:11:16","slug":"wangeditor%e6%b7%bb%e5%8a%a0%e9%a6%96%e8%a1%8c%e7%bc%a9%e8%bf%9b%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=1590","title":{"rendered":"WangEditor\u6dfb\u52a0\u9996\u884c\u7f29\u8fdb\u529f\u80fd"},"content":{"rendered":"<p><strong>\u5b98\u65b9\u4e5f\u4e0d\u589e\u52a0\u8fd9\u4e2a\uff0c\u4f46\u5bf9\u4e8e\u7ecf\u5e38\u5199\u6587\u7ae0\u7684\u7528\u6237\u6765\u8bf4\uff0c\u9996\u884c\u7f29\u8fdb\u662f\u5f88\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u6539\u53d8\u4e00\u4e0b\u6837\u5f0f\u5c31\u53ef\u4ee5\u4e86text-indent: 2em;<\/strong><\/p>\n<p><strong>\u4fee\u6539\u4e00\u4e0b\u521d\u59cb\u5316wangEditor\u7684\u5730\u65b9\uff0c\u6ce8\u518c\u4e00\u4e2a\u83dc\u5355<\/strong><\/p>\n<blockquote><p>\/\/\u521d\u59cb\u5316\u7f16\u8f91\u5668<br \/>\nvar E = window.wangEditor;<br \/>\n\/\/ \u83dc\u5355 key \uff0c\u5404\u4e2a\u83dc\u5355\u4e0d\u80fd\u91cd\u590d<br \/>\nconst menuKey = &#8216;MyTextIndentMenuKey&#8217;<\/p>\n<p>\/\/ \u6ce8\u518c\u83dc\u5355<br \/>\nE.registerMenu(menuKey, MyTextIndentMenu)<\/p>\n<p>var editor = new E(&#8216;#&#8217;+id);<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><strong>\u65b0\u589e\u4e00\u4e2aMyTextIndentMenu.js\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<blockquote><p>const E = window.wangEditor<br \/>\nconst { $, BtnMenu , DropListMenu, PanelMenu, DropList, Panel, Tooltip } = E<\/p>\n<p>const lengthRegex = \/^(\\d+)(\\w+)$\/<br \/>\nconst percentRegex = \/^(\\d+)%$\/<br \/>\nconst reg = \/^(SECTION|P|H[0-9]*)$\/<\/p>\n<p>\/\/ \u7b2c\u4e00\uff0c\u83dc\u5355 class \uff0cButton \u83dc\u5355\u7ee7\u627f BtnMenu class https:\/\/www.wangeditor.com\/doc\/<br \/>\nclass MyTextIndentMenu extends BtnMenu{<br \/>\nconstructor(editor) {<br \/>\n\/\/ data-title\u5c5e\u6027\u8868\u793a\u5f53\u9f20\u6807\u60ac\u505c\u5728\u8be5\u6309\u94ae\u4e0a\u65f6\u63d0\u793a\u8be5\u6309\u94ae\u7684\u529f\u80fd\u7b80\u8ff0text-indent: 2em;<br \/>\nconst $elem = E.$(<br \/>\n`&lt;div class=&#8221;w-e-menu w-e-icon-indent-increase&#8221; data-title=&#8221;\u9996\u884c\u7f29\u8fdb&#8221;&gt;<br \/>\n&lt;\/div&gt;`<br \/>\n)<br \/>\nsuper($elem, editor)<br \/>\n}<br \/>\n\/\/ \u83dc\u5355\u70b9\u51fb\u4e8b\u4ef6<br \/>\nclickHandler() {<br \/>\n\/\/ \u505a\u4efb\u4f55\u4f60\u60f3\u505a\u7684\u4e8b\u60c5text-indent: 2em;<br \/>\n\/\/ \u53ef\u53c2\u8003\u3010\u5e38\u7528 API\u3011\u6587\u6863\uff0c\u6765\u64cd\u4f5c\u7f16\u8f91\u5668<br \/>\nthis.command();<br \/>\n}<br \/>\n\/\/ \u83dc\u5355\u662f\u5426\u88ab\u6fc0\u6d3b\uff08\u5982\u679c\u4e0d\u9700\u8981\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u7a7a\u7740\uff09<br \/>\n\/\/ 1. \u6fc0\u6d3b\u662f\u4ec0\u4e48\uff1f\u5149\u6807\u653e\u5728\u4e00\u6bb5\u52a0\u7c97\u3001\u4e0b\u5212\u7ebf\u7684\u6587\u672c\u65f6\uff0c\u83dc\u5355\u680f\u91cc\u7684 B \u548c U \u88ab\u6fc0\u6d3b\uff0c\u5982\u4e0b\u56fe<br \/>\n\/\/ 2. \u4ec0\u4e48\u65f6\u5019\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\uff1f\u6bcf\u6b21\u7f16\u8f91\u5668\u533a\u57df\u7684\u9009\u533a\u53d8\u5316\uff08\u5982\u9f20\u6807\u64cd\u4f5c\u3001\u952e\u76d8\u64cd\u4f5c\u7b49\uff09\uff0c\u90fd\u4f1a\u89e6\u53d1\u5404\u4e2a\u83dc\u5355\u7684 tryChangeActive \u51fd\u6570\uff0c\u91cd\u65b0\u8ba1\u7b97\u83dc\u5355\u7684\u6fc0\u6d3b\u72b6\u6001<br \/>\ntryChangeActive() {<br \/>\n\/\/ \u6fc0\u6d3b\u83dc\u5355<br \/>\n\/\/ 1. \u83dc\u5355 DOM \u8282\u70b9\u4f1a\u589e\u52a0\u4e00\u4e2a .w-e-active \u7684 css class<br \/>\n\/\/ 2. this.this.isActive === true<br \/>\nconst editor = this.editor<br \/>\nconst $selectionElem = editor.selection.getSelectionStartElem()<br \/>\nconst $selectionStartElem = $($selectionElem).getNodeTop(editor)<\/p>\n<p>if ($selectionStartElem.length &lt;= 0) return<\/p>\n<p>if ($selectionStartElem.elems[0].style[&#8216;textIndent&#8217;] !== &#8221;) {<br \/>\nthis.active()<br \/>\n} else {<br \/>\nthis.unActive()<br \/>\n}<br \/>\n}<br \/>\n\/**<br \/>\n* \u6267\u884c\u547d\u4ee4<br \/>\n* @param value value<br \/>\n*\/<br \/>\ncommand() {<br \/>\nconst editor = this.editor<br \/>\nconst $selectionElem = editor.selection.getSelectionContainerElem()<\/p>\n<p>\/\/ \u5224\u65ad \u5f53\u524d\u9009\u533a\u4e3a textElem \u65f6<br \/>\nif ($selectionElem &amp;&amp; editor.$textElem.equal($selectionElem)) {<br \/>\n\/\/ \u5f53 \u5f53\u524d\u9009\u533a \u7b49\u4e8e textElem \u65f6<br \/>\n\/\/ \u4ee3\u8868 \u5f53\u524d\u9009\u533a \u53ef\u80fd\u662f\u4e00\u4e2a\u9009\u62e9\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u6bb5\u843d\u6216\u8005\u591a\u4e2a\u6bb5\u843d<br \/>\nconst $elems = editor.selection.getSelectionRangeTopNodes()<br \/>\nif ($elems.length &gt; 0) {<br \/>\n$elems.forEach((item) =&gt; {<br \/>\nthis.operateElement($(item), editor)<br \/>\n})<br \/>\n}<br \/>\n} else {<br \/>\n\/\/ \u5f53 \u5f53\u524d\u9009\u533a \u4e0d\u7b49\u4e8e textElem \u65f6<br \/>\n\/\/ \u4ee3\u8868 \u5f53\u524d\u9009\u533a\u8981\u4e48\u662f\u4e00\u4e2a\u6bb5\u843d\uff0c\u8981\u4e48\u662f\u6bb5\u843d\u4e2d\u7684\u4e00\u90e8\u5206<br \/>\nif ($selectionElem &amp;&amp; $selectionElem.length &gt; 0) {<br \/>\n$selectionElem.forEach((item) =&gt; {<br \/>\nthis.operateElement($(item), editor)<br \/>\n})<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ \u6062\u590d\u9009\u533a<br \/>\neditor.selection.restoreSelection()<br \/>\nthis.tryChangeActive()<br \/>\n}<\/p>\n<p>operateElement($node, editor) {<br \/>\nconst $elem = $node.getNodeTop(editor)<br \/>\nlet type = &#8216;increase&#8217;;<br \/>\nif($node.elems[0].style[&#8216;textIndent&#8217;] !== &#8221;)<br \/>\ntype = &#8216;decrease&#8217;;<\/p>\n<p>if (reg.test($elem.getNodeName())) {<br \/>\nif (type === &#8216;increase&#8217;) this.increaseIndentStyle($elem, this.parseIndentation(editor))<br \/>\nelse if (type === &#8216;decrease&#8217;) this.decreaseIndentStyle($elem, this.parseIndentation(editor))<br \/>\n}<br \/>\n}<br \/>\nincreaseIndentStyle($node, options) {<br \/>\nconst $elem = $node.elems[0]<br \/>\nif ($elem.style[&#8216;textIndent&#8217;] === &#8221;) {<br \/>\n$node.css(&#8216;text-indent&#8217;, options.value + options.unit)<br \/>\n} else {<br \/>\nconst oldPL = $elem.style[&#8216;textIndent&#8217;]<br \/>\nconst oldVal = oldPL.slice(0, oldPL.length &#8211; options.unit.length)<br \/>\nconst newVal = Number(oldVal) + options.value<br \/>\n$node.css(&#8216;text-indent&#8217;, `${newVal}${options.unit}`)<br \/>\n}<br \/>\n}<br \/>\ndecreaseIndentStyle($node, options) {<br \/>\nconst $elem = $node.elems[0]<br \/>\nif ($elem.style[&#8216;textIndent&#8217;] !== &#8221;) {<br \/>\nconst oldPL = $elem.style[&#8216;textIndent&#8217;]<br \/>\nconst oldVal = oldPL.slice(0, oldPL.length &#8211; options.unit.length)<br \/>\nconst newVal = Number(oldVal) &#8211; options.value<br \/>\nif (newVal &gt; 0) {<br \/>\n$node.css(&#8216;text-indent&#8217;, `${newVal}${options.unit}`)<br \/>\n} else {<br \/>\n$node.css(&#8216;text-indent&#8217;, &#8221;)<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>parseIndentation(editor) {<br \/>\nconst { indentation } = editor.config<\/p>\n<p>if (typeof indentation === &#8216;string&#8217;) {<br \/>\nif (lengthRegex.test(indentation)) {<br \/>\nconst [value, unit] = indentation.trim().match(lengthRegex).slice(1, 3)<br \/>\nreturn {<br \/>\nvalue: Number(value),<br \/>\nunit,<br \/>\n}<br \/>\n} else if (percentRegex.test(indentation)) {<br \/>\nreturn {<br \/>\nvalue: Number(indentation.trim().match(percentRegex)?indentation.trim().match(percentRegex):[1]),<br \/>\nunit: &#8216;%&#8217;,<br \/>\n}<br \/>\n}<br \/>\n} else if (indentation.value !== void 0 &amp;&amp; indentation.unit) {<br \/>\nreturn indentation<br \/>\n}<\/p>\n<p>return {<br \/>\nvalue: 2,<br \/>\nunit: &#8217;em&#8217;,<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u5b98\u65b9\u4e5f\u4e0d\u589e\u52a0\u8fd9\u4e2a\uff0c\u4f46\u5bf9\u4e8e\u7ecf\u5e38\u5199\u6587\u7ae0\u7684\u7528\u6237\u6765\u8bf4\uff0c\u9996\u884c\u7f29\u8fdb\u662f\u5f88\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u6539\u53d8\u4e00\u4e0b\u6837\u5f0f\u5c31\u53ef\u4ee5\u4e86text-indent: 2em; \u4fee\u6539\u4e00\u4e0b\u521d\u59cb\u5316wangEditor\u7684\u5730\u65b9\uff0c\u6ce8\u518c\u4e00\u4e2a\u83dc\u5355 \/\/\u521d\u59cb\u5316\u7f16\u8f91\u5668 var E = window.wangEditor; \/\/ \u83dc\u5355 key \uff0c\u5404\u4e2a\u83dc\u5355\u4e0d\u80fd\u91cd\u590d const menuKey = &#8216;MyTextIndentMenuKey&#8217; \/\/ \u6ce8\u518c\u83dc\u5355 E.registerMenu(menuKey, MyTextIndentMenu) var editor = new E(&#8216;#&#8217;+id); &nbsp; \u65b0\u589e\u4e00\u4e2aMyTextIndentMenu.js\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a &nbsp; const E = window.wangEditor const { $, BtnMenu , DropListMenu, PanelMenu, DropList, Panel, Tooltip } = E const lengthRegex = \/^(\\d+)(\\w+)$\/ const percentRegex = \/^(\\d+)%$\/ const reg &hellip; <a href=\"https:\/\/www.strongd.net\/?p=1590\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">WangEditor\u6dfb\u52a0\u9996\u884c\u7f29\u8fdb\u529f\u80fd<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-1590","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1590"}],"version-history":[{"count":3,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1590\/revisions"}],"predecessor-version":[{"id":1593,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1590\/revisions\/1593"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}