@blockslides/react-prebuilts 0.1.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.md +36 -0
- package/dist/index.cjs +381 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +86 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.js +354 -0
- package/dist/index.js.map +1 -0
- package/dist/menus/index.cjs +281 -0
- package/dist/menus/index.cjs.map +1 -0
- package/dist/menus/index.d.cts +28 -0
- package/dist/menus/index.d.ts +28 -0
- package/dist/menus/index.js +252 -0
- package/dist/menus/index.js.map +1 -0
- package/package.json +76 -0
- package/src/SlideEditor.tsx +73 -0
- package/src/index.ts +8 -0
- package/src/menus/BubbleMenu.tsx +89 -0
- package/src/menus/BubbleMenuPreset.tsx +144 -0
- package/src/menus/FloatingMenu.tsx +69 -0
- package/src/menus/index.ts +3 -0
- package/src/useSlideEditor.ts +250 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react'
|
|
2
|
+
import { createPortal } from 'react-dom'
|
|
3
|
+
import { useCurrentEditor } from '@blockslides/react'
|
|
4
|
+
import {
|
|
5
|
+
BubbleMenuPlugin,
|
|
6
|
+
type BubbleMenuPluginProps,
|
|
7
|
+
} from '@blockslides/extension-bubble-menu'
|
|
8
|
+
import { NodeSelection } from '@blockslides/pm/state'
|
|
9
|
+
import {
|
|
10
|
+
type BubbleMenuPresetOptions,
|
|
11
|
+
buildMenuElement,
|
|
12
|
+
DEFAULT_ITEMS,
|
|
13
|
+
DEFAULT_COLOR_PALETTE,
|
|
14
|
+
DEFAULT_HIGHLIGHT_PALETTE,
|
|
15
|
+
DEFAULT_FONTS,
|
|
16
|
+
DEFAULT_FONT_SIZES,
|
|
17
|
+
DEFAULT_ALIGNMENTS,
|
|
18
|
+
} from '@blockslides/extension-bubble-menu-preset'
|
|
19
|
+
import { isNodeSelection, isTextSelection, type Editor } from '@blockslides/core'
|
|
20
|
+
|
|
21
|
+
export type BubbleMenuPresetProps = Omit<BubbleMenuPresetOptions, 'element' | 'pluginKey'> &
|
|
22
|
+
React.HTMLAttributes<HTMLElement> & {
|
|
23
|
+
editor?: Editor | null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const BubbleMenuPreset = React.forwardRef<HTMLElement, BubbleMenuPresetProps>(
|
|
27
|
+
(
|
|
28
|
+
{
|
|
29
|
+
editor,
|
|
30
|
+
updateDelay,
|
|
31
|
+
resizeDelay,
|
|
32
|
+
appendTo,
|
|
33
|
+
shouldShow,
|
|
34
|
+
getReferencedVirtualElement,
|
|
35
|
+
options,
|
|
36
|
+
items,
|
|
37
|
+
className,
|
|
38
|
+
injectStyles,
|
|
39
|
+
textColors,
|
|
40
|
+
highlightColors,
|
|
41
|
+
fonts,
|
|
42
|
+
fontSizes,
|
|
43
|
+
alignments,
|
|
44
|
+
onTextAction,
|
|
45
|
+
onImageReplace,
|
|
46
|
+
...rest
|
|
47
|
+
},
|
|
48
|
+
ref,
|
|
49
|
+
) => {
|
|
50
|
+
const pluginKey = 'bubbleMenuPreset'
|
|
51
|
+
const { editor: currentEditor } = useCurrentEditor()
|
|
52
|
+
const menuEl = useRef<HTMLElement | null>(null)
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const attachToEditor = (editor || currentEditor) as Editor | null
|
|
56
|
+
if (!attachToEditor || (attachToEditor as any).isDestroyed) {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const { element, cleanup } = buildMenuElement(attachToEditor, {
|
|
61
|
+
items: items ?? DEFAULT_ITEMS,
|
|
62
|
+
className: className ?? '',
|
|
63
|
+
injectStyles: injectStyles !== false,
|
|
64
|
+
textColors: textColors ?? DEFAULT_COLOR_PALETTE,
|
|
65
|
+
highlightColors: highlightColors ?? DEFAULT_HIGHLIGHT_PALETTE,
|
|
66
|
+
fonts: fonts ?? DEFAULT_FONTS,
|
|
67
|
+
fontSizes: fontSizes ?? DEFAULT_FONT_SIZES,
|
|
68
|
+
alignments: alignments ?? DEFAULT_ALIGNMENTS,
|
|
69
|
+
onTextAction,
|
|
70
|
+
onImageReplace,
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
menuEl.current = element
|
|
74
|
+
|
|
75
|
+
if (typeof ref === 'function') {
|
|
76
|
+
ref(element)
|
|
77
|
+
} else if (ref) {
|
|
78
|
+
;(ref as React.MutableRefObject<HTMLElement | null>).current = element
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const defaultShouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({
|
|
82
|
+
state,
|
|
83
|
+
editor,
|
|
84
|
+
}) => {
|
|
85
|
+
const sel = state.selection
|
|
86
|
+
const imageSelection =
|
|
87
|
+
(sel instanceof NodeSelection &&
|
|
88
|
+
['image', 'imageBlock'].includes((sel as any).node?.type?.name)) ||
|
|
89
|
+
editor.isActive('image') ||
|
|
90
|
+
editor.isActive('imageBlock')
|
|
91
|
+
|
|
92
|
+
if (imageSelection) return true
|
|
93
|
+
if (isTextSelection(sel) && !sel.empty && !imageSelection) return true
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const plugin = BubbleMenuPlugin({
|
|
98
|
+
editor: attachToEditor,
|
|
99
|
+
element,
|
|
100
|
+
updateDelay,
|
|
101
|
+
resizeDelay,
|
|
102
|
+
appendTo,
|
|
103
|
+
pluginKey,
|
|
104
|
+
shouldShow: shouldShow ?? defaultShouldShow,
|
|
105
|
+
getReferencedVirtualElement,
|
|
106
|
+
options,
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
attachToEditor.registerPlugin(plugin)
|
|
110
|
+
|
|
111
|
+
return () => {
|
|
112
|
+
attachToEditor.unregisterPlugin(pluginKey)
|
|
113
|
+
cleanup?.()
|
|
114
|
+
if (element.parentNode) {
|
|
115
|
+
element.parentNode.removeChild(element)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}, [
|
|
119
|
+
editor,
|
|
120
|
+
currentEditor,
|
|
121
|
+
updateDelay,
|
|
122
|
+
resizeDelay,
|
|
123
|
+
appendTo,
|
|
124
|
+
shouldShow,
|
|
125
|
+
getReferencedVirtualElement,
|
|
126
|
+
options,
|
|
127
|
+
items,
|
|
128
|
+
className,
|
|
129
|
+
injectStyles,
|
|
130
|
+
textColors,
|
|
131
|
+
highlightColors,
|
|
132
|
+
fonts,
|
|
133
|
+
fontSizes,
|
|
134
|
+
alignments,
|
|
135
|
+
onTextAction,
|
|
136
|
+
onImageReplace,
|
|
137
|
+
ref,
|
|
138
|
+
])
|
|
139
|
+
|
|
140
|
+
return menuEl.current ? createPortal(<div {...rest} />, menuEl.current) : null
|
|
141
|
+
},
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
BubbleMenuPreset.displayName = 'BubbleMenuPreset'
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { FloatingMenuPluginProps } from '@blockslides/extension-floating-menu'
|
|
2
|
+
import { FloatingMenuPlugin } from '@blockslides/extension-floating-menu'
|
|
3
|
+
import { useCurrentEditor } from '@blockslides/react'
|
|
4
|
+
import React, { useEffect, useRef } from 'react'
|
|
5
|
+
import { createPortal } from 'react-dom'
|
|
6
|
+
|
|
7
|
+
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
|
|
8
|
+
|
|
9
|
+
export type FloatingMenuProps = Omit<Optional<FloatingMenuPluginProps, 'pluginKey'>, 'element' | 'editor'> & {
|
|
10
|
+
editor: FloatingMenuPluginProps['editor'] | null
|
|
11
|
+
options?: FloatingMenuPluginProps['options']
|
|
12
|
+
} & React.HTMLAttributes<HTMLDivElement>
|
|
13
|
+
|
|
14
|
+
export const FloatingMenu = React.forwardRef<HTMLDivElement, FloatingMenuProps>(
|
|
15
|
+
({ pluginKey = 'floatingMenu', editor, appendTo, shouldShow = null, options, children, ...restProps }, ref) => {
|
|
16
|
+
const menuEl = useRef(document.createElement('div'))
|
|
17
|
+
|
|
18
|
+
if (typeof ref === 'function') {
|
|
19
|
+
ref(menuEl.current)
|
|
20
|
+
} else if (ref) {
|
|
21
|
+
ref.current = menuEl.current
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const { editor: currentEditor } = useCurrentEditor()
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const floatingMenuElement = menuEl.current
|
|
28
|
+
|
|
29
|
+
floatingMenuElement.style.visibility = 'hidden'
|
|
30
|
+
floatingMenuElement.style.position = 'absolute'
|
|
31
|
+
|
|
32
|
+
if (editor?.isDestroyed || (currentEditor as any)?.isDestroyed) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const attachToEditor = editor || currentEditor
|
|
37
|
+
|
|
38
|
+
if (!attachToEditor) {
|
|
39
|
+
console.warn(
|
|
40
|
+
'FloatingMenu component is not rendered inside of an editor component or does not have editor prop.',
|
|
41
|
+
)
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const plugin = FloatingMenuPlugin({
|
|
46
|
+
editor: attachToEditor,
|
|
47
|
+
element: floatingMenuElement,
|
|
48
|
+
pluginKey,
|
|
49
|
+
appendTo,
|
|
50
|
+
shouldShow,
|
|
51
|
+
options,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
attachToEditor.registerPlugin(plugin)
|
|
55
|
+
|
|
56
|
+
return () => {
|
|
57
|
+
attachToEditor.unregisterPlugin(pluginKey)
|
|
58
|
+
window.requestAnimationFrame(() => {
|
|
59
|
+
if (floatingMenuElement.parentNode) {
|
|
60
|
+
floatingMenuElement.parentNode.removeChild(floatingMenuElement)
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
65
|
+
}, [editor, currentEditor, appendTo, pluginKey, shouldShow, options])
|
|
66
|
+
|
|
67
|
+
return createPortal(<div {...restProps}>{children}</div>, menuEl.current)
|
|
68
|
+
},
|
|
69
|
+
)
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { useEffect, useMemo } from "react";
|
|
2
|
+
import type { DependencyList } from "react";
|
|
3
|
+
import { templatesV1 } from "@blockslides/ai-context";
|
|
4
|
+
import type { AnyExtension, Editor, JSONContent } from "@blockslides/core";
|
|
5
|
+
import type { SlideOptions } from "@blockslides/extension-slide";
|
|
6
|
+
import {
|
|
7
|
+
ExtensionKit,
|
|
8
|
+
type ExtensionKitOptions,
|
|
9
|
+
} from "@blockslides/extension-kit";
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
useEditor,
|
|
13
|
+
type UseEditorOptions,
|
|
14
|
+
} from "@blockslides/react";
|
|
15
|
+
|
|
16
|
+
type PresetTemplates = ReturnType<typeof templatesV1.listPresetTemplates>;
|
|
17
|
+
|
|
18
|
+
export type UseSlideEditorProps = {
|
|
19
|
+
/**
|
|
20
|
+
* Initial content for the editor. If omitted, a single preset slide is used.
|
|
21
|
+
*/
|
|
22
|
+
content?: UseEditorOptions["content"];
|
|
23
|
+
/**
|
|
24
|
+
* Called on every update with the current JSON document.
|
|
25
|
+
*/
|
|
26
|
+
onChange?: (doc: JSONContent, editor: Editor) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Additional extensions to append after the ExtensionKit bundle.
|
|
29
|
+
*/
|
|
30
|
+
extensions?: AnyExtension[];
|
|
31
|
+
/**
|
|
32
|
+
* Customize or disable pieces of ExtensionKit (e.g., bubbleMenu: false).
|
|
33
|
+
*/
|
|
34
|
+
extensionKitOptions?: ExtensionKitOptions;
|
|
35
|
+
/**
|
|
36
|
+
* Optional preset list to power the add-slide button.
|
|
37
|
+
*/
|
|
38
|
+
presetTemplates?: PresetTemplates;
|
|
39
|
+
/**
|
|
40
|
+
* Dependencies array forwarded to useEditor for re-instantiation control.
|
|
41
|
+
* Leave empty to avoid recreating the editor on prop changes.
|
|
42
|
+
*/
|
|
43
|
+
deps?: DependencyList;
|
|
44
|
+
/**
|
|
45
|
+
* Called once when an editor instance is ready.
|
|
46
|
+
*/
|
|
47
|
+
onEditorReady?: (editor: Editor) => void;
|
|
48
|
+
} & Omit<UseEditorOptions, "extensions" | "content">;
|
|
49
|
+
|
|
50
|
+
const defaultSlide = () => ({
|
|
51
|
+
/**
|
|
52
|
+
* Placeholder slide if no content is provided.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
type: "doc",
|
|
56
|
+
content: [
|
|
57
|
+
{
|
|
58
|
+
type: "slide",
|
|
59
|
+
attrs: {
|
|
60
|
+
size: "16x9",
|
|
61
|
+
className: "",
|
|
62
|
+
id: "slide-1",
|
|
63
|
+
backgroundMode: "none",
|
|
64
|
+
backgroundColor: null,
|
|
65
|
+
backgroundImage: null,
|
|
66
|
+
backgroundOverlayColor: null,
|
|
67
|
+
backgroundOverlayOpacity: null,
|
|
68
|
+
},
|
|
69
|
+
content: [
|
|
70
|
+
{
|
|
71
|
+
type: "column",
|
|
72
|
+
attrs: {
|
|
73
|
+
align: "center",
|
|
74
|
+
padding: "lg",
|
|
75
|
+
margin: null,
|
|
76
|
+
gap: "md",
|
|
77
|
+
backgroundColor: "#ffffff",
|
|
78
|
+
backgroundImage: null,
|
|
79
|
+
borderRadius: null,
|
|
80
|
+
border: null,
|
|
81
|
+
fill: true,
|
|
82
|
+
width: null,
|
|
83
|
+
height: null,
|
|
84
|
+
justify: "center",
|
|
85
|
+
},
|
|
86
|
+
content: [
|
|
87
|
+
{
|
|
88
|
+
type: "heading",
|
|
89
|
+
attrs: {
|
|
90
|
+
align: null,
|
|
91
|
+
padding: null,
|
|
92
|
+
margin: null,
|
|
93
|
+
gap: null,
|
|
94
|
+
backgroundColor: null,
|
|
95
|
+
backgroundImage: null,
|
|
96
|
+
borderRadius: null,
|
|
97
|
+
border: null,
|
|
98
|
+
fill: null,
|
|
99
|
+
width: null,
|
|
100
|
+
height: null,
|
|
101
|
+
justify: null,
|
|
102
|
+
id: "1fc4664c-333d-4203-a3f1-3ad27a54c535",
|
|
103
|
+
"data-toc-id": "1fc4664c-333d-4203-a3f1-3ad27a54c535",
|
|
104
|
+
level: 1,
|
|
105
|
+
},
|
|
106
|
+
content: [
|
|
107
|
+
{
|
|
108
|
+
type: "text",
|
|
109
|
+
text: "Lorem ipsum dolor sit amet",
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
type: "paragraph",
|
|
115
|
+
attrs: {
|
|
116
|
+
align: null,
|
|
117
|
+
padding: null,
|
|
118
|
+
margin: null,
|
|
119
|
+
gap: null,
|
|
120
|
+
backgroundColor: null,
|
|
121
|
+
backgroundImage: null,
|
|
122
|
+
borderRadius: null,
|
|
123
|
+
border: null,
|
|
124
|
+
fill: null,
|
|
125
|
+
width: null,
|
|
126
|
+
height: null,
|
|
127
|
+
justify: null,
|
|
128
|
+
},
|
|
129
|
+
content: [
|
|
130
|
+
{
|
|
131
|
+
type: "text",
|
|
132
|
+
text: "Consectetur adipiscing elit. Sed do eiusmod tempor incididunt. ",
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const defaultAddSlideButton = (presets: PresetTemplates) => ({
|
|
144
|
+
showPresets: true,
|
|
145
|
+
presets,
|
|
146
|
+
presetBackground: "#0f172a",
|
|
147
|
+
presetForeground: "#e5e7eb",
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const defaultSlideOptions: Partial<SlideOptions> = {
|
|
151
|
+
renderMode: "dynamic",
|
|
152
|
+
hoverOutline: { color: "#3b82f6", width: "1.5px", offset: "4px" },
|
|
153
|
+
hoverOutlineCascade: false,
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export const useSlideEditor = ({
|
|
157
|
+
content,
|
|
158
|
+
onChange,
|
|
159
|
+
extensions,
|
|
160
|
+
extensionKitOptions,
|
|
161
|
+
presetTemplates,
|
|
162
|
+
deps = [],
|
|
163
|
+
onEditorReady,
|
|
164
|
+
immediatelyRender = false,
|
|
165
|
+
shouldRerenderOnTransaction = false,
|
|
166
|
+
theme = "light",
|
|
167
|
+
editorProps,
|
|
168
|
+
onUpdate,
|
|
169
|
+
...editorOptions
|
|
170
|
+
}: UseSlideEditorProps = {}) => {
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Presets for add slide button.
|
|
175
|
+
*/
|
|
176
|
+
const presets = useMemo<PresetTemplates>(
|
|
177
|
+
() => presetTemplates ?? templatesV1.listPresetTemplates(),
|
|
178
|
+
[presetTemplates]
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
const mergedExtensionKitOptions = useMemo<ExtensionKitOptions>(() => {
|
|
182
|
+
const addSlideButton =
|
|
183
|
+
extensionKitOptions?.addSlideButton === false
|
|
184
|
+
? false
|
|
185
|
+
: {
|
|
186
|
+
...defaultAddSlideButton(presets),
|
|
187
|
+
...(extensionKitOptions?.addSlideButton ?? {}),
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const slide =
|
|
191
|
+
extensionKitOptions?.slide === false
|
|
192
|
+
? false
|
|
193
|
+
: {
|
|
194
|
+
...defaultSlideOptions,
|
|
195
|
+
...(extensionKitOptions?.slide ?? {}),
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
...extensionKitOptions,
|
|
200
|
+
addSlideButton,
|
|
201
|
+
slide,
|
|
202
|
+
};
|
|
203
|
+
}, [extensionKitOptions, presets]);
|
|
204
|
+
|
|
205
|
+
const resolvedExtensions = useMemo<AnyExtension[]>(() => {
|
|
206
|
+
const kit = ExtensionKit.configure(mergedExtensionKitOptions);
|
|
207
|
+
return extensions ? [kit, ...extensions] : [kit];
|
|
208
|
+
}, [extensions, mergedExtensionKitOptions]);
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Initial content for the editor.
|
|
212
|
+
* content, initialContent, defaultValue, value etc are all the same thing
|
|
213
|
+
*/
|
|
214
|
+
const initialContent = content ?? defaultSlide();
|
|
215
|
+
|
|
216
|
+
const editor = useEditor(
|
|
217
|
+
{
|
|
218
|
+
content: initialContent,
|
|
219
|
+
extensions: resolvedExtensions,
|
|
220
|
+
immediatelyRender,
|
|
221
|
+
shouldRerenderOnTransaction,
|
|
222
|
+
theme,
|
|
223
|
+
editorProps: {
|
|
224
|
+
attributes: {
|
|
225
|
+
autocomplete: "off",
|
|
226
|
+
autocorrect: "off",
|
|
227
|
+
autocapitalize: "off",
|
|
228
|
+
class: "min-h-full min-w-full",
|
|
229
|
+
...(editorProps?.attributes ?? {}),
|
|
230
|
+
},
|
|
231
|
+
...editorProps,
|
|
232
|
+
},
|
|
233
|
+
...editorOptions,
|
|
234
|
+
onUpdate: (ctx) => {
|
|
235
|
+
const json = ctx.editor.getJSON();
|
|
236
|
+
onChange?.(json, ctx.editor);
|
|
237
|
+
onUpdate?.(ctx);
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
deps
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
if (editor && !editor.isDestroyed) {
|
|
245
|
+
onEditorReady?.(editor);
|
|
246
|
+
}
|
|
247
|
+
}, [editor, onEditorReady]);
|
|
248
|
+
|
|
249
|
+
return { editor, presets };
|
|
250
|
+
};
|