diff --git a/client/codemirror/editor_state.ts b/client/codemirror/editor_state.ts index aa6e8fa9..2262862e 100644 --- a/client/codemirror/editor_state.ts +++ b/client/codemirror/editor_state.ts @@ -15,11 +15,13 @@ import { } from "@codemirror/autocomplete"; import { codeFolding, + foldEffect, indentOnInput, indentUnit, LanguageDescription, LanguageSupport, syntaxHighlighting, + unfoldEffect, } from "@codemirror/language"; import { Compartment, EditorState, type Extension } from "@codemirror/state"; import { @@ -274,6 +276,18 @@ export function createEditorState( ViewPlugin.fromClass( class { update(update: ViewUpdate): void { + if (update.transactions.length > 0) { + for (const tr of update.transactions) { + for (const e of tr.effects) { + if (e.is(foldEffect)) { + client.dispatchAppEvent("editor:fold", e.value); + } + if (e.is(unfoldEffect)) { + client.dispatchAppEvent("editor:unfold", e.value); + } + } + } + } if (update.docChanged) { // Find if there's a history isolate in the transaction, if so it came from a local reload and we don't do anything if ( diff --git a/plug-api/types/client.ts b/plug-api/types/client.ts index 1838f373..c9d870ff 100644 --- a/plug-api/types/client.ts +++ b/plug-api/types/client.ts @@ -49,11 +49,10 @@ export type UploadFile = { export type AppEvent = | "page:click" - | "editor:complete" | "minieditor:complete" | "slash:complete" + | "editor:complete" | "editor:lint" - | "page:load" | "editor:init" | "editor:pageLoaded" // args: pageName, previousPage, isSynced | "editor:pageReloaded" @@ -64,6 +63,8 @@ export type AppEvent = | "editor:documentSaving" | "editor:documentSaved" | "editor:modeswitch" + | "editor:fold" + | "editor:unfold" | "plugs:loaded" | "cron:secondPassed" | "hooks:renderTopWidgets" diff --git a/website/API/event.md b/website/API/event.md index 028f025f..5bd3e5b7 100644 --- a/website/API/event.md +++ b/website/API/event.md @@ -2,22 +2,7 @@ The Event API provides functions for working with SilverBullet's event bus system, allowing communication between different parts of the application. -## Notable events to listen to: - -* `page:click` - Fired when a user clicks somehwere in the page. -* `editor:complete` - Fired when the editor requests code completion. -* `editor:pageSaving` - Fired when the editor is saving a page. -* `editor:pageSaved` - Fired when the editor has saved a page. -* `editor:pageCreating` - Fired when the editor is creating a new page. -* `editor:pageModified` - Fired when the editor modifies a page. -* `editor:documentSaving` - Fired when the editor is saving a document. -* `editor:documentSaved` - Fired when the editor has saved a document. -* `cron:secondPassed` - Fired every second. -* `hooks:renderTopWidgets` - Fired when rendering top widgets. -* `hooks:renderBottomWidgets` - Fired when rendering bottom widgets. -* `system:ready` - Fired when all plugs are loaded. - -## Event Operations +# API ### event.listen(listenerDef) Register an event listener. diff --git a/website/Event.md b/website/Event.md index be475c16..d4d5e4bc 100644 --- a/website/Event.md +++ b/website/Event.md @@ -2,10 +2,37 @@ SilverBullet has its own event bus. Events can be subscribed to and dispatched via the [[API/event]] API. Other features, such as [[Service]] are built on top of Events. -# Available events -Here’s a dynamically generated list of events to give a sense of what’s used: +# Built-in events +Here is a list of built-in events (triggered by SilverBullet’s core). +To use them, simply subscribe to them via [[API/event#event.listen(listenerDef)]], and do a `print` to see what data you receive. Alternatively, grep the code base to see exactly where they are triggered. + +* `page:click`: user clicks a location on the page +* `page:index`: page has changed and requested to be indexed (used by [[Object]] indexing) +* `minieditor:complete`: auto complete triggered in the mini editor (mini editor is used in dialogs and the top bar) +* `editor:complete`: editor completion triggered, returns completion results +* `slash:complete`: slash completion triggered, returns completion results +* `editor:lint`: request to lint, returns errors +* `editor:init`: editor initialized +* `editor:pageLoaded`: page has loaded in the editor +* `editor:pageReloaded`: page reloaded in the editor (e.g. when it was changed on disk) +* `editor:pageSaving`: page is about to save +* `editor:pageSaved`: page has saved +* `editor:pageCreating`: page is creating (can return a page content object) +* `editor:pageModified`: a change was made to the document (real-time) +* `editor:documentSaving`: a document is about to be saved +* `editor:documentSaved`: a document was saved +* `editor:modeswitch`: triggered between vim mode and non-vim mode +* `editor:fold`: code was folded in the editor +* `editor:unfold`: code was unfolded in the editor +* `plugs:loaded`: plugs were loaded +* `cron:secondPassed`: a second has passed (useful to implement cron-like features) +* `hooks:renderTopWidgets`: top widgets have requested to render (return widgets) +* `hooks:renderBottomWidgets`: bottom widgets have requested to render (return widgets) + +# All subscribed events +Here’s a dynamically generated list of events that this SilverBullet instance has subscribed to, to give a sense of what’s there: ${query[[ from event.listEvents() where not _:startsWith("service:") order by _ -]]} \ No newline at end of file +]]}