1
0
silverbullet/web/editor_ui.tsx

309 lines
10 KiB
TypeScript
Raw Normal View History

2023-07-14 12:22:26 +00:00
import { isMacLike, safeRun } from "../common/util.ts";
import { Confirm, Prompt } from "./components/basic_modals.tsx";
import { CommandPalette } from "./components/command_palette.tsx";
import { FilterList } from "./components/filter.tsx";
import { PageNavigator } from "./components/page_navigator.tsx";
import { TopBar } from "./components/top_bar.tsx";
import reducer from "./reducer.ts";
import { Action, AppViewState, initialViewState } from "./types.ts";
import {
BookIcon,
HomeIcon,
preactRender,
2023-08-29 19:17:29 +00:00
RefreshCwIcon,
2023-07-14 14:48:35 +00:00
runScopeHandlers,
2023-07-14 12:22:26 +00:00
TerminalIcon,
useEffect,
useReducer,
} from "./deps.ts";
2023-07-14 14:56:20 +00:00
import type { Client } from "./client.ts";
2023-07-14 12:22:26 +00:00
import { Panel } from "./components/panel.tsx";
import { h } from "./deps.ts";
2023-09-04 18:02:35 +00:00
import { sleep } from "$sb/lib/async.ts";
2023-07-14 12:22:26 +00:00
export class MainUI {
viewState: AppViewState = initialViewState;
viewDispatch: (action: Action) => void = () => {};
constructor(private client: Client) {
2023-07-14 14:48:35 +00:00
// Make keyboard shortcuts work even when the editor is in read only mode or not focused
globalThis.addEventListener("keydown", (ev) => {
if (!client.editorView.hasFocus) {
2023-07-14 14:48:35 +00:00
if ((ev.target as any).closest(".cm-editor")) {
// In some cm element, let's back out
return;
}
if (runScopeHandlers(client.editorView, ev, "editor")) {
2023-07-14 14:48:35 +00:00
ev.preventDefault();
}
}
});
globalThis.addEventListener("touchstart", (ev) => {
// Launch the page picker on a two-finger tap
if (ev.touches.length === 2) {
ev.stopPropagation();
ev.preventDefault();
this.viewDispatch({ type: "start-navigate" });
}
// Launch the command palette using a three-finger tap
if (ev.touches.length === 3) {
ev.stopPropagation();
ev.preventDefault();
this.viewDispatch({
type: "show-palette",
context: client.getContext(),
2023-07-14 14:48:35 +00:00
});
}
});
2023-07-14 12:22:26 +00:00
}
ViewComponent() {
const [viewState, dispatch] = useReducer(reducer, initialViewState);
this.viewState = viewState;
this.viewDispatch = dispatch;
const editor = this.client;
2023-07-14 12:22:26 +00:00
useEffect(() => {
if (viewState.currentPage) {
document.title = viewState.currentPage;
}
}, [viewState.currentPage]);
useEffect(() => {
2023-07-27 09:41:44 +00:00
editor.tweakEditorDOM(
editor.editorView.contentDOM,
);
2023-07-14 12:22:26 +00:00
}, [viewState.uiOptions.forcedROMode]);
useEffect(() => {
this.client.rebuildEditorState();
this.client.dispatchAppEvent("editor:modeswitch");
2023-07-14 12:22:26 +00:00
}, [viewState.uiOptions.vimMode]);
useEffect(() => {
document.documentElement.dataset.theme = viewState.uiOptions.darkMode
? "dark"
: "light";
}, [viewState.uiOptions.darkMode]);
useEffect(() => {
// Need to dispatch a resize event so that the top_bar can pick it up
globalThis.dispatchEvent(new Event("resize"));
}, [viewState.panels]);
return (
<>
{viewState.showPageNavigator && (
<PageNavigator
allPages={viewState.allPages}
currentPage={editor.currentPage}
completer={editor.miniEditorComplete.bind(editor)}
vimMode={viewState.uiOptions.vimMode}
darkMode={viewState.uiOptions.darkMode}
onNavigate={(page) => {
dispatch({ type: "stop-navigate" });
setTimeout(() => {
editor.focus();
});
if (page) {
safeRun(async () => {
await editor.navigate(page);
});
}
}}
/>
)}
{viewState.showCommandPalette && (
<CommandPalette
onTrigger={(cmd) => {
dispatch({ type: "hide-palette" });
setTimeout(() => {
editor.focus();
});
if (cmd) {
dispatch({ type: "command-run", command: cmd.command.name });
cmd
.run()
.catch((e: any) => {
console.error("Error running command", e.message);
})
.then(() => {
// Always be focusing the editor after running a command
editor.focus();
});
}
}}
commands={editor.getCommandsByContext(viewState)}
vimMode={viewState.uiOptions.vimMode}
darkMode={viewState.uiOptions.darkMode}
completer={editor.miniEditorComplete.bind(editor)}
recentCommands={viewState.recentCommands}
/>
)}
{viewState.showFilterBox && (
<FilterList
label={viewState.filterBoxLabel}
placeholder={viewState.filterBoxPlaceHolder}
options={viewState.filterBoxOptions}
vimMode={viewState.uiOptions.vimMode}
darkMode={viewState.uiOptions.darkMode}
allowNew={false}
completer={editor.miniEditorComplete.bind(editor)}
helpText={viewState.filterBoxHelpText}
onSelect={viewState.filterBoxOnSelect}
/>
)}
{viewState.showPrompt && (
<Prompt
message={viewState.promptMessage!}
defaultValue={viewState.promptDefaultValue}
vimMode={viewState.uiOptions.vimMode}
darkMode={viewState.uiOptions.darkMode}
completer={editor.miniEditorComplete.bind(editor)}
callback={(value) => {
dispatch({ type: "hide-prompt" });
viewState.promptCallback!(value);
}}
/>
)}
{viewState.showConfirm && (
<Confirm
message={viewState.confirmMessage!}
callback={(value) => {
dispatch({ type: "hide-confirm" });
viewState.confirmCallback!(value);
}}
/>
)}
<TopBar
pageName={viewState.currentPage}
notifications={viewState.notifications}
2023-08-16 09:40:31 +00:00
syncFailures={viewState.syncFailures}
2023-07-14 12:22:26 +00:00
unsavedChanges={viewState.unsavedChanges}
isLoading={viewState.isLoading}
vimMode={viewState.uiOptions.vimMode}
darkMode={viewState.uiOptions.darkMode}
progressPerc={viewState.progressPerc}
completer={editor.miniEditorComplete.bind(editor)}
onRename={async (newName) => {
if (!newName) {
// Always move cursor to the start of the page
2023-07-27 09:41:44 +00:00
editor.editorView.dispatch({
2023-07-14 12:22:26 +00:00
selection: { anchor: 0 },
});
editor.focus();
return;
}
console.log("Now renaming page to...", newName);
2023-08-28 15:12:15 +00:00
await editor.system.system.loadedPlugs.get("index")!.invoke(
"renamePageCommand",
2023-07-14 12:22:26 +00:00
[{ page: newName }],
);
editor.focus();
}}
actionButtons={[
2023-08-30 15:25:54 +00:00
...!window.silverBulletConfig.syncOnly
// If we support syncOnly, don't show this toggle button
2023-08-29 19:17:29 +00:00
? [{
icon: RefreshCwIcon,
description: this.client.syncMode
? "Currently in Sync mode, click to switch to Online mode"
: "Currently in Online mode, click to switch to Sync mode",
class: this.client.syncMode ? "sb-enabled" : undefined,
2023-08-29 19:17:29 +00:00
callback: () => {
(async () => {
const newValue = !this.client.syncMode;
2023-08-29 19:17:29 +00:00
if (newValue) {
2023-09-04 18:02:35 +00:00
localStorage.setItem("syncMode", "true");
this.client.flashNotification(
2023-09-04 18:02:35 +00:00
"Now switching to sync mode, one moment please...",
);
await sleep(1000);
location.reload();
2023-08-29 19:17:29 +00:00
} else {
2023-09-04 18:02:35 +00:00
localStorage.removeItem("syncMode");
this.client.flashNotification(
2023-09-07 10:52:40 +00:00
"Now switching to online mode, one moment please...",
2023-09-04 18:02:35 +00:00
);
await sleep(1000);
location.reload();
2023-08-29 19:17:29 +00:00
}
})().catch(console.error);
},
}]
: [],
2023-07-14 12:22:26 +00:00
{
icon: HomeIcon,
description: `Go home (Alt-h)`,
callback: () => {
editor.navigate("");
},
href: "",
},
{
icon: BookIcon,
description: `Open page (${isMacLike() ? "Cmd-k" : "Ctrl-k"})`,
callback: () => {
dispatch({ type: "start-navigate" });
editor.space.updatePageList();
},
},
{
icon: TerminalIcon,
description: `Run command (${isMacLike() ? "Cmd-/" : "Ctrl-/"})`,
callback: () => {
dispatch({
type: "show-palette",
context: editor.getContext(),
});
},
},
]}
rhs={!!viewState.panels.rhs.mode && (
<div
className="panel"
style={{ flex: viewState.panels.rhs.mode }}
/>
)}
lhs={!!viewState.panels.lhs.mode && (
<div
className="panel"
style={{ flex: viewState.panels.lhs.mode }}
/>
)}
/>
<div id="sb-main">
{!!viewState.panels.lhs.mode && (
<Panel config={viewState.panels.lhs} editor={editor} />
)}
<div id="sb-editor" />
{!!viewState.panels.rhs.mode && (
<Panel config={viewState.panels.rhs} editor={editor} />
)}
</div>
{!!viewState.panels.modal.mode && (
<div
className="sb-modal"
style={{ inset: `${viewState.panels.modal.mode}px` }}
>
<Panel config={viewState.panels.modal} editor={editor} />
</div>
)}
{!!viewState.panels.bhs.mode && (
<div className="sb-bhs">
<Panel config={viewState.panels.bhs} editor={editor} />
</div>
)}
</>
);
}
render(container: Element) {
// const ViewComponent = this.ui.ViewComponent.bind(this.ui);
preactRender(h(this.ViewComponent.bind(this), {}), container);
}
}