简体   繁体   English

在CKEditor中,字体格式不适用于空段落

[英]in CKEditor font formatting is not applied to empty paragraphs

Consider the following markup: 考虑以下标记:

<p>spaceflight</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>spaceflight</p>

When user selects the whole block of text (eg using [Ctrl+A]) and applies font formatting in CKEditor the resulting markup is something like this: 当用户选择整个文本块(例如使用[Ctrl + A])并在CKEditor中应用字体格式时,生成的标记是这样的:

<p><span style="font-family:comic sans ms,cursive">spaceflight</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family:comic sans ms,cursive">spaceflight</span></p>

ie font is not applied to the empty paragraphs. 即字体不适用于空段落。

Steps to reproduce the problem: 重现问题的步骤: 在此输入图像描述

  1. type a couple of lines of text, use [Enter] to insert empty paragraphs in between the lines 键入几行文本,使用[Enter]在行之间插入空段落
  2. use [Ctrl+A] to select the whole block of text and apply "Comic Sans MS" font to the selection 使用[Ctrl + A]选择整个文本块,并将“Comic Sans MS”字体应用于选择
  3. use [Source] to check the resulting markup and see that the empty paragraphs are missing the font style 使用[Source]检查生成的标记,看到空段落缺少字体样式
  4. switch back to editing mode and type inside one of the empty paragraphs, the new text indeed uses default font 切换回编辑模式并在其中一个空段落中键入,新文本确实使用默认字体

Is there any known workaround allowing to make CKEditor to apply formatting to empty paragraphs inside the current selection? 是否有任何已知的解决方法允许CKEditor将格式应用于当前选择内的空段落?

The problem here is after user has explicitly applied font to the whole block they expect the whole block to be in that font, but when they use cursor and navigate to one of the empty paragraphs and start typing there it's a different font altogether 这里的问题是在用户明确地将字体应用到他们希望整个块都使用该字体的整个块之后,但是当他们使用光标并导航到其中一个空段并开始在那里输入时,它们完全是一个不同的字体

So the problem is actually different - it's not related to data, but to these empty blocks not preserving styling inside the editor. 所以问题实际上是不同的 - 它与数据无关,而是与这些空块无关,而不是在编辑器中保留样式。 There's a ticket for this somewhere , but the solution is not trivial. 这个地方有一张票,但解决方案并非无足轻重。 The problem is that when you apply style to an empty block this structure is created (inspect the DOM of the editor): 问题是当你将样式应用于空块时,会创建这个结构(检查编辑器的DOM):

<p><strong></strong><br /></p>

The <br> there is for making the paragraph visible - it's so called bogus br. <br>也用于制作段落可见-它所谓的假BR。 Otherwise, the paragraph would have 0 height. 否则,该段落的高度为0。

Now, when you try to place your caret there (using mouse or keyboard) the browser finds an empty strong element, which is 0x0 and a paragraph which has non-zero width and height. 现在,当您尝试将插入符号放在那里(使用鼠标或键盘)时,浏览器会找到一个空的强元素,即0x0和一个宽度和高度均为非零的段落。 Where the browser puts your caret? 浏览器放置你的插入符号? In the paragraph. 在该段中。

The same situation happens if you apply style on an empty selection and move your caret - there's still an empty inline element out there, but you won't be able to move your caret back to it. 如果你在一个空的选择上应用样式并移动你的插入符号也会出现同样的情况 - 那里仍然有一个空的内联元素,但你将无法将你的插入符号移回它。

It is possible to workaround this and there's even more than one way. 有可能解决这个问题,甚至还有不止一种方法。 Unfortunately, none is trivial. 不幸的是,没有一件是微不足道的。

If you want to attempt to implement a hack which should solve the most common cases, then it should work like this: 如果你想尝试实现一个应该解决最常见情况的hack,那么它应该像这样工作:

  1. Watch the selection changes ( editor#selectionChange ). 观察选择更改( editor#selectionChange )。
  2. If selection is collapsed and it is placed in an empty block (mind bogus brs) and there's an empty inline element, move caret there. 如果选择被折叠并且它被放置在一个空块中(假想brsus brs)并且有一个空的内联元素,请在那里移动插入符号。
  3. CKEditor's selection engine is able to place caret in an empty inline element (native implementations in Webkit and Blink are not able to do that). CKEditor的选择引擎能够将插入符号放在空的内联元素中(Webkit中的本机实现和Blink无法做到这一点)。

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

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