From a7cd3ea7e08674d4b4141a95272b67d5884e25aa Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Mon, 4 Apr 2022 15:25:07 +0200 Subject: [PATCH] A whole lot of enhancements --- common/tree.ts | 6 +- plugos-silverbullet-syscall/clientStore.ts | 13 +++++ plugos-silverbullet-syscall/editor.ts | 25 ++------- plugos/environments/sandbox_worker.ts | 65 ++++++++++------------ plugos/syscalls/store.dexie_browser.ts | 2 +- plugs/core/item.ts | 6 +- plugs/core/markdown.ts | 23 -------- plugs/core/materialized_queries.ts | 33 ++++++++++- plugs/core/navigate.ts | 7 +-- plugs/emoji/emoji.ts | 4 +- plugs/lib/tree.test.ts | 4 +- plugs/lib/tree.ts | 34 +++++++++-- plugs/markdown/markdown.plug.yaml | 14 +++-- plugs/markdown/markdown.ts | 46 ++++++++++++++- plugs/tasks/task.ts | 15 +++-- webapp/components/panel.html | 21 +++++++ webapp/components/panel.scss | 4 ++ webapp/components/panel.tsx | 28 ++++++++-- webapp/components/status_bar.tsx | 6 +- webapp/editor.tsx | 28 +++++++--- webapp/navigator.ts | 7 ++- webapp/styles/constants.scss | 3 + webapp/styles/editor.scss | 8 ++- webapp/styles/main.scss | 48 +++++++++++----- webapp/syscalls/clientStore.ts | 13 +++++ webapp/syscalls/editor.ts | 40 ++----------- 26 files changed, 322 insertions(+), 181 deletions(-) create mode 100644 plugos-silverbullet-syscall/clientStore.ts delete mode 100644 plugs/core/markdown.ts create mode 100644 webapp/components/panel.html create mode 100644 webapp/components/panel.scss create mode 100644 webapp/styles/constants.scss create mode 100644 webapp/syscalls/clientStore.ts diff --git a/common/tree.ts b/common/tree.ts index d1504d15..4724b3b7 100644 --- a/common/tree.ts +++ b/common/tree.ts @@ -3,8 +3,8 @@ import wikiMarkdownLang from "../webapp/parser"; export type MarkdownTree = { type?: string; // undefined === text node - from: number; - to: number; + from?: number; + to?: number; text?: string; children?: MarkdownTree[]; }; @@ -39,7 +39,7 @@ function treeToAST(text: string, n: SyntaxNode): MarkdownTree { }); } newChildren.push(child); - index = child.to; + index = child.to!; } let s = text.substring(index, n.to); if (s) { diff --git a/plugos-silverbullet-syscall/clientStore.ts b/plugos-silverbullet-syscall/clientStore.ts new file mode 100644 index 00000000..6e4fce9c --- /dev/null +++ b/plugos-silverbullet-syscall/clientStore.ts @@ -0,0 +1,13 @@ +import {syscall} from "./syscall"; + +export async function set(key: string, value: any): Promise { + return syscall("clientStore.set", key, value); +} + +export async function get(key: string): Promise { + return syscall("clientStore.get", key); +} + +export async function del(key: string): Promise { + return syscall("clientStore.delete", key); +} diff --git a/plugos-silverbullet-syscall/editor.ts b/plugos-silverbullet-syscall/editor.ts index 1f6ca763..98a68244 100644 --- a/plugos-silverbullet-syscall/editor.ts +++ b/plugos-silverbullet-syscall/editor.ts @@ -1,4 +1,4 @@ -import { syscall } from "./syscall"; +import {syscall} from "./syscall"; export function getCurrentPage(): Promise { return syscall("editor.getCurrentPage"); @@ -36,6 +36,10 @@ export function showRhs(html: string): Promise { return syscall("editor.showRhs", html); } +export function hideRhs(): Promise { + return syscall("editor.hideRhs"); +} + export function insertAtPos(text: string, pos: number): Promise { return syscall("editor.insertAtPos", text, pos); } @@ -56,31 +60,12 @@ export function insertAtCursor(text: string): Promise { return syscall("editor.insertAtCursor", text); } -export type SyntaxNode = { - name: string; - text: string; - from: number; - to: number; -}; - -export function getSyntaxNodeUnderCursor(): Promise { - return syscall("editor.getSyntaxNodeUnderCursor"); -} - -export function getLineUnderCursor(): Promise { - return syscall("editor.getLineUnderCursor"); -} - export function matchBefore( re: string ): Promise<{ from: number; to: number; text: string } | null> { return syscall("editor.matchBefore", re); } -export function getSyntaxNodeAtPos(pos: number): Promise { - return syscall("editor.getSyntaxNodeAtPos", pos); -} - export function dispatch(change: any): Promise { return syscall("editor.dispatch", change); } diff --git a/plugos/environments/sandbox_worker.ts b/plugos/environments/sandbox_worker.ts index 9bdeb0d0..afde0c6a 100644 --- a/plugos/environments/sandbox_worker.ts +++ b/plugos/environments/sandbox_worker.ts @@ -1,5 +1,5 @@ -import { safeRun } from "../util"; -import { ControllerMessage, WorkerMessage } from "./worker"; +import {safeRun} from "../util"; +import {ControllerMessage, WorkerMessage} from "./worker"; let loadedFunctions = new Map(); let pendingRequests = new Map< @@ -9,10 +9,13 @@ let pendingRequests = new Map< reject: (e: any) => void; } >(); -let postMessage = self.postMessage.bind(self); -if (window.parent !== window) { - postMessage = window.parent.postMessage.bind(window.parent); +function workerPostMessage(msg: ControllerMessage) { + if (typeof window !== "undefined" && window.parent !== window) { + window.parent.postMessage(msg, "*"); + } else { + self.postMessage(msg); + } } declare global { @@ -25,15 +28,12 @@ self.syscall = async (name: string, ...args: any[]) => { return await new Promise((resolve, reject) => { syscallReqId++; pendingRequests.set(syscallReqId, { resolve, reject }); - postMessage( - { - type: "syscall", - id: syscallReqId, - name, - args, - }, - "*" - ); + workerPostMessage({ + type: "syscall", + id: syscallReqId, + name, + args, + }); }); }; @@ -48,13 +48,10 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => { case "load": let fn2 = new Function(wrapScript(data.code!)); loadedFunctions.set(data.name!, fn2()); - postMessage( - { - type: "inited", - name: data.name, - } as ControllerMessage, - "*" - ); + workerPostMessage({ + type: "inited", + name: data.name, + }); break; case "invoke": let fn = loadedFunctions.get(data.name!); @@ -63,23 +60,17 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => { } try { let result = await Promise.resolve(fn(...(data.args || []))); - postMessage( - { - type: "result", - id: data.id, - result: result, - } as ControllerMessage, - "*" - ); + workerPostMessage({ + type: "result", + id: data.id, + result: result, + } as ControllerMessage); } catch (e: any) { - postMessage( - { - type: "result", - id: data.id, - error: e.message, - } as ControllerMessage, - "*" - ); + workerPostMessage({ + type: "result", + id: data.id, + error: e.message, + }); throw e; } diff --git a/plugos/syscalls/store.dexie_browser.ts b/plugos/syscalls/store.dexie_browser.ts index 466f6e96..9c1467ca 100644 --- a/plugos/syscalls/store.dexie_browser.ts +++ b/plugos/syscalls/store.dexie_browser.ts @@ -12,7 +12,7 @@ export function storeSyscalls( ): SysCallMapping { const db = new Dexie(dbName); db.version(1).stores({ - test: "key", + [tableName]: "key", }); const items = db.table(tableName); diff --git a/plugs/core/item.ts b/plugs/core/item.ts index c0eaa0c4..c47416b8 100644 --- a/plugs/core/item.ts +++ b/plugs/core/item.ts @@ -3,7 +3,7 @@ import {whiteOutQueries} from "./materialized_queries"; import {batchSet} from "plugos-silverbullet-syscall/index"; import {parseMarkdown} from "plugos-silverbullet-syscall/markdown"; -import {collectNodesMatching, MarkdownTree, render} from "../lib/tree"; +import {collectNodesMatching, MarkdownTree, renderMarkdown,} from "../lib/tree"; type Item = { item: string; @@ -27,12 +27,12 @@ export async function indexItems({ name, text }: IndexEvent) { let nested: string | undefined; for (let child of n.children!.slice(1)) { if (child.type === "OrderedList" || child.type === "BulletList") { - nested = render(child); + nested = renderMarkdown(child); break; } textNodes.push(child); } - let item = textNodes.map(render).join("").trim(); + let item = textNodes.map(renderMarkdown).join("").trim(); let value: Item = { item, }; diff --git a/plugs/core/markdown.ts b/plugs/core/markdown.ts deleted file mode 100644 index 804cc798..00000000 --- a/plugs/core/markdown.ts +++ /dev/null @@ -1,23 +0,0 @@ -import mdParser from "../../webapp/parser"; -import { getText } from "plugos-silverbullet-syscall/editor"; - -export async function renderMD() { - let text = await getText(); - let tree = mdParser.parser.parse(text); - let slicesToRemove: [number, number][] = []; - - tree.iterate({ - enter(type, from, to): false | void { - switch (type.name) { - case "Comment": - slicesToRemove.push([from, to]); - return false; - } - }, - }); - // console.log("output peices", JSON.stringify(tree)); - slicesToRemove.reverse().forEach(([from, to]) => { - text = text.slice(0, from) + text.slice(to); - }); - console.log("Clean md", text); -} diff --git a/plugs/core/materialized_queries.ts b/plugs/core/materialized_queries.ts index d9e7c583..f507c342 100644 --- a/plugs/core/materialized_queries.ts +++ b/plugs/core/materialized_queries.ts @@ -1,11 +1,11 @@ import {flashNotification, getCurrentPage, reloadPage, save,} from "plugos-silverbullet-syscall/editor"; -import {readPage, writePage} from "plugos-silverbullet-syscall/space"; +import {listPages, readPage, writePage,} from "plugos-silverbullet-syscall/space"; import {invokeFunctionOnServer} from "plugos-silverbullet-syscall/system"; import {scanPrefixGlobal} from "plugos-silverbullet-syscall"; export const queryRegex = - /()(.+?)()/gs; + /()(.+?)()/gs; export function whiteOutQueries(text: string): string { return text.replaceAll(queryRegex, (match) => @@ -40,11 +40,38 @@ export async function updateMaterializedQueriesCommand() { export async function updateMaterializedQueriesOnPage(pageName: string) { let { text } = await readPage(pageName); text = await replaceAsync(text, queryRegex, async (match, ...args) => { - let { table, filter, groupBy } = args[args.length - 1]; + let { table, filter, groupBy, limit, orderBy, orderDesc } = + args[args.length - 1]; const startQuery = args[0]; const endQuery = args[args.length - 4]; let results = []; switch (table) { + case "page": + let pages = await listPages(); + if (orderBy) { + pages = pages.sort((a: any, b: any) => { + console.log(a, orderBy, a[orderBy]); + if (a[orderBy] === b[orderBy]) { + return 0; + } + if (a[orderBy] < b[orderBy]) { + return !!orderDesc ? 1 : -1; + } else { + return !!orderDesc ? -1 : 1; + } + }); + } + let matchCount = 0; + for (let pageMeta of pages) { + if (!filter || (filter && pageMeta.name.includes(filter))) { + matchCount++; + results.push(`* [[${pageMeta.name}]]`); + if (limit && matchCount === +limit) { + break; + } + } + } + return `${startQuery}\n${results.join("\n")}\n${endQuery}`; case "task": for (let { key, diff --git a/plugs/core/navigate.ts b/plugs/core/navigate.ts index 1cf36351..45859089 100644 --- a/plugs/core/navigate.ts +++ b/plugs/core/navigate.ts @@ -3,8 +3,7 @@ import {updateMaterializedQueriesCommand} from "./materialized_queries"; import {getCursor, getText, navigate as navigateTo, openUrl,} from "plugos-silverbullet-syscall/editor"; import {taskToggleAtPos} from "../tasks/task"; import {parseMarkdown} from "plugos-silverbullet-syscall/markdown"; -import {nodeAtPos} from "../lib/tree"; -import type {MarkdownTree} from "../../common/tree"; +import {MarkdownTree, nodeAtPos} from "../lib/tree"; const materializedQueryPrefix = /