@ilamanownuk/blog-kit 0.0.0-stage → 0.2.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/dist/index.cjs ADDED
@@ -0,0 +1,963 @@
1
+ "use client";
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/editor/index.ts
32
+ var editor_exports = {};
33
+ __export(editor_exports, {
34
+ RichTextEditor: () => RichTextEditor,
35
+ defaultLabels: () => defaultLabels,
36
+ defaultToolbar: () => defaultToolbar,
37
+ isAllowedEmbedSrc: () => isAllowedEmbedSrc,
38
+ normalizeLinkUrl: () => normalizeLinkUrl,
39
+ toVideoEmbedUrl: () => toVideoEmbedUrl
40
+ });
41
+ module.exports = __toCommonJS(editor_exports);
42
+
43
+ // src/editor/rich-text-editor.tsx
44
+ var React7 = __toESM(require("react"), 1);
45
+ var import_react4 = require("@tiptap/react");
46
+
47
+ // src/shared/embed.ts
48
+ var ALL_EMBED_PROVIDERS = ["aparat", "youtube"];
49
+ var EMBED_PROVIDERS = {
50
+ aparat: {
51
+ hostname: "www.aparat.com",
52
+ prefix: "https://www.aparat.com/video/video/embed/",
53
+ embedBase: "https://www.aparat.com/video/video/embed/videohash/"
54
+ },
55
+ youtube: {
56
+ hostname: "www.youtube-nocookie.com",
57
+ prefix: "https://www.youtube-nocookie.com/embed/",
58
+ embedBase: "https://www.youtube-nocookie.com/embed/"
59
+ }
60
+ };
61
+ var YOUTUBE_ID = /^[A-Za-z0-9_-]{6,20}$/;
62
+ var APARAT_ID = /^[A-Za-z0-9]{3,20}$/;
63
+ function toVideoEmbedUrl(input, allowed = ALL_EMBED_PROVIDERS) {
64
+ let url;
65
+ try {
66
+ url = new URL(input.trim());
67
+ } catch {
68
+ return null;
69
+ }
70
+ if (url.protocol !== "https:") return null;
71
+ const host = url.hostname.replace(/^(www\.|m\.)/, "");
72
+ const segments = url.pathname.split("/").filter(Boolean);
73
+ if (host === "aparat.com") {
74
+ if (!allowed.includes("aparat")) return null;
75
+ const id2 = segments[0] === "v" ? segments[1] : segments.at(-1) === "frame" ? segments.at(-3) : void 0;
76
+ return id2 && APARAT_ID.test(id2) ? `${EMBED_PROVIDERS.aparat.embedBase}${id2}/vt/frame` : null;
77
+ }
78
+ if (!allowed.includes("youtube")) return null;
79
+ let id;
80
+ if (host === "youtu.be") id = segments[0];
81
+ else if (host === "youtube.com" || host === "youtube-nocookie.com") {
82
+ id = ["embed", "shorts", "live"].includes(segments[0] ?? "") ? segments[1] : url.searchParams.get("v");
83
+ }
84
+ return id && YOUTUBE_ID.test(id) ? `${EMBED_PROVIDERS.youtube.embedBase}${id}` : null;
85
+ }
86
+ function isAllowedEmbedSrc(src, allowed = ALL_EMBED_PROVIDERS) {
87
+ if (/[\s\\\u0000-\u001f]/.test(src)) return false;
88
+ let url;
89
+ try {
90
+ url = new URL(src);
91
+ } catch {
92
+ return false;
93
+ }
94
+ if (url.protocol !== "https:" || url.username || url.password || url.port) return false;
95
+ return allowed.some((name) => {
96
+ const provider = EMBED_PROVIDERS[name];
97
+ return src.startsWith(provider.prefix) && url.hostname === provider.hostname && url.pathname.startsWith(new URL(provider.prefix).pathname);
98
+ });
99
+ }
100
+ function normalizeLinkUrl(input) {
101
+ const value = input.trim();
102
+ if (!value) return null;
103
+ if (value.startsWith("//")) return null;
104
+ if (value.startsWith("/")) return value;
105
+ if (/^(mailto|tel):[^\s]+$/i.test(value)) return value;
106
+ try {
107
+ const url = new URL(/^[a-z][a-z0-9+.-]*:/i.test(value) ? value : `https://${value}`);
108
+ return url.protocol === "http:" || url.protocol === "https:" ? url.toString() : null;
109
+ } catch {
110
+ return null;
111
+ }
112
+ }
113
+
114
+ // src/editor/extensions.ts
115
+ var import_extension_highlight = __toESM(require("@tiptap/extension-highlight"), 1);
116
+ var import_extension_image = __toESM(require("@tiptap/extension-image"), 1);
117
+ var import_extension_placeholder = require("@tiptap/extension-placeholder");
118
+ var import_extension_table = require("@tiptap/extension-table");
119
+ var import_extension_text_align = __toESM(require("@tiptap/extension-text-align"), 1);
120
+ var import_extension_text_style = require("@tiptap/extension-text-style");
121
+ var import_starter_kit = __toESM(require("@tiptap/starter-kit"), 1);
122
+
123
+ // src/editor/video-node.ts
124
+ var import_react = require("@tiptap/react");
125
+ function createVideoEmbed(allowed) {
126
+ return import_react.Node.create({
127
+ name: "videoEmbed",
128
+ group: "block",
129
+ atom: true,
130
+ draggable: true,
131
+ addAttributes() {
132
+ return { src: { default: null } };
133
+ },
134
+ parseHTML() {
135
+ return [
136
+ {
137
+ tag: "iframe[src]",
138
+ getAttrs: (element) => {
139
+ const src = element.getAttribute("src");
140
+ return src && isAllowedEmbedSrc(src, allowed) ? { src } : false;
141
+ }
142
+ }
143
+ ];
144
+ },
145
+ renderHTML({ HTMLAttributes }) {
146
+ return [
147
+ "iframe",
148
+ (0, import_react.mergeAttributes)(HTMLAttributes, {
149
+ sandbox: "allow-scripts allow-same-origin allow-presentation",
150
+ loading: "lazy",
151
+ allowfullscreen: "true"
152
+ })
153
+ ];
154
+ }
155
+ });
156
+ }
157
+
158
+ // src/editor/extensions.ts
159
+ function buildEditorExtensions(placeholder, allowedEmbeds, dir) {
160
+ return [
161
+ import_starter_kit.default.configure({
162
+ heading: { levels: [2, 3, 4] },
163
+ link: {
164
+ openOnClick: false,
165
+ autolink: true,
166
+ defaultProtocol: "https",
167
+ isAllowedUri: (url) => normalizeLinkUrl(url) !== null
168
+ }
169
+ }),
170
+ // https images only; data: / relative paths are rejected on paste.
171
+ import_extension_image.default.extend({
172
+ parseHTML() {
173
+ return [
174
+ {
175
+ tag: "img[src]",
176
+ getAttrs: (element) => (element.getAttribute("src") ?? "").startsWith("https://") ? null : false
177
+ }
178
+ ];
179
+ }
180
+ }).configure({ allowBase64: false }),
181
+ import_extension_text_align.default.configure({
182
+ types: ["heading", "paragraph"],
183
+ alignments: ["right", "center", "left", "justify"],
184
+ defaultAlignment: dir === "rtl" ? "right" : "left"
185
+ }),
186
+ import_extension_highlight.default.configure({ multicolor: true }),
187
+ import_extension_text_style.TextStyle,
188
+ import_extension_text_style.Color,
189
+ import_extension_table.TableKit.configure({ table: { resizable: false } }),
190
+ import_extension_placeholder.Placeholder.configure({ placeholder }),
191
+ createVideoEmbed(allowedEmbeds)
192
+ ];
193
+ }
194
+
195
+ // src/editor/labels.ts
196
+ var import_react2 = require("react");
197
+ var defaultLabels = {
198
+ placeholder: "\u0645\u062A\u0646 \u0631\u0627 \u0628\u0646\u0648\u06CC\u0633\u06CC\u062F\u2026",
199
+ bold: "\u067E\u0631\u0631\u0646\u06AF",
200
+ italic: "\u0645\u0648\u0631\u0628",
201
+ underline: "\u0632\u06CC\u0631\u062E\u0637",
202
+ strike: "\u062E\u0637\u200C\u062E\u0648\u0631\u062F\u0647",
203
+ h2: "\u062A\u06CC\u062A\u0631 \u06F2",
204
+ h3: "\u062A\u06CC\u062A\u0631 \u06F3",
205
+ h4: "\u062A\u06CC\u062A\u0631 \u06F4",
206
+ alignRight: "\u0631\u0627\u0633\u062A\u200C\u0686\u06CC\u0646",
207
+ alignCenter: "\u0648\u0633\u0637\u200C\u0686\u06CC\u0646",
208
+ alignLeft: "\u0686\u067E\u200C\u0686\u06CC\u0646",
209
+ alignJustify: "\u062A\u0631\u0627\u0632 \u062F\u0648 \u0637\u0631\u0641",
210
+ bulletList: "\u0644\u06CC\u0633\u062A \u0646\u0642\u0637\u0647\u200C\u0627\u06CC",
211
+ orderedList: "\u0644\u06CC\u0633\u062A \u0634\u0645\u0627\u0631\u0647\u200C\u062F\u0627\u0631",
212
+ quote: "\u0646\u0642\u0644\u200C\u0642\u0648\u0644",
213
+ codeBlock: "\u0628\u0644\u0648\u06A9 \u06A9\u062F",
214
+ hr: "\u062E\u0637 \u062C\u062F\u0627\u06A9\u0646\u0646\u062F\u0647",
215
+ link: "\u0644\u06CC\u0646\u06A9",
216
+ unlink: "\u062D\u0630\u0641 \u0644\u06CC\u0646\u06A9",
217
+ image: "\u062A\u0635\u0648\u06CC\u0631",
218
+ video: "\u0648\u06CC\u062F\u06CC\u0648 (\u0622\u067E\u0627\u0631\u0627\u062A/\u06CC\u0648\u062A\u06CC\u0648\u0628)",
219
+ table: "\u062C\u062F\u0648\u0644",
220
+ undo: "\u0648\u0627\u06AF\u0631\u062F",
221
+ redo: "\u0627\u0632\u0646\u0648",
222
+ textColor: "\u0631\u0646\u06AF \u0645\u062A\u0646",
223
+ highlight: "\u0647\u0627\u06CC\u0644\u0627\u06CC\u062A",
224
+ remove: "\u062D\u0630\u0641",
225
+ addRow: "+ \u0633\u0637\u0631",
226
+ addColumn: "+ \u0633\u062A\u0648\u0646",
227
+ deleteRow: "\u062D\u0630\u0641 \u0633\u0637\u0631",
228
+ deleteColumn: "\u062D\u0630\u0641 \u0633\u062A\u0648\u0646",
229
+ deleteTable: "\u062D\u0630\u0641 \u062C\u062F\u0648\u0644",
230
+ cancel: "\u0627\u0646\u0635\u0631\u0627\u0641",
231
+ insert: "\u062F\u0631\u062C",
232
+ close: "\u0628\u0633\u062A\u0646",
233
+ linkTitle: "\u062F\u0631\u062C \u0644\u06CC\u0646\u06A9",
234
+ linkField: "\u0622\u062F\u0631\u0633",
235
+ linkPlaceholder: "https://\u2026 \u06CC\u0627 /news/\u2026",
236
+ linkError: "\u0622\u062F\u0631\u0633 \u0645\u0639\u062A\u0628\u0631 \u0646\u06CC\u0633\u062A (javascript: \u0648 data: \u0645\u062C\u0627\u0632 \u0646\u06CC\u0633\u062A).",
237
+ videoTitle: "\u062F\u0631\u062C \u0648\u06CC\u062F\u06CC\u0648",
238
+ videoField: "\u0644\u06CC\u0646\u06A9 \u0648\u06CC\u062F\u06CC\u0648 \u062F\u0631 \u0622\u067E\u0627\u0631\u0627\u062A \u06CC\u0627 \u06CC\u0648\u062A\u06CC\u0648\u0628",
239
+ videoPlaceholder: "https://www.aparat.com/v/\u2026",
240
+ videoError: "\u0641\u0642\u0637 \u0644\u06CC\u0646\u06A9 \u0648\u06CC\u062F\u06CC\u0648\u06CC \u0622\u067E\u0627\u0631\u0627\u062A \u06CC\u0627 \u06CC\u0648\u062A\u06CC\u0648\u0628 \u067E\u0630\u06CC\u0631\u0641\u062A\u0647 \u0645\u06CC\u200C\u0634\u0648\u062F.",
241
+ imageTitle: "\u062F\u0631\u062C \u062A\u0635\u0648\u06CC\u0631",
242
+ imageUrl: "\u0622\u062F\u0631\u0633 \u062A\u0635\u0648\u06CC\u0631 (https)",
243
+ imageUrlPlaceholder: "https://\u2026",
244
+ imageAlt: "\u0645\u062A\u0646 \u062C\u0627\u06CC\u06AF\u0632\u06CC\u0646 (alt)",
245
+ imageUpload: "\u0622\u067E\u0644\u0648\u062F \u062A\u0635\u0648\u06CC\u0631",
246
+ imageUploading: "\u062F\u0631 \u062D\u0627\u0644 \u0622\u067E\u0644\u0648\u062F\u2026",
247
+ imageLibrary: "\u0627\u0646\u062A\u062E\u0627\u0628 \u0627\u0632 \u06A9\u062A\u0627\u0628\u062E\u0627\u0646\u0647",
248
+ imageInsert: "\u062F\u0631\u062C \u062A\u0635\u0648\u06CC\u0631",
249
+ imageHttpsError: "\u0641\u0642\u0637 \u0622\u062F\u0631\u0633 https \u0628\u0631\u0627\u06CC \u062A\u0635\u0648\u06CC\u0631 \u067E\u0630\u06CC\u0631\u0641\u062A\u0647 \u0645\u06CC\u200C\u0634\u0648\u062F.",
250
+ imageUploadFailed: "\u0622\u067E\u0644\u0648\u062F \u0646\u0627\u0645\u0648\u0641\u0642 \u0628\u0648\u062F. \u062F\u0648\u0628\u0627\u0631\u0647 \u062A\u0644\u0627\u0634 \u06A9\u0646\u06CC\u062F.",
251
+ mediaTitle: "\u06A9\u062A\u0627\u0628\u062E\u0627\u0646\u0647\u200C\u06CC \u062A\u0635\u0627\u0648\u06CC\u0631",
252
+ mediaSearch: "\u062C\u0633\u062A\u062C\u0648",
253
+ mediaSearchPlaceholder: "\u062C\u0633\u062A\u062C\u0648 \u062F\u0631 \u0646\u0627\u0645 \u06CC\u0627 \u0645\u062A\u0646 \u062C\u0627\u06CC\u06AF\u0632\u06CC\u0646\u2026",
254
+ mediaEmpty: "\u062A\u0635\u0648\u06CC\u0631\u06CC \u06CC\u0627\u0641\u062A \u0646\u0634\u062F. \u06CC\u06A9 \u062A\u0635\u0648\u06CC\u0631 \u0622\u067E\u0644\u0648\u062F \u06A9\u0646\u06CC\u062F.",
255
+ mediaUpload: "\u0627\u0646\u062A\u062E\u0627\u0628 \u062A\u0635\u0648\u06CC\u0631",
256
+ mediaSelect: "\u0627\u0646\u062A\u062E\u0627\u0628",
257
+ mediaPrevPage: "\u0635\u0641\u062D\u0647\u200C\u06CC \u0642\u0628\u0644",
258
+ mediaNextPage: "\u0635\u0641\u062D\u0647\u200C\u06CC \u0628\u0639\u062F",
259
+ mediaLoadFailed: "\u062F\u0631\u06CC\u0627\u0641\u062A \u062A\u0635\u0627\u0648\u06CC\u0631 \u0646\u0627\u0645\u0648\u0641\u0642 \u0628\u0648\u062F.",
260
+ mediaUploadFailed: "\u0622\u067E\u0644\u0648\u062F \u0646\u0627\u0645\u0648\u0641\u0642 \u0628\u0648\u062F. \u062F\u0648\u0628\u0627\u0631\u0647 \u062A\u0644\u0627\u0634 \u06A9\u0646\u06CC\u062F."
261
+ };
262
+ var LabelsContext = (0, import_react2.createContext)(defaultLabels);
263
+ var useLabels = () => (0, import_react2.useContext)(LabelsContext);
264
+
265
+ // src/editor/toolbar.tsx
266
+ var React6 = __toESM(require("react"), 1);
267
+ var import_react3 = require("@tiptap/react");
268
+ var import_lucide_react5 = require("lucide-react");
269
+
270
+ // src/editor/ui/index.tsx
271
+ var React = __toESM(require("react"), 1);
272
+ var import_react_dom = require("react-dom");
273
+ var import_jsx_runtime = require("react/jsx-runtime");
274
+ var ClassNamesContext = React.createContext({});
275
+ var useClassNames = () => React.useContext(ClassNamesContext);
276
+ var cx = (...parts) => parts.filter(Boolean).join(" ");
277
+ function Button({ variant = "ghost", size = "md", pressed, className, type = "button", ...rest }) {
278
+ const slots = useClassNames();
279
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
280
+ "button",
281
+ {
282
+ type,
283
+ className: cx("bk-btn", `bk-btn-${variant}`, `bk-btn-${size}`, pressed && "bk-pressed", slots.button, className),
284
+ ...rest
285
+ }
286
+ );
287
+ }
288
+ function Input(props) {
289
+ const slots = useClassNames();
290
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { ...props, className: cx("bk-input", slots.input, props.className) });
291
+ }
292
+ function Label(props) {
293
+ const slots = useClassNames();
294
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", { ...props, className: cx("bk-label", slots.label, props.className) });
295
+ }
296
+ function Dialog({ open, ...rest }) {
297
+ if (!open || typeof document === "undefined") return null;
298
+ return (0, import_react_dom.createPortal)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(DialogShell, { ...rest }), document.body);
299
+ }
300
+ function DialogShell({ onClose, title, dir, wide, children }) {
301
+ const ref = React.useRef(null);
302
+ const slots = useClassNames();
303
+ React.useEffect(() => {
304
+ const dialog = ref.current;
305
+ if (dialog && !dialog.open) dialog.showModal();
306
+ }, []);
307
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
308
+ "dialog",
309
+ {
310
+ ref,
311
+ dir,
312
+ "aria-label": title,
313
+ className: cx("bk-dialog", wide && "bk-dialog-wide", slots.dialog),
314
+ onClose,
315
+ onClick: (event) => {
316
+ if (event.target === event.currentTarget) onClose();
317
+ },
318
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "bk-dialog-body", children })
319
+ }
320
+ );
321
+ }
322
+ function Popover({ trigger, children }) {
323
+ const [open, setOpen] = React.useState(false);
324
+ const ref = React.useRef(null);
325
+ const slots = useClassNames();
326
+ React.useEffect(() => {
327
+ if (!open) return;
328
+ const onDown = (event) => {
329
+ if (!ref.current?.contains(event.target)) setOpen(false);
330
+ };
331
+ const onKey = (event) => event.key === "Escape" && setOpen(false);
332
+ document.addEventListener("mousedown", onDown);
333
+ document.addEventListener("keydown", onKey);
334
+ return () => {
335
+ document.removeEventListener("mousedown", onDown);
336
+ document.removeEventListener("keydown", onKey);
337
+ };
338
+ }, [open]);
339
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { ref, className: "bk-popover", children: [
340
+ trigger({ onClick: () => setOpen((value) => !value), open }),
341
+ open ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: cx("bk-popover-content", slots.popover), children }) : null
342
+ ] });
343
+ }
344
+
345
+ // src/editor/color-menu.tsx
346
+ var import_jsx_runtime2 = require("react/jsx-runtime");
347
+ var TEXT_COLORS = ["#1c1c1c", "#997b2d", "#b91c1c", "#15803d", "#1d4ed8", "#7e22ce"];
348
+ var HIGHLIGHT_COLORS = ["#fde68a", "#bbf7d0", "#bfdbfe", "#fecaca", "#e9d5ff"];
349
+ var MODES = {
350
+ text: {
351
+ label: "textColor",
352
+ colors: TEXT_COLORS,
353
+ apply: (editor, color) => editor.chain().focus().setColor(color).run(),
354
+ clear: (editor) => editor.chain().focus().unsetColor().run()
355
+ },
356
+ highlight: {
357
+ label: "highlight",
358
+ colors: HIGHLIGHT_COLORS,
359
+ apply: (editor, color) => editor.chain().focus().setHighlight({ color }).run(),
360
+ clear: (editor) => editor.chain().focus().unsetHighlight().run()
361
+ }
362
+ };
363
+ function ColorMenu({ editor, mode, icon: Icon, active }) {
364
+ const labels = useLabels();
365
+ const config = MODES[mode];
366
+ const label = labels[config.label];
367
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
368
+ Popover,
369
+ {
370
+ trigger: ({ onClick, open }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
371
+ Button,
372
+ {
373
+ size: "icon",
374
+ pressed: active,
375
+ "aria-label": label,
376
+ title: label,
377
+ "aria-pressed": active,
378
+ "aria-expanded": open,
379
+ onMouseDown: (event) => event.preventDefault(),
380
+ onClick,
381
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Icon, { size: 16 })
382
+ }
383
+ ),
384
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "bk-colors", children: [
385
+ config.colors.map((color) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
386
+ "button",
387
+ {
388
+ type: "button",
389
+ className: "bk-swatch",
390
+ style: { backgroundColor: color },
391
+ "aria-label": `${label} ${color}`,
392
+ title: color,
393
+ onMouseDown: (event) => event.preventDefault(),
394
+ onClick: () => config.apply(editor, color)
395
+ },
396
+ color
397
+ )),
398
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Button, { size: "sm", onMouseDown: (event) => event.preventDefault(), onClick: () => config.clear(editor), children: labels.remove })
399
+ ] })
400
+ }
401
+ );
402
+ }
403
+
404
+ // src/editor/image-dialog.tsx
405
+ var React4 = __toESM(require("react"), 1);
406
+ var import_lucide_react4 = require("lucide-react");
407
+
408
+ // src/editor/media/media-picker.tsx
409
+ var React3 = __toESM(require("react"), 1);
410
+ var import_lucide_react3 = require("lucide-react");
411
+
412
+ // src/editor/media/media-grid.tsx
413
+ var import_lucide_react = require("lucide-react");
414
+ var import_jsx_runtime3 = require("react/jsx-runtime");
415
+ function MediaGrid({ items, selectedId, onSelect }) {
416
+ const labels = useLabels();
417
+ if (!items.length) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "bk-media-empty", children: labels.mediaEmpty });
418
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("ul", { className: "bk-media-grid", children: items.map((item) => {
419
+ const selected = item.id === selectedId;
420
+ const thumb = item.variants["320"];
421
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
422
+ "button",
423
+ {
424
+ type: "button",
425
+ "aria-pressed": selected,
426
+ title: item.originalName,
427
+ onClick: () => onSelect(item),
428
+ className: cx("bk-media-item", selected && "bk-selected"),
429
+ children: [
430
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("img", { src: thumb?.url ?? item.url, alt: item.alt ?? item.originalName, loading: "lazy" }),
431
+ selected ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "bk-media-check", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_lucide_react.Check, { size: 16 }) }) : null
432
+ ]
433
+ }
434
+ ) }, item.id);
435
+ }) });
436
+ }
437
+
438
+ // src/editor/media/media-upload-button.tsx
439
+ var React2 = __toESM(require("react"), 1);
440
+ var import_lucide_react2 = require("lucide-react");
441
+ var import_jsx_runtime4 = require("react/jsx-runtime");
442
+ function MediaUploadButton({ api, onUploaded }) {
443
+ const labels = useLabels();
444
+ const inputRef = React2.useRef(null);
445
+ const [busy, setBusy] = React2.useState(false);
446
+ const [error, setError] = React2.useState(null);
447
+ const handleChange = async (event) => {
448
+ const file = event.target.files?.[0];
449
+ event.target.value = "";
450
+ if (!file) return;
451
+ const formData = new FormData();
452
+ formData.append("file", file);
453
+ setBusy(true);
454
+ setError(null);
455
+ try {
456
+ const result = await api.upload(formData);
457
+ const item = result.items[0];
458
+ if (result.error || !item) setError(result.error ?? labels.mediaUploadFailed);
459
+ else onUploaded(item);
460
+ } catch {
461
+ setError(labels.mediaUploadFailed);
462
+ } finally {
463
+ setBusy(false);
464
+ }
465
+ };
466
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "bk-media-upload", children: [
467
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
468
+ "input",
469
+ {
470
+ ref: inputRef,
471
+ type: "file",
472
+ className: "bk-sr-only",
473
+ tabIndex: -1,
474
+ accept: "image/jpeg,image/png,image/webp,image/gif",
475
+ onChange: handleChange
476
+ }
477
+ ),
478
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(Button, { variant: "outline", size: "sm", disabled: busy, onClick: () => inputRef.current?.click(), children: [
479
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_lucide_react2.UploadCloud, { size: 16 }),
480
+ busy ? labels.imageUploading : labels.mediaUpload
481
+ ] }),
482
+ error ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { role: "alert", className: "bk-error", children: error }) : null
483
+ ] });
484
+ }
485
+
486
+ // src/editor/media/media-picker.tsx
487
+ var import_jsx_runtime5 = require("react/jsx-runtime");
488
+ var toPicked = (item) => ({ url: item.url, alt: item.alt ?? void 0 });
489
+ function MediaPicker({ api, dir, onSelect, onClose }) {
490
+ const labels = useLabels();
491
+ const [q, setQ] = React3.useState("");
492
+ const [debouncedQ, setDebouncedQ] = React3.useState("");
493
+ const [page, setPage] = React3.useState(1);
494
+ const [data, setData] = React3.useState({ items: [], total: 0, pageSize: 24 });
495
+ const [error, setError] = React3.useState(null);
496
+ const [loading, setLoading] = React3.useState(false);
497
+ const [selected, setSelected] = React3.useState(null);
498
+ const requestId = React3.useRef(0);
499
+ React3.useEffect(() => {
500
+ const timer = setTimeout(() => {
501
+ setDebouncedQ(q.trim());
502
+ setPage(1);
503
+ }, 250);
504
+ return () => clearTimeout(timer);
505
+ }, [q]);
506
+ React3.useEffect(() => {
507
+ const id = ++requestId.current;
508
+ setLoading(true);
509
+ api.list({ q: debouncedQ || void 0, page }).then((result) => {
510
+ if (id !== requestId.current) return;
511
+ if (result.error) setError(result.error);
512
+ else {
513
+ setError(null);
514
+ setData({ items: result.items, total: result.total, pageSize: result.pageSize || 24 });
515
+ }
516
+ }).catch(() => id === requestId.current && setError(labels.mediaLoadFailed)).finally(() => id === requestId.current && setLoading(false));
517
+ }, [api, debouncedQ, page, labels.mediaLoadFailed]);
518
+ const pageCount = Math.max(1, Math.ceil(data.total / data.pageSize));
519
+ const Prev = dir === "ltr" ? import_lucide_react3.ChevronLeft : import_lucide_react3.ChevronRight;
520
+ const Next = dir === "ltr" ? import_lucide_react3.ChevronRight : import_lucide_react3.ChevronLeft;
521
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(Dialog, { open: true, onClose, title: labels.mediaTitle, dir, wide: true, children: [
522
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("header", { className: "bk-dialog-header", children: [
523
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("h2", { children: labels.mediaTitle }),
524
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Button, { size: "icon", "aria-label": labels.close, onClick: onClose, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_lucide_react3.X, { size: 16 }) })
525
+ ] }),
526
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: "bk-media-toolbar", children: [
527
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
528
+ Input,
529
+ {
530
+ value: q,
531
+ onChange: (event) => setQ(event.target.value),
532
+ placeholder: labels.mediaSearchPlaceholder,
533
+ "aria-label": labels.mediaSearch
534
+ }
535
+ ),
536
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(MediaUploadButton, { api, onUploaded: (item) => onSelect(toPicked(item)) })
537
+ ] }),
538
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "bk-media-body", "aria-busy": loading, children: error ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("p", { role: "alert", className: "bk-error", children: error }) : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(MediaGrid, { items: data.items, selectedId: selected?.id ?? null, onSelect: setSelected }) }),
539
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("footer", { className: "bk-dialog-footer bk-media-footer", children: [
540
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: "bk-pager", children: [
541
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Button, { variant: "outline", size: "icon", "aria-label": labels.mediaPrevPage, disabled: page <= 1, onClick: () => setPage(page - 1), children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Prev, { size: 16 }) }),
542
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("span", { children: [
543
+ page,
544
+ " / ",
545
+ pageCount
546
+ ] }),
547
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Button, { variant: "outline", size: "icon", "aria-label": labels.mediaNextPage, disabled: page >= pageCount, onClick: () => setPage(page + 1), children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Next, { size: 16 }) })
548
+ ] }),
549
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: "bk-actions", children: [
550
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Button, { variant: "outline", onClick: onClose, children: labels.cancel }),
551
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Button, { variant: "primary", disabled: !selected, onClick: () => selected && onSelect(toPicked(selected)), children: labels.mediaSelect })
552
+ ] })
553
+ ] })
554
+ ] });
555
+ }
556
+
557
+ // src/editor/image-dialog.tsx
558
+ var import_jsx_runtime6 = require("react/jsx-runtime");
559
+ var isHttps = (value) => /^https:\/\/\S+$/i.test(value);
560
+ function ImageDialog({ open, onOpenChange, dir, ...form }) {
561
+ const labels = useLabels();
562
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Dialog, { open, onClose: () => onOpenChange(false), title: labels.imageTitle, dir, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ImageForm, { ...form, dir, onClose: () => onOpenChange(false) }) });
563
+ }
564
+ function ImageForm({
565
+ onSubmit,
566
+ onClose,
567
+ dir,
568
+ uploadImage,
569
+ mediaApi,
570
+ renderMediaPicker,
571
+ resolveImageSrc
572
+ }) {
573
+ const labels = useLabels();
574
+ const id = React4.useId();
575
+ const fileRef = React4.useRef(null);
576
+ const [src, setSrc] = React4.useState("");
577
+ const [alt, setAlt] = React4.useState("");
578
+ const [error, setError] = React4.useState(null);
579
+ const [uploading, setUploading] = React4.useState(false);
580
+ const [pickerOpen, setPickerOpen] = React4.useState(false);
581
+ const applyPicked = (image) => {
582
+ const url = resolveImageSrc ? resolveImageSrc(image.url) : image.url;
583
+ if (!isHttps(url)) {
584
+ setError(labels.imageHttpsError);
585
+ return false;
586
+ }
587
+ setError(null);
588
+ setSrc(url);
589
+ if (image.alt && !alt) setAlt(image.alt);
590
+ return true;
591
+ };
592
+ const handleFile = async (event) => {
593
+ const file = event.target.files?.[0];
594
+ event.target.value = "";
595
+ if (!file || !uploadImage) return;
596
+ setUploading(true);
597
+ setError(null);
598
+ try {
599
+ applyPicked(await uploadImage(file));
600
+ } catch (e) {
601
+ setError(e instanceof Error && e.message ? e.message : labels.imageUploadFailed);
602
+ } finally {
603
+ setUploading(false);
604
+ }
605
+ };
606
+ const submit = (event) => {
607
+ event.preventDefault();
608
+ event.stopPropagation();
609
+ const value = src.trim();
610
+ if (!isHttps(value)) {
611
+ setError(labels.imageHttpsError);
612
+ return;
613
+ }
614
+ onSubmit({ src: value, alt: alt.trim() });
615
+ onClose();
616
+ };
617
+ const hasLibrary = Boolean(renderMediaPicker || mediaApi);
618
+ const closePicker = () => setPickerOpen(false);
619
+ const onPicked = (image) => {
620
+ if (applyPicked(image)) closePicker();
621
+ };
622
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_jsx_runtime6.Fragment, { children: [
623
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("form", { onSubmit: submit, className: "bk-form", children: [
624
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("h2", { className: "bk-dialog-title", children: labels.imageTitle }),
625
+ uploadImage || hasLibrary ? /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "bk-actions bk-actions-start", children: [
626
+ uploadImage ? /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_jsx_runtime6.Fragment, { children: [
627
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("input", { ref: fileRef, type: "file", accept: "image/*", className: "bk-sr-only", tabIndex: -1, onChange: handleFile }),
628
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(Button, { variant: "outline", size: "sm", disabled: uploading, onClick: () => fileRef.current?.click(), children: [
629
+ uploading ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_lucide_react4.Loader2, { size: 16, className: "bk-spin" }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_lucide_react4.ImagePlus, { size: 16 }),
630
+ uploading ? labels.imageUploading : labels.imageUpload
631
+ ] })
632
+ ] }) : null,
633
+ hasLibrary ? /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(Button, { variant: "outline", size: "sm", onClick: () => setPickerOpen(true), children: [
634
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_lucide_react4.Images, { size: 16 }),
635
+ labels.imageLibrary
636
+ ] }) : null
637
+ ] }) : null,
638
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "bk-field", children: [
639
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Label, { htmlFor: `${id}-src`, children: labels.imageUrl }),
640
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
641
+ Input,
642
+ {
643
+ id: `${id}-src`,
644
+ dir: "ltr",
645
+ value: src,
646
+ placeholder: labels.imageUrlPlaceholder,
647
+ onChange: (event) => setSrc(event.target.value)
648
+ }
649
+ )
650
+ ] }),
651
+ src && isHttps(src) ? (
652
+ // eslint-disable-next-line @next/next/no-img-element
653
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("img", { src, alt: "", className: "bk-image-preview" })
654
+ ) : null,
655
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "bk-field", children: [
656
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Label, { htmlFor: `${id}-alt`, children: labels.imageAlt }),
657
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Input, { id: `${id}-alt`, value: alt, onChange: (event) => setAlt(event.target.value) })
658
+ ] }),
659
+ error ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { role: "alert", className: "bk-error", children: error }) : null,
660
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "bk-dialog-footer", children: [
661
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Button, { variant: "outline", onClick: onClose, children: labels.cancel }),
662
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Button, { variant: "primary", type: "submit", disabled: !src.trim() || uploading, children: labels.imageInsert })
663
+ ] })
664
+ ] }),
665
+ pickerOpen ? renderMediaPicker ? renderMediaPicker({ onSelect: onPicked, close: closePicker }) : mediaApi ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(MediaPicker, { api: mediaApi, dir, onSelect: onPicked, onClose: closePicker }) : null : null
666
+ ] });
667
+ }
668
+
669
+ // src/editor/url-dialog.tsx
670
+ var React5 = __toESM(require("react"), 1);
671
+ var import_jsx_runtime7 = require("react/jsx-runtime");
672
+ function UrlDialog({ open, onOpenChange, ...form }) {
673
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Dialog, { open, onClose: () => onOpenChange(false), title: form.title, dir: form.dir, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(UrlForm, { ...form, onClose: () => onOpenChange(false) }) });
674
+ }
675
+ function UrlForm({
676
+ title,
677
+ label,
678
+ placeholder,
679
+ initialValue = "",
680
+ errorMessage,
681
+ parse,
682
+ onSubmit,
683
+ onClose
684
+ }) {
685
+ const labels = useLabels();
686
+ const id = React5.useId();
687
+ const [value, setValue] = React5.useState(initialValue);
688
+ const [error, setError] = React5.useState(null);
689
+ const submit = (event) => {
690
+ event.preventDefault();
691
+ event.stopPropagation();
692
+ const url = parse(value);
693
+ if (!url) {
694
+ setError(errorMessage);
695
+ return;
696
+ }
697
+ onSubmit(url);
698
+ onClose();
699
+ };
700
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("form", { onSubmit: submit, className: "bk-form", children: [
701
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("h2", { className: "bk-dialog-title", children: title }),
702
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "bk-field", children: [
703
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Label, { htmlFor: id, children: label }),
704
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
705
+ Input,
706
+ {
707
+ id,
708
+ dir: "ltr",
709
+ value,
710
+ placeholder,
711
+ onChange: (event) => setValue(event.target.value),
712
+ autoFocus: true
713
+ }
714
+ ),
715
+ error ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("p", { role: "alert", className: "bk-error", children: error }) : null
716
+ ] }),
717
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "bk-dialog-footer", children: [
718
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Button, { variant: "outline", onClick: onClose, children: labels.cancel }),
719
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Button, { variant: "primary", type: "submit", children: labels.insert })
720
+ ] })
721
+ ] });
722
+ }
723
+
724
+ // src/editor/toolbar.tsx
725
+ var import_jsx_runtime8 = require("react/jsx-runtime");
726
+ var chain = (editor) => editor.chain().focus();
727
+ var GROUPS = [
728
+ [
729
+ { key: "bold", icon: import_lucide_react5.Bold, label: "bold", isActive: (e) => e.isActive("bold"), run: (e) => chain(e).toggleBold().run() },
730
+ { key: "italic", icon: import_lucide_react5.Italic, label: "italic", isActive: (e) => e.isActive("italic"), run: (e) => chain(e).toggleItalic().run() },
731
+ { key: "underline", icon: import_lucide_react5.Underline, label: "underline", isActive: (e) => e.isActive("underline"), run: (e) => chain(e).toggleUnderline().run() },
732
+ { key: "strike", icon: import_lucide_react5.Strikethrough, label: "strike", isActive: (e) => e.isActive("strike"), run: (e) => chain(e).toggleStrike().run() }
733
+ ],
734
+ [
735
+ { key: "h2", icon: import_lucide_react5.Heading2, label: "h2", isActive: (e) => e.isActive("heading", { level: 2 }), run: (e) => chain(e).toggleHeading({ level: 2 }).run() },
736
+ { key: "h3", icon: import_lucide_react5.Heading3, label: "h3", isActive: (e) => e.isActive("heading", { level: 3 }), run: (e) => chain(e).toggleHeading({ level: 3 }).run() },
737
+ { key: "h4", icon: import_lucide_react5.Heading4, label: "h4", isActive: (e) => e.isActive("heading", { level: 4 }), run: (e) => chain(e).toggleHeading({ level: 4 }).run() }
738
+ ],
739
+ [
740
+ { key: "right", icon: import_lucide_react5.AlignRight, label: "alignRight", isActive: (e) => e.isActive({ textAlign: "right" }), run: (e) => chain(e).setTextAlign("right").run() },
741
+ { key: "center", icon: import_lucide_react5.AlignCenter, label: "alignCenter", isActive: (e) => e.isActive({ textAlign: "center" }), run: (e) => chain(e).setTextAlign("center").run() },
742
+ { key: "left", icon: import_lucide_react5.AlignLeft, label: "alignLeft", isActive: (e) => e.isActive({ textAlign: "left" }), run: (e) => chain(e).setTextAlign("left").run() },
743
+ { key: "justify", icon: import_lucide_react5.AlignJustify, label: "alignJustify", isActive: (e) => e.isActive({ textAlign: "justify" }), run: (e) => chain(e).setTextAlign("justify").run() }
744
+ ],
745
+ [
746
+ { key: "bullet", icon: import_lucide_react5.List, label: "bulletList", isActive: (e) => e.isActive("bulletList"), run: (e) => chain(e).toggleBulletList().run() },
747
+ { key: "ordered", icon: import_lucide_react5.ListOrdered, label: "orderedList", isActive: (e) => e.isActive("orderedList"), run: (e) => chain(e).toggleOrderedList().run() },
748
+ { key: "quote", icon: import_lucide_react5.Quote, label: "quote", isActive: (e) => e.isActive("blockquote"), run: (e) => chain(e).toggleBlockquote().run() },
749
+ { key: "code", icon: import_lucide_react5.Code2, label: "codeBlock", isActive: (e) => e.isActive("codeBlock"), run: (e) => chain(e).toggleCodeBlock().run() },
750
+ { key: "hr", icon: import_lucide_react5.Minus, label: "hr", run: (e) => chain(e).setHorizontalRule().run() }
751
+ ],
752
+ [
753
+ { key: "link", icon: import_lucide_react5.Link2, label: "link", isActive: (e) => e.isActive("link"), run: (_e, open) => open("link") },
754
+ { key: "unlink", icon: import_lucide_react5.Link2Off, label: "unlink", isDisabled: (e) => !e.isActive("link"), run: (e) => chain(e).extendMarkRange("link").unsetLink().run() },
755
+ { key: "image", icon: import_lucide_react5.ImagePlus, label: "image", run: (_e, open) => open("image") },
756
+ { key: "video", icon: import_lucide_react5.Video, label: "video", run: (_e, open) => open("video") },
757
+ { key: "table", icon: import_lucide_react5.Table2, label: "table", isActive: (e) => e.isActive("table"), run: (e) => chain(e).insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run() }
758
+ ],
759
+ [
760
+ { key: "undo", icon: import_lucide_react5.Undo2, label: "undo", isDisabled: (e) => !e.can().undo(), run: (e) => chain(e).undo().run() },
761
+ { key: "redo", icon: import_lucide_react5.Redo2, label: "redo", isDisabled: (e) => !e.can().redo(), run: (e) => chain(e).redo().run() }
762
+ ]
763
+ ];
764
+ var TABLE_ACTIONS = [
765
+ { key: "row-after", label: "addRow", run: (e) => chain(e).addRowAfter().run() },
766
+ { key: "col-after", label: "addColumn", run: (e) => chain(e).addColumnAfter().run() },
767
+ { key: "row-delete", label: "deleteRow", run: (e) => chain(e).deleteRow().run() },
768
+ { key: "col-delete", label: "deleteColumn", run: (e) => chain(e).deleteColumn().run() },
769
+ { key: "table-delete", label: "deleteTable", run: (e) => chain(e).deleteTable().run() }
770
+ ];
771
+ var ALL_BUTTONS = GROUPS.flat();
772
+ var BUTTONS_BY_KEY = new Map(ALL_BUTTONS.map((b) => [b.key, b]));
773
+ var defaultToolbar = [
774
+ "bold",
775
+ "italic",
776
+ "underline",
777
+ "strike",
778
+ "textColor",
779
+ "highlight",
780
+ "|",
781
+ "h2",
782
+ "h3",
783
+ "h4",
784
+ "|",
785
+ "right",
786
+ "center",
787
+ "left",
788
+ "justify",
789
+ "|",
790
+ "bullet",
791
+ "ordered",
792
+ "quote",
793
+ "code",
794
+ "hr",
795
+ "|",
796
+ "link",
797
+ "unlink",
798
+ "image",
799
+ "video",
800
+ "table",
801
+ "|",
802
+ "undo",
803
+ "redo"
804
+ ];
805
+ function normalizeItems(items) {
806
+ const out = [];
807
+ for (const item of items) {
808
+ const valid = item === "|" || item === "textColor" || item === "highlight" || BUTTONS_BY_KEY.has(item);
809
+ if (!valid || item === "|" && (out.length === 0 || out[out.length - 1] === "|")) continue;
810
+ out.push(item);
811
+ }
812
+ while (out[out.length - 1] === "|") out.pop();
813
+ return out;
814
+ }
815
+ function RichTextToolbar({ editor, items = defaultToolbar, dir, allowedEmbeds, ...imageProps }) {
816
+ const labels = useLabels();
817
+ const slots = useClassNames();
818
+ const entries = React6.useMemo(() => normalizeItems(items), [items]);
819
+ const [dialog, setDialog] = React6.useState(null);
820
+ const state = (0, import_react3.useEditorState)({
821
+ editor,
822
+ selector: ({ editor: e }) => ({
823
+ active: Object.fromEntries(ALL_BUTTONS.map((b) => [b.key, b.isActive?.(e) ?? false])),
824
+ disabled: Object.fromEntries(ALL_BUTTONS.map((b) => [b.key, b.isDisabled?.(e) ?? false])),
825
+ color: e.isActive("textStyle", { color: /.+/ }),
826
+ highlight: e.isActive("highlight"),
827
+ inTable: e.isActive("table"),
828
+ href: e.getAttributes("link").href ?? ""
829
+ })
830
+ });
831
+ const closeDialog = (open) => !open && setDialog(null);
832
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { className: cx("bk-toolbar", slots.toolbar), children: [
833
+ entries.map((item, index) => {
834
+ if (item === "|") return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: cx("bk-sep", slots.separator), "aria-hidden": true }, `sep-${index}`);
835
+ if (item === "textColor") return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ColorMenu, { editor, mode: "text", icon: import_lucide_react5.Palette, active: state.color }, item);
836
+ if (item === "highlight") return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ColorMenu, { editor, mode: "highlight", icon: import_lucide_react5.Highlighter, active: state.highlight }, item);
837
+ const button = BUTTONS_BY_KEY.get(item);
838
+ const label = labels[button.label];
839
+ const active = state.active[button.key] ?? false;
840
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
841
+ Button,
842
+ {
843
+ size: "icon",
844
+ pressed: active,
845
+ "aria-label": label,
846
+ title: label,
847
+ "aria-pressed": button.isActive ? active : void 0,
848
+ disabled: state.disabled[button.key],
849
+ onMouseDown: (event) => event.preventDefault(),
850
+ onClick: () => button.run(editor, setDialog),
851
+ children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(button.icon, { size: 16 })
852
+ },
853
+ button.key
854
+ );
855
+ }),
856
+ state.inTable ? /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: cx("bk-table-tools", slots.tableTools), children: TABLE_ACTIONS.map((action) => /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(Button, { variant: "outline", size: "sm", onMouseDown: (event) => event.preventDefault(), onClick: () => action.run(editor), children: labels[action.label] }, action.key)) }) : null,
857
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
858
+ UrlDialog,
859
+ {
860
+ open: dialog === "link",
861
+ onOpenChange: closeDialog,
862
+ dir,
863
+ title: labels.linkTitle,
864
+ label: labels.linkField,
865
+ placeholder: labels.linkPlaceholder,
866
+ initialValue: state.href,
867
+ errorMessage: labels.linkError,
868
+ parse: normalizeLinkUrl,
869
+ onSubmit: (href) => chain(editor).extendMarkRange("link").setLink({ href }).run()
870
+ }
871
+ ),
872
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
873
+ UrlDialog,
874
+ {
875
+ open: dialog === "video",
876
+ onOpenChange: closeDialog,
877
+ dir,
878
+ title: labels.videoTitle,
879
+ label: labels.videoField,
880
+ placeholder: labels.videoPlaceholder,
881
+ errorMessage: labels.videoError,
882
+ parse: (value) => toVideoEmbedUrl(value, allowedEmbeds),
883
+ onSubmit: (src) => chain(editor).insertContent({ type: "videoEmbed", attrs: { src } }).run()
884
+ }
885
+ ),
886
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
887
+ ImageDialog,
888
+ {
889
+ ...imageProps,
890
+ dir,
891
+ open: dialog === "image",
892
+ onOpenChange: closeDialog,
893
+ onSubmit: (image) => chain(editor).setImage(image).run()
894
+ }
895
+ )
896
+ ] });
897
+ }
898
+
899
+ // src/editor/rich-text-editor.tsx
900
+ var import_jsx_runtime9 = require("react/jsx-runtime");
901
+ var EMPTY = {};
902
+ function RichTextEditor({
903
+ value,
904
+ onChange,
905
+ placeholder,
906
+ invalid,
907
+ id,
908
+ className,
909
+ classNames,
910
+ toolbar,
911
+ uploadImage,
912
+ mediaApi,
913
+ renderMediaPicker,
914
+ resolveImageSrc,
915
+ allowedEmbeds = ALL_EMBED_PROVIDERS,
916
+ labels,
917
+ dir = "rtl"
918
+ }) {
919
+ const merged = React7.useMemo(() => ({ ...defaultLabels, ...labels }), [labels]);
920
+ const onChangeRef = React7.useRef(onChange);
921
+ onChangeRef.current = onChange;
922
+ const editor = (0, import_react4.useEditor)({
923
+ extensions: buildEditorExtensions(placeholder ?? merged.placeholder, allowedEmbeds, dir),
924
+ content: value,
925
+ immediatelyRender: false,
926
+ // SSR-safe
927
+ editorProps: {
928
+ attributes: { class: cx("bk-rich bk-editor-content", classNames?.content), dir, ...id ? { id } : {} }
929
+ },
930
+ onUpdate: ({ editor: e }) => onChangeRef.current(e.isEmpty ? "" : e.getHTML())
931
+ });
932
+ React7.useEffect(() => {
933
+ if (!editor) return;
934
+ const current = editor.isEmpty ? "" : editor.getHTML();
935
+ if (value !== current) editor.commands.setContent(value, { emitUpdate: false });
936
+ }, [editor, value]);
937
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(LabelsContext.Provider, { value: merged, children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(ClassNamesContext.Provider, { value: classNames ?? EMPTY, children: /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("div", { className: cx("bk-editor", invalid && "bk-invalid", classNames?.root, className), dir, children: [
938
+ editor ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
939
+ RichTextToolbar,
940
+ {
941
+ editor,
942
+ dir,
943
+ items: toolbar,
944
+ allowedEmbeds,
945
+ uploadImage,
946
+ mediaApi,
947
+ renderMediaPicker,
948
+ resolveImageSrc
949
+ }
950
+ ) : null,
951
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_react4.EditorContent, { editor })
952
+ ] }) }) });
953
+ }
954
+ // Annotate the CommonJS export names for ESM import in node:
955
+ 0 && (module.exports = {
956
+ RichTextEditor,
957
+ defaultLabels,
958
+ defaultToolbar,
959
+ isAllowedEmbedSrc,
960
+ normalizeLinkUrl,
961
+ toVideoEmbedUrl
962
+ });
963
+ //# sourceMappingURL=index.cjs.map