Template cleanup, autocomplete for more query stuff

This commit is contained in:
Zef Hemel
2022-07-04 15:07:27 +02:00
parent e2e7e2f936
commit 60cfefbc95
18 changed files with 235 additions and 69 deletions
+51 -14
View File
@@ -123,34 +123,63 @@ functions:
# Template commands
insertPageMeta:
path: "./page.ts:insertPageMeta"
path: "./template.ts:insertTemplateText"
slashCommand:
name: meta
value: |
```meta
|^|
```
insertTask:
path: "./template.ts:insertTemplateText"
slashCommand:
name: task
value: "* [ ] |^|"
insertQuery:
path: "./template.ts:insertTemplateText"
slashCommand:
name: query
value: |
<!-- #query |^| -->
<!-- /query -->
insertTaskToday:
path: "./template.ts:insertTemplateText"
slashCommand:
name: task-today
value: "* [ ] |^| 📅 {{today}}"
quickNoteCommand:
path: ./template.ts:quickNoteCommand
command:
name: "Template: Quick Note"
key: "Alt-Shift-n"
quickTaskCommand:
path: ./template.ts:quickTaskCommand
command:
name: "Template: Quick Task"
key: "Alt-Shift-t"
instantiateTemplateCommand:
path: ./template.ts:instantiateTemplateCommand
command:
name: "Template: Instantiate for Page"
insertToday:
path: "./dates.ts:insertToday"
name: "Template: Instantiate Page"
insertSnippet:
path: ./template.ts:insertSnippet
command:
name: "Template: Insert Snippet"
slashCommand:
name: snippet
description: Insert a snippet
insertTodayCommand:
path: "./template.ts:insertTemplateText"
slashCommand:
name: today
insertTomorrow:
path: "./dates.ts:insertTomorrow"
description: Insert today's date
value: "{{today}}"
insertTomorrowCommand:
path: "./template.ts:insertTemplateText"
slashCommand:
name: tomorrow
description: Insert tomorrow's date
value: "{{tomorrow}}"
# Text editing commands
quoteSelection:
quoteSelectionCommand:
path: ./text.ts:quoteSelection
command:
name: "Text: Quote Selection"
@@ -165,17 +194,25 @@ functions:
command:
name: "Text: Number Listify Selection"
bold:
path: ./text.ts:boldCommand
path: ./text.ts:wrapSelection
command:
name: "Text: Bold"
key: "Ctrl-b"
mac: "Cmd-b"
wrapper: "**"
italic:
path: ./text.ts:italicCommand
path: ./text.ts:wrapSelection
command:
name: "Text: Italic"
key: "Ctrl-i"
mac: "Cmd-i"
wrapper: "_"
marker:
path: ./text.ts:wrapSelection
command:
name: "Text: Marker"
key: "Alt-m"
wrapper: "=="
# Plug manager
updatePlugsCommand:
-12
View File
@@ -1,15 +1,3 @@
import { insertAtCursor } from "@silverbulletmd/plugos-silverbullet-syscall/editor";
export function niceDate(d: Date): string {
return d.toISOString().split("T")[0];
}
export async function insertToday() {
await insertAtCursor(niceDate(new Date()));
}
export async function insertTomorrow() {
let d = new Date();
d.setDate(d.getDate() + 1);
await insertAtCursor(niceDate(d));
}
-6
View File
@@ -236,9 +236,3 @@ export async function parseIndexTextRepublish({ name, text }: IndexEvent) {
tree: await parseMarkdown(text),
});
}
export async function insertPageMeta() {
let cursorPos = await getCursor();
await insertAtCursor("```meta\n\n```");
await moveCursor(cursorPos + 8);
}
+50 -8
View File
@@ -5,6 +5,9 @@ import {
} from "@silverbulletmd/plugos-silverbullet-syscall/space";
import {
filterBox,
getCurrentPage,
getCursor,
insertAtCursor,
moveCursor,
navigate,
prompt,
@@ -15,6 +18,7 @@ import { renderToText } from "@silverbulletmd/common/tree";
import { niceDate } from "./dates";
const pageTemplatePrefix = `template/page/`;
const snippetPrefix = `snippet/`;
export async function instantiateTemplateCommand() {
let allPages = await listPages();
@@ -48,12 +52,45 @@ export async function instantiateTemplateCommand() {
await navigate(pageName);
}
export async function insertSnippet() {
let allPages = await listPages();
let cursorPos = await getCursor();
let page = await getCurrentPage();
let allSnippets = allPages.filter((pageMeta) =>
pageMeta.name.startsWith(snippetPrefix)
);
let selectedSnippet = await filterBox(
"Snippet",
allSnippets,
`Select the snippet to insert (listing any page starting with <tt>${snippetPrefix}</tt>)`
);
if (!selectedSnippet) {
return;
}
let { text } = await readPage(selectedSnippet.name);
let templateText = replaceTemplateVars(text, page);
let carretPos = templateText.indexOf("|^|");
templateText = templateText.replace("|^|", "");
templateText = replaceTemplateVars(templateText, page);
await insertAtCursor(templateText);
if (carretPos !== -1) {
await moveCursor(cursorPos + carretPos);
}
}
// TODO: This should probably be replaced with handlebards somehow?
export function replaceTemplateVars(s: string, pageName: string): string {
return s.replaceAll(/\{\{([^\}]+)\}\}/g, (match, v) => {
switch (v) {
case "today":
return niceDate(new Date());
case "tomorrow":
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
return niceDate(tomorrow);
case "yesterday":
let yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
@@ -62,6 +99,8 @@ export function replaceTemplateVars(s: string, pageName: string): string {
let lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() - 7);
return niceDate(lastWeek);
case "page":
return pageName;
}
return match;
});
@@ -75,12 +114,15 @@ export async function quickNoteCommand() {
await navigate(pageName);
}
export async function quickTaskCommand() {
let isoDate = new Date().toISOString();
let [date, time] = isoDate.split("T");
time = time.split(".")[0];
let pageName = `${date} ${time}`;
await writePage(pageName, "* [ ] ");
await navigate(pageName);
await moveCursor(6);
export async function insertTemplateText(cmdDef: any) {
let cursorPos = await getCursor();
let page = await getCurrentPage();
let templateText: string = cmdDef.value;
let carretPos = templateText.indexOf("|^|");
templateText = templateText.replace("|^|", "");
templateText = replaceTemplateVars(templateText, page);
await insertAtCursor(templateText);
if (carretPos !== -1) {
await moveCursor(cursorPos + carretPos);
}
}
+2 -6
View File
@@ -56,12 +56,8 @@ export async function numberListifySelection() {
await replaceRange(from, selection.to, text);
}
export function boldCommand() {
return insertMarker("**");
}
export function italicCommand() {
return insertMarker("_");
export function wrapSelection(cmdDef: any) {
return insertMarker(cmdDef.wrapper);
}
async function insertMarker(marker: string) {
-11
View File
@@ -1,11 +0,0 @@
import {
getCursor,
insertAtCursor,
moveCursor,
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
export async function insertQuery() {
let cursorPos = await getCursor();
await insertAtCursor(`<!-- #query -->\n\n<!-- /query -->`);
await moveCursor(cursorPos + 12);
}
+32
View File
@@ -0,0 +1,32 @@
import { listEvents } from "@plugos/plugos-syscall/event";
import { matchBefore } from "@silverbulletmd/plugos-silverbullet-syscall/editor";
import { listPages } from "@silverbulletmd/plugos-silverbullet-syscall/space";
export async function queryComplete() {
let prefix = await matchBefore("#query [\\w\\-_]*");
if (prefix) {
let allEvents = await listEvents();
// console.log("All events", allEvents);
return {
from: prefix.from + "#query ".length,
options: allEvents
.filter((eventName) => eventName.startsWith("query:"))
.map((source) => ({
label: source.substring("query:".length),
})),
};
}
prefix = await matchBefore('render "[^"]*');
if (prefix) {
let allPages = await listPages();
return {
from: prefix.from + 'render "'.length,
options: allPages.map((pageMeta) => ({
label: pageMeta.name,
})),
};
}
}
+11 -3
View File
@@ -19,7 +19,6 @@ import { replaceAsync } from "../lib/util";
export async function updateMaterializedQueriesCommand() {
const currentPage = await getCurrentPage();
await save();
// await flashNotification("Updating materialized queries...");
if (
await invokeFunction(
"server",
@@ -67,8 +66,17 @@ async function updateTemplateInstantiations(
export async function updateMaterializedQueriesOnPage(
pageName: string
): Promise<boolean> {
let { text } = await readPage(pageName);
let text = "";
try {
text = (await readPage(pageName)).text;
} catch {
console.warn(
"Could not read page",
pageName,
"perhaps it doesn't yet exist"
);
return false;
}
let newText = await updateTemplateInstantiations(text, pageName);
newText = await replaceAsync(
newText,
+4 -4
View File
@@ -17,7 +17,7 @@ functions:
path: ./data.ts:queryProvider
events:
- query:data
insertQueryCommand:
path: ./command.ts:insertQuery
slashCommand:
name: query
queryComplete:
path: ./complete.ts:queryComplete
events:
- page:complete