A whole lot of enhancements

pull/3/head
Zef Hemel 2022-04-04 15:25:07 +02:00
parent 16577c8ea2
commit a7cd3ea7e0
26 changed files with 322 additions and 181 deletions

View File

@ -3,8 +3,8 @@ import wikiMarkdownLang from "../webapp/parser";
export type MarkdownTree = { export type MarkdownTree = {
type?: string; // undefined === text node type?: string; // undefined === text node
from: number; from?: number;
to: number; to?: number;
text?: string; text?: string;
children?: MarkdownTree[]; children?: MarkdownTree[];
}; };
@ -39,7 +39,7 @@ function treeToAST(text: string, n: SyntaxNode): MarkdownTree {
}); });
} }
newChildren.push(child); newChildren.push(child);
index = child.to; index = child.to!;
} }
let s = text.substring(index, n.to); let s = text.substring(index, n.to);
if (s) { if (s) {

View File

@ -0,0 +1,13 @@
import {syscall} from "./syscall";
export async function set(key: string, value: any): Promise<void> {
return syscall("clientStore.set", key, value);
}
export async function get(key: string): Promise<any> {
return syscall("clientStore.get", key);
}
export async function del(key: string): Promise<void> {
return syscall("clientStore.delete", key);
}

View File

@ -1,4 +1,4 @@
import { syscall } from "./syscall"; import {syscall} from "./syscall";
export function getCurrentPage(): Promise<string> { export function getCurrentPage(): Promise<string> {
return syscall("editor.getCurrentPage"); return syscall("editor.getCurrentPage");
@ -36,6 +36,10 @@ export function showRhs(html: string): Promise<void> {
return syscall("editor.showRhs", html); return syscall("editor.showRhs", html);
} }
export function hideRhs(): Promise<void> {
return syscall("editor.hideRhs");
}
export function insertAtPos(text: string, pos: number): Promise<void> { export function insertAtPos(text: string, pos: number): Promise<void> {
return syscall("editor.insertAtPos", text, pos); return syscall("editor.insertAtPos", text, pos);
} }
@ -56,31 +60,12 @@ export function insertAtCursor(text: string): Promise<void> {
return syscall("editor.insertAtCursor", text); return syscall("editor.insertAtCursor", text);
} }
export type SyntaxNode = {
name: string;
text: string;
from: number;
to: number;
};
export function getSyntaxNodeUnderCursor(): Promise<SyntaxNode> {
return syscall("editor.getSyntaxNodeUnderCursor");
}
export function getLineUnderCursor(): Promise<string> {
return syscall("editor.getLineUnderCursor");
}
export function matchBefore( export function matchBefore(
re: string re: string
): Promise<{ from: number; to: number; text: string } | null> { ): Promise<{ from: number; to: number; text: string } | null> {
return syscall("editor.matchBefore", re); return syscall("editor.matchBefore", re);
} }
export function getSyntaxNodeAtPos(pos: number): Promise<SyntaxNode> {
return syscall("editor.getSyntaxNodeAtPos", pos);
}
export function dispatch(change: any): Promise<void> { export function dispatch(change: any): Promise<void> {
return syscall("editor.dispatch", change); return syscall("editor.dispatch", change);
} }

View File

@ -1,5 +1,5 @@
import { safeRun } from "../util"; import {safeRun} from "../util";
import { ControllerMessage, WorkerMessage } from "./worker"; import {ControllerMessage, WorkerMessage} from "./worker";
let loadedFunctions = new Map<string, Function>(); let loadedFunctions = new Map<string, Function>();
let pendingRequests = new Map< let pendingRequests = new Map<
@ -9,10 +9,13 @@ let pendingRequests = new Map<
reject: (e: any) => void; reject: (e: any) => void;
} }
>(); >();
let postMessage = self.postMessage.bind(self);
if (window.parent !== window) { function workerPostMessage(msg: ControllerMessage) {
postMessage = window.parent.postMessage.bind(window.parent); if (typeof window !== "undefined" && window.parent !== window) {
window.parent.postMessage(msg, "*");
} else {
self.postMessage(msg);
}
} }
declare global { declare global {
@ -25,15 +28,12 @@ self.syscall = async (name: string, ...args: any[]) => {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
syscallReqId++; syscallReqId++;
pendingRequests.set(syscallReqId, { resolve, reject }); pendingRequests.set(syscallReqId, { resolve, reject });
postMessage( workerPostMessage({
{ type: "syscall",
type: "syscall", id: syscallReqId,
id: syscallReqId, name,
name, args,
args, });
},
"*"
);
}); });
}; };
@ -48,13 +48,10 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => {
case "load": case "load":
let fn2 = new Function(wrapScript(data.code!)); let fn2 = new Function(wrapScript(data.code!));
loadedFunctions.set(data.name!, fn2()); loadedFunctions.set(data.name!, fn2());
postMessage( workerPostMessage({
{ type: "inited",
type: "inited", name: data.name,
name: data.name, });
} as ControllerMessage,
"*"
);
break; break;
case "invoke": case "invoke":
let fn = loadedFunctions.get(data.name!); let fn = loadedFunctions.get(data.name!);
@ -63,23 +60,17 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => {
} }
try { try {
let result = await Promise.resolve(fn(...(data.args || []))); let result = await Promise.resolve(fn(...(data.args || [])));
postMessage( workerPostMessage({
{ type: "result",
type: "result", id: data.id,
id: data.id, result: result,
result: result, } as ControllerMessage);
} as ControllerMessage,
"*"
);
} catch (e: any) { } catch (e: any) {
postMessage( workerPostMessage({
{ type: "result",
type: "result", id: data.id,
id: data.id, error: e.message,
error: e.message, });
} as ControllerMessage,
"*"
);
throw e; throw e;
} }

