Skip to content

Commit

Permalink
Hide pages from auto complete with Page Decorations (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarrk authored Jul 19, 2024
1 parent 5fe7708 commit 75cd4b1
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 20 deletions.
2 changes: 1 addition & 1 deletion plug-api/syscalls/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function navigate(
}

export function openPageNavigator(
mode: "page" | "meta" = "page",
mode: "page" | "meta" | "all" = "page",
): Promise<void> {
return syscall("editor.openPageNavigator", mode);
}
Expand Down
1 change: 1 addition & 0 deletions plug-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type PageDecoration = {
where?: string;
whereParsed?: QueryExpression;
prefix: string;
hide?: boolean;
};

export type AttachmentMeta = ObjectValue<
Expand Down
3 changes: 3 additions & 0 deletions plugs/editor/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export async function pageComplete(completeEvent: CompleteEvent) {
);
}

// Don't complete hidden pages
allPages = allPages.filter((page) => !(page.pageDecoration?.hide === true));

if (prefix.startsWith("!")) {
// Federation!
// Let's see if this URI is complete enough to try to fetch index.json
Expand Down
7 changes: 5 additions & 2 deletions plugs/editor/editor.plug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ functions:
key: "Ctrl-Shift-k"
mac: "Cmd-Shift-k"

openAllNavigator:
path: editor.ts:openAllNavigator
command:
name: "Navigate: All Pages Picker"

# Page operations
deletePage:
path: "./page.ts:deletePage"
Expand Down Expand Up @@ -201,7 +206,6 @@ functions:
events:
- unfurl:title-unfurl


# Title-based link unfurl
youtubeUnfurlOptions:
path: ./link.ts:youtubeUnfurlOptions
Expand All @@ -213,7 +217,6 @@ functions:
events:
- unfurl:youtube-unfurl


embedWidget:
path: ./embed.ts:embedWidget
codeWidget: embed
Expand Down
4 changes: 4 additions & 0 deletions plugs/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export async function openMetaNavigator() {
await editor.openPageNavigator("meta");
}

export async function openAllNavigator() {
await editor.openPageNavigator("all");
}

export async function toggleDarkMode() {
let darkMode = await clientStore.get("darkMode");
darkMode = !darkMode;
Expand Down
4 changes: 2 additions & 2 deletions type/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type AppViewState = {
};

// Page navigator mode
pageNavigatorMode: "page" | "meta";
pageNavigatorMode: "page" | "meta" | "all";

// Filter box
showFilterBox: boolean;
Expand Down Expand Up @@ -127,7 +127,7 @@ export type Action =
| { type: "update-current-page-meta"; meta: PageMeta }
| { type: "update-page-list"; allPages: PageMeta[] }
| { type: "settings-loaded"; settings: BuiltinSettings }
| { type: "start-navigate"; mode: "page" | "meta" }
| { type: "start-navigate"; mode: "page" | "meta" | "all" }
| { type: "stop-navigate" }
| {
type: "update-commands";
Expand Down
2 changes: 1 addition & 1 deletion web/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ export class Client {
);
}

startPageNavigate(mode: "page" | "meta") {
startPageNavigate(mode: "page" | "meta" | "all") {
// Then show the page navigator
this.ui.viewDispatch({ type: "start-navigate", mode });
this.updatePageListCache().catch(console.error);
Expand Down
52 changes: 41 additions & 11 deletions web/components/page_navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export function PageNavigator({
allPages: PageMeta[];
vimMode: boolean;
darkMode: boolean;
mode: "page" | "meta";
mode: "page" | "meta" | "all";
onNavigate: (page: string | undefined) => void;
onModeSwitch: (mode: "page" | "meta") => void;
onModeSwitch: (mode: "page" | "meta" | "all") => void;
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
currentPage?: string;
}) {
Expand Down Expand Up @@ -66,7 +66,7 @@ export function PageNavigator({
description,
orderId: orderId,
});
} else {
} else if (mode === "meta") {
// Special behavior for #template and #meta pages
options.push({
...pageMeta,
Expand All @@ -77,6 +77,19 @@ export function PageNavigator({
hint: pageMeta.tags![0],
orderId: orderId,
});
} else {
// In mode "all" just show the full path and all tags
let description: string | undefined;
if (pageMeta.tags) {
description = (description || "") +
pageMeta.tags.map((tag) => `#${tag}`).join(" ");
}
options.push({
...pageMeta,
name: pageMeta.name,
description,
orderId: orderId,
});
}
}
let completePrefix = currentPage + "/";
Expand All @@ -86,9 +99,11 @@ export function PageNavigator({
} else if (currentPage && currentPage.includes(" ")) {
completePrefix = currentPage.split(" ")[0] + " ";
}

const pageNoun = mode === "meta" ? mode : "page";
return (
<FilterList
placeholder={mode === "page" ? "Page" : "#template or #meta page"}
placeholder={mode === "page" ? "Page" : (mode === "meta" ? "#template or #meta page" : "Any page, also hidden")}
label="Open"
options={options}
vimMode={vimMode}
Expand All @@ -99,8 +114,19 @@ export function PageNavigator({
return phrase;
}}
onKeyPress={(key, text) => {
if (mode === "page" && key === "^" && text === "^") {
onModeSwitch("meta");
// Pages cannot start with ^, as documented in Page Name Rules
if (key === "^" && text === "^") {
switch(mode) {
case "page":
onModeSwitch("meta");
break;
case "meta":
onModeSwitch("all");
break;
case "all":
onModeSwitch("page");
break;
}
}
}}
preFilter={(options, phrase) => {
Expand All @@ -123,19 +149,23 @@ export function PageNavigator({
return !pageMeta.tags?.includes("template") &&
!pageMeta.tags?.includes("meta");
});
return options;
} else {
} else if (mode === "meta") {
// Filter on pages tagged with "template" or "meta"
options = options.filter((pageMeta) => {
return pageMeta.tags?.includes("template") ||
pageMeta.tags?.includes("meta");
});
return options;
}

if (mode !== "all") {
// Filter out hidden pages
options = options.filter((page) => !(page.pageDecoration?.hide === true));
}
return options;
}}
allowNew={true}
helpText={`Press <code>Enter</code> to open the selected ${mode}, or <code>Shift-Enter</code> to create a new ${mode} with this exact name.`}
newHint={`Create ${mode}`}
helpText={`Press <code>Enter</code> to open the selected ${pageNoun}, or <code>Shift-Enter</code> to create a new ${pageNoun} with this exact name.`}
newHint={`Create ${pageNoun}`}
completePrefix={completePrefix}
onSelect={(opt) => {
onNavigate(opt?.ref || opt?.name);
Expand Down
2 changes: 1 addition & 1 deletion web/syscalls/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export function editorSyscalls(client: Client): SysCallMapping {
const cm = vimGetCm(client.editorView)!;
return Vim.handleEx(cm, exCommand);
},
"editor.openPageNavigator": (_ctx, mode: "page" | "meta" = "page") => {
"editor.openPageNavigator": (_ctx, mode: "page" | "meta" | "all" = "page") => {
client.startPageNavigate(mode);
},
"editor.openCommandPalette": () => {
Expand Down
3 changes: 3 additions & 0 deletions website/All Pages Picker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The all pages picker can be used to open any page, including [[Meta Pages]] and pages hidden with [[Page Decorations]]. Otherwise it’s equivalent to the [[Page Picker]].

To open, run the command {[Navigate: All Pages Picker]} using this button or [[Command Palette]]. Alternatively cycle from any other Picker by typing `^`.
4 changes: 2 additions & 2 deletions website/Page Decorations.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ This will prefix all pages tagged with `#person` with a 🧑 emoji.
Here on silverbullet.md, we have a decoration like this for pages tagged with #plug: [[Plugs/Emoji]] and [[Plugs/Git]] for instance.

# Supported decorations
For now there’s just one:

* `prefix`: A (visual) string prefix (often an emoji) to add to all page names. This prefix will appear in the top bar as well as in (live preview) links to this page. For example, the name of this page is actually “Page Decorations”, but when you link to it, you’ll see it’s prefixed with a 🎄: [[Page Decorations]]
* `hide` When this is set to `true`, the page will not be shown in [[Page Picker]], [[Meta Picker]], or suggested for completion of [[Links]]. It will otherwise behave as normal - will be [[Plugs/Index|indexed]] and found in [[Live Queries]]. The page can be opened through [[All Pages Picker]], or linked normally when the full name is typed out without completion.

Again — later, more such decorations may be added.
Later, more such decorations may be added.
1 change: 1 addition & 0 deletions website/Page Picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ When entering a filter phrase, the best matches should appear closer to the top,
* `Home`: moves to the start of the list
* `End`: moves to the end of the list
* `Escape`: closes the page picker UI
* Typing `^` when filter phrase is empty will cycle to the next picker, first [[Meta Picker]], then [[All Pages Picker]]

# Mouse/touch operation
You can obviously scroll and select an item from the list by clicking with the mouse, as well as close the page picker by clicking outside of it.
Expand Down

0 comments on commit 75cd4b1

Please sign in to comment.