Skip to content

Commit 1fa0194

Browse files
committed
Fix rang of marker
1 parent bc306ba commit 1fa0194

1 file changed

Lines changed: 10 additions & 18 deletions

File tree

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ export class WoltlabSmileyUi extends Plugin {
163163
const model = editor.model;
164164
const item = data.item;
165165

166-
const mentionMarker = editor.model.markers.get(MARKER_NAME);
166+
const smileyMarker = editor.model.markers.get(MARKER_NAME);
167167

168168
// Create a range on matched text.
169169
const end = model.createPositionAt(model.document.selection.focus!);
170-
const start = model.createPositionAt(mentionMarker!.getStart());
170+
const start = model.createPositionAt(smileyMarker!.getStart());
171171
const range = model.createRange(start, end);
172172

173173
this.#hideBalloon();
@@ -196,24 +196,16 @@ export class WoltlabSmileyUi extends Plugin {
196196
#registerTextWatcher() {
197197
const editor = this.editor;
198198
const watcher = new TextWatcher(editor.model, (text: string) => {
199-
const position = getLastPosition(text);
200-
if (position === undefined) {
201-
return false;
202-
}
203-
204-
return text.substring(position).match(PATTERN);
199+
return getLastPosition(text) !== undefined;
205200
});
206201
watcher.on<TextWatcherMatchedEvent>("matched", (evt, data) => {
207-
const position = getLastPosition(data.text);
208-
const smileyCode = data.text.substring(position!).match(PATTERN)![0];
209-
const selection = editor.model.document.selection;
210-
const focus = selection.focus;
211-
const markerPosition = editor.model.createPositionAt(
212-
focus!.parent,
213-
position!,
202+
const position = getLastPosition(data.text)!;
203+
const smileyCode = data.text.substring(position).match(PATTERN)![0];
204+
const start = data.range.start.getShiftedBy(position);
205+
const markerRange = editor.model.createRange(
206+
start,
207+
start.getShiftedBy(1),
214208
);
215-
const end = markerPosition.getShiftedBy(1);
216-
const markerRange = editor.model.createRange(markerPosition, end);
217209

218210
if (checkIfMarkerExists(editor)) {
219211
// Update marker position
@@ -353,7 +345,7 @@ export class WoltlabSmileyUi extends Plugin {
353345

354346
function getLastPosition(text: string): number | undefined {
355347
const lastIndex = text.lastIndexOf(":");
356-
if (lastIndex === -1 || !text.substring(lastIndex - 1).match(PATTERN)) {
348+
if (lastIndex <= 0 || !text.substring(lastIndex - 1).match(PATTERN)) {
357349
return undefined;
358350
}
359351

0 commit comments

Comments
 (0)