简体   繁体   中英

TinyMCE: show and hide inline toolbar on demand

I use TinyMCE to edit content. I initialize it in the following way

<script type="text/javascript">
tinymce.init({
    selector: "div.editable",
    inline: true,
    plugins: [
        "advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen",
        "insertdatetime media table contextmenu paste"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>

So, clicking divs with class 'editable' will show the TinyMCE inline editor. I want to show and hide it by clicking buttons, something like that :

<input type="button" value="show inline editor for some div" onclick='tinymce.somediv.show()'>

I prepared a jsfiddle that shows the default behavior.

Please, help me find a way to show and hide the inline editor on demand for different divs.

在tinymce内联模式下,我简单地使用:

tinymce.EditorManager.activeEditor.getElement().blur();

I'm pretty sure you have already found an answer for your question. Since it was a bit hard for me to find exact answer, here's what I did at the end.

My TinyMCE Version 4.0.1

to show:

tinymce.init({
    selector: "div.editable",
    inline: true,
    plugins: [
        "advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen",
        "insertdatetime media table contextmenu paste"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});

to hide (hide only inline editors):

$("#some_input_element").trigger("focus");// to remove focus from active editors
for (var i = tinymce.editors.length - 1; i >= 0; i--) {
    if (tinymce.editors[i].inline)
        tinymce.editors[i].remove();
};

Removing editor while having focus gives unexpected results. So to be safe, I focus to a different input element before entering the for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM