Skip to content

Commit ecb74e6

Browse files
authored
Merge pull request #133 from WoltLab/bugfix/smiley-insert-newline
Do not insert a new paragraph when inserting a smiley
2 parents 6077dd0 + 34a8a36 commit ecb74e6

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

plugins/ckeditor5-woltlab-smiley/src/woltlabsmileycommand.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import { Command, Editor } from "@ckeditor/ckeditor5-core";
11-
import { Range } from "@ckeditor/ckeditor5-engine";
11+
import { Range, Node } from "@ckeditor/ckeditor5-engine";
1212

1313
export default class WoltlabSmileyCommand extends Command {
1414
/**
@@ -53,9 +53,26 @@ export default class WoltlabSmileyCommand extends Command {
5353
const modelFragment = this.editor.data.toModel(viewFragment);
5454

5555
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+
}
5976
});
6077
}
6178
}

0 commit comments

Comments
 (0)