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