From 1b922791f35563b57c776f0a463208395acff7e2 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 14 Jun 2023 19:27:18 +0200 Subject: [PATCH] Implements #51: Folding commands --- common/deps.ts | 8 +++++++- plug-api/silverbullet-syscall/editor.ts | 18 ++++++++++++++++++ plugs/core/core.plug.yaml | 24 ++++++++++++++++++++++++ plugs/core/editor.ts | 16 ++++++++++++++++ web/editor.tsx | 5 +++++ web/styles/editor.scss | 12 ++++++++++++ web/syscalls/editor.ts | 24 +++++++++++++++++++++++- 7 files changed, 105 insertions(+), 2 deletions(-) diff --git a/common/deps.ts b/common/deps.ts index 2a2d9bb0..e76277b7 100644 --- a/common/deps.ts +++ b/common/deps.ts @@ -51,6 +51,7 @@ export { drawSelection, dropCursor, EditorView, + gutter, highlightSpecialChars, keymap, placeholder, @@ -74,11 +75,14 @@ export { } from "@codemirror/state"; export type { ChangeSpec, Extension, StateCommand } from "@codemirror/state"; export { + codeFolding, defaultHighlightStyle, defineLanguageFacet, + foldAll, + foldCode, foldedRanges, + foldGutter, foldInside, - foldNodeProp, HighlightStyle, indentNodeProp, indentOnInput, @@ -90,6 +94,8 @@ export { StreamLanguage, syntaxHighlighting, syntaxTree, + unfoldAll, + unfoldCode, } from "@codemirror/language"; export { yaml as yamlLanguage } from "https://esm.sh/@codemirror/legacy-modes@6.3.2/mode/yaml?external=@codemirror/language"; diff --git a/plug-api/silverbullet-syscall/editor.ts b/plug-api/silverbullet-syscall/editor.ts index fe0613a2..6db188e4 100644 --- a/plug-api/silverbullet-syscall/editor.ts +++ b/plug-api/silverbullet-syscall/editor.ts @@ -128,3 +128,21 @@ export function setUiOption(key: string, value: any): Promise { export function vimEx(exCommand: string): Promise { return syscall("editor.vimEx", exCommand); } + +// Folding + +export function fold() { + return syscall("editor.fold"); +} + +export function unfold() { + return syscall("editor.unfold"); +} + +export function foldAll() { + return syscall("editor.foldAll"); +} + +export function unfoldAll() { + return syscall("editor.unfoldAll"); +} diff --git a/plugs/core/core.plug.yaml b/plugs/core/core.plug.yaml index 27a5ffc2..e287e26b 100644 --- a/plugs/core/core.plug.yaml +++ b/plugs/core/core.plug.yaml @@ -369,6 +369,30 @@ functions: path: ./embed.ts:embedWidget codeWidget: embed + # Folding commands + foldCommand: + path: ./editor.ts:foldCommand + command: + name: "Fold: Fold" + mac: "Cmd-Alt-[" + key: "Ctrl-Shift-[" + unfoldCommand: + path: ./editor.ts:unfoldCommand + command: + name: "Fold: Unfold" + mac: "Cmd-Alt-]" + key: "Ctrl-Shift-]" + foldAllCommand: + path: ./editor.ts:foldAllCommand + command: + name: "Fold: Fold All" + key: "Ctrl-Alt-[" + unfoldAllCommand: + path: ./editor.ts:unfoldAllCommand + command: + name: "Fold: Unfold All" + key: "Ctrl-Alt-]" + # Random stuff statsCommand: path: ./stats.ts:statsCommand diff --git a/plugs/core/editor.ts b/plugs/core/editor.ts index 59d67b58..2317016b 100644 --- a/plugs/core/editor.ts +++ b/plugs/core/editor.ts @@ -17,3 +17,19 @@ export async function toggleDarkMode() { await editor.setUiOption("darkMode", darkMode); await store.set("darkMode", darkMode); } + +export async function foldCommand() { + await editor.fold(); +} + +export async function unfoldCommand() { + await editor.unfold(); +} + +export async function foldAllCommand() { + await editor.foldAll(); +} + +export async function unfoldAllCommand() { + await editor.unfoldAll(); +} diff --git a/web/editor.tsx b/web/editor.tsx index d47d1322..3eddcba3 100644 --- a/web/editor.tsx +++ b/web/editor.tsx @@ -84,6 +84,7 @@ import { Panel } from "./components/panel.tsx"; import { TopBar } from "./components/top_bar.tsx"; import { BookIcon, + codeFolding, HomeIcon, preactRender, TerminalIcon, @@ -879,6 +880,9 @@ export class Editor { history(), drawSelection(), dropCursor(), + codeFolding({ + placeholderText: "…", + }), indentOnInput(), ...cleanModePlugins(this), EditorView.lineWrapping, @@ -914,6 +918,7 @@ export class Editor { run: (): boolean => { this.viewDispatch({ type: "start-navigate" }); this.space.updatePageList(); + return true; }, }, diff --git a/web/styles/editor.scss b/web/styles/editor.scss index e79a8a56..cee95a3e 100644 --- a/web/styles/editor.scss +++ b/web/styles/editor.scss @@ -21,6 +21,18 @@ max-width: 100%; } + // Gutter styling + .cm-gutters { + background-color: transparent; + border-right: none; + + } + + .cm-foldPlaceholder { + background-color: transparent; + border: 0; + } + // Indentation of follow-up lines @mixin lineOverflow($baseIndent, $bulletIndent: 0) { diff --git a/web/syscalls/editor.ts b/web/syscalls/editor.ts index 0adb72f3..8f2e8ef8 100644 --- a/web/syscalls/editor.ts +++ b/web/syscalls/editor.ts @@ -1,5 +1,14 @@ import { Editor } from "../editor.tsx"; -import { EditorView, Transaction, Vim, vimGetCm } from "../deps.ts"; +import { + EditorView, + foldAll, + foldCode, + Transaction, + unfoldAll, + unfoldCode, + Vim, + vimGetCm, +} from "../deps.ts"; import { SysCallMapping } from "../../plugos/system.ts"; import type { FilterOption } from "../types.ts"; @@ -172,6 +181,19 @@ export function editorSyscalls(editor: Editor): SysCallMapping { const cm = vimGetCm(editor.editorView!)!; return Vim.handleEx(cm, exCommand); }, + // Folding + "editor.fold": () => { + foldCode(editor.editorView!); + }, + "editor.unfold": () => { + unfoldCode(editor.editorView!); + }, + "editor.foldAll": () => { + foldAll(editor.editorView!); + }, + "editor.unfoldAll": () => { + unfoldAll(editor.editorView!); + }, }; return syscalls;