Skip to content

Commit a468840

Browse files
committed
Fix when smiley is at beginning of the line
1 parent 1fa0194 commit a468840

1 file changed

Lines changed: 34 additions & 21 deletions

File tree

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

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "@ckeditor/ckeditor5-typing";
1414
import {
1515
Collection,
16+
env,
1617
keyCodes,
1718
PositionOptions,
1819
Rect,
@@ -25,7 +26,6 @@ import MentionListItemView from "@ckeditor/ckeditor5-mention/src/ui/mentionlisti
2526
import DomWrapperView from "@ckeditor/ckeditor5-mention/src/ui/domwrapperview";
2627
import WoltlabSmileyCommand from "./woltlabsmileycommand";
2728

28-
const PATTERN = new RegExp(/:[a-z0-9_]+$/);
2929
const MARKER_NAME = "smiley";
3030
const VERTICAL_SPACING = 3;
3131

@@ -200,12 +200,13 @@ export class WoltlabSmileyUi extends Plugin {
200200
});
201201
watcher.on<TextWatcherMatchedEvent>("matched", (evt, data) => {
202202
const position = getLastPosition(data.text)!;
203-
const smileyCode = data.text.substring(position).match(PATTERN)![0];
203+
const smileyCode = data.text.substring(position).match(getRegexExp())![0];
204204
const start = data.range.start.getShiftedBy(position);
205205
const markerRange = editor.model.createRange(
206206
start,
207207
start.getShiftedBy(1),
208208
);
209+
console.log(smileyCode);
209210

210211
if (checkIfMarkerExists(editor)) {
211212
// Update marker position
@@ -231,6 +232,7 @@ export class WoltlabSmileyUi extends Plugin {
231232
return emoji.code.startsWith(smileyCode);
232233
})
233234
.forEach((emoji) => {
235+
console.log(emoji);
234236
this._items.add({
235237
item: {
236238
id: emoji.code,
@@ -345,7 +347,7 @@ export class WoltlabSmileyUi extends Plugin {
345347

346348
function getLastPosition(text: string): number | undefined {
347349
const lastIndex = text.lastIndexOf(":");
348-
if (lastIndex <= 0 || !text.substring(lastIndex - 1).match(PATTERN)) {
350+
if (lastIndex === -1 || !text.substring(lastIndex - 1).match(getRegexExp())) {
349351
return undefined;
350352
}
351353

@@ -365,7 +367,7 @@ function getBalloonPanelPositions(
365367
): PositionOptions["positions"] {
366368
const positions: Record<string, PositionOptions["positions"][0]> = {
367369
// Positions the panel to the southeast of the caret rectangle.
368-
"caret_se": (targetRect: Rect) => {
370+
caret_se: (targetRect: Rect) => {
369371
return {
370372
top: targetRect.bottom + VERTICAL_SPACING,
371373
left: targetRect.right,
@@ -377,7 +379,7 @@ function getBalloonPanelPositions(
377379
},
378380

379381
// Positions the panel to the northeast of the caret rectangle.
380-
"caret_ne": (targetRect: Rect, balloonRect: Rect) => {
382+
caret_ne: (targetRect: Rect, balloonRect: Rect) => {
381383
return {
382384
top: targetRect.top - balloonRect.height - VERTICAL_SPACING,
383385
left: targetRect.right,
@@ -389,7 +391,7 @@ function getBalloonPanelPositions(
389391
},
390392

391393
// Positions the panel to the southwest of the caret rectangle.
392-
"caret_sw": (targetRect: Rect, balloonRect: Rect) => {
394+
caret_sw: (targetRect: Rect, balloonRect: Rect) => {
393395
return {
394396
top: targetRect.bottom + VERTICAL_SPACING,
395397
left: targetRect.right - balloonRect.width,
@@ -401,7 +403,7 @@ function getBalloonPanelPositions(
401403
},
402404

403405
// Positions the panel to the northwest of the caret rect.
404-
"caret_nw": (targetRect: Rect, balloonRect: Rect) => {
406+
caret_nw: (targetRect: Rect, balloonRect: Rect) => {
405407
return {
406408
top: targetRect.top - balloonRect.height - VERTICAL_SPACING,
407409
left: targetRect.right - balloonRect.width,
@@ -415,23 +417,34 @@ function getBalloonPanelPositions(
415417

416418
// Returns only the last position if it was matched to prevent the panel from jumping after the first match.
417419
if (Object.prototype.hasOwnProperty.call(positions, preferredPosition!)) {
418-
return [
419-
positions[preferredPosition!],
420-
];
420+
return [positions[preferredPosition!]];
421421
}
422422

423423
// By default, return all position callbacks ordered depending on the UI language direction.
424-
return uiLanguageDirection !== "rtl" ? [
425-
positions.caret_se,
426-
positions.caret_sw,
427-
positions.caret_ne,
428-
positions.caret_nw,
429-
] : [
430-
positions.caret_sw,
431-
positions.caret_se,
432-
positions.caret_nw,
433-
positions.caret_ne,
434-
];
424+
return uiLanguageDirection !== "rtl"
425+
? [
426+
positions.caret_se,
427+
positions.caret_sw,
428+
positions.caret_ne,
429+
positions.caret_nw,
430+
]
431+
: [
432+
positions.caret_sw,
433+
positions.caret_se,
434+
positions.caret_nw,
435+
positions.caret_ne,
436+
];
437+
}
438+
439+
/**
440+
* {@link module:mention/mentionui#createRegExp()}
441+
*/
442+
export function getRegexExp(): RegExp {
443+
const openAfterCharacters = env.features.isRegExpUnicodePropertySupported
444+
? "\\p{Ps}\\p{Pi}\"'"
445+
: "\\(\\[{\"'";
446+
const pattern = `(?:^|[ ${openAfterCharacters}])(:)([a-z_]+)$`;
447+
return new RegExp(pattern, "u");
435448
}
436449

437450
function isHandledKey(keyCode: number): boolean {

0 commit comments

Comments
 (0)