简体   繁体   English

tinymce 5 换行插件

[英]tinymce 5 line break plugin

I've created a small TinyMCE v5 plugin that adds a line break button to the menu or toolbar :我创建了一个小的 TinyMCE v5 插件,它向菜单或工具栏添加了一个换行按钮:

 /* Plugin code */ (function () { 'use strict'; var global = tinymce.util.Tools.resolve('tinymce.PluginManager'); var register = function (editor) { // Translate button text tinymce.util.I18n.add('fr_FR', {'Inser Line Break' : 'Insérer un saut de ligne'}); // Register new Icon editor.ui.registry.addIcon('line-break', '<svg width="24" height="24"><path d="M 6.4,15.129563 H 12 c 3.7,0 6.2,-2 6.8,-5.1 0.6,-2.7 -0.4,-5.6 -2.3,-6.8 a 1.029563,1.029563 0 0 0 -1,1.8 c 1.1,0.6 1.8,2.7 1.4,4.6 -0.5,2.1 -2.1,3.5 -4.9,3.5 H 6.4 l 3.3,-3.3 a 1,1 0 1 0 -1.4,-1.4 l -5,5 a 1,1 0 0 0 0,1.4 l 5,5 a 1,1 0 0 0 1.4,-1.4 z" fill-rule="nonzero"/></svg>'); editor.addCommand('InsertLineBreak', function () { editor.execCommand('mceInsertContent', false, '<br>'); }); }; var register$1 = function (editor) { editor.ui.registry.addButton('br', { icon: 'line-break', tooltip: 'Inser Line Break', onAction: function () { return editor.execCommand('InsertLineBreak'); } }); editor.ui.registry.addMenuItem('br', { icon: 'line-break', text: 'Inser Line Break', onAction: function () { return editor.execCommand('InsertLineBreak'); } }); }; function Plugin () { global.add('br', function (editor) { register(editor); register$1(editor); }); } Plugin(); }()); /* Main code */ tinymce.init({ selector : "textarea#mceEditor", menubar: "", language : "fr_FR", plugins: ["advlist autolink link image lists preview hr anchor pagebreak wordcount fullscreen media nonbreaking emoticons template paste help", "br"], toolbar: "fullscreen undo redo | formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist | link unlink image hr br ihtml | media | emoticons ", block_formats: "Paragraph=p;Header 2=h2", branding: false, elementpath: false, content_css : "https://www.dev-book.fr/styles/editor.css?v=36", valid_elements : "h1,h2,hr,br,ul,ol,li,strong/b,em/i,p/div[align|style],a[href|target],img[class|src|border=0|width|height|align|style|alt],iframe[src|width|height]", });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.7.1/tinymce.min.js"></script> <textarea name="page_texte" style="height:"400px" id="mceEditor"></textarea>

The only way to make it works is adding a space behind <br> on line :让它工作的唯一方法是在<br>后面添加一个空格:

editor.execCommand('mceInsertContent', false, '<br> ');

otherwise the <br> tag is inserted but it has no effects meaning no line return occurs in the editor.否则将插入<br>标签,但它没有效果,这意味着编辑器中不会发生换行。 The problem by adding this space is that it adds a space char at new line startup and I don't wan't that ...添加这个空格的问题是它在新行启动时添加了一个空格字符,我不想那样......

I just want the same behavior when clicking my plugin button as hitting "MAJ+ENTER" on keyboard to insert line break instead of default new paragraph.当单击我的插件按钮时,我只想要与在键盘上点击“MAJ+ENTER”以插入换行符而不是默认的新段落时相同的行为。

Unfortunately the code snippet does not work because of external TinyMCE CDN resource not being able to access Stack Overflow DOM.不幸的是,代码片段不起作用,因为外部 TinyMCE CDN 资源无法访问 Stack Overflow DOM。 If one has suggestions to this sub-questions.如果有人对此子问题有建议。

I found replacing '<br> ' by '<br>\‌' was fixing my issue.我发现用'<br> ' '<br>\‌'替换'<br> ' '<br>\‌'正在解决我的问题。 \‌ is a code for " zero width non joiner " \‌是“零宽度非连接器”的代码

Not sure this is the perfect answer but will go on with it waiting for a better one !不确定这是完美的答案,但会继续等待更好的答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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