Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
All development on BookStack is currently done on the `development` branch.
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:

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

## Building CSS & JavaScript Assets

Expand Down
4 changes: 4 additions & 0 deletions dev/docs/javascript-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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).
51 changes: 46 additions & 5 deletions dev/docs/javascript-public-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ This event is called when the markdown editor loads, post configuration but befo

#### 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.
- `cmEditorView` - The CodeMirror [EditorView](https://codemirror.net/docs/ref/#view.EditorView) instance used for the markdown input editor.

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

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.
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.

#### Event Data

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

### `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 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:

<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 which is 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
const toolbar = api.ui.getMainToolbar();
if (toolbar) {
toolbar.getSections()[0]?.addButton(button, 0);
}
});
```
</details>

### `library-cm6::configure-theme`

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.
Expand All @@ -142,7 +183,7 @@ This event is called whenever a CodeMirror instance is loaded, as a method to co

- `darkModeActive` - A boolean to indicate if the current view/page is being loaded with dark mode active.
- `registerViewTheme(builder)` - A method that can be called to register a new view (CodeMirror UI) theme.
- `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.
- `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.
- `registerHighlightStyle(builder)` - A method that can be called to register a new HighlightStyle (code highlighting) theme.
- `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.

Expand Down Expand Up @@ -301,7 +342,7 @@ This event is called just after any CodeMirror instances are initialised so that

##### 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>
<summary>Show Example</summary>
Expand All @@ -318,4 +359,4 @@ window.addEventListener('library-cm6::post-init', event => {
}
});
```
</details>
</details>
127 changes: 127 additions & 0 deletions dev/docs/wysiwyg-js-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# WYSIWYG JavaScript API

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

Feedback is very much welcomed via this issue: https://github.com/BookStackApp/BookStack/issues/5937

This document covers the JavaScript API for the (newer Lexical-based) WYSIWYG editor.
This API is built and designed to abstract the internals of the editor away
to provide a stable interface for performing common customizations.

Only the methods and properties documented here are guaranteed to be stable **once this API
is out of initial development**.
Other elements may be accessible but are not designed to be used directly, and therefore may change
without notice.
Stable parts of the API may still change where needed, but such changes would be noted as part of BookStack update advisories.

The methods shown here are documented using standard TypeScript notation.

## Overview

The API is provided as an object, which itself provides a number of modules
via its properties:

- `ui` - Provides methods related to the UI of the editor, like buttons and toolbars.
- `content` - Provides methods related to the live user content being edited upon.

Each of these modules, and the relevant types used within, are documented in detail below.

---

## UI Module

This module provides methods related to the UI of the editor, like buttons and toolbars.

### Methods

#### createButton(options: object): EditorApiButton

Creates a new button which can be used by other methods.
This takes an option object with the following properties:

- `label` - string, optional - Used for the button text if no icon provided, or the button tooltip if an icon is provided.
- `icon` - string, optional - The icon to use for the button. Expected to be an SVG string.
- `action` - callback, required - The action to perform when the button is clicked.

The function returns an [EditorApiButton](#editorapibutton) object.

**Example**

```javascript
const button = api.ui.createButton({
label: 'Warn',
icon: '<svg>...</svg>',
action: () => {
window.alert('You clicked the button!');
}
});
```

### getMainToolbar(): EditorApiToolbar

Get the main editor toolbar. This is typically the toolbar at the top of the editor.
The function returns an [EditorApiToolbar](#editorapitoolbar) object, or null if no toolbar is found.

**Example**

```javascript
const toolbar = api.ui.getMainToolbar();
const sections = toolbar?.getSections() || [];
if (sections.length > 0) {
sections[0].addButton(button);
}
```

### Types

These are types which may be provided from UI module methods.

#### EditorApiButton

Represents a button created via the `createButton` method.
This has the following methods:

- `setActive(isActive: boolean): void` - Sets whether the button should be in an active state or not (typically active buttons appear as pressed).

#### EditorApiToolbar

Represents a toolbar within the editor. This is a bar typically containing sets of buttons.
This has the following methods:

- `getSections(): EditorApiToolbarSection[]` - Provides the main [EditorApiToolbarSections](#editorapitoolbarsection) contained within this toolbar.

#### EditorApiToolbarSection

Represents a section of the main editor toolbar, which contains a set of buttons.
This has the following methods:

- `getLabel(): string` - Provides the string label of the section.
- `addButton(button: EditorApiButton, targetIndex: number = -1): void` - Adds a button to the section.
- By default, this will append the button, although a target index can be provided to insert at a specific position.

---

## Content Module

This module provides methods related to the live user content being edited within the editor.

### Methods

#### insertHtml(html: string, position: string = 'selection'): void

Inserts the given HTML string at the given position string.
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).
Valid position string values are: `selection`, `start` and `end`. `start` & `end` are relative to the whole editor document.
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.

The function does not return anything.

**Example**

```javascript
// Basic insert at selection
api.content.insertHtml('<p>Hello <strong>world</strong>!</p>');

// Insert at the start of the editor content
api.content.insertHtml('<p>I\'m at the start!</p>', 'start');
```
Loading