neuphlo-editor 1.4.0 → 1.4.2

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,616 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all2) => {
6
+ for (var name in all2)
7
+ __defProp(target, name, { get: all2[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+
19
+ // src/headless/index.ts
20
+ import { useCurrentEditor as useCurrentEditor4 } from "@tiptap/react";
21
+
22
+ // src/headless/components/editor.tsx
23
+ import { EditorProvider } from "@tiptap/react";
24
+ import { forwardRef } from "react";
25
+ import { Provider } from "jotai";
26
+
27
+ // src/headless/utils/store.ts
28
+ var store_exports = {};
29
+ __export(store_exports, {
30
+ novelStore: () => novelStore
31
+ });
32
+ __reExport(store_exports, jotai_star);
33
+ import { createStore } from "jotai";
34
+ import * as jotai_star from "jotai";
35
+ var novelStore = createStore();
36
+
37
+ // src/headless/components/editor.tsx
38
+ import { jsx } from "react/jsx-runtime";
39
+ var EditorRoot = ({ children }) => {
40
+ return /* @__PURE__ */ jsx(Provider, { store: novelStore, children });
41
+ };
42
+ var EditorContent = forwardRef(
43
+ ({ className, children, initialContent, content, ...rest }, ref) => {
44
+ const effectiveContent = content ?? initialContent;
45
+ return /* @__PURE__ */ jsx("div", { ref, className, children: /* @__PURE__ */ jsx(EditorProvider, { ...rest, content: effectiveContent, children }) });
46
+ }
47
+ );
48
+ EditorContent.displayName = "EditorContent";
49
+
50
+ // src/headless/components/editor-bubble.tsx
51
+ import { useCurrentEditor } from "@tiptap/react";
52
+ import { BubbleMenu as BubbleMenuReact } from "@tiptap/react/menus";
53
+ import { jsx as jsx2 } from "react/jsx-runtime";
54
+ function EditorBubble({ className, children, ...rest }) {
55
+ const { editor } = useCurrentEditor();
56
+ if (!editor) return null;
57
+ return /* @__PURE__ */ jsx2(BubbleMenuReact, { editor, ...rest, children: /* @__PURE__ */ jsx2("div", { className, children }) });
58
+ }
59
+
60
+ // src/headless/components/editor-bubble-item.tsx
61
+ import { forwardRef as forwardRef2, isValidElement, cloneElement } from "react";
62
+ import { useCurrentEditor as useCurrentEditor2 } from "@tiptap/react";
63
+ import { jsx as jsx3 } from "react/jsx-runtime";
64
+ var EditorBubbleItem = forwardRef2(({ children, asChild, onSelect, ...rest }, ref) => {
65
+ const { editor } = useCurrentEditor2();
66
+ if (!editor) return null;
67
+ const handleClick = (e) => {
68
+ e.preventDefault();
69
+ onSelect?.(editor);
70
+ };
71
+ if (asChild && isValidElement(children)) {
72
+ const child = children;
73
+ const childOnClick = child.props?.onClick;
74
+ const mergedOnClick = (e) => {
75
+ childOnClick?.(e);
76
+ if (!e?.defaultPrevented) onSelect?.(editor);
77
+ };
78
+ return cloneElement(child, {
79
+ ...rest,
80
+ ref: child.ref ?? ref,
81
+ onClick: mergedOnClick
82
+ });
83
+ }
84
+ return /* @__PURE__ */ jsx3("div", { ref, ...rest, onClick: handleClick, children });
85
+ });
86
+ EditorBubbleItem.displayName = "EditorBubbleItem";
87
+
88
+ // src/headless/components/editor-command.tsx
89
+ import { useAtom, useSetAtom } from "jotai";
90
+ import { useEffect, forwardRef as forwardRef3 } from "react";
91
+ import { Command } from "cmdk";
92
+
93
+ // src/headless/utils/atoms.ts
94
+ import { atom } from "jotai";
95
+ var queryAtom = atom("");
96
+ var rangeAtom = atom(null);
97
+
98
+ // src/headless/components/editor-command.tsx
99
+ import tunnel from "tunnel-rat";
100
+ import { jsx as jsx4, jsxs } from "react/jsx-runtime";
101
+ var commandTunnel = tunnel();
102
+ var EditorCommandOut = ({
103
+ query,
104
+ range
105
+ }) => {
106
+ const setQuery = useSetAtom(queryAtom, { store: novelStore });
107
+ const setRange = useSetAtom(rangeAtom, { store: novelStore });
108
+ useEffect(() => {
109
+ setQuery(query);
110
+ }, [query, setQuery]);
111
+ useEffect(() => {
112
+ setRange(range);
113
+ }, [range, setRange]);
114
+ useEffect(() => {
115
+ const navigationKeys = ["ArrowUp", "ArrowDown", "Enter"];
116
+ const onKeyDown = (e) => {
117
+ if (navigationKeys.includes(e.key)) {
118
+ e.preventDefault();
119
+ const commandRef = document.querySelector("#slash-command");
120
+ if (commandRef)
121
+ commandRef.dispatchEvent(
122
+ new KeyboardEvent("keydown", {
123
+ key: e.key,
124
+ cancelable: true,
125
+ bubbles: true
126
+ })
127
+ );
128
+ return false;
129
+ }
130
+ };
131
+ document.addEventListener("keydown", onKeyDown);
132
+ return () => {
133
+ document.removeEventListener("keydown", onKeyDown);
134
+ };
135
+ }, []);
136
+ return /* @__PURE__ */ jsx4(commandTunnel.Out, {});
137
+ };
138
+ var CommandAny = Command;
139
+ var EditorCommand = forwardRef3(
140
+ ({ children, className, ...rest }, ref) => {
141
+ const [query, setQuery] = useAtom(queryAtom);
142
+ return /* @__PURE__ */ jsx4(commandTunnel.In, { children: /* @__PURE__ */ jsxs(
143
+ CommandAny,
144
+ {
145
+ ref,
146
+ onKeyDown: (e) => {
147
+ e.stopPropagation();
148
+ },
149
+ id: "slash-command",
150
+ className,
151
+ ...rest,
152
+ children: [
153
+ /* @__PURE__ */ jsx4(
154
+ CommandAny.Input,
155
+ {
156
+ value: query,
157
+ onValueChange: setQuery,
158
+ style: { display: "none" }
159
+ }
160
+ ),
161
+ children
162
+ ]
163
+ }
164
+ ) });
165
+ }
166
+ );
167
+ var EditorCommandList = Command.List;
168
+ EditorCommand.displayName = "EditorCommand";
169
+
170
+ // src/headless/components/editor-command-item.tsx
171
+ import { forwardRef as forwardRef4 } from "react";
172
+ import { CommandEmpty, CommandItem } from "cmdk";
173
+ import { useCurrentEditor as useCurrentEditor3 } from "@tiptap/react";
174
+ import { useAtomValue } from "jotai";
175
+ import { jsx as jsx5 } from "react/jsx-runtime";
176
+ var CommandItemAny = CommandItem;
177
+ var CommandEmptyAny = CommandEmpty;
178
+ var EditorCommandItem = forwardRef4(({ children, onCommand, ...rest }, ref) => {
179
+ const { editor } = useCurrentEditor3();
180
+ const range = useAtomValue(rangeAtom);
181
+ if (!editor || !range) return null;
182
+ return /* @__PURE__ */ jsx5(
183
+ CommandItemAny,
184
+ {
185
+ ref,
186
+ ...rest,
187
+ onSelect: () => onCommand({ editor, range }),
188
+ children
189
+ }
190
+ );
191
+ });
192
+ EditorCommandItem.displayName = "EditorCommandItem";
193
+ var EditorCommandEmpty = CommandEmptyAny;
194
+
195
+ // src/headless/extensions/index.ts
196
+ import { StarterKit } from "@tiptap/starter-kit";
197
+ import { Placeholder } from "@tiptap/extension-placeholder";
198
+
199
+ // src/headless/extensions/CodeBlock/CodeBlock.ts
200
+ import { CodeBlockLowlight } from "@tiptap/extension-code-block-lowlight";
201
+ import { all, createLowlight } from "lowlight";
202
+ var lowlight = createLowlight(all);
203
+ var CodeBlock = CodeBlockLowlight.configure({
204
+ lowlight
205
+ });
206
+
207
+ // src/headless/extensions/Link/Link.ts
208
+ import { mergeAttributes } from "@tiptap/core";
209
+ import TiptapLink from "@tiptap/extension-link";
210
+ import { Plugin } from "@tiptap/pm/state";
211
+ var Link = TiptapLink.extend({
212
+ inclusive: false,
213
+ parseHTML() {
214
+ return [
215
+ {
216
+ tag: 'a[href]:not([data-type="button"]):not([href *= "javascript:" i])'
217
+ }
218
+ ];
219
+ },
220
+ renderHTML({ HTMLAttributes }) {
221
+ return [
222
+ "a",
223
+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
224
+ class: "link"
225
+ }),
226
+ 0
227
+ ];
228
+ },
229
+ addProseMirrorPlugins() {
230
+ const { editor } = this;
231
+ return [
232
+ ...this.parent?.() || [],
233
+ new Plugin({
234
+ props: {
235
+ handleKeyDown: (view, event) => {
236
+ const { selection } = editor.state;
237
+ if (event.key === "Escape" && selection.empty !== true) {
238
+ editor.commands.focus(selection.to, { scrollIntoView: false });
239
+ }
240
+ return false;
241
+ }
242
+ }
243
+ })
244
+ ];
245
+ }
246
+ });
247
+
248
+ // src/headless/extensions/ImageBlock/ImageBlock.ts
249
+ import { mergeAttributes as mergeAttributes2 } from "@tiptap/core";
250
+ import { Image as TiptapImage } from "@tiptap/extension-image";
251
+ import { ReactNodeViewRenderer } from "@tiptap/react";
252
+ import { Plugin as Plugin2, PluginKey } from "@tiptap/pm/state";
253
+ var ImageBlock = TiptapImage.extend({
254
+ name: "imageBlock",
255
+ group: "block",
256
+ defining: true,
257
+ isolating: true,
258
+ addOptions() {
259
+ return {
260
+ ...this.parent?.(),
261
+ uploadImage: void 0,
262
+ inline: false
263
+ };
264
+ },
265
+ addAttributes() {
266
+ return {
267
+ src: {
268
+ default: "",
269
+ parseHTML: (element) => element.getAttribute("src"),
270
+ renderHTML: (attributes) => ({
271
+ src: attributes.src
272
+ })
273
+ },
274
+ width: {
275
+ default: "100%",
276
+ parseHTML: (element) => element.getAttribute("data-width"),
277
+ renderHTML: (attributes) => ({
278
+ "data-width": attributes.width
279
+ })
280
+ },
281
+ align: {
282
+ default: "center",
283
+ parseHTML: (element) => element.getAttribute("data-align"),
284
+ renderHTML: (attributes) => ({
285
+ "data-align": attributes.align
286
+ })
287
+ },
288
+ alt: {
289
+ default: void 0,
290
+ parseHTML: (element) => element.getAttribute("alt"),
291
+ renderHTML: (attributes) => ({
292
+ alt: attributes.alt
293
+ })
294
+ },
295
+ loading: {
296
+ default: false,
297
+ parseHTML: () => false,
298
+ renderHTML: () => ({})
299
+ }
300
+ };
301
+ },
302
+ parseHTML() {
303
+ return [
304
+ {
305
+ tag: 'img[src]:not([src^="data:"])',
306
+ getAttrs: (element) => {
307
+ const el = element;
308
+ return {
309
+ src: el.getAttribute("src"),
310
+ alt: el.getAttribute("alt"),
311
+ width: el.getAttribute("data-width") || "100%",
312
+ align: el.getAttribute("data-align") || "center"
313
+ };
314
+ }
315
+ }
316
+ ];
317
+ },
318
+ renderHTML({ HTMLAttributes }) {
319
+ return ["img", mergeAttributes2(HTMLAttributes)];
320
+ },
321
+ addCommands() {
322
+ return {
323
+ setImageBlock: (attrs) => ({ commands }) => {
324
+ return commands.insertContent({
325
+ type: "imageBlock",
326
+ attrs: { src: attrs.src }
327
+ });
328
+ },
329
+ setImageBlockAt: (attrs) => ({ commands }) => {
330
+ return commands.insertContentAt(attrs.pos, {
331
+ type: "imageBlock",
332
+ attrs: { src: attrs.src }
333
+ });
334
+ },
335
+ setImageBlockAlign: (align) => ({ commands }) => commands.updateAttributes("imageBlock", { align }),
336
+ setImageBlockWidth: (width) => ({ commands }) => commands.updateAttributes("imageBlock", {
337
+ width: `${Math.max(0, Math.min(100, width))}%`
338
+ })
339
+ };
340
+ },
341
+ addNodeView() {
342
+ if (this.options.nodeView) {
343
+ return ReactNodeViewRenderer(this.options.nodeView);
344
+ }
345
+ return null;
346
+ },
347
+ addProseMirrorPlugins() {
348
+ return [
349
+ new Plugin2({
350
+ key: new PluginKey("imageBlockDrop"),
351
+ props: {
352
+ handleDOMEvents: {
353
+ drop: (view, event) => {
354
+ const hasFiles = event.dataTransfer && event.dataTransfer.files && event.dataTransfer.files.length;
355
+ if (!hasFiles) {
356
+ return false;
357
+ }
358
+ const images = Array.from(event.dataTransfer.files).filter(
359
+ (file) => /image/i.test(file.type)
360
+ );
361
+ if (images.length === 0) {
362
+ return false;
363
+ }
364
+ event.preventDefault();
365
+ const { schema } = view.state;
366
+ const coordinates = view.posAtCoords({
367
+ left: event.clientX,
368
+ top: event.clientY
369
+ });
370
+ if (!coordinates) return false;
371
+ images.forEach(async (image) => {
372
+ if (this.options.uploadImage) {
373
+ try {
374
+ const placeholderNode = schema.nodes.imageBlock.create({
375
+ src: "",
376
+ loading: true
377
+ });
378
+ const placeholderTr = view.state.tr.insert(
379
+ coordinates.pos,
380
+ placeholderNode
381
+ );
382
+ view.dispatch(placeholderTr);
383
+ const url = await this.options.uploadImage(image);
384
+ const node = schema.nodes.imageBlock.create({ src: url });
385
+ const currentState = view.state;
386
+ let foundPos = -1;
387
+ currentState.doc.descendants((node2, pos) => {
388
+ if (node2.type.name === "imageBlock" && node2.attrs.loading) {
389
+ foundPos = pos;
390
+ return false;
391
+ }
392
+ });
393
+ if (foundPos !== -1) {
394
+ const transaction = view.state.tr.replaceWith(
395
+ foundPos,
396
+ foundPos + 1,
397
+ node
398
+ );
399
+ view.dispatch(transaction);
400
+ }
401
+ } catch (error) {
402
+ console.error("Failed to upload image:", error);
403
+ const currentState = view.state;
404
+ let foundPos = -1;
405
+ currentState.doc.descendants((node, pos) => {
406
+ if (node.type.name === "imageBlock" && node.attrs.loading) {
407
+ foundPos = pos;
408
+ return false;
409
+ }
410
+ });
411
+ if (foundPos !== -1) {
412
+ const transaction = view.state.tr.delete(
413
+ foundPos,
414
+ foundPos + 1
415
+ );
416
+ view.dispatch(transaction);
417
+ }
418
+ }
419
+ }
420
+ });
421
+ return true;
422
+ },
423
+ paste: (view, event) => {
424
+ const hasFiles = event.clipboardData && event.clipboardData.files && event.clipboardData.files.length;
425
+ if (!hasFiles) {
426
+ return false;
427
+ }
428
+ const images = Array.from(event.clipboardData.files).filter(
429
+ (file) => /image/i.test(file.type)
430
+ );
431
+ if (images.length === 0) {
432
+ return false;
433
+ }
434
+ event.preventDefault();
435
+ images.forEach(async (image) => {
436
+ if (this.options.uploadImage) {
437
+ try {
438
+ const placeholderNode = view.state.schema.nodes.imageBlock.create({
439
+ src: "",
440
+ loading: true
441
+ });
442
+ view.dispatch(
443
+ view.state.tr.replaceSelectionWith(placeholderNode)
444
+ );
445
+ const url = await this.options.uploadImage(image);
446
+ const node = view.state.schema.nodes.imageBlock.create({
447
+ src: url
448
+ });
449
+ const currentState = view.state;
450
+ let foundPos = -1;
451
+ currentState.doc.descendants((node2, pos) => {
452
+ if (node2.type.name === "imageBlock" && node2.attrs.loading) {
453
+ foundPos = pos;
454
+ return false;
455
+ }
456
+ });
457
+ if (foundPos !== -1) {
458
+ const transaction = view.state.tr.replaceWith(
459
+ foundPos,
460
+ foundPos + 1,
461
+ node
462
+ );
463
+ view.dispatch(transaction);
464
+ }
465
+ } catch (error) {
466
+ console.error("Failed to upload image:", error);
467
+ const currentState = view.state;
468
+ let foundPos = -1;
469
+ currentState.doc.descendants((node, pos) => {
470
+ if (node.type.name === "imageBlock" && node.attrs.loading) {
471
+ foundPos = pos;
472
+ return false;
473
+ }
474
+ });
475
+ if (foundPos !== -1) {
476
+ const transaction = view.state.tr.delete(
477
+ foundPos,
478
+ foundPos + 1
479
+ );
480
+ view.dispatch(transaction);
481
+ }
482
+ }
483
+ }
484
+ });
485
+ return true;
486
+ }
487
+ }
488
+ }
489
+ })
490
+ ];
491
+ }
492
+ });
493
+
494
+ // src/headless/extensions/slash-command.tsx
495
+ import { ReactRenderer } from "@tiptap/react";
496
+ import Suggestion from "@tiptap/suggestion";
497
+ import { Extension } from "@tiptap/core";
498
+ var Command2 = Extension.create({
499
+ name: "slash-command",
500
+ addOptions() {
501
+ return {
502
+ suggestion: {
503
+ char: "/",
504
+ command: (ctx) => {
505
+ ctx.props.command({ editor: ctx.editor, range: ctx.range });
506
+ }
507
+ }
508
+ };
509
+ },
510
+ addProseMirrorPlugins() {
511
+ const base = this.options.suggestion ?? {};
512
+ return [
513
+ Suggestion({
514
+ editor: this.editor,
515
+ char: base.char ?? "/",
516
+ items: base.items ?? (() => ["/"]),
517
+ command: (ctx) => {
518
+ if (typeof ctx?.props?.command === "function") {
519
+ ctx.props.command({ editor: ctx.editor, range: ctx.range });
520
+ }
521
+ },
522
+ ...base
523
+ })
524
+ ];
525
+ }
526
+ });
527
+ var renderItems = (elementRef) => {
528
+ let component = null;
529
+ let container = null;
530
+ const destroy = () => {
531
+ component?.destroy();
532
+ component = null;
533
+ if (container) {
534
+ container.remove();
535
+ container = null;
536
+ }
537
+ };
538
+ const updatePosition = (clientRect) => {
539
+ if (!container || !clientRect) return;
540
+ const top = Math.round(clientRect.bottom + 8);
541
+ const left = Math.round(clientRect.left);
542
+ container.style.top = `${top}px`;
543
+ container.style.left = `${left}px`;
544
+ };
545
+ return {
546
+ onStart: (props) => {
547
+ const { selection } = props.editor.state;
548
+ const parentNode = selection.$from.node(selection.$from.depth);
549
+ const blockType = parentNode.type.name;
550
+ if (blockType === "codeBlock") return false;
551
+ component = new ReactRenderer(EditorCommandOut, {
552
+ props: {
553
+ query: props.query ?? "",
554
+ range: props.range
555
+ },
556
+ editor: props.editor
557
+ });
558
+ container = document.createElement("div");
559
+ container.style.position = "fixed";
560
+ container.style.zIndex = "9999";
561
+ container.style.minWidth = "240px";
562
+ (elementRef?.current ?? document.body).appendChild(container);
563
+ container.appendChild(component.element);
564
+ const rect = typeof props.clientRect === "function" ? props.clientRect() : null;
565
+ if (rect) updatePosition(rect);
566
+ },
567
+ onUpdate: (props) => {
568
+ component?.updateProps({
569
+ query: props.query ?? "",
570
+ range: props.range
571
+ });
572
+ const rect = typeof props.clientRect === "function" ? props.clientRect() : null;
573
+ if (rect) updatePosition(rect);
574
+ },
575
+ onKeyDown: ({ event }) => {
576
+ if (event.key === "Escape") {
577
+ destroy();
578
+ return true;
579
+ }
580
+ return false;
581
+ },
582
+ onExit: () => {
583
+ destroy();
584
+ }
585
+ };
586
+ };
587
+ var createSuggestionItems = (items) => items;
588
+ var handleCommandNavigation = (event) => {
589
+ if (["ArrowUp", "ArrowDown", "Enter"].includes(event.key)) {
590
+ const slashCommand = document.querySelector("#slash-command");
591
+ if (slashCommand) return true;
592
+ }
593
+ };
594
+
595
+ export {
596
+ EditorRoot,
597
+ EditorContent,
598
+ EditorBubble,
599
+ EditorBubbleItem,
600
+ EditorCommandOut,
601
+ EditorCommand,
602
+ EditorCommandList,
603
+ EditorCommandItem,
604
+ EditorCommandEmpty,
605
+ CodeBlock,
606
+ Link,
607
+ ImageBlock,
608
+ StarterKit,
609
+ Placeholder,
610
+ Command2 as Command,
611
+ renderItems,
612
+ createSuggestionItems,
613
+ handleCommandNavigation,
614
+ useCurrentEditor4 as useCurrentEditor
615
+ };
616
+ //# sourceMappingURL=chunk-GOCX7NUN.js.map