Skip to content

A platform for community discussion. Free, open, simple.

FIX: emojis should insert right after punctuation correctly (#35830)

Kelv5ae2289db54d
diff --git a/frontend/discourse/app/components/d-editor.gjs b/frontend/discourse/app/components/d-editor.gjs
index fff6ec733db..904b1f2de0b 100644
--- a/frontend/discourse/app/components/d-editor.gjs
+++ b/frontend/discourse/app/components/d-editor.gjs
@@ -45,7 +45,10 @@ import {
initUserStatusHtml,
renderUserStatusHtml,
} from "discourse/lib/user-status-on-autocomplete";
-import { SKIP } from "discourse/modifiers/d-autocomplete";
+import {
+ EMOJI_ALLOWED_PRECEDING_CHARS_REGEXP,
+ SKIP,
+} from "discourse/modifiers/d-autocomplete";
import { i18n } from "discourse-i18n";
let _createCallbacks = [];
@@ -324,10 +327,10 @@ export default class DEditor extends Component {
},
onKeyUp: (text, cp) => {
- const matches =
- /(?:^|[\s.\?,@\/#!%&*;:\[\]{}=\-_()])(:(?!:).?[\w-]*:?(?!:)(?:t\d?)?:?) ?$/gi.exec(
- text.substring(0, cp)
- );
+ const matches = new RegExp(
+ `(?:^|${EMOJI_ALLOWED_PRECEDING_CHARS_REGEXP.source})(:(?!:).?[\\w-]*:?(?!:)(?:t\\d?)?:?) ?$`,
+ "gi"
+ ).exec(text.substring(0, cp));
if (matches && matches[1]) {
return [matches[1]];
diff --git a/frontend/discourse/app/modifiers/d-autocomplete.js b/frontend/discourse/app/modifiers/d-autocomplete.js
index 0760f64a10b..989e0486926 100644
--- a/frontend/discourse/app/modifiers/d-autocomplete.js
+++ b/frontend/discourse/app/modifiers/d-autocomplete.js
@@ -13,6 +13,14 @@ import { VISIBILITY_OPTIMIZERS } from "float-kit/lib/constants";
export const SKIP = "skip";
export const CANCELLED_STATUS = "__CANCELLED";
+/**
+ * Regex for characters that can precede an emoji autocomplete trigger.
+ * This matches the same characters allowed in emoji autocomplete's onKeyUp regex.
+ * Used to ensure consistency between trigger detection and completion position calculation.
+ */
+export const EMOJI_ALLOWED_PRECEDING_CHARS_REGEXP =
+ /[\s.?,@/#!%&*;:\[\]{}=\-_()+]/;
+
/**
* Class-based modifier for adding autocomplete functionality to input elements
* Preserves exact CSS structure for backward compatibility
@@ -606,10 +614,20 @@ export default class DAutocompleteModifier extends Modifier {
prev = this.getValue()[caretPos - 1];
const shouldTrigger = await this.shouldTrigger({ backSpace });
- if (
- shouldTrigger &&
- (prev === undefined || this.ALLOWED_LETTERS_REGEXP.test(prev))
- ) {
+ // For emoji autocomplete (key === ':'), use a more permissive check that includes
+ // common punctuation like comma, period, etc. that can appear before emoji
+ let isAllowed;
+ if (this.options.key === ":") {
+ // Match the same characters allowed in emoji autocomplete's onKeyUp regex
+ isAllowed =
+ prev === undefined ||
+ EMOJI_ALLOWED_PRECEDING_CHARS_REGEXP.test(prev);
+ } else {
+ isAllowed =
+ prev === undefined || this.ALLOWED_LETTERS_REGEXP.test(prev);
+ }
+
+ if (shouldTrigger && isAllowed) {
start = caretPos;
term = this.getValue().substring(caretPos + 1, initialCaretPos);
end = caretPos + term.length;
diff --git a/frontend/discourse/tests/acceptance/emoji-test.js b/frontend/discourse/tests/acceptance/emoji-test.js
index 186be534e84..17e373c0552 100644
--- a/frontend/discourse/tests/acceptance/emoji-test.js
+++ b/frontend/discourse/tests/acceptance/emoji-test.js
@@ -63,6 +63,23 @@ acceptance("Emoji", function (needs) {
);
});
+ test("emoji autocomplete after punctuation", async function (assert) {
+ await visit("/t/internationalization-localization/280");
+ await click("#topic-footer-buttons .btn.create");
+
+ await simulateKeys(".d-editor-input", "Hello world,:smi");
+ assert.dom(".autocomplete.ac-emoji").exists();
+
+ await simulateKey(".d-editor-input", "\t");
+
+ assert
+ .dom(".d-editor-input")
+ .hasValue(
+ "Hello world,:smiley: ",
+ "emoji inserted at correct position after comma"
+ );
+ });
+
needs.settings({ emoji_autocomplete_min_chars: 2 });
test("siteSetting:emoji_autocomplete_min_chars", async function (assert) {