79 lines
3.1 KiB
JavaScript
79 lines
3.1 KiB
JavaScript
// The rules behind the note editor's widget autocomplete (type `@` while editing a note and the
|
|
// widgets of Marker.js are offered to insert). These helpers hold no state, touch no DOM and import
|
|
// nothing, so tests can load them straight from source.
|
|
|
|
// What counts as a widget name while the user is typing one, matching the `\w+` Marker.js reads.
|
|
const NAME_CHARACTER = /[A-Za-z0-9_]/;
|
|
|
|
// The `@name` the caret is sitting in. Typing `@ro` answers `{ start, query: 'ro' }`, ready to be
|
|
// replaced by the chosen widget.
|
|
//
|
|
// The `@` has to open a word (start of the note, whitespace or punctuation before it) so an email
|
|
// address does not pop the menu open. Returns null when the caret is not in such a token.
|
|
function FindWidgetTrigger(text, caret) {
|
|
if (typeof text !== 'string') return null;
|
|
const position = Math.max(0, Math.min(Number(caret) || 0, text.length));
|
|
|
|
let start = position;
|
|
while (start > 0 && NAME_CHARACTER.test(text[start - 1])) start--;
|
|
|
|
const at = start - 1;
|
|
if (at < 0 || text[at] !== '@') return null;
|
|
|
|
const before = at > 0 ? text[at - 1] : '';
|
|
if (before && NAME_CHARACTER.test(before)) return null;
|
|
|
|
return { start: at, query: text.slice(start, position) };
|
|
}
|
|
|
|
// The widgets to offer, one entry per way to write each one (`type` is what Marker.js renders:
|
|
// `inline` for `@roll[...]`, `display` for the `@roll … @end` block). `registry` is the map of
|
|
// widget names per type that GetWidgetRegistry() answers with, so a widget is described once, in
|
|
// Marker.js, and shows up here on its own.
|
|
function BuildWidgetSuggestions(registry) {
|
|
const suggestions = [];
|
|
for (const type of ['inline', 'display']) {
|
|
for (const name of registry?.[type] ?? []) {
|
|
suggestions.push({ name: String(name), type });
|
|
}
|
|
}
|
|
return suggestions;
|
|
}
|
|
|
|
// The suggestions whose name starts with what has been typed. An empty query offers them all.
|
|
function FilterWidgetSuggestions(suggestions, query) {
|
|
const needle = String(query ?? '').toLowerCase();
|
|
const all = suggestions ?? [];
|
|
if (!needle) return all.slice();
|
|
return all.filter((suggestion) => suggestion.name.toLowerCase().startsWith(needle));
|
|
}
|
|
|
|
// Replaces the typed `@query` with the widget's syntax and says where the caret goes afterwards:
|
|
// inside the brackets for an inline widget (`@roll[]`), on the blank line of a block (`@roll\n\n@end`).
|
|
function InsertWidgetSuggestion(text, trigger, suggestion) {
|
|
const source = String(text ?? '');
|
|
const start = trigger?.start ?? 0;
|
|
const query = trigger?.query ?? '';
|
|
const name = suggestion?.name ?? '';
|
|
|
|
const before = source.slice(0, start);
|
|
const after = source.slice(start + 1 + query.length);
|
|
|
|
const snippet = suggestion?.type === 'display'
|
|
? `@${name}\n\n@end`
|
|
: `@${name}[]`;
|
|
|
|
return {
|
|
text: before + snippet + after,
|
|
// 1 for the `@` and 1 for either the opening bracket or the blank line.
|
|
caret: start + name.length + 2
|
|
};
|
|
}
|
|
|
|
export {
|
|
BuildWidgetSuggestions,
|
|
FilterWidgetSuggestions,
|
|
FindWidgetTrigger,
|
|
InsertWidgetSuggestion
|
|
};
|