简体   繁体   English

如何根据 TinyMCE 5 中的光标位置在 CodeMirror 中设置光标位置?

[英]How to set cursor position in CodeMirror in accordance to cursor position in TinyMCE 5?

I have a form on the webpage, which uses TinyMCE 5 to edit one of the fields.我在网页上有一个表单,它使用TinyMCE 5来编辑其中一个字段。 In my configuration, the TinyMCE uses CodeMirror in order to inspect/edit the html-code of the field's content.在我的配置中, TinyMCE使用CodeMirror来检查/编辑字段内容的 html 代码。 When the CodeMirror starts, it positions the cursor at the beginning of the code, but I would like to find a way how to set initial cursor position in CodeMirror to the place in html-code, which corresponds to the position of cursor in TinyMCE .CodeMirror启动时,它将光标定位在代码的开头,但我想找到一种方法如何将CodeMirror中的初始光标位置设置为 html-code 中的位置,该位置对应于TinyMCE的光标位置。 In other words, if the cursor is located eg inside a table in TinyMCE when I call the CodeMirror (by pressing "code" button on menu panel), I would like the CodeMirror to set its cursor close to or inside the <table> tag in the html-code.换句话说,如果当我调用CodeMirror时光标位于例如TinyMCE的表格内(通过按菜单面板上的“代码”按钮),我希望CodeMirror将其光标设置在<table>标签附近或内部在 html 代码中。

The solution, which comes to my mind, is to get the tag name at the cursor position in TinyMCE , then start CodeMirror , find the line in CodeMirror , which contains that tag, and finally position the cursor to that line.我想到的解决方案是在TinyMCE中的光标位置获取标签名称,然后启动CodeMirror ,在CodeMirror找到包含该标签的行,最后将光标定位到该行。 But I don't really understand how to implement this.但我真的不明白如何实现这一点。 Should I edit the codemirror/plugin.js or there is another less destructive way?我应该编辑codemirror/plugin.js还是有另一种破坏性较小的方法?

Any ideas are welcome!欢迎任何想法! Thanks!谢谢!

After some research I've found the answer to my question.经过一番研究,我找到了我的问题的答案。 I hope it will be helpful for someone else.我希望它对其他人有帮助。

First of all, the functionality described in my question has already been implemented in the CodeMirror .首先,我的问题中描述的功能已经在CodeMirror实现了。 It's controlled by the CodeMirror option saveCursorPosition .它由CodeMirror选项saveCursorPosition If it's set to true , CodeMirror positions cursor to the place in the html-code, which corresponds to the position of the cursor in the TinyMCE editor and vice versa.如果设置为true ,则CodeMirror将光标定位到 html 代码中的位置,该位置对应于光标在TinyMCE编辑器中的位置,反之亦然。

But the most funny thing is that all of the above doesn't work :) In order to save the cursor position, the tinymce-codemirror plugin adds the invisible span to the html-code of the TinyMCE content ( <span style="display: none;" class="CmCaReT">&#x0;</span> ).但最有趣的是,以上所有方法都不起作用:) 为了保存光标位置, tinymce-codemirror插件在TinyMCE内容的 html-code 中添加了不可见的span<span style="display: none;" class="CmCaReT">&#x0;</span> )。 Just before the CodeMirror instance is activated, the plugin replaces that span with invisible character (UTF code = 0), otherwise the span tag would be visible in the CodeMirror .就在CodeMirror实例被激活之前,插件用不可见字符(UTF 代码 = 0)替换该跨度,否则span标记将在CodeMirror可见。

This replacement is performed by the following code (in the source.html file located at the same folder as plugin.js and plugin.min.js ): html.replace(/<span\\s+style="display: none;"\\s+class="CmCaReT"([^>]*)>([^<]*)<\\/span>/gm, String.fromCharCode(chr)) .此替换由以下代码执行(在与plugin.jsplugin.min.js位于同一文件夹的source.html文件中): html.replace(/<span\\s+style="display: none;"\\s+class="CmCaReT"([^>]*)>([^<]*)<\\/span>/gm, String.fromCharCode(chr)) The regular expression should find all those span -s, but it doesn't!正则表达式应该找到所有这些span -s,但它没有! The reason is that style and class attributes appear to be in opposite order: the class is the first, then follows the style .原因是styleclass属性似乎处于相反的顺序: class是第一个,然后是style (I think this is the surprise made by the browser - I use Firefox.) (我想这是浏览器带来的惊喜——我用的是火狐。)

Now, we have to change the regular expression to /<span\\s+(style="display: none;")*\\s*(class="CmCaReT")\\s+(style="display: none;")*([^>]*)>([^<]*)<\\/span>/gm , which catches the span tag regardless of the order of its attributes.现在,我们必须将正则表达式更改为/<span\\s+(style="display: none;")*\\s*(class="CmCaReT")\\s+(style="display: none;")*([^>]*)>([^<]*)<\\/span>/gm ,无论其属性的顺序如何,它都会捕获span标记。

So, now jumping between corresponding positions in TinyMCE and CodeMirror works fine!所以,现在在 TinyMCE 和 CodeMirror 的相应位置之间跳转工作正常!

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

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