|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | import { Command, Editor } from "@ckeditor/ckeditor5-core"; |
11 | | -import { Range } from "@ckeditor/ckeditor5-engine"; |
| 11 | +import { Range, Node } from "@ckeditor/ckeditor5-engine"; |
12 | 12 |
|
13 | 13 | export default class WoltlabSmileyCommand extends Command { |
14 | 14 | /** |
@@ -53,9 +53,26 @@ export default class WoltlabSmileyCommand extends Command { |
53 | 53 | const modelFragment = this.editor.data.toModel(viewFragment); |
54 | 54 |
|
55 | 55 | const smileyRange = model.insertContent(modelFragment, range); |
56 | | - writer.setSelection( |
57 | | - model.insertContent(writer.createText(" "), smileyRange.end).end, |
58 | | - ); |
| 56 | + |
| 57 | + // If a smiley is inserted at the beginning of a paragraph, `smileyRange` |
| 58 | + // is not the range of the smiley, but the entire paragraph in which the smiley was inserted. |
| 59 | + let element: Node = smileyRange.getContainedElement()!; |
| 60 | + if (element.is("element", "paragraph")) { |
| 61 | + element = element.getChild(0)!; |
| 62 | + } |
| 63 | + |
| 64 | + writer.setSelection(element, "after"); |
| 65 | + |
| 66 | + // Don't add a white space if the smiley is followed by a white space. |
| 67 | + const nodeAfter = element.nextSibling; |
| 68 | + const isFollowedByWhiteSpace = |
| 69 | + nodeAfter && nodeAfter.is("$text") && nodeAfter.data.startsWith(" "); |
| 70 | + |
| 71 | + if (!isFollowedByWhiteSpace) { |
| 72 | + writer.setSelection( |
| 73 | + model.insertContent(writer.createText(" "), element, "after").end, |
| 74 | + ); |
| 75 | + } |
59 | 76 | }); |
60 | 77 | } |
61 | 78 | } |
0 commit comments