diff --git a/packages/plugs/lib/settings_page.ts b/packages/plugs/lib/settings_page.ts index c9d746e..c2bc2a9 100644 --- a/packages/plugs/lib/settings_page.ts +++ b/packages/plugs/lib/settings_page.ts @@ -1,4 +1,7 @@ import { readYamlPage } from "./yaml_page"; +import { notifyUser } from "./util"; +import YAML from "yaml"; +import { writePage } from "@silverbulletmd/plugos-silverbullet-syscall/space"; /** * Convenience function to read a specific set of settings from the `SETTINGS` page as well as default values @@ -10,13 +13,15 @@ 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"; + 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 +36,22 @@ 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) { + await notifyUser("Creating a new SETTINGS page...", "info"); + } + const writeSettings = {...readSettings, ...settings}; + const doc = new YAML.Document(); + doc.contents = writeSettings; + const contents = `This page contains settings for configuring SilverBullet and its Plugs.\nAny changes outside of the yaml block will be overwritten.\n\`\`\`yaml\n${doc.toString()}\n\`\`\``; // might need \r\n for windows? + await writePage(SETTINGS_PAGE, contents) +} \ No newline at end of file diff --git a/packages/plugs/lib/util.ts b/packages/plugs/lib/util.ts index 6bc6993..af0a2ce 100644 --- a/packages/plugs/lib/util.ts +++ b/packages/plugs/lib/util.ts @@ -1,3 +1,5 @@ +import { flashNotification } from "@silverbulletmd/plugos-silverbullet-syscall/editor"; + export async function replaceAsync( str: string, regex: RegExp, @@ -12,3 +14,21 @@ export async function replaceAsync( const data = await Promise.all(promises); return str.replace(regex, () => data.shift()!); } + +export function isServer() { + return typeof window === 'undefined' || typeof window.document === "undefined"; // if something defines window the same way as the browser, this will fail. +} + +// this helps keep if's condition as positive +export function isBrowser() { + return !isServer(); +} + +export async function notifyUser(message: string, type?: "info"|"error") { + if (isBrowser()) { + return flashNotification(message, type); + } + const log = type === "error" ? console.error : console.log; + log(message); // we should end up sending the message to the user, users dont read logs. + return; +} \ No newline at end of file diff --git a/packages/plugs/lib/yaml_page.ts b/packages/plugs/lib/yaml_page.ts index 902539a..c0deaaf 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( 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 = `