@portabletext/toolbar 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +306 -0
- package/dist/index.cjs +1945 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +614 -0
- package/dist/index.d.ts +614 -0
- package/dist/index.js +1936 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -0
- package/src/disable-listener.ts +18 -0
- package/src/index.ts +11 -0
- package/src/use-annotation-button.ts +302 -0
- package/src/use-annotation-popover.ts +301 -0
- package/src/use-block-object-button.ts +189 -0
- package/src/use-block-object-popover.ts +285 -0
- package/src/use-decorator-button.ts +185 -0
- package/src/use-decorator-keyboard-shortcut.ts +96 -0
- package/src/use-history-buttons.ts +97 -0
- package/src/use-inline-object-button.ts +183 -0
- package/src/use-inline-object-popover.ts +285 -0
- package/src/use-list-button.ts +175 -0
- package/src/use-mutually-exclusive-decorator.ts +36 -0
- package/src/use-style-keyboard-shortcuts.ts +105 -0
- package/src/use-style-selector.ts +149 -0
- package/src/use-toolbar-schema.ts +164 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 - 2025 Sanity.io
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
# `@portabletext/toolbar`
|
|
2
|
+
|
|
3
|
+
Powered by [Behaviors](https://www.portabletext.org/concepts/behavior/) and [State Machines](https://stately.ai/docs/xstate), `@portabletext/toolbar` is a collection of robust React hooks for building toolbars and related UI components
|
|
4
|
+
for the Portable Text editor.
|
|
5
|
+
|
|
6
|
+
Refer to the toolbar in the [Portable Text Playground](../../apps/playground/) for a comprehensive example.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Schema Extension**: Extend the editor's schema with default values, icons, shortcuts and more. This makes it easier to use the schema to render toolbars, forms and other UI components.
|
|
11
|
+
- **Hooks**: Headless React hooks to help building UI components for toggle buttons, popovers and insert dialogs.
|
|
12
|
+
- **Keyboard Shortcuts**: Seamless integration with `@portabletext/keyboard-shortcuts` for platform-aware shortcuts.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @portabletext/toolbar
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Basic Example
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
import {bold, link} from '@portabletext/keyboard-shortcuts'
|
|
24
|
+
import {
|
|
25
|
+
useDecoratorButton,
|
|
26
|
+
useHistoryButtons,
|
|
27
|
+
useToolbarSchema,
|
|
28
|
+
type ExtendAnnotationSchemaType,
|
|
29
|
+
type ExtendDecoratorSchemaType,
|
|
30
|
+
} from '@portabletext/toolbar'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 1. Define extensions for your schema types
|
|
34
|
+
*/
|
|
35
|
+
const extendDecorator: ExtendDecoratorSchemaType = (decorator) => {
|
|
36
|
+
if (decorator.name === 'strong') {
|
|
37
|
+
return {
|
|
38
|
+
...decorator,
|
|
39
|
+
icon: BoldIcon,
|
|
40
|
+
shortcut: bold,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return decorator
|
|
45
|
+
}
|
|
46
|
+
const extendAnnotation: ExtendAnnotationSchemaType = (annotation) => {
|
|
47
|
+
if (annotation.name === 'link') {
|
|
48
|
+
return {
|
|
49
|
+
...annotation,
|
|
50
|
+
icon: LinkIcon,
|
|
51
|
+
defaultValues: {
|
|
52
|
+
href: 'https://example.com',
|
|
53
|
+
},
|
|
54
|
+
shortcut: link,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return annotation
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 2. Create a Toolbar plugin that can be used inside an `EditorProvider`.
|
|
63
|
+
*/
|
|
64
|
+
function ToolbarPlugin() {
|
|
65
|
+
/**
|
|
66
|
+
* 3. Obtain a `ToolbarSchema` by extending the `EditorSchema`.
|
|
67
|
+
*/
|
|
68
|
+
const toolbarSchema = useToolbarSchema({
|
|
69
|
+
extendDecorator,
|
|
70
|
+
extendAnnotation,
|
|
71
|
+
// extendStyle,
|
|
72
|
+
// extendList,
|
|
73
|
+
// extendBlockObject,
|
|
74
|
+
// extendInlineObject,
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 4. Render the toolbar
|
|
79
|
+
*/
|
|
80
|
+
return (
|
|
81
|
+
<div>
|
|
82
|
+
{toolbarSchema.decorators?.map((decorator) => (
|
|
83
|
+
<DecoratorButton key={decorator.name} schemaType={decorator} />
|
|
84
|
+
))}
|
|
85
|
+
{toolbarSchema.annotations?.map((annotation) => {
|
|
86
|
+
/** ... */
|
|
87
|
+
})}
|
|
88
|
+
<HistoryButtons />
|
|
89
|
+
</div>
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function DecoratorButton(props: {schemaType: ToolbarDecoratorSchemaType}) {
|
|
94
|
+
/**
|
|
95
|
+
* 5. Use the provided hooks to manage state, set up keyboard shortcuts and
|
|
96
|
+
* more.
|
|
97
|
+
*/
|
|
98
|
+
const decoratorButton = useDecoratorButton(props)
|
|
99
|
+
|
|
100
|
+
return (
|
|
101
|
+
<button
|
|
102
|
+
onClick={() => decoratorButton.send({type: 'toggle'})}
|
|
103
|
+
disabled={decoratorButton.snapshot.matches('disabled')}
|
|
104
|
+
className={
|
|
105
|
+
decoratorButton.snapshot.matches({enabled: 'active'}) ? 'active' : ''
|
|
106
|
+
}
|
|
107
|
+
title={props.schemaType.shortcut?.description}
|
|
108
|
+
>
|
|
109
|
+
{props.schemaType.icon && <props.schemaType.icon />}
|
|
110
|
+
{props.schemaType.title}
|
|
111
|
+
</button>
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function HistoryButtons() {
|
|
116
|
+
const historyButtons = useHistoryButtons()
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<>
|
|
120
|
+
<button
|
|
121
|
+
onClick={() => historyButtons.send({type: 'history.undo'})}
|
|
122
|
+
disabled={historyButtons.snapshot.matches('disabled')}
|
|
123
|
+
>
|
|
124
|
+
Undo
|
|
125
|
+
</button>
|
|
126
|
+
<button
|
|
127
|
+
onClick={() => historyButtons.send({type: 'history.redo'})}
|
|
128
|
+
disabled={historyButtons.snapshot.matches('disabled')}
|
|
129
|
+
>
|
|
130
|
+
Redo
|
|
131
|
+
</button>
|
|
132
|
+
</>
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Hooks
|
|
138
|
+
|
|
139
|
+
### `useToolbarSchema`
|
|
140
|
+
|
|
141
|
+
Extends the editor's schema with default values, icons, shortcuts and more to make it easier to render toolbars, forms and other UI components.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
function useToolbarSchema(props: {
|
|
145
|
+
extendDecorator?: (
|
|
146
|
+
decorator: DecoratorSchemaType,
|
|
147
|
+
) => ToolbarDecoratorSchemaType
|
|
148
|
+
extendAnnotation?: (
|
|
149
|
+
annotation: AnnotationSchemaType,
|
|
150
|
+
) => ToolbarAnnotationSchemaType
|
|
151
|
+
extendList?: (list: ListSchemaType) => ToolbarListSchemaType
|
|
152
|
+
extendBlockObject?: (
|
|
153
|
+
blockObject: BlockObjectSchemaType,
|
|
154
|
+
) => ToolbarBlockObjectSchemaType
|
|
155
|
+
extendInlineObject?: (
|
|
156
|
+
inlineObject: InlineObjectSchemaType,
|
|
157
|
+
) => ToolbarInlineObjectSchemaType
|
|
158
|
+
extendStyle?: (style: StyleSchemaType) => ToolbarStyleSchemaType
|
|
159
|
+
}): ToolbarSchema
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### `useDecoratorButton`
|
|
163
|
+
|
|
164
|
+
Manages the state and behavior of decorator toggle buttons (bold, italic, etc.) with keyboard shortcut support.
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
function useDecoratorButton(props: {
|
|
168
|
+
schemaType: ToolbarDecoratorSchemaType
|
|
169
|
+
}): DecoratorButton
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**Returns:** An object with:
|
|
173
|
+
|
|
174
|
+
- `snapshot`: Current state snapshot with `matches()` method
|
|
175
|
+
- `send`: Function to dispatch events like `{type: 'toggle'}`
|
|
176
|
+
|
|
177
|
+
### `useAnnotationButton`
|
|
178
|
+
|
|
179
|
+
Manages the state and behavior of annotation buttons in the toolbar. Handles adding/removing annotations, keyboard shortcuts, and dialog interactions.
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
function useAnnotationButton(props: {
|
|
183
|
+
schemaType: ToolbarAnnotationSchemaType
|
|
184
|
+
}): AnnotationButton
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
**Returns:** An object with:
|
|
188
|
+
|
|
189
|
+
- `snapshot`: Current state snapshot with `matches()` method
|
|
190
|
+
- `send`: Function to dispatch events like `{type: 'add'}`, `{type: 'remove'}`, `{type: 'open dialog'}`, `{type: 'close dialog'}`
|
|
191
|
+
|
|
192
|
+
### `useAnnotationPopover`
|
|
193
|
+
|
|
194
|
+
Provides state management for annotation popover dialogs, including form handling and validation.
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
function useAnnotationPopover(props: {
|
|
198
|
+
schemaTypes: ReadonlyArray<ToolbarAnnotationSchemaType>
|
|
199
|
+
}): AnnotationPopover
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
**Returns:** An object with:
|
|
203
|
+
|
|
204
|
+
- `snapshot`: Current state snapshot with `matches()` method and `context` containing active annotations
|
|
205
|
+
- `send`: Function to dispatch events like `{type: 'remove'}`, `{type: 'edit'}`, `{type: 'close'}`
|
|
206
|
+
|
|
207
|
+
### `useStyleSelector`
|
|
208
|
+
|
|
209
|
+
Manages style selection (headings, paragraphs, etc.) with keyboard shortcut support.
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
function useStyleSelector(props: {
|
|
213
|
+
schemaTypes: ReadonlyArray<ToolbarStyleSchemaType>
|
|
214
|
+
}): StyleSelector
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
**Returns:** An object with:
|
|
218
|
+
|
|
219
|
+
- `snapshot`: Current state snapshot with `matches()` method and `context.activeStyle`
|
|
220
|
+
- `send`: Function to dispatch events like `{type: 'toggle', style: 'h1'}`
|
|
221
|
+
|
|
222
|
+
### `useListButton`
|
|
223
|
+
|
|
224
|
+
Manages list item toggle buttons (bullet lists, numbered lists, etc.).
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
function useListButton(props: {schemaType: ToolbarListSchemaType}): ListButton
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
**Returns:** An object with:
|
|
231
|
+
|
|
232
|
+
- `snapshot`: Current state snapshot with `matches()` method
|
|
233
|
+
- `send`: Function to dispatch events like `{type: 'toggle'}`
|
|
234
|
+
|
|
235
|
+
### `useBlockObjectButton`
|
|
236
|
+
|
|
237
|
+
Manages insertion of block objects like images into the editor.
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
function useBlockObjectButton(props: {
|
|
241
|
+
schemaType: ToolbarBlockObjectSchemaType
|
|
242
|
+
}): BlockObjectButton
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
**Returns:** An object with:
|
|
246
|
+
|
|
247
|
+
- `snapshot`: Current state snapshot with `matches()` method
|
|
248
|
+
- `send`: Function to dispatch events like `{type: 'open dialog'}`, `{type: 'insert'}`
|
|
249
|
+
|
|
250
|
+
### `useBlockObjectPopover`
|
|
251
|
+
|
|
252
|
+
Provides state management for block object insertion dialogs.
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
function useBlockObjectPopover(props: {
|
|
256
|
+
schemaType: ToolbarBlockObjectSchemaType
|
|
257
|
+
}): BlockObjectPopover
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
**Returns:** An object with:
|
|
261
|
+
|
|
262
|
+
- `snapshot`: Current state snapshot with `matches()` method
|
|
263
|
+
- `send`: Function to dispatch events like `{type: 'open dialog'}`, `{type: 'close dialog'}`, `{type: 'insert'}`
|
|
264
|
+
|
|
265
|
+
### `useInlineObjectButton`
|
|
266
|
+
|
|
267
|
+
Manages insertion of inline objects into the editor.
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
function useInlineObjectButton(props: {
|
|
271
|
+
schemaType: ToolbarInlineObjectSchemaType
|
|
272
|
+
}): InlineObjectButton
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
**Returns:** An object with:
|
|
276
|
+
|
|
277
|
+
- `snapshot`: Current state snapshot with `matches()` method
|
|
278
|
+
- `send`: Function to dispatch events like `{type: 'open dialog'}`, `{type: 'insert'}`
|
|
279
|
+
|
|
280
|
+
### `useInlineObjectPopover`
|
|
281
|
+
|
|
282
|
+
Provides state management for inline object insertion dialogs.
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
function useInlineObjectPopover(props: {
|
|
286
|
+
schemaType: ToolbarInlineObjectSchemaType
|
|
287
|
+
}): InlineObjectPopover
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
**Returns:** An object with:
|
|
291
|
+
|
|
292
|
+
- `snapshot`: Current state snapshot with `matches()` method
|
|
293
|
+
- `send`: Function to dispatch events like `{type: 'open dialog'}`, `{type: 'close dialog'}`, `{type: 'insert'}`
|
|
294
|
+
|
|
295
|
+
### `useHistoryButtons`
|
|
296
|
+
|
|
297
|
+
Provides undo/redo functionality for the editor.
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
function useHistoryButtons(): HistoryButtons
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
**Returns:** An object with:
|
|
304
|
+
|
|
305
|
+
- `snapshot`: Current state snapshot with `matches()` method
|
|
306
|
+
- `send`: Function to dispatch events like `{type: 'history.undo'}`, `{type: 'history.redo'}`
|