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">�</span>
).
(但是最有趣的是,以上所有方法均无效:)为了保存光标位置, tinymce-codemirror
插件将不可见的span
添加到TinyMCE
内容的html代码中( <span style="display: none;" class="CmCaReT">�</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.js
和plugin.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
.(原因是style
和class
属性的显示顺序相反: class
是第一个,然后是style
。)
(I think this is the surprise made by the browser - I use Firefox.)((我认为这是浏览器带来的惊喜-我使用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中的相应位置之间跳转可以正常工作!)