View File

@ -12,7 +12,7 @@ export function storeSyscalls(
): SysCallMapping { ): SysCallMapping {
const db = new Dexie(dbName); const db = new Dexie(dbName);
db.version(1).stores({ db.version(1).stores({
test: "key", [tableName]: "key",
}); });
const items = db.table(tableName); const items = db.table(tableName);

View File

@ -3,7 +3,7 @@ import {whiteOutQueries} from "./materialized_queries";
import {batchSet} from "plugos-silverbullet-syscall/index"; import {batchSet} from "plugos-silverbullet-syscall/index";
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown"; import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
import {collectNodesMatching, MarkdownTree, render} from "../lib/tree"; import {collectNodesMatching, MarkdownTree, renderMarkdown,} from "../lib/tree";
type Item = { type Item = {
item: string; item: string;
@ -27,12 +27,12 @@ export async function indexItems({ name, text }: IndexEvent) {
let nested: string | undefined; let nested: string | undefined;
for (let child of n.children!.slice(1)) { for (let child of n.children!.slice(1)) {
if (child.type === "OrderedList" || child.type === "BulletList") { if (child.type === "OrderedList" || child.type === "BulletList") {
nested = render(child); nested = renderMarkdown(child);
break; break;
} }
textNodes.push(child); textNodes.push(child);
} }
let item = textNodes.map(render).join("").trim(); let item = textNodes.map(renderMarkdown).join("").trim();
let value: Item = { let value: Item = {
item, item,
}; };

View File

@ -1,23 +0,0 @@
import mdParser from "../../webapp/parser";
import { getText } from "plugos-silverbullet-syscall/editor";
export async function renderMD() {
let text = await getText();
let tree = mdParser.parser.parse(text);
let slicesToRemove: [number, number][] = [];
tree.iterate({
enter(type, from, to): false | void {
switch (type.name) {
case "Comment":
slicesToRemove.push([from, to]);
return false;
}
},
});
// console.log("output peices", JSON.stringify(tree));
slicesToRemove.reverse().forEach(([from, to]) => {
text = text.slice(0, from) + text.slice(to);
});
console.log("Clean md", text);
}

View File

@ -1,11 +1,11 @@
import {flashNotification, getCurrentPage, reloadPage, save,} from "plugos-silverbullet-syscall/editor"; import {flashNotification, getCurrentPage, reloadPage, save,} from "plugos-silverbullet-syscall/editor";
import {readPage, writePage} from "plugos-silverbullet-syscall/space"; import {listPages, readPage, writePage,} from "plugos-silverbullet-syscall/space";
import {invokeFunctionOnServer} from "plugos-silverbullet-syscall/system"; import {invokeFunctionOnServer} from "plugos-silverbullet-syscall/system";
import {scanPrefixGlobal} from "plugos-silverbullet-syscall"; import {scanPrefixGlobal} from "plugos-silverbullet-syscall";
export const queryRegex = export const queryRegex =
/(<!--\s*#query\s+(?<table>\w+)\s*(filter\s+["'](?<filter>[^"']+)["'])?\s*(group by\s+(?<groupBy>\w+))?\s*-->)(.+?)(<!--\s*#end\s*-->)/gs; /(<!--\s*#query\s+(?<table>\w+)\s*(filter\s+["'](?<filter>[^"']+)["'])?\s*\s*(order by\s+(?<orderBy>\w+)(?<orderDesc>\s+desc)?)?(group by\s+(?<groupBy>\w+))?\s*(limit\s+(?<limit>\d+))?\s*-->)(.+?)(<!--\s*#end\s*-->)/gs;
export function whiteOutQueries(text: string): string { export function whiteOutQueries(text: string): string {
return text.replaceAll(queryRegex, (match) => return text.replaceAll(queryRegex, (match) =>
@ -40,11 +40,38 @@ export async function updateMaterializedQueriesCommand() {
export async function updateMaterializedQueriesOnPage(pageName: string) { export async function updateMaterializedQueriesOnPage(pageName: string) {
let { text } = await readPage(pageName); let { text } = await readPage(pageName);
text = await replaceAsync(text, queryRegex, async (match, ...args) => { text = await replaceAsync(text, queryRegex, async (match, ...args) => {
let { table, filter, groupBy } = args[args.length - 1]; let { table, filter, groupBy, limit, orderBy, orderDesc } =
args[args.length - 1];
const startQuery = args[0]; const startQuery = args[0];
const endQuery = args[args.length - 4]; const endQuery = args[args.length - 4];
let results = []; let results = [];
switch (table) { switch (table) {
case "page":
let pages = await listPages();
if (orderBy) {
pages = pages.sort((a: any, b: any) => {
console.log(a, orderBy, a[orderBy]);
if (a[orderBy] === b[orderBy]) {
return 0;
}
if (a[orderBy] < b[orderBy]) {
return !!orderDesc ? 1 : -1;
} else {
return !!orderDesc ? -1 : 1;
}
});
}
let matchCount = 0;
for (let pageMeta of pages) {
if (!filter || (filter && pageMeta.name.includes(filter))) {
matchCount++;
results.push(`* [[${pageMeta.name}]]`);
if (limit && matchCount === +limit) {
break;
}
}
}
return `${startQuery}\n${results.join("\n")}\n${endQuery}`;
case "task": case "task":
for (let { for (let {
key, key,

View File

@ -3,8 +3,7 @@ import {updateMaterializedQueriesCommand} from "./materialized_queries";
import {getCursor, getText, navigate as navigateTo, openUrl,} from "plugos-silverbullet-syscall/editor"; import {getCursor, getText, navigate as navigateTo, openUrl,} from "plugos-silverbullet-syscall/editor";
import {taskToggleAtPos} from "../tasks/task"; import {taskToggleAtPos} from "../tasks/task";
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown"; import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
import {nodeAtPos} from "../lib/tree"; import {MarkdownTree, nodeAtPos} from "../lib/tree";
import type {MarkdownTree} from "../../common/tree";
const materializedQueryPrefix = /<!--\s*#query\s+/; const materializedQueryPrefix = /<!--\s*#query\s+/;
@ -34,14 +33,14 @@ async function actionClickOrActionEnter(mdTree: MarkdownTree | null) {
await openUrl(mdTree.children![4].children![0].text!); await openUrl(mdTree.children![4].children![0].text!);
break; break;
case "TaskMarker": case "TaskMarker":
await taskToggleAtPos(mdTree.from + 1); await taskToggleAtPos(mdTree.from! + 1);
break; break;
} }
} }
export async function linkNavigate() { export async function linkNavigate() {
let mdTree = await parseMarkdown(await getText()); let mdTree = await parseMarkdown(await getText());
let newNode = await nodeAtPos(mdTree, await getCursor()); let newNode = nodeAtPos(mdTree, await getCursor());
await actionClickOrActionEnter(newNode); await actionClickOrActionEnter(newNode);
} }

View File

@ -1,11 +1,11 @@
// @ts-ignore // @ts-ignore
import emojis from "./emoji.json"; import emojis from "./emoji.json";
import { matchBefore } from "plugos-silverbullet-syscall/editor"; import {matchBefore} from "plugos-silverbullet-syscall/editor";
const emojiMatcher = /\(([^\)]+)\)\s+(.+)$/; const emojiMatcher = /\(([^\)]+)\)\s+(.+)$/;
export async function emojiCompleter() { export async function emojiCompleter() {
let prefix = await matchBefore(":[\\w\\s]*"); let prefix = await matchBefore(":[\\w]+");
if (!prefix) { if (!prefix) {
return null; return null;
} }

View File

@ -1,6 +1,6 @@
import {expect, test} from "@jest/globals"; import {expect, test} from "@jest/globals";
import {parse} from "../../common/tree"; import {parse} from "../../common/tree";
import {addParentPointers, collectNodesMatching, findParentMatching, nodeAtPos, render,} from "./tree"; import {addParentPointers, collectNodesMatching, findParentMatching, nodeAtPos, renderMarkdown,} from "./tree";
const mdTest1 = ` const mdTest1 = `
# Heading # Heading
@ -45,7 +45,7 @@ test("Run a Node sandbox", async () => {
expect(allTodos.length).toBe(2); expect(allTodos.length).toBe(2);
// Render back into markdown should be equivalent // Render back into markdown should be equivalent
expect(render(mdTree)).toBe(mdTest1); expect(renderMarkdown(mdTree)).toBe(mdTest1);
let mdTree2 = parse(mdTest2); let mdTree2 = parse(mdTest2);
console.log(JSON.stringify(mdTree2, null, 2)); console.log(JSON.stringify(mdTree2, null, 2));

View File

@ -1,7 +1,7 @@
export type MarkdownTree = { export type MarkdownTree = {
type?: string; // undefined === text node type?: string; // undefined === text node
from: number; from?: number;
to: number; to?: number;
text?: string; text?: string;
children?: MarkdownTree[]; children?: MarkdownTree[];
parent?: MarkdownTree; parent?: MarkdownTree;
@ -57,6 +57,30 @@ export function collectNodesMatching(
return results; return results;
} }
export function replaceNodesMatching(
mdTree: MarkdownTree,
substituteFn: (mdTree: MarkdownTree) => MarkdownTree | null | undefined
) {
let subst = substituteFn(mdTree);
if (subst !== undefined) {
if (!mdTree.parent) {
throw Error("Need parent pointers for this");
}
let parentChildren = mdTree.parent.children!;
let pos = parentChildren.indexOf(mdTree);
if (subst) {
parentChildren.splice(pos, 1, subst);
} else {
// null = delete
parentChildren.splice(pos, 1);
}
} else if (mdTree.children) {
for (let child of mdTree.children) {
replaceNodesMatching(child, substituteFn);
}
}
}
export function findNodeMatching( export function findNodeMatching(
mdTree: MarkdownTree, mdTree: MarkdownTree,
matchFn: (mdTree: MarkdownTree) => boolean matchFn: (mdTree: MarkdownTree) => boolean
@ -69,7 +93,7 @@ export function nodeAtPos(
mdTree: MarkdownTree, mdTree: MarkdownTree,
pos: number pos: number
): MarkdownTree | null { ): MarkdownTree | null {
if (pos < mdTree.from || pos > mdTree.to) { if (pos < mdTree.from! || pos > mdTree.to!) {
return null; return null;
} }
if (!mdTree.children) { if (!mdTree.children) {
@ -89,13 +113,13 @@ export function nodeAtPos(
} }
// Turn MarkdownTree back into regular markdown text // Turn MarkdownTree back into regular markdown text
export function render(mdTree: MarkdownTree): string { export function renderMarkdown(mdTree: MarkdownTree): string {
let pieces: string[] = []; let pieces: string[] = [];
if (mdTree.text !== undefined) { if (mdTree.text !== undefined) {
return mdTree.text; return mdTree.text;
} }
for (let child of mdTree.children!) { for (let child of mdTree.children!) {
pieces.push(render(child)); pieces.push(renderMarkdown(child));
} }
return pieces.join(""); return pieces.join("");
} }

View File

@ -1,6 +1,12 @@
functions: functions:
mdTest: toggle:
path: "./markdown.ts:renderMarkdown" path: "./markdown.ts:togglePreview"
env: client
command: command:
name: "Markdown: Render" name: "Toggle Markdown Preview"
preview:
path: "./markdown.ts:updateMarkdownPreview"
env: client
events:
- plug:load
- editor:updated
- editor:pageSwitched

View File

@ -1,5 +1,8 @@
import MarkdownIt from "markdown-it"; import MarkdownIt from "markdown-it";
import { getText, showRhs } from "plugos-silverbullet-syscall/editor"; import {getText, hideRhs, showRhs} from "plugos-silverbullet-syscall/editor";
import * as clientStore from "plugos-silverbullet-syscall/clientStore";
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
import {addParentPointers, renderMarkdown, replaceNodesMatching,} from "../lib/tree";
var taskLists = require("markdown-it-task-lists"); var taskLists = require("markdown-it-task-lists");
@ -9,8 +12,45 @@ const md = new MarkdownIt({
typographer: true, typographer: true,
}).use(taskLists); }).use(taskLists);
export async function renderMarkdown() { export async function togglePreview() {
let currentValue = !!(await clientStore.get("enableMarkdownPreview"));
await clientStore.set("enableMarkdownPreview", !currentValue);
if (!currentValue) {
updateMarkdownPreview();
} else {
hideMarkdownPreview();
}
}
function encodePageUrl(name: string): string {
return name.replaceAll(" ", "_");
}
export async function updateMarkdownPreview() {
if (!(await clientStore.get("enableMarkdownPreview"))) {
return;
}
let text = await getText(); let text = await getText();
let html = md.render(text); let mdTree = await parseMarkdown(text);
// console.log("The tree", mdTree);
addParentPointers(mdTree);
replaceNodesMatching(mdTree, (n) => {
if (n.type === "WikiLink") {
const page = n.children![1].children![0].text!;
return {
// HACK
text: `[${page}](/${encodePageUrl(page)})`,
};
}
// Simply get rid of these
if (n.type === "CommentBlock" || n.type === "Comment") {
return null;
}
});
let html = md.render(renderMarkdown(mdTree));
await showRhs(`<html><body>${html}</body></html>`); await showRhs(`<html><body>${html}</body></html>`);
} }
async function hideMarkdownPreview() {
await hideRhs();
}

View File

@ -5,8 +5,8 @@ import {whiteOutQueries} from "../core/materialized_queries";
import {batchSet} from "plugos-silverbullet-syscall/index"; import {batchSet} from "plugos-silverbullet-syscall/index";
import {readPage, writePage} from "plugos-silverbullet-syscall/space"; import {readPage, writePage} from "plugos-silverbullet-syscall/space";
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown"; import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
import {dispatch, getText,} from "plugos-silverbullet-syscall/editor"; import {dispatch, getText} from "plugos-silverbullet-syscall/editor";
import {addParentPointers, collectNodesMatching, nodeAtPos, render,} from "../lib/tree"; import {addParentPointers, collectNodesMatching, nodeAtPos, renderMarkdown,} from "../lib/tree";
type Task = { type Task = {
task: string; task: string;
@ -22,7 +22,7 @@ export async function indexTasks({ name, text }: IndexEvent) {
let mdTree = await parseMarkdown(text); let mdTree = await parseMarkdown(text);
addParentPointers(mdTree); addParentPointers(mdTree);
collectNodesMatching(mdTree, (n) => n.type === "Task").forEach((n) => { collectNodesMatching(mdTree, (n) => n.type === "Task").forEach((n) => {
let task = n.children!.slice(1).map(render).join("").trim(); let task = n.children!.slice(1).map(renderMarkdown).join("").trim();
let complete = n.children![0].children![0].text! !== "[ ]"; let complete = n.children![0].children![0].text! !== "[ ]";
let value: Task = { let value: Task = {
@ -32,7 +32,7 @@ export async function indexTasks({ name, text }: IndexEvent) {
let taskIndex = n.parent!.children!.indexOf(n); let taskIndex = n.parent!.children!.indexOf(n);
let nestedItems = n.parent!.children!.slice(taskIndex + 1); let nestedItems = n.parent!.children!.slice(taskIndex + 1);
if (nestedItems.length > 0) { if (nestedItems.length > 0) {
value.nested = nestedItems.map(render).join("").trim(); value.nested = nestedItems.map(renderMarkdown).join("").trim();
} }
tasks.push({ tasks.push({
key: `task:${n.from}`, key: `task:${n.from}`,
@ -93,8 +93,11 @@ export async function taskToggleAtPos(pos: number) {
return; return;
} }
taskMarkerNode.children![0].text = changeTo; taskMarkerNode.children![0].text = changeTo;
console.log("This will be the new marker", render(taskMarkerNode)); console.log(
text = render(referenceMdTree); "This will be the new marker",
renderMarkdown(taskMarkerNode)
);
text = renderMarkdown(referenceMdTree);
console.log("Updated reference paged text", text); console.log("Updated reference paged text", text);
await writePage(page, text); await writePage(page, text);
} }

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="panel.scss" rel="stylesheet" />
<base target="_top">
<script type="module">
window.addEventListener("message", (message) => {
const data = message.data;
switch(data.type) {
case "html":
document.body.innerHTML = data.html;
break;
}
})
</script>
</head>
<body>
Send me HTML
</body>
</html>

View File

@ -0,0 +1,4 @@
body {
background: white;
font-family: "Menlo";
}

View File

@ -1,12 +1,32 @@
import { useRef } from "react"; import {useEffect, useRef} from "react";
// @ts-ignore
import iframeHtml from "bundle-text:./panel.html";
import {Simulate} from "react-dom/test-utils";
export function Panel({ html }: { html: string }) { export function Panel({ html }: { html: string }) {
const iFrameRef = useRef<HTMLIFrameElement>(null); const iFrameRef = useRef<HTMLIFrameElement>(null);
// @ts-ignore useEffect(() => {
window.iframeRef = iFrameRef; function loadContent() {
if (iFrameRef.current?.contentWindow) {
iFrameRef.current.contentWindow.postMessage({
type: "html",
html: html,
});
}
}
if (!iFrameRef.current) {
return;
}
let iframe = iFrameRef.current;
iframe.onload = loadContent;
loadContent();
return () => {
iframe.onload = null;
};
}, [html]);
return ( return (
<div className="panel"> <div className="panel">
<iframe srcDoc={html} ref={iFrameRef} /> <iframe srcDoc={iframeHtml} ref={iFrameRef} />
</div> </div>
); );
} }

View File

@ -1,4 +1,4 @@
import { EditorView } from "@codemirror/view"; import {EditorView} from "@codemirror/view";
import * as util from "../util"; import * as util from "../util";
export function StatusBar({ editorView }: { editorView?: EditorView }) { export function StatusBar({ editorView }: { editorView?: EditorView }) {
@ -11,7 +11,9 @@ export function StatusBar({ editorView }: { editorView?: EditorView }) {
} }
return ( return (
<div id="bottom"> <div id="bottom">
{wordCount} words | {readingTime} min <div className="inner">
{wordCount} words | {readingTime} min
</div>
</div> </div>
); );
} }

View File

@ -6,18 +6,18 @@ import {bracketMatching} from "@codemirror/matchbrackets";
import {searchKeymap} from "@codemirror/search"; import {searchKeymap} from "@codemirror/search";
import {EditorSelection, EditorState} from "@codemirror/state"; import {EditorSelection, EditorState} from "@codemirror/state";
import { import {
drawSelection, drawSelection,
dropCursor, dropCursor,
EditorView, EditorView,
highlightSpecialChars, highlightSpecialChars,
KeyBinding, KeyBinding,
keymap, keymap,
ViewPlugin, ViewPlugin,
ViewUpdate, ViewUpdate,
} from "@codemirror/view"; } from "@codemirror/view";
import React, {useEffect, useReducer} from "react"; import React, {useEffect, useReducer} from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import {createSandbox as createIFrameSandbox} from "../plugos/environments/iframe_sandbox"; import {createSandbox as createIFrameSandbox} from "../plugos/environments/webworker_sandbox";
import {AppEvent, AppEventDispatcher, ClickEvent} from "./app_event"; import {AppEvent, AppEventDispatcher, ClickEvent} from "./app_event";
import * as commands from "./commands"; import * as commands from "./commands";
import {CommandPalette} from "./components/command_palette"; import {CommandPalette} from "./components/command_palette";
@ -46,6 +46,8 @@ import {SlashCommandHook} from "./hooks/slash_command";
import {CompleterHook} from "./hooks/completer"; import {CompleterHook} from "./hooks/completer";
import {pasteLinkExtension} from "./editor_paste"; import {pasteLinkExtension} from "./editor_paste";
import {markdownSyscalls} from "../common/syscalls/markdown"; import {markdownSyscalls} from "../common/syscalls/markdown";
import {clientStoreSyscalls} from "./syscalls/clientStore";
import {StatusBar} from "./components/status_bar";
class PageState { class PageState {
scrollTop: number; scrollTop: number;
@ -71,6 +73,9 @@ export class Editor implements AppEventDispatcher {
pageNavigator: PathPageNavigator; pageNavigator: PathPageNavigator;
eventHook: EventHook; eventHook: EventHook;
saveTimeout: any; saveTimeout: any;
debouncedUpdateEvent = throttle(() => {
this.eventHook.dispatchEvent("editor:updated");
}, 1000);
private system = new System<SilverBulletHooks>("client"); private system = new System<SilverBulletHooks>("client");
constructor(space: Space, parent: Element) { constructor(space: Space, parent: Element) {
@ -114,6 +119,7 @@ export class Editor implements AppEventDispatcher {
this.system.registerSyscalls([], indexerSyscalls(this.space)); this.system.registerSyscalls([], indexerSyscalls(this.space));
this.system.registerSyscalls([], systemSyscalls(this.space)); this.system.registerSyscalls([], systemSyscalls(this.space));
this.system.registerSyscalls([], markdownSyscalls()); this.system.registerSyscalls([], markdownSyscalls());
this.system.registerSyscalls([], clientStoreSyscalls());
} }
get currentPage(): string | undefined { get currentPage(): string | undefined {
@ -357,6 +363,7 @@ export class Editor implements AppEventDispatcher {
update(update: ViewUpdate): void { update(update: ViewUpdate): void {
if (update.docChanged) { if (update.docChanged) {
editor.viewDispatch({ type: "page-changed" }); editor.viewDispatch({ type: "page-changed" });
editor.debouncedUpdateEvent();
editor.save(); editor.save();
} }
} }
@ -438,6 +445,8 @@ export class Editor implements AppEventDispatcher {
type: "page-loaded", type: "page-loaded",
name: pageName, name: pageName,
}); });
await this.eventHook.dispatchEvent("editor:pageSwitched");
} }
ViewComponent(): React.ReactElement { ViewComponent(): React.ReactElement {
@ -494,6 +503,7 @@ export class Editor implements AppEventDispatcher {
}} }}
/> />
<div id="editor" /> <div id="editor" />
<StatusBar editorView={editor.editorView} />
</div> </div>
); );
} }

View File

@ -1,4 +1,4 @@
import { safeRun } from "./util"; import {safeRun} from "./util";
function encodePageUrl(name: string): string { function encodePageUrl(name: string): string {
return name.replaceAll(" ", "_"); return name.replaceAll(" ", "_");
@ -33,7 +33,10 @@ export class PathPageNavigator {
return; return;
} }
safeRun(async () => { safeRun(async () => {
await pageLoadCallback(this.getCurrentPage(), event && event.state.pos); await pageLoadCallback(
this.getCurrentPage(),
event?.state && event.state.pos
);
if (this.navigationResolve) { if (this.navigationResolve) {
this.navigationResolve(); this.navigationResolve();
} }

View File

@ -0,0 +1,3 @@
$max-editor-width: 800px;
$top-bar-height: 55px;
$bottom-bar-height: 30px;

View File

@ -1,3 +1,9 @@
@import "constants.scss";
div.rhs-open #editor .cm-editor .cm-content {
max-width: 550px;
}
.cm-editor { .cm-editor {
width: 100%; width: 100%;
height: 100%; height: 100%;
@ -6,7 +12,7 @@
.cm-content { .cm-content {
font-family: var(--editor-font); font-family: var(--editor-font);
margin: auto; margin: auto;
max-width: 800px; max-width: $max-editor-width;
} }
.other-cursor { .other-cursor {

View File

@ -1,5 +1,7 @@
@use "editor.scss"; @use "editor.scss";
@use "filter_box.scss"; @use "filter_box.scss";
@import "constants";
:root { :root {
--ident: 18px; --ident: 18px;
@ -19,25 +21,24 @@ body {
.panel { .panel {
position: absolute; position: absolute;
top: 55px; top: $top-bar-height + 1px;
bottom: 0; bottom: $bottom-bar-height;
right: 0; right: 0;
width: 400px; width: 50%;
z-index: 20; z-index: 20;
background: #efefef;
iframe { iframe {
border: 0; border: 0;
width: 100%; width: 100%;
height: 100%; height: 100%;
padding: 10px; padding: 0px;
scroll: auto; margin: 0;
} }
} }
#top { #top {
height: 55px; height: $top-bar-height;
position: absolute; position: fixed;
top: 0; top: 0;
left: 0; left: 0;
right: 0; right: 0;
@ -47,7 +48,7 @@ body {
.inner { .inner {
padding-top: 12px; padding-top: 12px;
max-width: 800px; max-width: $max-editor-width;
font-size: 28px; font-size: 28px;
margin: auto; margin: auto;
@ -82,19 +83,40 @@ body {
#editor { #editor {
position: absolute; position: absolute;
top: 55px; top: $top-bar-height;
bottom: 50px; bottom: $bottom-bar-height;
left: 0; left: 0;
right: 0; right: 0;
overflow-y: hidden; overflow-y: hidden;
} }
div.rhs-open #editor { div.rhs-open #editor {
right: 350px; right: 50%;
} }
@media only screen and (max-width: 800px) { div.rhs-open #top .inner {
max-width: 550px;
padding-right: 50%;
}
@media only screen and (max-width: $max-editor-width) {
.cm-editor .cm-content { .cm-editor .cm-content {
margin: 0 10px !important; margin: 0 10px !important;
} }
} }
#bottom {
height: $bottom-bar-height;
background-color: #ccc;
position: fixed;
text-align: right;
bottom: 0;
left: 0;
right: 0;
background-color: rgb(213, 213, 213);
border-top: rgb(193, 193, 193) 1px solid;
.inner {
padding: 5px;
}
}

View File

@ -0,0 +1,13 @@
import {transportSyscalls} from "../../plugos/syscalls/transport";
import {SysCallMapping} from "../../plugos/system";
import {storeSyscalls} from "../../plugos/syscalls/store.dexie_browser";
export function clientStoreSyscalls(): SysCallMapping {
const storeCalls = storeSyscalls("local", "localData");
return transportSyscalls(
["clientStore.get", "clientStore.set", "clientStore.delete"],
(ctx, name, ...args) => {
return storeCalls[name.replace("clientStore.", "store.")](ctx, ...args);
}
);
}

View File

@ -1,5 +1,4 @@
import {Editor} from "../editor"; import {Editor} from "../editor";
import {syntaxTree} from "@codemirror/language";
import {Transaction} from "@codemirror/state"; import {Transaction} from "@codemirror/state";
import {SysCallMapping} from "../../plugos/system"; import {SysCallMapping} from "../../plugos/system";
@ -58,6 +57,11 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
html: html, html: html,
}); });
}, },
"editor.hideRhs": (ctx, html: string) => {
editor.viewDispatch({
type: "hide-rhs",
});
},
"editor.insertAtPos": (ctx, text: string, pos: number) => { "editor.insertAtPos": (ctx, text: string, pos: number) => {
editor.editorView!.dispatch({ editor.editorView!.dispatch({
changes: { changes: {
@ -95,27 +99,7 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
}, },
}); });
}, },
"editor.getSyntaxNodeUnderCursor": (): SyntaxNode | undefined => {
const editorState = editor.editorView!.state;
let selection = editorState.selection.main;
if (selection.empty) {
let node = syntaxTree(editorState).resolveInner(selection.from);
if (node) {
return {
name: node.name,
text: editorState.sliceDoc(node.from, node.to),
from: node.from,
to: node.to,
};
}
}
},
"editor.getLineUnderCursor": (): string => {
const editorState = editor.editorView!.state;
let selection = editorState.selection.main;
let line = editorState.doc.lineAt(selection.from);
return editorState.sliceDoc(line.from, line.to);
},
"editor.matchBefore": ( "editor.matchBefore": (
ctx, ctx,
regexp: string regexp: string
@ -135,18 +119,6 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
} }
return null; return null;
}, },
"editor.getSyntaxNodeAtPos": (ctx, pos: number): SyntaxNode | undefined => {
const editorState = editor.editorView!.state;
let node = syntaxTree(editorState).resolveInner(pos);
if (node) {
return {
name: node.name,
text: editorState.sliceDoc(node.from, node.to),
from: node.from,
to: node.to,
};
}
},
"editor.dispatch": (ctx, change: Transaction) => { "editor.dispatch": (ctx, change: Transaction) => {
editor.editorView!.dispatch(change); editor.editorView!.dispatch(change);
}, },