@blockslides/react 0.2.0 → 0.3.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.
@@ -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 "./useEditor.js";
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
+ };