Skip to content

Commit

Permalink
Search for the underlying element:ImageInline that corresponds to t…
Browse files Browse the repository at this point in the history
…he inserted smiley and select its end and insert a blank line if necessary.
  • Loading branch information
Cyperghost committed Oct 8, 2024
1 parent 6077dd0 commit 34a8a36
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions plugins/ckeditor5-woltlab-smiley/src/woltlabsmileycommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

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

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

const smileyRange = model.insertContent(modelFragment, range);
writer.setSelection(
model.insertContent(writer.createText(" "), smileyRange.end).end,
);

// If a smiley is inserted at the beginning of a paragraph, `smileyRange`
// is not the range of the smiley, but the entire paragraph in which the smiley was inserted.
let element: Node = smileyRange.getContainedElement()!;
if (element.is("element", "paragraph")) {
element = element.getChild(0)!;
}

writer.setSelection(element, "after");

// Don't add a white space if the smiley is followed by a white space.
const nodeAfter = element.nextSibling;
const isFollowedByWhiteSpace =
nodeAfter && nodeAfter.is("$text") && nodeAfter.data.startsWith(" ");

if (!isFollowedByWhiteSpace) {
writer.setSelection(
model.insertContent(writer.createText(" "), element, "after").end,
);
}
});
}
}

0 comments on commit 34a8a36

Please sign in to comment.