Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Open @ and / menus with a Japanese input method (PRD-9408) (#6412)

Andrew Mikofalvy6a54a2758d78
diff --git a/.changeset/ime-menu-triggers.md b/.changeset/ime-menu-triggers.md
new file mode 100644
index 000000000..dee6d8da4
--- /dev/null
+++ b/.changeset/ime-menu-triggers.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+The `@` and `/` menus now open when typing with a Japanese input method: after an ideographic space, on full-width `@` and `/`, and once the input method commits the character.
diff --git a/packages/app/src/editor/composer-mention/composer-mention.ts b/packages/app/src/editor/composer-mention/composer-mention.ts
index dcc1ded59..53c2f5d9d 100644
--- a/packages/app/src/editor/composer-mention/composer-mention.ts
+++ b/packages/app/src/editor/composer-mention/composer-mention.ts
@@ -18,6 +18,13 @@ import {
type SuggestionPositionState,
setSuggestionSelectableCount,
} from '../extensions/suggestion-floating-ui';
+import {
+ allowOutsideComposition,
+ createTriggerMatcher,
+ MENTION_TRIGGER_CHARS,
+ SUGGESTION_TRIGGER_PREFIXES,
+ SuggestionCompositionEnd,
+} from '../extensions/suggestion-ime';
import { fetchPages, filterPages, type PageItem } from '../extensions/wiki-link-suggestion';
import { lucideIconToSvgString } from '../registry/lucide-svg';
import { ComposerMentionMenu } from './ComposerMentionMenu';
@@ -241,6 +248,7 @@ export function composerMentionExtensions(options?: {
ComposerHistory,
ComposerMention,
ComposerCommand,
+ SuggestionCompositionEnd,
...(options?.slashCommands !== undefined
? [ComposerSlashCommand.configure({ commands: options.slashCommands })]
: []),
@@ -320,6 +328,9 @@ function configureComposerMentionSuggestion(editor: Editor) {
editor,
pluginKey: composerMentionSuggestionKey,
char: '@',
+ allowedPrefixes: [...SUGGESTION_TRIGGER_PREFIXES],
+ findSuggestionMatch: createTriggerMatcher(MENTION_TRIGGER_CHARS),
+ allow: allowOutsideComposition,
items: ({ query }) => corpus.getItems(query, getMentionRecency(editor)),
diff --git a/packages/app/src/editor/composer-mention/composer-slash-command.ts b/packages/app/src/editor/composer-mention/composer-slash-command.ts
index 047cf84c1..f6a467d2c 100644
--- a/packages/app/src/editor/composer-mention/composer-slash-command.ts
+++ b/packages/app/src/editor/composer-mention/composer-slash-command.ts
@@ -12,6 +12,12 @@ import {
type SuggestionPositionState,
setSuggestionSelectableCount,
} from '../extensions/suggestion-floating-ui';
+import {
+ allowOutsideComposition,
+ createTriggerMatcher,
+ IDEOGRAPHIC_SPACE,
+ SLASH_TRIGGER_CHARS,
+} from '../extensions/suggestion-ime';
import { ComposerSlashMenu } from './ComposerSlashMenu';
export interface SlashCommandItem {
@@ -140,7 +146,7 @@ export function leadingSlashToken(text: string): { name: string; length: number
return { name: match[1] ?? '', length: match[0].length };
}
-const SLASH_TRIGGER_PREFIXES: readonly string[] = [' ', '\t', '\n', '\u00a0'];
+const SLASH_TRIGGER_PREFIXES: readonly string[] = [' ', '\t', '\n', '\u00a0', IDEOGRAPHIC_SPACE];
export function slashTokenTailLength(textAfterCaret: string): number {
return /^\S*/.exec(textAfterCaret)?.[0].length ?? 0;
@@ -283,6 +289,8 @@ export const ComposerSlashCommand = Extension.create<SlashCommandOptions, SlashC
pluginKey: composerSlashSuggestionKey,
char: '/',
allowedPrefixes: [...SLASH_TRIGGER_PREFIXES],
+ findSuggestionMatch: createTriggerMatcher(SLASH_TRIGGER_CHARS),
+ allow: allowOutsideComposition,
items: ({ query }) => {
const commands = liveCommands();
diff --git a/packages/app/src/editor/extensions/shared.ts b/packages/app/src/editor/extensions/shared.ts
index 08bf7eff6..71f7c68a5 100644
--- a/packages/app/src/editor/extensions/shared.ts
+++ b/packages/app/src/editor/extensions/shared.ts
@@ -32,6 +32,7 @@ import { RawMdxFallback } from './raw-mdx-fallback';
import { SelectionStatePlugin } from './selection-state-plugin';
import { SlashCommand } from './slash-command';
import { SourceDirtyObserver } from './source-dirty-observer';
+import { SuggestionCompositionEnd } from './suggestion-ime';
import { TabFocusTrap } from './tab-focus-trap';
import { TableInsertControls } from './table-insert-controls';
import { TableRowEnter } from './table-row-enter';
@@ -78,6 +79,7 @@ export const sharedExtensions = [
embed: 'Embeds',
},
}),
+ SuggestionCompositionEnd,
FormattingShortcuts,
TabFocusTrap,
FileHandler.configure({
diff --git a/packages/app/src/editor/extensions/slash-command.ts b/packages/app/src/editor/extensions/slash-command.ts
index e84a52952..221c60abd 100644
--- a/packages/app/src/editor/extensions/slash-command.ts
+++ b/packages/app/src/editor/extensions/slash-command.ts
@@ -12,6 +12,11 @@ import {
destroySuggestionPopup,
type SuggestionPositionState,
} from './suggestion-floating-ui';
+import {
+ createTriggerMatcher,
+ SLASH_TRIGGER_CHARS,
+ SUGGESTION_TRIGGER_PREFIXES,
+} from './suggestion-ime';
const slashCommandKey = new PluginKey('slashCommand');
@@ -87,6 +92,8 @@ export const SlashCommand = Extension.create<SlashCommandOptions>({
pluginKey: slashCommandKey,
char: '/',
startOfLine: false,
+ allowedPrefixes: [...SUGGESTION_TRIGGER_PREFIXES],
+ findSuggestionMatch: createTriggerMatcher(SLASH_TRIGGER_CHARS),
allow: suggestionAllow,
items: ({ query, editor }) =>
diff --git a/packages/app/src/editor/extensions/suggestion-allow.ts b/packages/app/src/editor/extensions/suggestion-allow.ts
index f1b3c10c0..cd8750e70 100644
--- a/packages/app/src/editor/extensions/suggestion-allow.ts
+++ b/packages/app/src/editor/extensions/suggestion-allow.ts
@@ -2,16 +2,20 @@ import type { Editor, Range } from '@tiptap/core';
import type { EditorState } from '@tiptap/pm/state';
import { isInLiteralTextContext } from '../literal-text-context';
import { getEditorSourceMode } from './editor-mode-context';
+import { allowOutsideComposition } from './suggestion-ime';
export function suggestionAllow({
editor,
state,
range,
+ isActive,
}: {
editor: Editor;
state: EditorState;
range: Range;
+ isActive?: boolean;
}): boolean {
if (getEditorSourceMode(editor)) return false;
+ if (!allowOutsideComposition({ editor, isActive })) return false;
return !isInLiteralTextContext(state, range.from, range.to);
}
diff --git a/packages/app/src/editor/extensions/suggestion-ime-triggers.dom.test.tsx b/packages/app/src/editor/extensions/suggestion-ime-triggers.dom.test.tsx
new file mode 100644
index 000000000..b9e2326dc
--- /dev/null
+++ b/packages/app/src/editor/extensions/suggestion-ime-triggers.dom.test.tsx
@@ -0,0 +1,323 @@
+import { cleanup } from '@testing-library/react';
+import type { Extensions } from '@tiptap/core';
+import { Editor } from '@tiptap/core';
+import type { Plugin, PluginKey } from '@tiptap/pm/state';
+import { afterEach, describe, expect, test, vi } from 'vitest';
+import {
+ composerMentionExtensions,
+ composerMentionSuggestionKey,
+} from '../composer-mention/composer-mention';
+import {
+ composerSlashSuggestionKey,
+ type SlashCommandItem,
+} from '../composer-mention/composer-slash-command';
+import { sharedExtensions } from './shared';
+
+interface SuggestionState {
+ active: boolean;
+ query: string | null;
+ text: string | null;
+ range: { from: number; to: number };
+}
+
+const COMMANDS: SlashCommandItem[] = [
+ { name: 'review', description: 'Review the current diff' },
+ { name: 'create_plan', description: 'Draft an implementation plan' },
+];
+
+function fetchResponse(status: number, body: unknown): unknown {
+ return { ok: status >= 200 && status < 300, status, json: async () => body };
+}
+
+function stubCorpusFetch(): void {
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async (input: unknown) => {
+ if (String(input).includes('/api/pages')) {
+ return fetchResponse(200, {
+ pages: [
+ {
+ docName: 'alpha',
+ title: 'Alpha',
+ docExt: '.md',
+ size: 1,
+ modified: '2026-01-01T00:00:00.000Z',
+ },
+ ],
+ });
+ }
+ return fetchResponse(404, {});
+ }),
+ );
+}
+
+function mountEditor(extensions: Extensions): { editor: Editor; container: HTMLDivElement } {
+ const container = document.createElement('div');
+ document.body.appendChild(container);
+ const editor = new Editor({
+ element: container,
+ extensions,
+ editable: true,
+ content: { type: 'doc', content: [{ type: 'paragraph' }] },
+ });
+ return { editor, container };
+}
+
+function teardown(editor: Editor, container: HTMLDivElement): void {
+ editor.destroy();
+ container.remove();
+ for (const node of Array.from(document.body.children)) {
+ if (node !== container) node.remove();
+ }
+}
+
+function editorSlashPlugin(editor: Editor): Plugin | undefined {
+ return editor.state.plugins.find((p) => {
+ const keyName = (p as { spec?: { key?: { key?: string } } }).spec?.key?.key;
+ return typeof keyName === 'string' && keyName.startsWith('slashCommand');
+ });
+}
+
+function editorSlashState(editor: Editor): SuggestionState | null {
+ return (editorSlashPlugin(editor)?.getState(editor.state) as SuggestionState | undefined) ?? null;
+}
+
+function stateOf(editor: Editor, key: PluginKey): SuggestionState | null {
+ return (key.getState(editor.state) as SuggestionState | undefined) ?? null;
+}
+
+function pressEnterOnSuggestion(editor: Editor, plugin: Plugin | undefined): boolean {
+ const handleKeyDown = plugin?.props.handleKeyDown;
+ if (!plugin || !handleKeyDown) return false;
+ const event = new KeyboardEvent('keydown', { key: 'Enter', bubbles: true });
+ return handleKeyDown.call(plugin, editor.view, event) === true;
+}
+
+async function pressEnterOnceItemsLoad(
+ editor: Editor,
+ plugin: (editor: Editor) => Plugin | undefined,
+): Promise<boolean> {
+ for (let i = 0; i < 50; i++) {
+ if (pressEnterOnSuggestion(editor, plugin(editor))) return true;
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ }
+ return false;
+}
+
+function startComposition(editor: Editor): void {
+ editor.view.dom.dispatchEvent(new CompositionEvent('compositionstart', { bubbles: true }));
+}
+
+function composeText(editor: Editor, text: string): void {
+ editor.view.dispatch(editor.state.tr.insertText(text));
+}
+
+function endComposition(editor: Editor, data: string): void {
+ editor.view.dom.dispatchEvent(new CompositionEvent('compositionend', { bubbles: true, data }));
+}
+
+async function settle(): Promise<void> {
+ for (let i = 0; i < 5; i++) await new Promise((resolve) => setTimeout(resolve, 0));
+}
+
+interface Surface {
+ name: string;
+ extensions: () => Extensions;
+ state: (editor: Editor) => SuggestionState | null;
+ ascii: string;
+ fullWidth: string;
+}
+
+const SURFACES: Surface[] = [
+ {
+ name: 'composer @ mention',
+ extensions: () => composerMentionExtensions(),
+ state: (editor) => stateOf(editor, composerMentionSuggestionKey),
+ ascii: '@',
+ fullWidth: '\uff20',
+ },
+ {
+ name: 'composer / command',
+ extensions: () => composerMentionExtensions({ slashCommands: COMMANDS }),
+ state: (editor) => stateOf(editor, composerSlashSuggestionKey),
+ ascii: '/',
+ fullWidth: '\uff0f',
+ },
+ {
+ name: 'editor / menu',
+ extensions: () => sharedExtensions,
+ state: editorSlashState,
+ ascii: '/',
+ fullWidth: '\uff0f',
+ },
+];
+
+afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+});
+
+describe.each(SURFACES)('$name with a Japanese input method', (surface) => {
+ async function typeAndRead(text: string): Promise<SuggestionState | null> {
+ stubCorpusFetch();
+ const { editor, container } = mountEditor(surface.extensions());
+ try {
+ editor.commands.focus('end');
+ editor.commands.insertContent(text);
+ await settle();
+ return surface.state(editor);
+ } finally {
+ teardown(editor, container);
+ }
+ }
+
+ test('the ASCII trigger after an ideographic space opens the menu', async () => {
+ const state = await typeAndRead(`メモ\u3000${surface.ascii}re`);
+ expect(state?.active).toBe(true);
+ expect(state?.query).toBe('re');
+ });
+
+ test('the full-width trigger opens the menu at the start of a line', async () => {
+ const state = await typeAndRead(`${surface.fullWidth}re`);
+ expect(state?.active).toBe(true);
+ expect(state?.query).toBe('re');
+ expect(state?.text).toBe(`${surface.fullWidth}re`);
+ });
+
+ test('the full-width trigger after an ideographic space opens the menu', async () => {
+ const state = await typeAndRead(`メモ\u3000${surface.fullWidth}`);
+ expect(state?.active).toBe(true);
+ expect(state?.query).toBe('');
+ });
+
+ test('a full-width trigger glued to a preceding word stays prose, like the ASCII one', async () => {
+ expect((await typeAndRead(`and${surface.ascii}or`))?.active).toBeFalsy();
+ expect((await typeAndRead(`and${surface.fullWidth}or`))?.active).toBeFalsy();
+ });
+
+ test('a second trigger glued into the query closes the menu, whichever width either is', async () => {
+ expect((await typeAndRead(`${surface.ascii}re${surface.ascii}x`))?.active).toBeFalsy();
+ expect((await typeAndRead(`${surface.fullWidth}re${surface.ascii}x`))?.active).toBeFalsy();
+ expect((await typeAndRead(`${surface.ascii}re${surface.fullWidth}x`))?.active).toBeFalsy();
+ });
+
+ test('the query stops at an ideographic space', async () => {
+ expect((await typeAndRead(`${surface.fullWidth}re\u3000x`))?.active).toBeFalsy();
+ });
+
+ test.each([
+ ['full-width', 'メモ\u3000', surface.fullWidth],
+ ['ASCII', 'memo ', surface.ascii],
+ ])(
+ 'a %s trigger composed by the IME opens no menu until the composition commits',
+ async (_label, prefix, trigger) => {
+ stubCorpusFetch();
+ const { editor, container } = mountEditor(surface.extensions());
+ try {
+ editor.commands.focus('end');
+ editor.commands.insertContent(prefix);
+ startComposition(editor);
+ composeText(editor, trigger);
+ await settle();
+ expect(editor.view.composing).toBe(true);
+ expect(surface.state(editor)?.active).toBeFalsy();
+
+ endComposition(editor, trigger);
+ await settle();
+ expect(editor.view.composing).toBe(false);
+ expect(surface.state(editor)?.active).toBe(true);
+ } finally {
+ teardown(editor, container);
+ }
+ },
+ );
+
+ test('an open menu keeps filtering while the IME composes its query', async () => {
+ stubCorpusFetch();
+ const { editor, container } = mountEditor(surface.extensions());
+ try {
+ editor.commands.focus('end');
+ editor.commands.insertContent(surface.ascii);
+ await settle();
+ expect(surface.state(editor)?.active).toBe(true);
+
+ startComposition(editor);
+ composeText(editor, 'れ');
+ await settle();
+ expect(surface.state(editor)?.active).toBe(true);
+ expect(surface.state(editor)?.query).toBe('れ');
+ } finally {
+ teardown(editor, container);
+ }
+ });
+});
+
+describe('picking an item replaces the full-width trigger like the ASCII one', () => {
+ test('composer @ mention: the mention chip replaces \uff20 and its query', async () => {
+ stubCorpusFetch();
+ const { editor, container } = mountEditor(composerMentionExtensions());
+ try {
+ editor.commands.focus('end');
+ editor.commands.insertContent('見て\u3000\uff20alp');
+ expect(
+ await pressEnterOnceItemsLoad(editor, (e) => composerMentionSuggestionKey.get(e.state)),
+ ).toBe(true);
+ let mentions = 0;
+ editor.state.doc.descendants((node) => {
+ if (node.type.name === 'composerMention') mentions += 1;
+ });
+ expect(mentions).toBe(1);
+ expect(editor.state.doc.textContent).toBe('見て\u3000 ');
+ } finally {
+ teardown(editor, container);
+ }
+ });
+
+ test('composer / command at message start: \uff0frev becomes the /review pill', async () => {
+ const { editor, container } = mountEditor(
+ composerMentionExtensions({ slashCommands: COMMANDS }),
+ );
+ try {
+ editor.commands.focus('end');
+ editor.commands.insertContent('\uff0frev');
+ expect(
+ await pressEnterOnceItemsLoad(editor, (e) => composerSlashSuggestionKey.get(e.state)),
+ ).toBe(true);
+ const lead = editor.state.doc.firstChild?.firstChild;
+ expect(lead?.type.name).toBe('composerCommand');
+ expect(lead?.attrs.name).toBe('review');
+ expect(editor.state.doc.textContent).not.toContain('\uff0f');
+ } finally {
+ teardown(editor, container);
+ }
+ });
+
+ test('composer / command mid-message: \uff0frev completes to plain /review', async () => {
+ const { editor, container } = mountEditor(
+ composerMentionExtensions({ slashCommands: COMMANDS }),
+ );
+ try {
+ editor.commands.focus('end');
+ editor.commands.insertContent('見て\u3000\uff0frev');
+ expect(
+ await pressEnterOnceItemsLoad(editor, (e) => composerSlashSuggestionKey.get(e.state)),
+ ).toBe(true);
+ expect(editor.state.doc.textContent).toBe('見て\u3000/review ');
+ } finally {
+ teardown(editor, container);
+ }
+ });
+
+ test('editor / menu: picking a heading consumes \uff0f and its query', async () => {
+ const { editor, container } = mountEditor(sharedExtensions);
+ try {
+ editor.commands.focus('end');
+ editor.commands.insertContent('\uff0fheading1');
+ expect(await pressEnterOnceItemsLoad(editor, editorSlashPlugin)).toBe(true);
+ expect(editor.state.doc.firstChild?.type.name).toBe('heading');
+ expect(editor.state.doc.textContent).toBe('');
+ } finally {
+ teardown(editor, container);
+ }
+ });
+});
diff --git a/packages/app/src/editor/extensions/suggestion-ime.ts b/packages/app/src/editor/extensions/suggestion-ime.ts
new file mode 100644
index 000000000..a6c9888f6
--- /dev/null
+++ b/packages/app/src/editor/extensions/suggestion-ime.ts
@@ -0,0 +1,68 @@
+import { type Editor, Extension, escapeForRegEx } from '@tiptap/core';
+import { Plugin, PluginKey } from '@tiptap/pm/state';
+import type { SuggestionMatch, Trigger } from '@tiptap/suggestion';
+
+export const IDEOGRAPHIC_SPACE = '\u3000';
+
+export const SUGGESTION_TRIGGER_PREFIXES: readonly string[] = [' ', IDEOGRAPHIC_SPACE];
+
+export const MENTION_TRIGGER_CHARS: readonly string[] = ['@', '\uff20'];
+
+export const SLASH_TRIGGER_CHARS: readonly string[] = ['/', '\uff0f'];
+
+export function createTriggerMatcher(
+ triggerChars: readonly string[],
+): (config: Trigger) => SuggestionMatch {
+ const chars = triggerChars.map(escapeForRegEx).join('');
+ const pattern = new RegExp(`[${chars}][^\\s${chars}]*`, 'g');
+ return ({ allowedPrefixes, $position }) => {
+ const text = $position.nodeBefore?.isText ? $position.nodeBefore.text : undefined;
+ if (!text) return null;
+ const match = Array.from(text.matchAll(pattern)).pop();
+ if (match === undefined) return null;
+ const prefix = text.slice(Math.max(0, match.index - 1), match.index);
+ if (allowedPrefixes !== null && prefix !== '' && !allowedPrefixes.includes(prefix)) {
+ return null;
+ }
+ const from = $position.pos - text.length + match.index;
+ const to = from + match[0].length;
+ if (from >= $position.pos || to < $position.pos) return null;
+ return { range: { from, to }, query: match[0].slice(1), text: match[0] };
+ };
+}
+
+export function allowOutsideComposition({
+ editor,
+ isActive,
+}: {
+ editor: Editor;
+ isActive?: boolean;
+}): boolean {
+ return isActive === true || !editor.view.composing;
+}
+
+const suggestionCompositionEndKey = new PluginKey('suggestionCompositionEnd');
+
+export const SuggestionCompositionEnd = Extension.create({
+ name: 'suggestionCompositionEnd',
+
+ addProseMirrorPlugins() {
+ return [
+ new Plugin({
+ key: suggestionCompositionEndKey,
+ props: {
+ handleDOMEvents: {
+ compositionend(view) {
+ const committed = view.state;
+ setTimeout(() => {
+ if (view.isDestroyed || view.composing || view.state !== committed) return;
+ view.dispatch(view.state.tr.setMeta('addToHistory', false));
+ }, 0);
+ return false;
+ },
+ },
+ },
+ }),
+ ];
+ },
+});