Work on #587: revamped templates
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { CompleteEvent } from "$sb/app_event.ts";
|
||||
import { space } from "$sb/syscalls.ts";
|
||||
import { FileMeta, PageMeta } from "$sb/types.ts";
|
||||
import { cacheFileListing } from "../federation/federation.ts";
|
||||
import { queryObjects } from "../index/plug_api.ts";
|
||||
|
||||
// Completion
|
||||
export async function pageComplete(completeEvent: CompleteEvent) {
|
||||
@@ -9,7 +9,16 @@ export async function pageComplete(completeEvent: CompleteEvent) {
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
let allPages: PageMeta[] = await space.listPages();
|
||||
// When we're in fenced code block, we likely want to complete a page name without an alias, and only complete template pages
|
||||
// so let's check if we're in a template context
|
||||
const isInTemplateContext =
|
||||
completeEvent.parentNodes.find((node) => node.startsWith("FencedCode")) &&
|
||||
// either a render [[bla]] clause or page: "[[bla]]" template block
|
||||
/render\s+\[\[|page:\s*["']\[\[/.test(
|
||||
completeEvent.linePrefix,
|
||||
);
|
||||
const tagToQuery = isInTemplateContext ? "template" : "page";
|
||||
let allPages: PageMeta[] = await queryObjects<PageMeta>(tagToQuery, {});
|
||||
const prefix = match[1];
|
||||
if (prefix.startsWith("!")) {
|
||||
// Federation prefix, let's first see if we're matching anything from federation that is locally synced
|
||||
@@ -34,12 +43,38 @@ export async function pageComplete(completeEvent: CompleteEvent) {
|
||||
return {
|
||||
from: completeEvent.pos - match[1].length,
|
||||
options: allPages.map((pageMeta) => {
|
||||
return {
|
||||
const completions: any[] = [];
|
||||
if (pageMeta.displayName) {
|
||||
completions.push({
|
||||
label: pageMeta.displayName,
|
||||
boost: pageMeta.lastModified,
|
||||
apply: isInTemplateContext
|
||||
? pageMeta.name
|
||||
: `${pageMeta.name}|${pageMeta.displayName}`,
|
||||
detail: "alias",
|
||||
type: "page",
|
||||
});
|
||||
}
|
||||
if (Array.isArray(pageMeta.aliases)) {
|
||||
for (const alias of pageMeta.aliases) {
|
||||
completions.push({
|
||||
label: alias,
|
||||
boost: pageMeta.lastModified,
|
||||
apply: isInTemplateContext
|
||||
? pageMeta.name
|
||||
: `${pageMeta.name}|${alias}`,
|
||||
detail: "alias",
|
||||
type: "page",
|
||||
});
|
||||
}
|
||||
}
|
||||
completions.push({
|
||||
label: pageMeta.name,
|
||||
boost: pageMeta.lastModified,
|
||||
type: "page",
|
||||
};
|
||||
}),
|
||||
});
|
||||
return completions;
|
||||
}).flat(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -45,14 +45,3 @@ export async function copyPage() {
|
||||
console.log("Navigating to new page");
|
||||
await editor.navigate(newName);
|
||||
}
|
||||
|
||||
export async function newPageCommand() {
|
||||
const allPages = await space.listPages();
|
||||
let pageName = `Untitled`;
|
||||
let i = 1;
|
||||
while (allPages.find((p) => p.name === pageName)) {
|
||||
pageName = `Untitled ${i}`;
|
||||
i++;
|
||||
}
|
||||
await editor.navigate(pageName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user