From dd3cfd8e989aac82f0e4359dd56cf2a2b2bb31d9 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Sat, 23 Jul 2022 19:18:03 +0200 Subject: [PATCH 1/2] Enable writing on the settings page from plugs --- packages/plugs/lib/settings_page.ts | 31 ++++++++++++++-- packages/plugs/lib/yaml_page.ts | 43 +++++++++++++++++++++- packages/plugs/markdown/markdown.plug.yaml | 4 ++ packages/plugs/markdown/markdown.ts | 8 +++- packages/plugs/markdown/preview.ts | 24 +++++++++++- 5 files changed, 102 insertions(+), 8 deletions(-) diff --git a/packages/plugs/lib/settings_page.ts b/packages/plugs/lib/settings_page.ts index c9d746e..a10ae22 100644 --- a/packages/plugs/lib/settings_page.ts +++ b/packages/plugs/lib/settings_page.ts @@ -1,4 +1,5 @@ -import { readYamlPage } from "./yaml_page"; +import { flashNotification } from "@silverbulletmd/plugos-silverbullet-syscall/editor"; +import { readYamlPage, writeYamlPage } from "./yaml_page"; /** * Convenience function to read a specific set of settings from the `SETTINGS` page as well as default values @@ -10,13 +11,16 @@ import { readYamlPage } from "./yaml_page"; * @returns an object with the same shape as `settings` but with non-default values override based on `SETTINGS` */ +const SETTINGS_PAGE = "SETTINGS"; +const SETTINGS_TEMPLATE = `This page contains settings for configuring SilverBullet and its Plugs:\n\`\`\`yaml\n\`\`\``; // might need \r\n for windows? + export async function readSettings(settings: T): Promise { try { - let allSettings = (await readYamlPage("SETTINGS", ["yaml"])) || {}; + let allSettings = (await readYamlPage(SETTINGS_PAGE, ["yaml"])) || {}; // TODO: I'm sure there's a better way to type this than "any" let collectedSettings: any = {}; for (let [key, defaultVal] of Object.entries(settings)) { - if (allSettings[key]) { + if (key in allSettings) { collectedSettings[key] = allSettings[key]; } else { collectedSettings[key] = defaultVal; @@ -31,3 +35,24 @@ export async function readSettings(settings: T): Promise { throw e; } } + +/** + * Convenience function to write a specific set of settings from the `SETTINGS` page. + * If the SETTiNGS page doesn't exist it will create it. + * @param settings + */ +export async function writeSettings(settings: T) { + let readSettings = {}; + try { + readSettings = (await readYamlPage(SETTINGS_PAGE, ["yaml"])) || {}; + } catch (e: any) { + console.log("Couldn't read settings, generating a new settings page"); + flashNotification("Creating a new SETTINGS page...", "info"); + } + const writeSettings = {...readSettings, ...settings}; + if(await writeYamlPage(SETTINGS_PAGE, writeSettings, SETTINGS_TEMPLATE)) { + flashNotification("SETTINGS page written successfully", "info"); + } else { + flashNotification("SETTINGS page failed to update", "error"); + } +} \ No newline at end of file diff --git a/packages/plugs/lib/yaml_page.ts b/packages/plugs/lib/yaml_page.ts index 902539a..4d97da1 100644 --- a/packages/plugs/lib/yaml_page.ts +++ b/packages/plugs/lib/yaml_page.ts @@ -1,6 +1,6 @@ -import { findNodeOfType, traverseTree } from "@silverbulletmd/common/tree"; +import { findNodeOfType, ParseTree, renderToText, replaceNodesMatching, traverseTree } from "@silverbulletmd/common/tree"; import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown"; -import { readPage } from "@silverbulletmd/plugos-silverbullet-syscall/space"; +import { readPage, writePage } from "@silverbulletmd/plugos-silverbullet-syscall/space"; import YAML from "yaml"; export async function readYamlPage( @@ -40,3 +40,42 @@ export async function readYamlPage( return data; } + +export async function writeYamlPage( + pageName: string, + properties: any, + templateOnEmpty: string, +): Promise { + let text; + try { + const page = await readPage(pageName); + text = page.text; + } catch { + // page doesn't exist, so let's create one. + text = templateOnEmpty; + } + const tree = await parseMarkdown(text); + // if we use any other language than yaml... how to create it? + const doc = new YAML.Document(); + doc.contents = properties; + // generate a new node for the properties + const newCode = `\`\`\`yaml\n${doc.toString()}\n\`\`\``; + const subtree = await parseMarkdown(newCode); + // find the original set of properties and replace + let replaced = false; + replaceNodesMatching(tree, (node: ParseTree) => { + if (node.type !== 'FencedCode') { + return; + } + const codeinfoNode = findNodeOfType(node, "CodeInfo"); + if (!codeinfoNode || codeinfoNode.children![0].text! !== "yaml") { + return; + } + replaced = true; + return subtree; + }); + if (replaced) { + await writePage(pageName, renderToText(tree)); + } + return replaced; +} \ No newline at end of file diff --git a/packages/plugs/markdown/markdown.plug.yaml b/packages/plugs/markdown/markdown.plug.yaml index 4e53d55..3f50a81 100644 --- a/packages/plugs/markdown/markdown.plug.yaml +++ b/packages/plugs/markdown/markdown.plug.yaml @@ -14,3 +14,7 @@ functions: - editor:updated - editor:pageLoaded - editor:pageReloaded + switchSide: + path: "./preview.ts:switchSide" + command: + name: "Swith Preview to other Sidebar" diff --git a/packages/plugs/markdown/markdown.ts b/packages/plugs/markdown/markdown.ts index 3c69ad7..78af980 100644 --- a/packages/plugs/markdown/markdown.ts +++ b/packages/plugs/markdown/markdown.ts @@ -1,6 +1,8 @@ -import { hideRhs } from "@silverbulletmd/plugos-silverbullet-syscall/editor"; +import { hideRhs, hideLhs } from "@silverbulletmd/plugos-silverbullet-syscall/editor"; import { invokeFunction } from "@silverbulletmd/plugos-silverbullet-syscall/system"; import * as clientStore from "@silverbulletmd/plugos-silverbullet-syscall/clientStore"; +import { readSettings, writeSettings } from "@silverbulletmd/plugs/lib/settings_page";; + export async function togglePreview() { let currentValue = !!(await clientStore.get("enableMarkdownPreview")); @@ -13,5 +15,7 @@ export async function togglePreview() { } async function hideMarkdownPreview() { - await hideRhs(); + const setting = await readSettings({previewOnRHS: true}); + const hide = setting.previewOnRHS ? hideRhs : hideLhs; + await hide(); } diff --git a/packages/plugs/markdown/preview.ts b/packages/plugs/markdown/preview.ts index 32956a9..c47a549 100644 --- a/packages/plugs/markdown/preview.ts +++ b/packages/plugs/markdown/preview.ts @@ -1,10 +1,14 @@ import MarkdownIt from "markdown-it"; import { getText, + showLhs, showRhs, + hideRhs, + hideLhs, } from "@silverbulletmd/plugos-silverbullet-syscall/editor"; import * as clientStore from "@silverbulletmd/plugos-silverbullet-syscall/clientStore"; import { cleanMarkdown } from "./util"; +import { readSettings, writeSettings } from "@silverbulletmd/plugs/lib/settings_page"; const css = `