Merge pull request #45 from Willyfrog/writesettings
Enable writing on the settings page from plugs
This commit is contained in:
commit
9827ba7f76
@ -1,4 +1,7 @@
|
|||||||
import { readYamlPage } from "./yaml_page";
|
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
|
* 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`
|
* @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<T extends object>(settings: T): Promise<T> {
|
export async function readSettings<T extends object>(settings: T): Promise<T> {
|
||||||
try {
|
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"
|
// TODO: I'm sure there's a better way to type this than "any"
|
||||||
let collectedSettings: any = {};
|
let collectedSettings: any = {};
|
||||||
for (let [key, defaultVal] of Object.entries(settings)) {
|
for (let [key, defaultVal] of Object.entries(settings)) {
|
||||||
if (allSettings[key]) {
|
if (key in allSettings) {
|
||||||
collectedSettings[key] = allSettings[key];
|
collectedSettings[key] = allSettings[key];
|
||||||
} else {
|
} else {
|
||||||
collectedSettings[key] = defaultVal;
|
collectedSettings[key] = defaultVal;
|
||||||
@ -31,3 +36,22 @@ export async function readSettings<T extends object>(settings: T): Promise<T> {
|
|||||||
throw e;
|
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<T extends object>(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)
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
import { flashNotification } from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
||||||
|
|
||||||
export async function replaceAsync(
|
export async function replaceAsync(
|
||||||
str: string,
|
str: string,
|
||||||
regex: RegExp,
|
regex: RegExp,
|
||||||
@ -12,3 +14,21 @@ export async function replaceAsync(
|
|||||||
const data = await Promise.all(promises);
|
const data = await Promise.all(promises);
|
||||||
return str.replace(regex, () => data.shift()!);
|
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;
|
||||||
|
}
|
@ -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 { 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";
|
import YAML from "yaml";
|
||||||
|
|
||||||
export async function readYamlPage(
|
export async function readYamlPage(
|
||||||
|
@ -14,3 +14,7 @@ functions:
|
|||||||
- editor:updated
|
- editor:updated
|
||||||
- editor:pageLoaded
|
- editor:pageLoaded
|
||||||
- editor:pageReloaded
|
- editor:pageReloaded
|
||||||
|
switchSide:
|
||||||
|
path: "./preview.ts:switchSide"
|
||||||
|
command:
|
||||||
|
name: "Swith Preview to other Sidebar"
|
||||||
|
@ -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 { invokeFunction } from "@silverbulletmd/plugos-silverbullet-syscall/system";
|
||||||
import * as clientStore from "@silverbulletmd/plugos-silverbullet-syscall/clientStore";
|
import * as clientStore from "@silverbulletmd/plugos-silverbullet-syscall/clientStore";
|
||||||
|
import { readSettings, writeSettings } from "@silverbulletmd/plugs/lib/settings_page";;
|
||||||
|
|
||||||
|
|
||||||
export async function togglePreview() {
|
export async function togglePreview() {
|
||||||
let currentValue = !!(await clientStore.get("enableMarkdownPreview"));
|
let currentValue = !!(await clientStore.get("enableMarkdownPreview"));
|
||||||
@ -13,5 +15,7 @@ export async function togglePreview() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function hideMarkdownPreview() {
|
async function hideMarkdownPreview() {
|
||||||
await hideRhs();
|
const setting = await readSettings({previewOnRHS: true});
|
||||||
|
const hide = setting.previewOnRHS ? hideRhs : hideLhs;
|
||||||
|
await hide();
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
import MarkdownIt from "markdown-it";
|
import MarkdownIt from "markdown-it";
|
||||||
import {
|
import {
|
||||||
getText,
|
getText,
|
||||||
|
showLhs,
|
||||||
showRhs,
|
showRhs,
|
||||||
|
hideRhs,
|
||||||
|
hideLhs,
|
||||||
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
||||||
import * as clientStore from "@silverbulletmd/plugos-silverbullet-syscall/clientStore";
|
import * as clientStore from "@silverbulletmd/plugos-silverbullet-syscall/clientStore";
|
||||||
import { cleanMarkdown } from "./util";
|
import { cleanMarkdown } from "./util";
|
||||||
|
import { readSettings, writeSettings } from "@silverbulletmd/plugs/lib/settings_page";
|
||||||
|
|
||||||
const css = `
|
const css = `
|
||||||
<style>
|
<style>
|
||||||
@ -76,9 +80,27 @@ export async function updateMarkdownPreview() {
|
|||||||
}
|
}
|
||||||
let text = await getText();
|
let text = await getText();
|
||||||
let cleanMd = await cleanMarkdown(text);
|
let cleanMd = await cleanMarkdown(text);
|
||||||
await showRhs(
|
const setting = await readSettings({previewOnRHS: true});
|
||||||
|
const show = setting.previewOnRHS ? showRhs : showLhs;
|
||||||
|
await show(
|
||||||
`<html><head>${css}</head><body>${md.render(cleanMd)}</body></html>`,
|
`<html><head>${css}</head><body>${md.render(cleanMd)}</body></html>`,
|
||||||
undefined,
|
undefined,
|
||||||
2
|
2
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function switchSide() {
|
||||||
|
const {previewOnRHS} = await readSettings({previewOnRHS: true});
|
||||||
|
const isVisible = await clientStore.get("enableMarkdownPreview");
|
||||||
|
if (isVisible) {
|
||||||
|
if (previewOnRHS){
|
||||||
|
hideRhs();
|
||||||
|
} else {
|
||||||
|
hideLhs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await writeSettings({previewOnRHS: !previewOnRHS});
|
||||||
|
if (isVisible) {
|
||||||
|
updateMarkdownPreview();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user