Skip to content

Commit 8890746

Browse files
committed
Lexical API: Added public event to access editor API
Updated documentation to match. Ran manual testing of examples.
1 parent dfdcfcf commit 8890746

File tree

5 files changed

+62
-15
lines changed

5 files changed

+62
-15
lines changed

dev/docs/javascript-code.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,7 @@ window.$components.firstOnElement(element, name);
161161
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.
162162

163163
Details on these events can be found in the [JavaScript Public Events file](javascript-public-events.md).
164+
165+
## WYSIWYG Editor API
166+
167+
Details on the API for our custom-built WYSIWYG editor can be found in the [WYSIWYG JavaScript API file](./wysiwyg-js-api.md).

dev/docs/javascript-public-events.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ This event is called when the markdown editor loads, post configuration but befo
6060

6161
#### Event Data
6262

63-
- `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).
63+
- `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).
6464
- `displayEl` - The IFrame Element that wraps the HTML preview display.
6565
- `cmEditorView` - The CodeMirror [EditorView](https://codemirror.net/docs/ref/#view.EditorView) instance used for the markdown input editor.
6666

@@ -79,7 +79,7 @@ window.addEventListener('editor-markdown::setup', event => {
7979
This event is called as the embedded diagrams.net drawing editor loads, to allow configuration of the diagrams.net interface.
8080
See [this diagrams.net page](https://www.diagrams.net/doc/faq/configure-diagram-editor) for details on the available options for the configure event.
8181

82-
If using a custom diagrams.net instance, via the `DRAWIO` option, you will need to ensure your DRAWIO option URL has the `configure=1` query parameter.
82+
If using a custom diagrams.net instance, via the `DRAWIO` option, you will need to ensure your DRAWIO option URL has the `configure=1` query parameter.
8383

8484
#### Event Data
8585

@@ -142,7 +142,7 @@ This event is called whenever a CodeMirror instance is loaded, as a method to co
142142

143143
- `darkModeActive` - A boolean to indicate if the current view/page is being loaded with dark mode active.
144144
- `registerViewTheme(builder)` - A method that can be called to register a new view (CodeMirror UI) theme.
145-
- `builder` - A function that will return an object that will be passed into the CodeMirror [EditorView.theme()](https://codemirror.net/docs/ref/#view.EditorView^theme) function as a StyleSpec.
145+
- `builder` - A function that will return an object that will be passed into the CodeMirror [EditorView.theme()](https://codemirror.net/docs/ref/#view.EditorView^theme) function as a StyleSpec.
146146
- `registerHighlightStyle(builder)` - A method that can be called to register a new HighlightStyle (code highlighting) theme.
147147
- `builder` - A function, that receives a reference to [Tag.tags](https://lezer.codemirror.net/docs/ref/#highlight.tags) and returns an array of [TagStyle](https://codemirror.net/docs/ref/#language.TagStyle) objects.
148148

@@ -301,7 +301,7 @@ This event is called just after any CodeMirror instances are initialised so that
301301

302302
##### Example
303303

304-
The below shows how you'd prepend some default text to all content (page) code blocks.
304+
The below example shows how you'd prepend some default text to all content (page) code blocks.
305305

306306
<details>
307307
<summary>Show Example</summary>
@@ -318,4 +318,42 @@ window.addEventListener('library-cm6::post-init', event => {
318318
}
319319
});
320320
```
321+
</details>
322+
323+
### `editor-wysiwyg::post-init`
324+
325+
This is called after the (new custom-built Lexical-based) WYSIWYG editor has been initialised.
326+
327+
#### Event Data
328+
329+
- `usage` - A string label to identify the usage type of the WYSIWYG editor in BookStack.
330+
- `api` - An instance to the WYSIWYG editor API, as documented in the [WYSIWYG JavaScript API file](./wysiwyg-js-api.md).
331+
332+
##### Example
333+
334+
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:
335+
336+
<details>
337+
<summary>Show Example</summary>
338+
339+
```javascript
340+
window.addEventListener('editor-wysiwyg::post-init', event => {
341+
const {usage, api} = event.detail;
342+
// Check that it's the page editor being loaded.
343+
if (usage !== 'page-editor') {
344+
return;
345+
}
346+
347+
// Create a custom button which inserts bold hello text on press.
348+
const button = api.ui.createButton({
349+
label: 'Greet',
350+
action: () => {
351+
api.content.insertHtml(`<strong>Hello!</strong>`);
352+
}
353+
});
354+
355+
// Add the button to the start of the first section within the main toolbar.
356+
api.ui.getMainToolbarSections()[0]?.addButton(button, 0);
357+
});
358+
```
321359
</details>

dev/docs/wysiwyg-js-api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# WYSIWYG JavaScript API
22

3-
TODO - Link to this from JS code doc.
43
TODO - Create JS events and add to the js public events doc.
54

65
**Warning: This API is currently in development and may change without notice.**

resources/js/wysiwyg/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,10 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
9595

9696
registerCommonNodeMutationListeners(context);
9797

98-
// TODO - Emit this as a public event instead
99-
// TODO - Add support to basic editor below
100-
const api = new EditorApi(context);
101-
// @ts-ignore
102-
window.editorApi = api;
98+
window.$events.emitPublic(container, 'editor-wysiwyg::post-init', {
99+
usage: 'page-editor',
100+
api: new EditorApi(context),
101+
});
103102

104103
return new SimpleWysiwygEditorInterface(context);
105104
}
@@ -129,6 +128,11 @@ export function createBasicEditorInstance(container: HTMLElement, htmlContent: s
129128

130129
setEditorContentFromHtml(editor, htmlContent);
131130

131+
window.$events.emitPublic(container, 'editor-wysiwyg::post-init', {
132+
usage: 'description-editor',
133+
api: new EditorApi(context),
134+
});
135+
132136
return new SimpleWysiwygEditorInterface(context);
133137
}
134138

resources/js/wysiwyg/ui/defaults/toolbars.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,13 @@ export function getMainEditorFullToolbar(context: EditorUiContext): EditorContai
223223

224224
export function getBasicEditorToolbar(context: EditorUiContext): EditorContainerUiElement {
225225
return new EditorSimpleClassContainer('editor-toolbar-main', [
226-
new EditorButton(bold),
227-
new EditorButton(italic),
228-
new EditorButton(link),
229-
new EditorButton(bulletList),
230-
new EditorButton(numberList),
226+
new EditorOverflowContainer('formats', 7, [
227+
new EditorButton(bold),
228+
new EditorButton(italic),
229+
new EditorButton(link),
230+
new EditorButton(bulletList),
231+
new EditorButton(numberList),
232+
])
231233
]);
232234
}
233235

0 commit comments

Comments
 (0)