More instant page navigator

This commit is contained in:
Zef Hemel
2023-12-22 15:55:50 +01:00
parent 8a181145ad
commit 3350c7f076
11 changed files with 79 additions and 60 deletions
+9 -10
View File
@@ -13,7 +13,7 @@ import { resolvePath } from "$sb/lib/resolve.ts";
/**
* Plugin to hide path prefix when the cursor is not inside.
*/
export function cleanWikiLinkPlugin(editor: Client) {
export function cleanWikiLinkPlugin(client: Client) {
return decoratorStateField((state) => {
const widgets: any[] = [];
// let parentRange: [number, number];
@@ -28,21 +28,20 @@ export function cleanWikiLinkPlugin(editor: Client) {
if (!match) return;
const [_fullMatch, page, pipePart, alias] = match;
const allPages = editor.space.listPages();
let pageExists = !editor.fullSyncCompleted;
let pageExists = !client.fullSyncCompleted;
let cleanPage = page;
cleanPage = page.split(/[@$]/)[0];
cleanPage = resolvePath(editor.currentPage!, cleanPage);
cleanPage = resolvePath(client.currentPage!, cleanPage);
const lowerCasePageName = cleanPage.toLowerCase();
for (const pageMeta of allPages) {
if (pageMeta.name.toLowerCase() === lowerCasePageName) {
for (const pageName of client.allKnownPages) {
if (pageName.toLowerCase() === lowerCasePageName) {
pageExists = true;
break;
}
}
if (
cleanPage === "" ||
editor.plugSpaceRemotePrimitives.isLikelyHandled(cleanPage)
client.plugSpaceRemotePrimitives.isLikelyHandled(cleanPage)
) {
// Empty page name with local @anchor use or a link to a page that dynamically generated by a plug
pageExists = true;
@@ -90,19 +89,19 @@ export function cleanWikiLinkPlugin(editor: Client) {
callback: (e) => {
if (e.altKey) {
// Move cursor into the link
return editor.editorView.dispatch({
return client.editorView.dispatch({
selection: { anchor: from + 2 },
});
}
// Dispatch click event to navigate there without moving the cursor
const clickEvent: ClickEvent = {
page: editor.currentPage!,
page: client.currentPage!,
ctrlKey: e.ctrlKey,
metaKey: e.metaKey,
altKey: e.altKey,
pos: from,
};
editor.dispatchAppEvent("page:click", clickEvent).catch(
client.dispatchAppEvent("page:click", clickEvent).catch(
console.error,
);
},