2022-10-10 12:50:21 +00:00
|
|
|
import { FilterList } from "./filter.tsx";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { FilterOption } from "../types.ts";
|
2022-12-21 13:55:24 +00:00
|
|
|
import { CompletionContext, CompletionResult } from "../deps.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { PageMeta } from "$sb/types.ts";
|
2023-11-12 09:43:08 +00:00
|
|
|
import { isFederationPath } from "$sb/lib/resolve.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2023-12-21 18:49:25 +00:00
|
|
|
const tagRegex = /#[^#\d\s\[\]]+\w+/g;
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
export function PageNavigator({
|
|
|
|
allPages,
|
|
|
|
onNavigate,
|
2022-12-21 13:55:24 +00:00
|
|
|
completer,
|
|
|
|
vimMode,
|
|
|
|
darkMode,
|
2022-03-20 08:56:28 +00:00
|
|
|
currentPage,
|
|
|
|
}: {
|
2023-05-23 18:53:53 +00:00
|
|
|
allPages: PageMeta[];
|
2022-12-21 13:55:24 +00:00
|
|
|
vimMode: boolean;
|
|
|
|
darkMode: boolean;
|
2022-03-20 08:56:28 +00:00
|
|
|
onNavigate: (page: string | undefined) => void;
|
2022-12-21 13:55:24 +00:00
|
|
|
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
|
2022-03-20 08:56:28 +00:00
|
|
|
currentPage?: string;
|
|
|
|
}) {
|
2022-10-10 12:50:21 +00:00
|
|
|
const options: FilterOption[] = [];
|
|
|
|
for (const pageMeta of allPages) {
|
2023-12-22 10:27:07 +00:00
|
|
|
// Sanitize the page name
|
|
|
|
if (!pageMeta.name) {
|
|
|
|
pageMeta.name = pageMeta.ref;
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
// Order by last modified date in descending order
|
2023-11-12 09:43:08 +00:00
|
|
|
let orderId = -new Date(pageMeta.lastModified).getTime();
|
2022-04-10 09:04:07 +00:00
|
|
|
// Unless it was opened in this session
|
2022-03-20 08:56:28 +00:00
|
|
|
if (pageMeta.lastOpened) {
|
|
|
|
orderId = -pageMeta.lastOpened;
|
|
|
|
}
|
2022-08-01 13:06:32 +00:00
|
|
|
// Or it's the currently open page
|
|
|
|
if (currentPage && currentPage === pageMeta.name) {
|
|
|
|
// ... then we put it all the way to the end
|
|
|
|
orderId = Infinity;
|
|
|
|
}
|
2023-11-12 09:43:08 +00:00
|
|
|
// And deprioritize federated pages too
|
|
|
|
if (isFederationPath(pageMeta.name)) {
|
|
|
|
orderId = Math.round(orderId / 10); // Just 10x lower the timestamp to push them down, should work
|
|
|
|
}
|
2023-12-21 17:37:50 +00:00
|
|
|
let description: string | undefined;
|
|
|
|
let aliases: string[] = [];
|
|
|
|
if (pageMeta.displayName) {
|
|
|
|
aliases.push(pageMeta.displayName);
|
|
|
|
}
|
|
|
|
if (Array.isArray(pageMeta.aliases)) {
|
|
|
|
aliases = aliases.concat(pageMeta.aliases);
|
|
|
|
}
|
|
|
|
if (aliases.length > 0) {
|
|
|
|
description = "(a.k.a. " + aliases.join(", ") + ") ";
|
|
|
|
}
|
|
|
|
if (pageMeta.tags.length > 1) {
|
|
|
|
// Every page has the "page" tag, so it only gets interesting beyond that
|
|
|
|
const interestingTags = pageMeta.tags.filter((tag) => tag !== "page");
|
|
|
|
description = (description || "") +
|
|
|
|
interestingTags.map((tag) => `#${tag}`).join(" ");
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
options.push({
|
|
|
|
...pageMeta,
|
2023-12-21 17:37:50 +00:00
|
|
|
description,
|
2022-03-20 08:56:28 +00:00
|
|
|
orderId: orderId,
|
|
|
|
});
|
|
|
|
}
|
2023-11-10 09:57:57 +00:00
|
|
|
let completePrefix = currentPage + "/";
|
2022-04-01 13:02:35 +00:00
|
|
|
if (currentPage && currentPage.includes("/")) {
|
|
|
|
const pieces = currentPage.split("/");
|
|
|
|
completePrefix = pieces.slice(0, pieces.length - 1).join("/") + "/";
|
2022-07-04 09:38:16 +00:00
|
|
|
} else if (currentPage && currentPage.includes(" ")) {
|
|
|
|
completePrefix = currentPage.split(" ")[0] + " ";
|
2022-04-01 13:02:35 +00:00
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
return (
|
|
|
|
<FilterList
|
|
|
|
placeholder="Page"
|
|
|
|
label="Open"
|
|
|
|
options={options}
|
2022-12-21 13:55:24 +00:00
|
|
|
vimMode={vimMode}
|
|
|
|
darkMode={darkMode}
|
|
|
|
completer={completer}
|
2023-12-21 18:49:25 +00:00
|
|
|
phrasePreprocessor={(phrase) => {
|
|
|
|
phrase = phrase.replaceAll(tagRegex, "").trim();
|
|
|
|
return phrase;
|
|
|
|
}}
|
|
|
|
preFilter={(options, phrase) => {
|
|
|
|
const allTags = phrase.match(tagRegex);
|
|
|
|
if (allTags) {
|
|
|
|
// Search phrase contains hash tags, let's pre-filter the results based on this
|
|
|
|
const filterTags = allTags.map((t) => t.slice(1));
|
|
|
|
options = options.filter((pageMeta) => {
|
|
|
|
if (!pageMeta.tags) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return filterTags.every((tag) =>
|
|
|
|
pageMeta.tags.find((itemTag: string) => itemTag.startsWith(tag))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}}
|
2022-03-20 08:56:28 +00:00
|
|
|
allowNew={true}
|
2023-11-15 13:56:34 +00:00
|
|
|
helpText="Press <code>Enter</code> to open the selected page, or <code>Shift-Enter</code> to create a new page with this exact name."
|
2022-03-20 08:56:28 +00:00
|
|
|
newHint="Create page"
|
2022-04-01 13:02:35 +00:00
|
|
|
completePrefix={completePrefix}
|
2022-03-20 08:56:28 +00:00
|
|
|
onSelect={(opt) => {
|
|
|
|
onNavigate(opt?.name);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|