Deploy deepseek 2
This commit is contained in:
109
frontend/app/services/LinkAutocomplete.js
Normal file
109
frontend/app/services/LinkAutocomplete.js
Normal file
@@ -0,0 +1,109 @@
|
||||
// The rules behind the note editor's wiki-link autocomplete (type `[[` while editing a note and the
|
||||
// campaign's notes, actors, items and maps are offered to link to). These helpers hold no state,
|
||||
// touch no DOM and import nothing, so tests can load them straight from source. Marker.js renders
|
||||
// `[[target]]` as a link, so the target written here is the entry's label.
|
||||
|
||||
// Accent- and case-insensitive, the way the search palette compares names.
|
||||
function Normalize(text) {
|
||||
return String(text ?? '')
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.toLowerCase()
|
||||
.trim();
|
||||
}
|
||||
|
||||
// The `[[target` the caret is sitting in. `[[The Wr` answers `{ start, query: 'The Wr' }`.
|
||||
//
|
||||
// A link is a single line: a `]]` or a newline between the `[[` and the caret closes the token, so
|
||||
// the menu only follows the link currently being typed. Returns null when there is none.
|
||||
function FindLinkTrigger(text, caret) {
|
||||
if (typeof text !== 'string') return null;
|
||||
const position = Math.max(0, Math.min(Number(caret) || 0, text.length));
|
||||
|
||||
const before = text.slice(0, position);
|
||||
const start = before.lastIndexOf('[[');
|
||||
if (start === -1) return null;
|
||||
|
||||
const query = before.slice(start + 2);
|
||||
if (query.includes(']]') || query.includes('\n')) return null;
|
||||
|
||||
return { start, query };
|
||||
}
|
||||
|
||||
// One `{ id, label, type }` per linkable entry, from the campaign's four lists. Entries without a
|
||||
// name are left out (they would show as an empty menu row), the rest are sorted by label so the menu
|
||||
// reads alphabetically whatever order the API answered with.
|
||||
function BuildLinkSuggestions({ notes, actors, items, maps } = {}) {
|
||||
const suggestions = [];
|
||||
const seen = new Set();
|
||||
|
||||
const push = (type, list, labelOf) => {
|
||||
for (const entry of list ?? []) {
|
||||
const label = String(labelOf(entry) ?? '').trim();
|
||||
if (!entry?._id || !label) continue;
|
||||
const key = `${type}:${label}`;
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
suggestions.push({ id: String(entry._id), label, type });
|
||||
}
|
||||
};
|
||||
|
||||
push('note', notes, (note) => note.title);
|
||||
push('actor', actors, (actor) => actor.name);
|
||||
push('item', items, (item) => item.name);
|
||||
push('map', maps, (map) => map.name);
|
||||
|
||||
return suggestions.sort((a, b) => a.label.localeCompare(b.label));
|
||||
}
|
||||
|
||||
// The suggestions matching what has been typed. An empty query offers them all; the ones whose label
|
||||
// starts with it come first, and a match further inside still shows, the way search does it. The list
|
||||
// is capped so a large campaign cannot open a menu taller than the note.
|
||||
const MAX_SUGGESTIONS = 50;
|
||||
|
||||
function FilterLinkSuggestions(suggestions, query) {
|
||||
const needle = Normalize(query);
|
||||
const all = suggestions ?? [];
|
||||
if (!needle) return all.slice(0, MAX_SUGGESTIONS);
|
||||
|
||||
return all
|
||||
.filter((suggestion) => Normalize(suggestion.label).includes(needle))
|
||||
.sort((a, b) => {
|
||||
const startsA = Normalize(a.label).startsWith(needle) ? 0 : 1;
|
||||
const startsB = Normalize(b.label).startsWith(needle) ? 0 : 1;
|
||||
return startsA - startsB || a.label.localeCompare(b.label);
|
||||
})
|
||||
.slice(0, MAX_SUGGESTIONS);
|
||||
}
|
||||
|
||||
// The entry a written `[[label]]` points at. Links store the label (not the id), so the note looks
|
||||
// the target up again when it is rendered; the comparison is the same case- and accent-insensitive
|
||||
// one the menu uses. Returns null when nothing in the campaign carries that label any more.
|
||||
function FindLinkTarget(suggestions, label) {
|
||||
const needle = Normalize(label);
|
||||
if (!needle) return null;
|
||||
|
||||
return (suggestions ?? []).find((suggestion) => Normalize(suggestion.label) === needle) ?? null;
|
||||
}
|
||||
|
||||
// Replaces the typed `[[query` with `[[label]]` and returns where the caret goes: just after the
|
||||
// closing brackets, ready to carry on with the note.
|
||||
function InsertLinkSuggestion(text, trigger, suggestion) {
|
||||
const source = String(text ?? '');
|
||||
const start = trigger?.start ?? 0;
|
||||
const query = trigger?.query ?? '';
|
||||
|
||||
const before = source.slice(0, start);
|
||||
const after = source.slice(start + 2 + query.length);
|
||||
const snippet = `[[${suggestion?.label ?? ''}]]`;
|
||||
|
||||
return { text: before + snippet + after, caret: start + snippet.length };
|
||||
}
|
||||
|
||||
export {
|
||||
BuildLinkSuggestions,
|
||||
FilterLinkSuggestions,
|
||||
FindLinkTarget,
|
||||
FindLinkTrigger,
|
||||
InsertLinkSuggestion
|
||||
};
|
||||
71
frontend/app/services/LinkTargets.js
Normal file
71
frontend/app/services/LinkTargets.js
Normal file
@@ -0,0 +1,71 @@
|
||||
// Fetches the campaign entries a note can link to (`[[…]]`) and keeps them briefly, so the several
|
||||
// notes a campaign has open share one round of requests. The rules that turn the entries into
|
||||
// suggestions live in LinkAutocomplete.js; this only talks to the API.
|
||||
import Server from './Server';
|
||||
import { BuildLinkSuggestions } from './LinkAutocomplete';
|
||||
|
||||
// Long enough to spare the API while a note is being written, short enough that an entry created in
|
||||
// another note shows up soon after.
|
||||
const CACHE_TTL = 30 * 1000;
|
||||
const cache = new Map(); // campaignId → { at, promise }
|
||||
|
||||
// The campaign's four lists as the API answers them. The note preview needs the entry itself (a
|
||||
// character sheet, an item), not only the label the suggestion carries, so the lists are kept as
|
||||
// they arrive and both readers resolve from the same round of requests.
|
||||
const LIST_PATHS = {
|
||||
note: { path: '/note/all', key: 'notes' },
|
||||
actor: { path: '/actor/list', key: 'actors' },
|
||||
item: { path: '/item/list', key: 'items' },
|
||||
map: { path: '/map/list', key: 'maps' }
|
||||
};
|
||||
|
||||
async function fetchList(campaignId, path, key) {
|
||||
try {
|
||||
const response = await Server().get(path, { params: { campaign: campaignId } });
|
||||
if (response.data.status !== 'ok') return [];
|
||||
return response.data[key] ?? [];
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function LoadCampaignEntries(campaignId) {
|
||||
const id = campaignId ? String(campaignId) : '';
|
||||
if (!id) {
|
||||
return Promise.resolve({ notes: [], actors: [], items: [], maps: [] });
|
||||
}
|
||||
|
||||
const cached = cache.get(id);
|
||||
if (cached && Date.now() - cached.at < CACHE_TTL) return cached.promise;
|
||||
|
||||
const promise = Promise.all(
|
||||
Object.values(LIST_PATHS).map(({ path, key }) => fetchList(id, path, key))
|
||||
).then(([notes, actors, items, maps]) => ({ notes, actors, items, maps }));
|
||||
|
||||
cache.set(id, { at: Date.now(), promise });
|
||||
return promise;
|
||||
}
|
||||
|
||||
// The campaign's linkable entries, as BuildLinkSuggestions answers. Without a campaign id there is
|
||||
// nothing to offer.
|
||||
function LoadLinkTargets(campaignId) {
|
||||
return LoadCampaignEntries(campaignId).then((lists) => BuildLinkSuggestions(lists));
|
||||
}
|
||||
|
||||
// The entry a link points at, for the preview the note shows on hover (and to open its sheet). The
|
||||
// target is one of the suggestions `LoadLinkTargets` answered; the lists it lives in are already
|
||||
// cached, so this does not make another request.
|
||||
function LoadLinkEntry(campaignId, type, id) {
|
||||
const list = LIST_PATHS[type];
|
||||
if (!list) return Promise.resolve(null);
|
||||
|
||||
return LoadCampaignEntries(campaignId).then((lists) => {
|
||||
return (lists[list.key] ?? []).find((entry) => String(entry._id) === String(id)) ?? null;
|
||||
});
|
||||
}
|
||||
|
||||
function ClearLinkTargets() {
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
export { LoadLinkTargets, LoadLinkEntry, ClearLinkTargets };
|
||||
@@ -88,6 +88,32 @@ const inlineExtension = {
|
||||
},
|
||||
};
|
||||
|
||||
// `![[Name]]` embeds the entry itself instead of linking it: the note mounts a card with the
|
||||
// character's sheet or the item's description where the token was written.
|
||||
const embedExtension = {
|
||||
name: "embed",
|
||||
level: "inline",
|
||||
start(src) {
|
||||
return src.indexOf("![[");
|
||||
},
|
||||
|
||||
tokenizer(src) {
|
||||
const rule = /^!\[\[([^\n]*)\]\]/;
|
||||
const match = rule.exec(src);
|
||||
if (!match) return;
|
||||
|
||||
return {
|
||||
type: "embed",
|
||||
raw: match[0],
|
||||
link: match[1],
|
||||
};
|
||||
},
|
||||
|
||||
renderer(token) {
|
||||
return `<span class="vue-embed" data-href="${escapeAttr(token.link)}"></span>`;
|
||||
},
|
||||
};
|
||||
|
||||
const linkExtension = {
|
||||
name: "link_to",
|
||||
level: "inline",
|
||||
@@ -108,16 +134,32 @@ const linkExtension = {
|
||||
},
|
||||
|
||||
renderer(token) {
|
||||
return `<span class="vue-link" data-href="${escapeAttr(token.link)}">Link</span>`;
|
||||
// The link shows the entry's name, not a generic "Link", so a note reads as written. The
|
||||
// note looks the target up by this label and turns the span into a preview link.
|
||||
const label = String(token.link ?? "").trim() || token.link;
|
||||
return `<span class="vue-link" data-href="${escapeAttr(token.link)}" role="link" tabindex="0">${escapeAttr(label)}</span>`;
|
||||
},
|
||||
};
|
||||
|
||||
marker.use({
|
||||
extensions: [extension, inlineExtension, linkExtension],
|
||||
// The embed comes before the link: at the `!` of `![[…]]` the embed tokenizer is offered first,
|
||||
// so the same label written with the bang does not fall through to the plain link.
|
||||
extensions: [extension, inlineExtension, embedExtension, linkExtension],
|
||||
});
|
||||
|
||||
function ParseMarkdown(source) {
|
||||
return marker.parse(source || "");
|
||||
}
|
||||
|
||||
export { ParseMarkdown, GetWidget };
|
||||
// The widgets a note can use, as `{ inline: [...names], display: [...names] }`. The note editor's
|
||||
// `@` autocomplete reads this, so a widget added to `widget_map` is offered there without any other
|
||||
// change.
|
||||
const GetWidgetRegistry = () => {
|
||||
const registry = {};
|
||||
for (const type of Object.keys(widget_map)) {
|
||||
registry[type] = Object.keys(widget_map[type]);
|
||||
}
|
||||
return registry;
|
||||
};
|
||||
|
||||
export { ParseMarkdown, GetWidget, GetWidgetRegistry };
|
||||
78
frontend/app/services/WidgetAutocomplete.js
Normal file
78
frontend/app/services/WidgetAutocomplete.js
Normal file
@@ -0,0 +1,78 @@
|
||||
// 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
|
||||
};
|
||||
@@ -82,6 +82,12 @@ const defWindows = {
|
||||
close: () => ClearWindow({type: 'character_sheet'}),
|
||||
movable: true
|
||||
},
|
||||
trait_sheet: {
|
||||
title: 'windows.trait-sheet',
|
||||
component: () => import('~/components/windows/TraitWindow.vue'),
|
||||
close: () => ClearWindow({ type: 'trait_sheet' }),
|
||||
movable: true
|
||||
},
|
||||
item_sheet: {
|
||||
title: 'windows.item-sheet',
|
||||
component: () => import('~/components/windows/ItemSheetWindow.vue'),
|
||||
|
||||
Reference in New Issue
Block a user