$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>editor - How can a TinyMCE modal return formattedvisual text?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

editor - How can a TinyMCE modal return formattedvisual text?

matteradmin8PV0评论

I am trying to customize the behavior of an editor button in a plugin. On click, it opens a modal where the user can input some text. On confirmation, I want to wrap this text into code tags. But I don't want to take this text as if it comes from a text editor, I want to handle it as visual text. This means, I want to preserve any formatting (whitespaces and linebreaks) but not accept any other tags besides the code tags that I add afterward.

function showDialog() {
    var win = ed.windowManager.open({
            title: "Insert code",
            body: {
                type: 'textbox',
                name: 'code',
                multiline: true,
                minWidth: ed.getParam("code_dialog_width", 600),
                minHeight: ed.getParam("code_dialog_height", Math.min(tinymce.DOM.getViewPort().h - 200, 500)),
                spellcheck: false,
                style: 'direction: ltr; text-align: left'
            },
            onSubmit: function(e) {
                ed.focus();

                ed.undoManager.transact(function() {
                    ed.insertContent('<code>' + e.data.code + '</code>');
                });

                ed.selection.setCursorLocation();
                ed.nodeChanged();
            }
        });
    }

I am trying to customize the behavior of an editor button in a plugin. On click, it opens a modal where the user can input some text. On confirmation, I want to wrap this text into code tags. But I don't want to take this text as if it comes from a text editor, I want to handle it as visual text. This means, I want to preserve any formatting (whitespaces and linebreaks) but not accept any other tags besides the code tags that I add afterward.

function showDialog() {
    var win = ed.windowManager.open({
            title: "Insert code",
            body: {
                type: 'textbox',
                name: 'code',
                multiline: true,
                minWidth: ed.getParam("code_dialog_width", 600),
                minHeight: ed.getParam("code_dialog_height", Math.min(tinymce.DOM.getViewPort().h - 200, 500)),
                spellcheck: false,
                style: 'direction: ltr; text-align: left'
            },
            onSubmit: function(e) {
                ed.focus();

                ed.undoManager.transact(function() {
                    ed.insertContent('<code>' + e.data.code + '</code>');
                });

                ed.selection.setCursorLocation();
                ed.nodeChanged();
            }
        });
    }
Share Improve this question edited Feb 19, 2019 at 23:48 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Feb 19, 2019 at 23:13 Florian WaltherFlorian Walther 1591 silver badge8 bronze badges 2
  • You mean like this? – Sally CJ Commented Feb 20, 2019 at 10:39
  • @SallyCJ Close! I also want to keep whitespaces in the beginning of each line if that's possible! – Florian Walther Commented Feb 20, 2019 at 10:47
Add a comment  | 

1 Answer 1

Reset to default 1

(Revised answer)

You can use tinymce.DOM.encode() to convert all HTML tags to their entities, e.g. &lt; for < and &gt; for >:

var html = tinymce.DOM.encode(e.data.code);

Then to preserve trailing white-spaces:

html = html.replace(/(^ +| +$)/gm, function(match, p1){
  return p1.replace(/ /g, '&nbsp;');
});

And this to convert all line breaks to <br>:

html = html.replace(/(?:\r\n|\r|\n)/g, '<br>');

So your onSubmit would look like, which you can try here:

onSubmit: function(e) {
  ed.focus();

  ed.undoManager.transact(function() {
    // Encode all HTML tags to their entities.
    var html = tinymce.DOM.encode(e.data.code);

    // Then convert trailing whitespaces to `&nbsp;`.
    html = html.replace(/(^ +| +$)/gm, function(match, p1){
      return p1.replace(/ /g, '&nbsp;');
    });

    // Finally, convert line breaks to `<br>`.
    html = html.replace(/(?:\r\n|\r|\n)/g, '<br>');

    // Make sure the format is "raw".
    ed.insertContent('<code>' + html + '</code> ');
  });

  ed.selection.setCursorLocation();
  ed.nodeChanged();
}

Credit to this SO answer for the line breaks conversion.

Post a comment

comment list (0)

  1. No comments so far