Skip to content

Commit 92a3c22

Browse files
authored
Merge pull request #5928 from BookStackApp/lexical_dev_api
Initial developer API for WYSIWYG editor
2 parents 21730ae + b5246a2 commit 92a3c22

File tree

20 files changed

+1212
-634
lines changed

20 files changed

+1212
-634
lines changed

dev/docs/development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
All development on BookStack is currently done on the `development` branch.
44
When it's time for a release the `development` branch is merged into release with built & minified CSS & JS then tagged at its version. Here are the current development requirements:
55

6-
* [Node.js](https://nodejs.org/en/) v20.0+
6+
* [Node.js](https://nodejs.org/en/) v22.0+
77

88
## Building CSS & JavaScript Assets
99

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: 46 additions & 5 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

@@ -134,6 +134,47 @@ window.addEventListener('editor-tinymce::setup', event => {
134134
});
135135
```
136136

137+
### `editor-wysiwyg::post-init`
138+
139+
This is called after the (new custom-built Lexical-based) WYSIWYG editor has been initialised.
140+
141+
#### Event Data
142+
143+
- `usage` - A string label to identify the usage type of the WYSIWYG editor in BookStack.
144+
- `api` - An instance to the WYSIWYG editor API, as documented in the [WYSIWYG JavaScript API file](./wysiwyg-js-api.md).
145+
146+
##### Example
147+
148+
The below example shows how you'd use this API to create a button, with that button added to the main toolbar of the page editor, which inserts bold "Hello!" text on press:
149+
150+
<details>
151+
<summary>Show Example</summary>
152+
153+
```javascript
154+
window.addEventListener('editor-wysiwyg::post-init', event => {
155+
const {usage, api} = event.detail;
156+
// Check that it's the page editor which is being loaded
157+
if (usage !== 'page-editor') {
158+
return;
159+
}
160+
161+
// Create a custom button which inserts bold hello text on press
162+
const button = api.ui.createButton({
163+
label: 'Greet',
164+
action: () => {
165+
api.content.insertHtml(`<strong>Hello!</strong>`);
166+
}
167+
});
168+
169+
// Add the button to the start of the first section within the main toolbar
170+
const toolbar = api.ui.getMainToolbar();
171+
if (toolbar) {
172+
toolbar.getSections()[0]?.addButton(button, 0);
173+
}
174+
});
175+
```
176+
</details>
177+
137178
### `library-cm6::configure-theme`
138179
139180
This event is called whenever a CodeMirror instance is loaded, as a method to configure the theme used by CodeMirror. This applies to all CodeMirror instances including in-page code blocks, editors using in BookStack settings, and the Page markdown editor.
@@ -142,7 +183,7 @@ This event is called whenever a CodeMirror instance is loaded, as a method to co
142183
143184
- `darkModeActive` - A boolean to indicate if the current view/page is being loaded with dark mode active.
144185
- `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.
186+
- `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.
146187
- `registerHighlightStyle(builder)` - A method that can be called to register a new HighlightStyle (code highlighting) theme.
147188
- `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.
148189
@@ -301,7 +342,7 @@ This event is called just after any CodeMirror instances are initialised so that
301342
302343
##### Example
303344
304-
The below shows how you'd prepend some default text to all content (page) code blocks.
345+
The below example shows how you'd prepend some default text to all content (page) code blocks.
305346
306347
<details>
307348
<summary>Show Example</summary>
@@ -318,4 +359,4 @@ window.addEventListener('library-cm6::post-init', event => {
318359
}
319360
});
320361
```
321-
</details>
362+
</details>

dev/docs/wysiwyg-js-api.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# WYSIWYG JavaScript API
2+
3+
**Warning: This API is currently in development and may change without notice.**
4+
5+
Feedback is very much welcomed via this issue: https://github.com/BookStackApp/BookStack/issues/5937
6+
7+
This document covers the JavaScript API for the (newer Lexical-based) WYSIWYG editor.
8+
This API is built and designed to abstract the internals of the editor away
9+
to provide a stable interface for performing common customizations.
10+
11+
Only the methods and properties documented here are guaranteed to be stable **once this API
12+
is out of initial development**.
13+
Other elements may be accessible but are not designed to be used directly, and therefore may change
14+
without notice.
15+
Stable parts of the API may still change where needed, but such changes would be noted as part of BookStack update advisories.
16+
17+
The methods shown here are documented using standard TypeScript notation.
18+
19+
## Overview
20+
21+
The API is provided as an object, which itself provides a number of modules
22+
via its properties:
23+
24+
- `ui` - Provides methods related to the UI of the editor, like buttons and toolbars.
25+
- `content` - Provides methods related to the live user content being edited upon.
26+
27+
Each of these modules, and the relevant types used within, are documented in detail below.
28+
29+
---
30+
31+
## UI Module
32+
33+
This module provides methods related to the UI of the editor, like buttons and toolbars.
34+
35+
### Methods
36+
37+
#### createButton(options: object): EditorApiButton
38+
39+
Creates a new button which can be used by other methods.
40+
This takes an option object with the following properties:
41+
42+
- `label` - string, optional - Used for the button text if no icon provided, or the button tooltip if an icon is provided.
43+
- `icon` - string, optional - The icon to use for the button. Expected to be an SVG string.
44+
- `action` - callback, required - The action to perform when the button is clicked.
45+
46+
The function returns an [EditorApiButton](#editorapibutton) object.
47+
48+
**Example**
49+
50+
```javascript
51+
const button = api.ui.createButton({
52+
label: 'Warn',
53+
icon: '<svg>...</svg>',
54+
action: () => {
55+
window.alert('You clicked the button!');
56+
}
57+
});
58+
```
59+
60+
### getMainToolbar(): EditorApiToolbar
61+
62+
Get the main editor toolbar. This is typically the toolbar at the top of the editor.
63+
The function returns an [EditorApiToolbar](#editorapitoolbar) object, or null if no toolbar is found.
64+
65+
**Example**
66+
67+
```javascript
68+
const toolbar = api.ui.getMainToolbar();
69+
const sections = toolbar?.getSections() || [];
70+
if (sections.length > 0) {
71+
sections[0].addButton(button);
72+
}
73+
```
74+
75+
### Types
76+
77+
These are types which may be provided from UI module methods.
78+
79+
#### EditorApiButton
80+
81+
Represents a button created via the `createButton` method.
82+
This has the following methods:
83+
84+
- `setActive(isActive: boolean): void` - Sets whether the button should be in an active state or not (typically active buttons appear as pressed).
85+
86+
#### EditorApiToolbar
87+
88+
Represents a toolbar within the editor. This is a bar typically containing sets of buttons.
89+
This has the following methods:
90+
91+
- `getSections(): EditorApiToolbarSection[]` - Provides the main [EditorApiToolbarSections](#editorapitoolbarsection) contained within this toolbar.
92+
93+
#### EditorApiToolbarSection
94+
95+
Represents a section of the main editor toolbar, which contains a set of buttons.
96+
This has the following methods:
97+
98+
- `getLabel(): string` - Provides the string label of the section.
99+
- `addButton(button: EditorApiButton, targetIndex: number = -1): void` - Adds a button to the section.
100+
- By default, this will append the button, although a target index can be provided to insert at a specific position.
101+
102+
---
103+
104+
## Content Module
105+
106+
This module provides methods related to the live user content being edited within the editor.
107+
108+
### Methods
109+
110+
#### insertHtml(html: string, position: string = 'selection'): void
111+
112+
Inserts the given HTML string at the given position string.
113+
The position, if not provided, will default to `'selection'`, replacing any existing selected content (or inserting at the selection if there's no active selection range).
114+
Valid position string values are: `selection`, `start` and `end`. `start` & `end` are relative to the whole editor document.
115+
The HTML is not assured to be added to the editor exactly as provided, since it will be parsed and serialised to fit the editor's internal known model format. Different parts of the HTML content may be handled differently depending on if it's block or inline content.
116+
117+
The function does not return anything.
118+
119+
**Example**
120+
121+
```javascript
122+
// Basic insert at selection
123+
api.content.insertHtml('<p>Hello <strong>world</strong>!</p>');
124+
125+
// Insert at the start of the editor content
126+
api.content.insertHtml('<p>I\'m at the start!</p>', 'start');
127+
```

0 commit comments

Comments
 (0)