Fixes #1730 Introduces editor:fold and editor:unfold event, also

documents some other events while at it.
This commit is contained in:
Zef Hemel
2026-01-08 09:30:40 +01:00
parent cd1fdca1e0
commit 757896d502
4 changed files with 48 additions and 21 deletions
+14
View File
@@ -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 (
+3 -2
View File
@@ -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"
+1 -16
View File
@@ -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.
+30 -3
View File
@@ -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 _
]]}
]]}