From e4da0b7849fdf41e621d83255ed6a98c36ec7979 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 14 Jul 2024 11:29:43 +0200 Subject: [PATCH] Refined implementation of #560 --- lib/web.ts | 6 --- plug-api/syscalls.ts | 1 + plug-api/syscalls/decoration.ts | 7 +++ plug-api/types.ts | 12 ++++- plugs/editor/complete.ts | 36 +++------------ plugs/markdown/markdown_render.ts | 6 ++- type/web.ts | 5 +-- web/client.ts | 35 ++++++++++++--- web/client_system.ts | 2 + web/cm_plugins/wiki_link.ts | 2 +- web/components/page_navigator.tsx | 4 +- web/editor_ui.tsx | 5 ++- web/reducer.ts | 70 ++++++----------------------- web/styles/main.scss | 9 +--- web/syscalls/decoration.ts | 74 +++++++++++++++++++++++++++++++ website/CHANGELOG.md | 4 +- website/Frontmatter.md | 1 - website/Page Decorations.md | 35 +++++++++++++++ website/SETTINGS.md | 5 +++ 19 files changed, 200 insertions(+), 119 deletions(-) create mode 100644 plug-api/syscalls/decoration.ts create mode 100644 web/syscalls/decoration.ts create mode 100644 website/Page Decorations.md diff --git a/lib/web.ts b/lib/web.ts index 8b85b272..13a79800 100644 --- a/lib/web.ts +++ b/lib/web.ts @@ -33,9 +33,3 @@ export type ActionButton = { export type EmojiConfig = { aliases: string[][]; }; - -export type Decoration = { - tag: string; - prefix: string; -}; - diff --git a/plug-api/syscalls.ts b/plug-api/syscalls.ts index 9081f127..814ca4f0 100644 --- a/plug-api/syscalls.ts +++ b/plug-api/syscalls.ts @@ -16,3 +16,4 @@ export * as YAML from "./syscalls/yaml.ts"; export * as mq from "./syscalls/mq.ts"; export * from "./syscall.ts"; export * as datastore from "./syscalls/datastore.ts"; +export * as decoration from "./syscalls/decoration.ts"; diff --git a/plug-api/syscalls/decoration.ts b/plug-api/syscalls/decoration.ts new file mode 100644 index 00000000..cd622e12 --- /dev/null +++ b/plug-api/syscalls/decoration.ts @@ -0,0 +1,7 @@ +import { PageMeta } from "$sb/types.ts"; + +export function applyDecorationsToPages( + pages: PageMeta[], +): Promise { + return syscall("decoration.applyDecorationsToPages", pages); +} diff --git a/plug-api/types.ts b/plug-api/types.ts index 2729cce9..bdfa3e90 100644 --- a/plug-api/types.ts +++ b/plug-api/types.ts @@ -16,11 +16,21 @@ export type PageMeta = ObjectValue< name: string; created: string; // indexing it as a string lastModified: string; // indexing it as a string - lastOpened?: number; perm: "ro" | "rw"; + lastOpened?: number; + pageDecoration?: PageDecoration; } & Record >; +/** + * Decorates a page when it matches certain criteria + */ +export type PageDecoration = { + where?: string; + whereParsed?: QueryExpression; + prefix: string; +}; + export type AttachmentMeta = ObjectValue< { name: string; diff --git a/plugs/editor/complete.ts b/plugs/editor/complete.ts index f2fa760d..d4ecc572 100644 --- a/plugs/editor/complete.ts +++ b/plugs/editor/complete.ts @@ -7,20 +7,9 @@ import { import { listFilesCached } from "../federation/federation.ts"; import { queryObjects } from "../index/plug_api.ts"; import { folderName } from "$sb/lib/resolve.ts"; -import { readSetting } from "$sb/lib/settings_page.ts"; -import { editor } from "$sb/syscalls.ts" -import type { Decoration } from "$lib/web.ts"; - -let decorations: Decoration[] = []; - +import { decoration } from "$sb/syscalls.ts"; // Completion export async function pageComplete(completeEvent: CompleteEvent) { - try { - await updateDecoratorConfig(); - } catch (err: any) { - await editor.flashNotification(err.message, "error"); - } - // Try to match [[wikilink]] let isWikilink = true; let match = /\[\[([^\]@$#:\{}]*)$/.exec(completeEvent.linePrefix); @@ -89,14 +78,16 @@ export async function pageComplete(completeEvent: CompleteEvent) { } const folder = folderName(completeEvent.pageName); + + allPages = await decoration.applyDecorationsToPages(allPages as PageMeta[]); + return { from: completeEvent.pos - match[1].length, options: allPages.map((pageMeta) => { const completions: any[] = []; let namePrefix = ""; - const decor = decorations.find(d => pageMeta.tags?.some((t: any) => d.tag === t)); - if (decor) { - namePrefix = decor.prefix; + if ((pageMeta as PageMeta).pageDecoration?.prefix) { + namePrefix = pageMeta.pageDecoration?.prefix; } if (isWikilink) { if (pageMeta.displayName) { @@ -157,7 +148,6 @@ export async function pageComplete(completeEvent: CompleteEvent) { }; } - function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta { const name = fileMeta.name.substring(0, fileMeta.name.length - 3); return { @@ -169,17 +159,3 @@ function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta { lastModified: new Date(fileMeta.lastModified).toISOString(), } as PageMeta; } - -let lastConfigUpdate = 0; - -async function updateDecoratorConfig() { - // Update at most every 5 seconds - if (Date.now() < lastConfigUpdate + 5000) return; - lastConfigUpdate = Date.now(); - const decoratorConfig = await readSetting("decorations"); - if (!decoratorConfig) { - return; - } - - decorations = decoratorConfig; -} diff --git a/plugs/markdown/markdown_render.ts b/plugs/markdown/markdown_render.ts index f3c1cde3..63a7543a 100644 --- a/plugs/markdown/markdown_render.ts +++ b/plugs/markdown/markdown_render.ts @@ -573,9 +573,11 @@ export function renderMarkdownToHtml( t.attrs!.href = options.translateUrls!(t.attrs!.href, "link"); } if (t.attrs!["data-ref"]?.length) { - const pageMeta = allPages.find(p => t.attrs!["data-ref"]!.startsWith(p.name)); + const pageMeta = allPages.find((p) => + t.attrs!["data-ref"]!.startsWith(p.name) + ); if (pageMeta) { - t.body = [(pageMeta.pageDecorations?.prefix ?? "") + t.body] + t.body = [(pageMeta.pageDecoration?.prefix ?? "") + t.body]; } } if (t.body.length === 0) { diff --git a/type/web.ts b/type/web.ts index 6991bc61..8f705316 100644 --- a/type/web.ts +++ b/type/web.ts @@ -1,10 +1,9 @@ import { Manifest } from "../lib/manifest.ts"; -import { PageMeta } from "../plug-api/types.ts"; +import { PageDecoration, PageMeta } from "../plug-api/types.ts"; import { AppCommand } from "../lib/command.ts"; import { defaultSettings } from "$common/settings.ts"; import { ActionButton, - Decoration, EmojiConfig, FilterOption, Notification, @@ -22,10 +21,10 @@ export type BuiltinSettings = { maximumAttachmentSize?: number; defaultLinkStyle?: string; actionButtons: ActionButton[]; + pageDecorations?: PageDecoration[]; // Format: compatible with docker ignore spaceIgnore?: string; emoji?: EmojiConfig; - decorations?: Decoration[]; }; export type PanelConfig = { diff --git a/web/client.ts b/web/client.ts index 66efc265..f410105b 100644 --- a/web/client.ts +++ b/web/client.ts @@ -713,6 +713,11 @@ export class Client { this.currentPage, meta, ); + + // At this all the essential stuff is done, let's proceed + resolve(); + + // In the background we'll fetch any enriched meta data, if any const enrichedMeta = await this.clientSystem.getObjectByRef< PageMeta >( @@ -720,12 +725,12 @@ export class Client { "page", this.currentPage, ); - this.ui.viewDispatch({ - type: "update-current-page-meta", - meta: enrichedMeta, - }); - - resolve(); + if (enrichedMeta) { + this.ui.viewDispatch({ + type: "update-current-page-meta", + meta: enrichedMeta, + }); + } }) .catch((e) => { this.flashNotification( @@ -1104,6 +1109,24 @@ export class Client { meta: doc.meta, }); + // Fetch (possibly) enriched meta data asynchronously + await this.clientSystem.getObjectByRef< + PageMeta + >( + this.currentPage, + "page", + this.currentPage, + ).then((enrichedMeta) => { + if (!enrichedMeta) { + // Nothing in the store, revert to default + enrichedMeta = doc.meta; + } + this.ui.viewDispatch({ + type: "update-current-page-meta", + meta: enrichedMeta, + }); + }).catch(console.error); + const editorState = createEditorState( this, pageName, diff --git a/web/client_system.ts b/web/client_system.ts index 4ef6c068..5036ed58 100644 --- a/web/client_system.ts +++ b/web/client_system.ts @@ -43,6 +43,7 @@ import { createKeyBindings } from "./editor_state.ts"; import { CommonSystem } from "$common/common_system.ts"; import { DataStoreMQ } from "$lib/data/mq.datastore.ts"; import { plugPrefix } from "$common/spaces/constants.ts"; +import { decorationSyscalls } from "./syscalls/decoration.ts"; const plugNameExtractRegex = /\/(.+)\.plug\.js$/; @@ -168,6 +169,7 @@ export class ClientSystem extends CommonSystem { spaceReadSyscalls(this.client), systemSyscalls(this.system, false, this, this.client), markdownSyscalls(), + decorationSyscalls(this.client), assetSyscalls(this.system), yamlSyscalls(), templateSyscalls(this.ds), diff --git a/web/cm_plugins/wiki_link.ts b/web/cm_plugins/wiki_link.ts index f1817195..985783dd 100644 --- a/web/cm_plugins/wiki_link.ts +++ b/web/cm_plugins/wiki_link.ts @@ -70,7 +70,7 @@ export function cleanWikiLinkPlugin(client: Client) { p.name == url ); const linkText = alias || - (pageMeta?.pageDecorations?.prefix ?? "") + + (pageMeta?.pageDecoration?.prefix ?? "") + (url.includes("/") ? url.split("/").pop()! : url); // And replace it with a widget diff --git a/web/components/page_navigator.tsx b/web/components/page_navigator.tsx index e9adca83..382bdfd2 100644 --- a/web/components/page_navigator.tsx +++ b/web/components/page_navigator.tsx @@ -1,5 +1,5 @@ import { FilterList } from "./filter.tsx"; -import { FilterOption, Decoration } from "$lib/web.ts"; +import { FilterOption } from "$lib/web.ts"; import { CompletionContext, CompletionResult } from "@codemirror/autocomplete"; import { PageMeta } from "../../plug-api/types.ts"; import { isFederationPath } from "$sb/lib/resolve.ts"; @@ -65,7 +65,7 @@ export function PageNavigator({ } options.push({ ...pageMeta, - name: (pageMeta.pageDecorations?.prefix ?? "") + pageMeta.name, + name: (pageMeta.pageDecoration?.prefix ?? "") + pageMeta.name, description, orderId: orderId, }); diff --git a/web/editor_ui.tsx b/web/editor_ui.tsx index 8e362d1b..5df30ef1 100644 --- a/web/editor_ui.tsx +++ b/web/editor_ui.tsx @@ -78,7 +78,7 @@ export class MainUI { useEffect(() => { if (viewState.currentPage) { document.title = - (viewState.currentPageMeta?.pageDecorations?.prefix ?? "") + + (viewState.currentPageMeta?.pageDecoration?.prefix ?? "") + viewState.currentPage; } }, [viewState.currentPage, viewState.currentPageMeta]); @@ -316,7 +316,8 @@ export class MainUI { style={{ flex: viewState.panels.lhs.mode }} /> )} - pageNamePrefix={viewState.currentPageMeta?.pageDecorations?.prefix ?? + pageNamePrefix={viewState.currentPageMeta?.pageDecoration + ?.prefix ?? ""} />
diff --git a/web/reducer.ts b/web/reducer.ts index 36479f04..67311ae6 100644 --- a/web/reducer.ts +++ b/web/reducer.ts @@ -1,6 +1,6 @@ -import { Decoration } from "$lib/web.ts"; import { PageMeta } from "../plug-api/types.ts"; import { Action, AppViewState } from "../type/web.ts"; +import { decoratePageMeta } from "./syscalls/decoration.ts"; export default function reducer( state: AppViewState, @@ -22,20 +22,6 @@ export default function reducer( }; case "page-loaded": { const mouseDetected = window.matchMedia("(pointer:fine)").matches; - const pageMeta = state.allPages.find((p) => p.name == action.meta.name); - const decor = state.settings.decorations?.filter((d) => - pageMeta?.tags?.some((t) => d.tag === t) - ); - if (decor && decor.length > 0) { - const mergedDecorations = decor.reduceRight((accumulator, el) => { - accumulator = { ...accumulator, ...el }; - return accumulator; - }); - if (mergedDecorations) { - const { tag, ...currPageDecorations } = mergedDecorations; - action.meta.pageDecorations = currPageDecorations; - } - } return { ...state, isLoading: false, @@ -61,12 +47,14 @@ export default function reducer( }; } case "update-current-page-meta": { - if (state.settings.decorations) { + if (state.settings.pageDecorations) { decoratePageMeta( action.meta, - "", - action.meta, - state.settings.decorations, + state.settings.pageDecorations, + ); + // Update in the allPages list as well + state.allPages = state.allPages.map((pageMeta) => + pageMeta.name === action.meta.name ? action.meta : pageMeta ); } return { @@ -89,23 +77,21 @@ export default function reducer( const oldPageMeta = new Map( [...state.allPages].map((pm) => [pm.name, pm]), ); - let currPageMeta = oldPageMeta.get(state.currentPage!); - if (currPageMeta === undefined) { - currPageMeta = {} as PageMeta; - } + let currPageMeta: PageMeta | undefined; for (const pageMeta of action.allPages) { const oldPageMetaItem = oldPageMeta.get(pageMeta.name); if (oldPageMetaItem && oldPageMetaItem.lastOpened) { pageMeta.lastOpened = oldPageMetaItem.lastOpened; } - if (state.settings.decorations) { + if (state.settings.pageDecorations) { decoratePageMeta( pageMeta, - state.currentPage!, - currPageMeta, - state.settings.decorations, + state.settings.pageDecorations, ); } + if (pageMeta.name === state.currentPage) { + currPageMeta = pageMeta; + } } return { ...state, @@ -246,33 +232,3 @@ export default function reducer( }; } } - -/** - * Decorates (= attaches a pageDecorations field) to the pageMeta object when a matching decorator is found - */ -function decoratePageMeta( - pageMeta: PageMeta, - currentPage: string, - currPageMeta: PageMeta, - decorations: Decoration[], -) { - const decor = decorations.filter((d) => - pageMeta.tags?.some((t: any) => d.tag === t) - ); - // Page can have multiple decorations applied via different tags, accumulate them. - // The decorations higher in the decorations list defined in SETTINGS gets - // higher precedence. - if (decor && decor.length > 0) { - const mergedDecorations = decor.reduceRight((accumulator, el) => { - accumulator = { ...accumulator, ...el }; - return accumulator; - }); - if (mergedDecorations) { - const { tag, ...currPageDecorations } = mergedDecorations; - pageMeta.pageDecorations = currPageDecorations; - if (pageMeta.name === currentPage) { - currPageMeta!.pageDecorations = currPageDecorations; - } - } - } -} diff --git a/web/styles/main.scss b/web/styles/main.scss index 0bd3d5ae..744b9b18 100644 --- a/web/styles/main.scss +++ b/web/styles/main.scss @@ -212,14 +212,9 @@ body { align-items: baseline; flex: 0 0 auto; text-align: left; - padding: 1px; - margin-right: 3px; + padding-top: 3px; font-family: var(--ui-font); - - &:empty { - margin-right: 0; - padding: 0; - } + white-space: pre-wrap; } .sb-panel { diff --git a/web/syscalls/decoration.ts b/web/syscalls/decoration.ts new file mode 100644 index 00000000..c7bdd6f2 --- /dev/null +++ b/web/syscalls/decoration.ts @@ -0,0 +1,74 @@ +import { PageDecoration, PageMeta } from "$sb/types.ts"; +import { SysCallMapping } from "$lib/plugos/system.ts"; +import { Client } from "../client.ts"; +import { parseTreeToAST } from "$sb/lib/tree.ts"; +import { lezerToParseTree } from "$common/markdown_parser/parse_tree.ts"; +import { expressionLanguage } from "$common/template/template_parser.ts"; +import { expressionToKvQueryExpression } from "$sb/lib/parse-query.ts"; +import { evalQueryExpression } from "$sb/lib/query_expression.ts"; +import { builtinFunctions } from "$lib/builtin_query_functions.ts"; + +export function decorationSyscalls( + client: Client, +): SysCallMapping { + return { + "decoration.applyDecorationsToPages": ( + _ctx, + pages: PageMeta[], + ): PageMeta[] => { + if (client.settings.pageDecorations) { + for (const pageMeta of pages) { + decoratePageMeta(pageMeta, client.settings.pageDecorations); + } + } + return pages; + }, + }; +} + +/** + * Decorates (= attaches a pageDecoration field) to the pageMeta object when a matching decorator is found + */ +export function decoratePageMeta( + pageMeta: PageMeta, + decorations: PageDecoration[], +) { + if (!pageMeta) { + return; + } + for (const decoration of decorations) { + if (!decoration.where) { + continue; + } + // whereParsed is effectively a cached version of the parsed where expression + // Let's check if it's populated + if (!decoration.whereParsed) { + // If not, populate it + try { + const ast = parseTreeToAST(lezerToParseTree( + decoration.where, + expressionLanguage.parser.parse(decoration.where).topNode, + )); + decoration.whereParsed = expressionToKvQueryExpression( + ast[1], + ); + } catch (e: any) { + console.error( + "Failed to parse 'where' expression in decoration:", + e, + ); + continue; + } + } + if ( + evalQueryExpression( + decoration.whereParsed, + pageMeta, + {}, + builtinFunctions, + ) + ) { + pageMeta.pageDecoration = decoration; + } + } +} diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index 066a7ff9..e35a9f86 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -6,7 +6,9 @@ release. ## Edge _These features are not yet properly released, you need to use [the edge builds](https://community.silverbullet.md/t/living-on-the-edge-builds/27) to try them._ -* Nothing new since 0.8.1 yet! +* [[Page Decorations]] are here (initial implementation by [Deepak Narayan](https://github.com/silverbulletmd/silverbullet/pull/940), later refined by Zef) +* **Fix:** very large spaces would let the server blow up when saving snapshots. This is now fixed. +* **Fix:** Conflict copies could no longer be edited, whoops (initial fix by [Semyon Novikov](https://github.com/silverbulletmd/silverbullet/pull/939), later refined by Zef) --- diff --git a/website/Frontmatter.md b/website/Frontmatter.md index 2231ef7d..d9d3e0d6 100644 --- a/website/Frontmatter.md +++ b/website/Frontmatter.md @@ -1,6 +1,5 @@ --- status: Complete -tags: meta --- Frontmatter is a common format to attach additional metadata (data about data) to markdown documents. diff --git a/website/Page Decorations.md b/website/Page Decorations.md new file mode 100644 index 00000000..45937389 --- /dev/null +++ b/website/Page Decorations.md @@ -0,0 +1,35 @@ +--- +pageDecoration.prefix: "🎄 " +--- +Page decorations allow you to “decorate” pages in various ways. + +For now “various ways” means just one way (adding a visual prefix), but in the future, more such decorations will likely be added. + +There are two ways to decorate a page. + +# Frontmatter +The first is demonstrated in the [[Frontmatter]] of this page, by using the special `pageDecoration` attribute. + +# Settings +The more useful way is to apply decorations to pages _dynamically_, you can use the `pageDecorations` attribute in [[SETTINGS]]. + +Every page decoration has two parts: +* `where`: the [[Expression Language]] expression that has to evaluate to `true` for a given page for that decoration to be applied. +* A set of decorations to apply, see [[#Supported decorations]] + +For example: +```yaml +- where: 'tags = "person"' + prefix: "🧑 " +``` + +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]] + +Again — later, more such decorations may be added. \ No newline at end of file diff --git a/website/SETTINGS.md b/website/SETTINGS.md index 2545ff00..53231ab9 100644 --- a/website/SETTINGS.md +++ b/website/SETTINGS.md @@ -48,6 +48,11 @@ shortcuts: - command: "{[Upload: File]}" priority: 1 # Make sure this appears at the top of the list in the command palette +# Page decorations +pageDecorations: +- where: 'tags = "plug"' + prefix: "🔌 " + # Defines files to ignore in a format compatible with .gitignore spaceIgnore: | dist