From a9ff91d51e01367d5fa0df9f36f8d53e4314148c Mon Sep 17 00:00:00 2001 From: thecodrr Date: Sat, 22 Jan 2022 13:26:45 +0500 Subject: [PATCH] fix(tinymce-plugins): make inlinecode toggleable --- packages/tinymce-plugins/inlinecode/index.js | 27 +++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/tinymce-plugins/inlinecode/index.js b/packages/tinymce-plugins/inlinecode/index.js index 14229d262..f52bf7e01 100644 --- a/packages/tinymce-plugins/inlinecode/index.js +++ b/packages/tinymce-plugins/inlinecode/index.js @@ -6,11 +6,11 @@ function register(editor) { editor.ui.registry.addToggleButton("inlinecode", { icon: "sourcecode", tooltip: "Inline code", - onAction: function () { + onAction: function() { return toggleInlineCode(editor); }, - onSetup: function (api) { - var nodeChangeHandler = function (e) { + onSetup: function(api) { + var nodeChangeHandler = function(e) { if ( e.element.tagName === TAGNAME && !e.element.innerHTML.trim().length @@ -21,13 +21,13 @@ function register(editor) { }; editor.on("NodeChange", nodeChangeHandler); - return function () { - return editor.off("NodeChange", nodeChangeHandler); + return function() { + editor.off("NodeChange", nodeChangeHandler); }; }, }); - editor.addCommand("mceInsertInlineCode", function () { + editor.addCommand("mceInsertInlineCode", function() { toggleInlineCode(editor); }); } @@ -38,11 +38,20 @@ function toggleInlineCode(editor) { const range = editor.selection.getRng(); const node = editor.selection.getNode(); - if (node.tagName !== TAGNAME && range.startOffset === range.endOffset) { - editor.selection.setContent(``); + if (!!node.closest("code")) { + editor.execCommand("mceToggleFormat", false, "code"); + } else if ( + node.tagName !== TAGNAME && + range.startOffset === range.endOffset + ) { + editor.selection.setContent( + `` + ); } else { const content = editor.selection.getContent(); - editor.selection.setContent(`${content}`); + editor.selection.setContent( + `${content}` + ); } }); }