Make attribute extensible
This commit is contained in:
+33
-69
@@ -2,36 +2,10 @@ import { events } from "$sb/plugos-syscall/mod.ts";
|
||||
import { CompleteEvent } from "$sb/app_event.ts";
|
||||
import { buildHandebarOptions } from "./util.ts";
|
||||
import type { PageMeta } from "../../web/types.ts";
|
||||
import { index } from "$sb/silverbullet-syscall/mod.ts";
|
||||
|
||||
const builtinAttributes: Record<string, Record<string, string>> = {
|
||||
page: {
|
||||
name: "string",
|
||||
lastModified: "number",
|
||||
perm: "rw|ro",
|
||||
contentType: "string",
|
||||
size: "number",
|
||||
tags: "array",
|
||||
},
|
||||
task: {
|
||||
name: "string",
|
||||
done: "boolean",
|
||||
page: "string",
|
||||
deadline: "string",
|
||||
pos: "number",
|
||||
tags: "array",
|
||||
},
|
||||
item: {
|
||||
name: "string",
|
||||
page: "string",
|
||||
pos: "number",
|
||||
tags: "array",
|
||||
},
|
||||
tag: {
|
||||
name: "string",
|
||||
freq: "number",
|
||||
},
|
||||
};
|
||||
import {
|
||||
AttributeCompleteEvent,
|
||||
AttributeCompletion,
|
||||
} from "../core/attributes.ts";
|
||||
|
||||
export async function queryComplete(completeEvent: CompleteEvent) {
|
||||
const querySourceMatch = /#query\s+([\w\-_]*)$/.exec(
|
||||
@@ -61,51 +35,22 @@ export async function queryComplete(completeEvent: CompleteEvent) {
|
||||
if (querySourceMatch && whereMatch) {
|
||||
const type = querySourceMatch[1];
|
||||
const attributePrefix = whereMatch[3];
|
||||
// console.log("Type", type);
|
||||
// console.log("Where", attributePrefix);
|
||||
const allAttributes = await index.queryPrefix(
|
||||
`attr:${type}:`,
|
||||
);
|
||||
|
||||
const completions = (await events.dispatchEvent(
|
||||
`attribute:complete:${type}`,
|
||||
{
|
||||
source: type,
|
||||
prefix: attributePrefix,
|
||||
} as AttributeCompleteEvent,
|
||||
)).flat() as AttributeCompletion[];
|
||||
return {
|
||||
from: completeEvent.pos - attributePrefix.length,
|
||||
options: compileAttributeCompletions(allAttributes, type),
|
||||
options: attributeCompletionsToCMCompletion(completions),
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function compileAttributeCompletions(
|
||||
allAttributes: { key: string; value: any }[],
|
||||
type?: string,
|
||||
) {
|
||||
let allCompletions: any[] = allAttributes.map((attr) => {
|
||||
const [_prefix, context, name] = attr.key.split(":");
|
||||
return {
|
||||
label: name,
|
||||
detail: `${attr.value.type} (${context})`,
|
||||
type: "attribute",
|
||||
};
|
||||
});
|
||||
const allContexts = type ? [type] : Object.keys(builtinAttributes);
|
||||
|
||||
for (const context of allContexts) {
|
||||
allCompletions = allCompletions.concat(
|
||||
builtinAttributes[context]
|
||||
? Object.entries(
|
||||
builtinAttributes[context],
|
||||
).map(([name, type]) => ({
|
||||
label: name,
|
||||
detail: `${type} (${context}: builtin)`,
|
||||
type: "attribute",
|
||||
}))
|
||||
: [],
|
||||
);
|
||||
}
|
||||
return allCompletions;
|
||||
}
|
||||
|
||||
export async function templateVariableComplete(completeEvent: CompleteEvent) {
|
||||
const match = /\{\{([\w@]*)$/.exec(completeEvent.linePrefix);
|
||||
if (!match) {
|
||||
@@ -123,9 +68,16 @@ export async function templateVariableComplete(completeEvent: CompleteEvent) {
|
||||
})),
|
||||
);
|
||||
|
||||
const allAttributes = await index.queryPrefix(`attr:`);
|
||||
const completions = (await events.dispatchEvent(
|
||||
`attribute:complete:*`,
|
||||
{
|
||||
source: "*",
|
||||
prefix: match[1],
|
||||
} as AttributeCompleteEvent,
|
||||
)).flat() as AttributeCompletion[];
|
||||
|
||||
allCompletions = allCompletions.concat(
|
||||
compileAttributeCompletions(allAttributes),
|
||||
attributeCompletionsToCMCompletion(completions),
|
||||
);
|
||||
|
||||
return {
|
||||
@@ -133,3 +85,15 @@ export async function templateVariableComplete(completeEvent: CompleteEvent) {
|
||||
options: allCompletions,
|
||||
};
|
||||
}
|
||||
|
||||
export function attributeCompletionsToCMCompletion(
|
||||
completions: AttributeCompletion[],
|
||||
) {
|
||||
return completions.map(
|
||||
(completion) => ({
|
||||
label: completion.name,
|
||||
detail: `${completion.type} (${completion.source})`,
|
||||
type: "attribute",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user