Files
outline/shared/editor/plugins/SuggestionsMenuPlugin.ts
T
Tom MoorandClaude Opus 5 9181bf61b6 Add [[ trigger for mentioning or creating a document (#13402)
* feat: Add [[ trigger for mentioning or creating a document

Opens a suggestions menu filtered to documents, backed by a title search,
with options to create a new or nested document from the search term.

Generalizes the suggestion machinery to support multi-character triggers,
and extracts the mention menu item builders into app/editor/menus/mention.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Trim the mention search term

Spaces are allowed in the search term, so a whitespace-only query rendered
create-document items with an empty title and issued a title search that the
server rejects.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Close suggestion menus on a space directly after the trigger

Spaces remain allowed within a search term, they are only rejected as its
first character.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Return models from fetchNamedPage

The raw API payload was returned rather than the models added to the store,
so callers that render the result are missing computed properties.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor: Render the document menu from the store

Matches the mention menu and paginated lists, where the request populates the
store and the list is rendered from observables.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat: Order people above documents in the mention menu

Uses a mention specific section so the command bar, where the section holds
user management actions, is unaffected.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Hide create document options without a create handler

Editors that do not pass onCreateLink, such as comments, inserted a mention
pointing at a document that was never created.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 07:57:13 -04:00

212 lines
6.7 KiB
TypeScript

import { action } from "mobx";
import type { EditorState } from "prosemirror-state";
import { Plugin } from "prosemirror-state";
import type { EditorView } from "prosemirror-view";
import { getMarksBetween } from "@shared/editor/queries/getMarksBetween";
const MAX_MATCH = 500;
type ExtensionState = {
open: boolean;
query: string;
};
/**
* Determine whether the trigger character of a suggestion match carries any
* marks (e.g. bold, code, link).
*
* @param state The editor state.
* @param cursorPos The document position of the cursor (end of the match).
* @param match The regex match where group 1 is the search term.
* @param triggerLength The length of the trigger in characters.
* @returns True if the trigger character has one or more marks applied.
*/
export function isTriggerMarked(
state: EditorState,
cursorPos: number,
match: RegExpMatchArray,
triggerLength: number
): boolean {
const queryLength = match[1]?.length ?? 0;
const triggerEnd = cursorPos - queryLength;
const triggerStart = triggerEnd - triggerLength;
if (triggerStart < 0) {
return false;
}
return getMarksBetween(triggerStart, triggerEnd, state).length > 0;
}
export class SuggestionsMenuPlugin extends Plugin {
constructor(
extensionState: ExtensionState,
openRegex: RegExp,
enabledInMarks: boolean,
triggerLength: number
) {
// The menu only opens on a freshly typed trigger, that is a match made up
// of the optional preceding character and the trigger itself.
const maxOpenLength = triggerLength + 1;
super({
props: {
handleDOMEvents: {
// IME composition (e.g. Korean, Japanese, Chinese) fires compositionupdate
// as each character is being built up. ProseMirror's view.composing flag
// blocks the normal handleKeyDown path, so we handle it separately here.
compositionupdate: (view) => {
setTimeout(() => {
const { pos: fromPos } = view.state.selection.$from;
const state = view.state;
const $from = state.doc.resolve(fromPos);
if ($from.parent.type.spec.code) {
return;
}
const textBefore = $from.parent.textBetween(
Math.max(0, $from.parentOffset - MAX_MATCH),
$from.parentOffset,
undefined,
"\ufffc"
);
const match = openRegex.exec(textBefore);
action(() => {
if (
match &&
(enabledInMarks ||
!isTriggerMarked(state, fromPos, match, triggerLength))
) {
if (match[0].length <= maxOpenLength) {
extensionState.open = true;
}
extensionState.query = match[1];
}
})();
}, 0);
return false;
},
},
handleKeyDown: (view, event) => {
// Prosemirror input rules are not triggered on backspace, however
// we need them to be evaluated for the filter trigger to work
// correctly. This additional handler adds inputrules-like handling.
if (event.key === "Backspace") {
// timeout ensures that the delete has been handled by prosemirror
// and any characters removed, before we evaluate the rule.
setTimeout(() => {
const { pos: fromPos } = view.state.selection.$from;
this.execute(
view,
fromPos,
fromPos,
openRegex,
action((state, match) => {
if (
match &&
(enabledInMarks ||
!isTriggerMarked(state, fromPos, match, triggerLength))
) {
extensionState.query = match[1];
} else {
extensionState.open = false;
}
return null;
})
);
}, 0);
}
// Another plugin (e.g. the Placeholder mark) may consume the
// handleTextInput event by returning true, which prevents the
// InputRule from evaluating the trigger character. We use a timeout
// here so the re-evaluation happens after all synchronous handlers
// have run, ensuring the suggestion menu still opens in those cases.
if (
!event.ctrlKey &&
!event.metaKey &&
!event.altKey &&
event.key.length === 1
) {
setTimeout(() => {
const { pos: fromPos } = view.state.selection.$from;
this.execute(
view,
fromPos,
fromPos,
openRegex,
action((state, match) => {
if (
match &&
(enabledInMarks ||
!isTriggerMarked(state, fromPos, match, triggerLength))
) {
if (match[0].length <= maxOpenLength) {
extensionState.open = true;
}
extensionState.query = match[1];
}
return null;
})
);
}, 0);
}
// If the menu is open then just ignore the key events in the editor
// itself until we're done.
if (
event.key === "Enter" ||
event.key === "ArrowUp" ||
event.key === "ArrowDown" ||
event.key === "Tab"
) {
return extensionState.open;
}
return false;
},
},
});
}
// based on the input rules code in Prosemirror, here:
// https://github.com/ProseMirror/prosemirror-inputrules/blob/master/src/inputrules.js
private execute(
view: EditorView,
from: number,
to: number,
regex: RegExp,
handler: (
state: EditorState,
match: RegExpExecArray | null,
from?: number,
to?: number
) => boolean | null
) {
if (view.composing) {
return false;
}
const state = view.state;
const $from = state.doc.resolve(from);
if ($from.parent.type.spec.code) {
return false;
}
const textBefore = $from.parent.textBetween(
Math.max(0, $from.parentOffset - MAX_MATCH),
$from.parentOffset,
undefined,
"\ufffc"
);
const match = regex.exec(textBefore);
const result = handler(
state,
match,
match ? from - match[0].length : from,
to
);
if (!result) {
return false;
}
return true;
}
}