Read-only mode

pull/80/head
Zef Hemel 2022-09-16 14:26:47 +02:00
parent a598b32ce1
commit b35c60ee60
8 changed files with 62 additions and 5 deletions

View File

@ -133,3 +133,7 @@ export function prompt(
): Promise<string | undefined> { ): Promise<string | undefined> {
return syscall("editor.prompt", message, defaultValue); return syscall("editor.prompt", message, defaultValue);
} }
export function enableReadOnlyMode(enabled: boolean) {
return syscall("editor.enableReadOnlyMode", enabled);
}

View File

@ -6,7 +6,7 @@ import {
renderToText, renderToText,
replaceNodesMatching, replaceNodesMatching,
} from "@silverbulletmd/common/tree"; } from "@silverbulletmd/common/tree";
import type { FileMeta, PageMeta } from "@silverbulletmd/common/types"; import type { FileMeta } from "@silverbulletmd/common/types";
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown"; import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
const pagePrefix = "💭 "; const pagePrefix = "💭 ";

View File

@ -46,6 +46,16 @@ functions:
path: "./page.ts:deletePage" path: "./page.ts:deletePage"
command: command:
name: "Page: Delete" name: "Page: Delete"
editorLoad:
path: "./editor.ts:editorLoad"
events:
- plugs:loaded
toggleReadOnlyode:
path: "./editor.ts:toggleReadOnlyMode"
command:
name: "Editor: Toggle Read Only Mode"
# Backlinks # Backlinks
indexLinks: indexLinks:
path: "./page.ts:indexLinks" path: "./page.ts:indexLinks"

View File

@ -0,0 +1,16 @@
import * as clientStore from "@silverbulletmd/plugos-silverbullet-syscall/clientStore";
import { enableReadOnlyMode } from "@silverbulletmd/plugos-silverbullet-syscall/editor";
export async function editorLoad() {
let readOnlyMode = await clientStore.get("readOnlyMode");
if (readOnlyMode) {
await enableReadOnlyMode(true);
}
}
export async function toggleReadOnlyMode() {
let readOnlyMode = await clientStore.get("readOnlyMode");
readOnlyMode = !readOnlyMode;
await enableReadOnlyMode(readOnlyMode);
await clientStore.set("readOnlyMode", readOnlyMode);
}

View File

@ -658,7 +658,10 @@ export class Editor {
contentDOM.spellcheck = true; contentDOM.spellcheck = true;
contentDOM.setAttribute("autocorrect", "on"); contentDOM.setAttribute("autocorrect", "on");
contentDOM.setAttribute("autocapitalize", "on"); contentDOM.setAttribute("autocapitalize", "on");
contentDOM.setAttribute("contenteditable", readOnly ? "false" : "true"); contentDOM.setAttribute(
"contenteditable",
readOnly || this.viewState.forcedROMode ? "false" : "true"
);
if (isMobileSafari() && readOnly) { if (isMobileSafari() && readOnly) {
console.log("Safari read only hack"); console.log("Safari read only hack");
@ -720,6 +723,15 @@ export class Editor {
} }
}, [viewState.currentPage]); }, [viewState.currentPage]);
useEffect(() => {
if (editor.editorView) {
editor.tweakEditorDOM(
editor.editorView.contentDOM,
viewState.perm === "ro"
);
}
}, [viewState.forcedROMode]);
return ( return (
<> <>
{viewState.showPageNavigator && ( {viewState.showPageNavigator && (

View File

@ -163,6 +163,11 @@ export default function reducer(
filterBoxOptions: [], filterBoxOptions: [],
filterBoxHelpText: "", filterBoxHelpText: "",
}; };
case "set-editor-ro":
return {
...state,
forcedROMode: action.enabled,
};
} }
return state; return state;
} }

View File

@ -192,5 +192,11 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
): string | null => { ): string | null => {
return prompt(message, defaultValue); return prompt(message, defaultValue);
}, },
"editor.enableReadOnlyMode": (ctx, enabled: boolean) => {
editor.viewDispatch({
type: "set-editor-ro",
enabled,
});
},
}; };
} }

View File

@ -8,10 +8,12 @@ export type Notification = {
date: Date; date: Date;
}; };
type EditorMode = "ro" | "rw";
export type AppViewState = { export type AppViewState = {
currentPage?: string; currentPage?: string;
perm: "ro" | "rw"; perm: EditorMode;
forcedROMode: boolean;
isLoading: boolean; isLoading: boolean;
showPageNavigator: boolean; showPageNavigator: boolean;
showCommandPalette: boolean; showCommandPalette: boolean;
@ -40,6 +42,7 @@ export type AppViewState = {
export const initialViewState: AppViewState = { export const initialViewState: AppViewState = {
perm: "rw", perm: "rw",
forcedROMode: false,
isLoading: false, isLoading: false,
showPageNavigator: false, showPageNavigator: false,
showCommandPalette: false, showCommandPalette: false,
@ -93,4 +96,5 @@ export type Action =
label: string; label: string;
onSelect: (option: FilterOption | undefined) => void; onSelect: (option: FilterOption | undefined) => void;
} }
| { type: "hide-filterbox" }; | { type: "hide-filterbox" }
| { type: "set-editor-ro"; enabled: boolean };