1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-12-11 19:57:23 +03:00

Lexical API: Added public event to access editor API

Updated documentation to match.
Ran manual testing of examples.
This commit is contained in:
Dan Brown
2025-12-05 14:07:24 +00:00
parent dfdcfcfdb8
commit 8890746278
5 changed files with 62 additions and 15 deletions

View File

@@ -161,3 +161,7 @@ window.$components.firstOnElement(element, name);
There are a range of available events that are emitted as part of a public & supported API for accessing or extending JavaScript libraries & components used in the system. There are a range of available events that are emitted as part of a public & supported API for accessing or extending JavaScript libraries & components used in the system.
Details on these events can be found in the [JavaScript Public Events file](javascript-public-events.md). Details on these events can be found in the [JavaScript Public Events file](javascript-public-events.md).
## WYSIWYG Editor API
Details on the API for our custom-built WYSIWYG editor can be found in the [WYSIWYG JavaScript API file](./wysiwyg-js-api.md).

View File

@@ -60,7 +60,7 @@ This event is called when the markdown editor loads, post configuration but befo
#### Event Data #### Event Data
- `markdownIt` - A references to the [MarkdownIt](https://markdown-it.github.io/markdown-it/#MarkdownIt) instance used to render markdown to HTML (Just for the preview). - `markdownIt` - A reference to the [MarkdownIt](https://markdown-it.github.io/markdown-it/#MarkdownIt) instance used to render markdown to HTML (Just for the preview).
- `displayEl` - The IFrame Element that wraps the HTML preview display. - `displayEl` - The IFrame Element that wraps the HTML preview display.
- `cmEditorView` - The CodeMirror [EditorView](https://codemirror.net/docs/ref/#view.EditorView) instance used for the markdown input editor. - `cmEditorView` - The CodeMirror [EditorView](https://codemirror.net/docs/ref/#view.EditorView) instance used for the markdown input editor.
@@ -301,7 +301,7 @@ This event is called just after any CodeMirror instances are initialised so that
##### Example ##### Example
The below shows how you'd prepend some default text to all content (page) code blocks. The below example shows how you'd prepend some default text to all content (page) code blocks.
<details> <details>
<summary>Show Example</summary> <summary>Show Example</summary>
@@ -319,3 +319,41 @@ window.addEventListener('library-cm6::post-init', event => {
}); });
``` ```
</details> </details>
### `editor-wysiwyg::post-init`
This is called after the (new custom-built Lexical-based) WYSIWYG editor has been initialised.
#### Event Data
- `usage` - A string label to identify the usage type of the WYSIWYG editor in BookStack.
- `api` - An instance to the WYSIWYG editor API, as documented in the [WYSIWYG JavaScript API file](./wysiwyg-js-api.md).
##### Example
The below shows how you'd use this API to create a button, with that button added to the toolbar of the page editor, which inserts bold hello text on press:
<details>
<summary>Show Example</summary>
```javascript
window.addEventListener('editor-wysiwyg::post-init', event => {
const {usage, api} = event.detail;
// Check that it's the page editor being loaded.
if (usage !== 'page-editor') {
return;
}
// Create a custom button which inserts bold hello text on press.
const button = api.ui.createButton({
label: 'Greet',
action: () => {
api.content.insertHtml(`<strong>Hello!</strong>`);
}
});
// Add the button to the start of the first section within the main toolbar.
api.ui.getMainToolbarSections()[0]?.addButton(button, 0);
});
```
</details>

View File

@@ -1,6 +1,5 @@
# WYSIWYG JavaScript API # WYSIWYG JavaScript API
TODO - Link to this from JS code doc.
TODO - Create JS events and add to the js public events doc. TODO - Create JS events and add to the js public events doc.
**Warning: This API is currently in development and may change without notice.** **Warning: This API is currently in development and may change without notice.**

View File

@@ -95,11 +95,10 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
registerCommonNodeMutationListeners(context); registerCommonNodeMutationListeners(context);
// TODO - Emit this as a public event instead window.$events.emitPublic(container, 'editor-wysiwyg::post-init', {
// TODO - Add support to basic editor below usage: 'page-editor',
const api = new EditorApi(context); api: new EditorApi(context),
// @ts-ignore });
window.editorApi = api;
return new SimpleWysiwygEditorInterface(context); return new SimpleWysiwygEditorInterface(context);
} }
@@ -129,6 +128,11 @@ export function createBasicEditorInstance(container: HTMLElement, htmlContent: s
setEditorContentFromHtml(editor, htmlContent); setEditorContentFromHtml(editor, htmlContent);
window.$events.emitPublic(container, 'editor-wysiwyg::post-init', {
usage: 'description-editor',
api: new EditorApi(context),
});
return new SimpleWysiwygEditorInterface(context); return new SimpleWysiwygEditorInterface(context);
} }

View File

@@ -223,11 +223,13 @@ export function getMainEditorFullToolbar(context: EditorUiContext): EditorContai
export function getBasicEditorToolbar(context: EditorUiContext): EditorContainerUiElement { export function getBasicEditorToolbar(context: EditorUiContext): EditorContainerUiElement {
return new EditorSimpleClassContainer('editor-toolbar-main', [ return new EditorSimpleClassContainer('editor-toolbar-main', [
new EditorOverflowContainer('formats', 7, [
new EditorButton(bold), new EditorButton(bold),
new EditorButton(italic), new EditorButton(italic),
new EditorButton(link), new EditorButton(link),
new EditorButton(bulletList), new EditorButton(bulletList),
new EditorButton(numberList), new EditorButton(numberList),
])
]); ]);
} }