dn-react-text-editor 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.
Files changed (48) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +222 -0
  3. package/dist/attach_file.d.mts +28 -0
  4. package/dist/attach_file.d.ts +28 -0
  5. package/dist/attach_file.js +169 -0
  6. package/dist/attach_file.mjs +144 -0
  7. package/dist/cn.d.mts +3 -0
  8. package/dist/cn.d.ts +3 -0
  9. package/dist/cn.js +32 -0
  10. package/dist/cn.mjs +7 -0
  11. package/dist/index.d.mts +9 -0
  12. package/dist/index.d.ts +9 -0
  13. package/dist/index.js +735 -0
  14. package/dist/index.mjs +698 -0
  15. package/dist/plugins/drag_and_drop.d.mts +12 -0
  16. package/dist/plugins/drag_and_drop.d.ts +12 -0
  17. package/dist/plugins/drag_and_drop.js +57 -0
  18. package/dist/plugins/drag_and_drop.mjs +32 -0
  19. package/dist/plugins/keymap.d.mts +6 -0
  20. package/dist/plugins/keymap.d.ts +6 -0
  21. package/dist/plugins/keymap.js +59 -0
  22. package/dist/plugins/keymap.mjs +34 -0
  23. package/dist/plugins/placehoder.d.mts +5 -0
  24. package/dist/plugins/placehoder.d.ts +5 -0
  25. package/dist/plugins/placehoder.js +114 -0
  26. package/dist/plugins/placehoder.mjs +89 -0
  27. package/dist/plugins/trailing_paragraph.d.mts +5 -0
  28. package/dist/plugins/trailing_paragraph.d.ts +5 -0
  29. package/dist/plugins/trailing_paragraph.js +46 -0
  30. package/dist/plugins/trailing_paragraph.mjs +21 -0
  31. package/dist/plugins/upload_placeholder.d.mts +7 -0
  32. package/dist/plugins/upload_placeholder.d.ts +7 -0
  33. package/dist/plugins/upload_placeholder.js +94 -0
  34. package/dist/plugins/upload_placeholder.mjs +68 -0
  35. package/dist/prosemirror/index.d.mts +7 -0
  36. package/dist/prosemirror/index.d.ts +7 -0
  37. package/dist/prosemirror/index.js +36 -0
  38. package/dist/prosemirror/index.mjs +8 -0
  39. package/dist/schema.d.mts +7 -0
  40. package/dist/schema.d.ts +7 -0
  41. package/dist/schema.js +286 -0
  42. package/dist/schema.mjs +261 -0
  43. package/dist/text_editor.d.mts +36 -0
  44. package/dist/text_editor.d.ts +36 -0
  45. package/dist/text_editor.js +729 -0
  46. package/dist/text_editor.mjs +696 -0
  47. package/package.json +56 -0
  48. package/tsup.config.ts +10 -0
package/dist/index.js ADDED
@@ -0,0 +1,735 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ createAttachFile: () => createAttachFile,
34
+ createSchema: () => createSchema,
35
+ createTextEditor: () => createTextEditor
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+
39
+ // src/text_editor.tsx
40
+ var import_react = __toESM(require("react"));
41
+ var import_prosemirror_state4 = require("prosemirror-state");
42
+ var import_prosemirror_view4 = require("prosemirror-view");
43
+ var import_react2 = require("react");
44
+ var import_prosemirror_commands2 = require("prosemirror-commands");
45
+ var import_prosemirror_keymap = require("prosemirror-keymap");
46
+
47
+ // src/plugins/drag_and_drop.tsx
48
+ var import_prosemirror_state = require("prosemirror-state");
49
+ function dragAndDropPlugin({ attachFile }) {
50
+ return new import_prosemirror_state.Plugin({
51
+ props: {
52
+ handleDOMEvents: {
53
+ drop(view, event) {
54
+ const files = event.dataTransfer?.files;
55
+ if (!files || files.length === 0) {
56
+ return;
57
+ }
58
+ event.preventDefault();
59
+ const pos = view.state.selection.$from.pos || view.posAtCoords({
60
+ left: event.clientX,
61
+ top: event.clientY
62
+ })?.pos || null;
63
+ if (pos === null) {
64
+ return;
65
+ }
66
+ const medias = Array.from(files).filter(
67
+ (file) => file.type.startsWith("image/") || file.type.startsWith("video/")
68
+ );
69
+ attachFile(view, medias);
70
+ return true;
71
+ }
72
+ }
73
+ }
74
+ });
75
+ }
76
+
77
+ // src/plugins/upload_placeholder.tsx
78
+ var import_prosemirror_state2 = require("prosemirror-state");
79
+ var import_prosemirror_view = require("prosemirror-view");
80
+ var uploadPlaceholderPlugin = new import_prosemirror_state2.Plugin({
81
+ state: {
82
+ init() {
83
+ return import_prosemirror_view.DecorationSet.empty;
84
+ },
85
+ apply(tr, set) {
86
+ set = set.map(tr.mapping, tr.doc);
87
+ const action = tr.getMeta(this);
88
+ if (action && action.add) {
89
+ const { type, width, height } = action.add;
90
+ const widget = document.createElement("div");
91
+ widget.className = "upload-placeholder";
92
+ widget.style.width = `100%`;
93
+ if (type.startsWith("image/") || type.startsWith("video/")) {
94
+ widget.style.aspectRatio = `${width} / ${height}`;
95
+ widget.style.maxWidth = `${width}px`;
96
+ } else {
97
+ widget.style.height = "80px";
98
+ }
99
+ const progress = document.createElement("div");
100
+ progress.className = "upload-progress";
101
+ widget.appendChild(progress);
102
+ const deco = import_prosemirror_view.Decoration.widget(action.add.pos, widget, {
103
+ id: action.add.id
104
+ });
105
+ set = set.add(tr.doc, [deco]);
106
+ } else if (action && action.progress) {
107
+ const found = set.find(
108
+ void 0,
109
+ void 0,
110
+ (spec) => spec.id === action.progress.id
111
+ );
112
+ if (found.length) {
113
+ const widget = found[0].type.toDOM;
114
+ const progress = widget.querySelector(".upload-progress");
115
+ if (progress) {
116
+ progress.innerHTML = `${Math.round(action.progress.progress)}%`;
117
+ }
118
+ }
119
+ } else if (action && action.remove) {
120
+ set = set.remove(
121
+ set.find(void 0, void 0, (spec) => spec.id === action.remove.id)
122
+ );
123
+ }
124
+ return set;
125
+ }
126
+ },
127
+ props: {
128
+ decorations(state) {
129
+ return this.getState(state);
130
+ }
131
+ }
132
+ });
133
+ var findPlaceholder = (state, id) => {
134
+ const decos = uploadPlaceholderPlugin.getState(state);
135
+ if (!decos) {
136
+ return null;
137
+ }
138
+ const found = decos.find(void 0, void 0, (spec) => spec.id === id);
139
+ return found.length ? found[0].from : null;
140
+ };
141
+
142
+ // src/plugins/placehoder.tsx
143
+ var import_prosemirror_model = require("prosemirror-model");
144
+ var import_prosemirror_state3 = require("prosemirror-state");
145
+ var import_prosemirror_view2 = require("prosemirror-view");
146
+ var getFirstChildDescendants = (view) => {
147
+ const nodes = [];
148
+ view.state.doc?.descendants((n) => {
149
+ nodes.push(n);
150
+ });
151
+ return nodes;
152
+ };
153
+ function placeholderPlugin(text) {
154
+ const update = (view) => {
155
+ const decos = uploadPlaceholderPlugin.getState(view.state);
156
+ if (decos && decos.find().length > 0 || view.state.doc.content.content.some((e) => e.type.name !== "paragraph") || view.state.doc.childCount > 1 || getFirstChildDescendants(view).length > 1 || view.state.doc.textContent) {
157
+ view.dom.removeAttribute("data-placeholder");
158
+ } else {
159
+ view.dom.setAttribute("data-placeholder", text);
160
+ }
161
+ };
162
+ return new import_prosemirror_state3.Plugin({
163
+ view(view) {
164
+ update(view);
165
+ return { update };
166
+ }
167
+ });
168
+ }
169
+
170
+ // src/text_editor.tsx
171
+ var import_prosemirror_model3 = require("prosemirror-model");
172
+ var import_prosemirror_history2 = require("prosemirror-history");
173
+
174
+ // src/plugins/keymap.tsx
175
+ var import_prosemirror_history = require("prosemirror-history");
176
+ var import_prosemirror_commands = require("prosemirror-commands");
177
+ var import_prosemirror_schema_list = require("prosemirror-schema-list");
178
+ function buildKeymap(schema) {
179
+ const keys = {};
180
+ function bind(key, cmd) {
181
+ keys[key] = cmd;
182
+ }
183
+ bind("Mod-z", import_prosemirror_history.undo);
184
+ bind("Shift-Mod-z", import_prosemirror_history.redo);
185
+ bind("Mod-y", import_prosemirror_history.redo);
186
+ const li = schema.nodes.list_item;
187
+ bind(
188
+ "Enter",
189
+ (0, import_prosemirror_commands.chainCommands)((0, import_prosemirror_schema_list.splitListItem)(li), (state, dispatch) => {
190
+ const { $head } = state.selection;
191
+ if ($head.parent.type === state.schema.nodes.paragraph) {
192
+ (0, import_prosemirror_commands.splitBlockAs)((n) => {
193
+ return {
194
+ type: n.type,
195
+ attrs: n.attrs
196
+ };
197
+ })(state, dispatch);
198
+ return true;
199
+ }
200
+ return false;
201
+ })
202
+ );
203
+ return keys;
204
+ }
205
+
206
+ // src/schema.tsx
207
+ var import_prosemirror_model2 = require("prosemirror-model");
208
+ var import_prosemirror_schema_list2 = require("prosemirror-schema-list");
209
+ function createSchema() {
210
+ const customSchema = new import_prosemirror_model2.Schema({
211
+ nodes: {
212
+ doc: { content: "block+" },
213
+ paragraph: {
214
+ attrs: { align: { default: null } },
215
+ content: "inline*",
216
+ group: "block",
217
+ parseDOM: [
218
+ {
219
+ tag: "p",
220
+ getAttrs(dom) {
221
+ return {
222
+ align: dom.style.textAlign || null
223
+ };
224
+ }
225
+ }
226
+ ],
227
+ toDOM(node) {
228
+ return [
229
+ "p",
230
+ {
231
+ style: node.attrs.align ? `text-align: ${node.attrs.align}` : null
232
+ },
233
+ 0
234
+ ];
235
+ }
236
+ },
237
+ text: {
238
+ group: "inline"
239
+ },
240
+ hard_break: {
241
+ inline: true,
242
+ group: "inline",
243
+ selectable: false,
244
+ parseDOM: [{ tag: "br" }],
245
+ toDOM() {
246
+ return ["br"];
247
+ }
248
+ },
249
+ heading: {
250
+ attrs: { level: { default: 1 }, align: { default: null } },
251
+ content: "inline*",
252
+ group: "block",
253
+ defining: true,
254
+ parseDOM: [
255
+ {
256
+ tag: "h1",
257
+ getAttrs(node) {
258
+ return {
259
+ level: 1,
260
+ algin: node.style.textAlign || null
261
+ };
262
+ }
263
+ },
264
+ {
265
+ tag: "h2",
266
+ getAttrs(node) {
267
+ return {
268
+ level: 2,
269
+ algin: node.style.textAlign || null
270
+ };
271
+ }
272
+ },
273
+ {
274
+ tag: "h3",
275
+ getAttrs(node) {
276
+ return {
277
+ level: 3,
278
+ algin: node.style.textAlign || null
279
+ };
280
+ }
281
+ },
282
+ {
283
+ tag: "h4",
284
+ getAttrs(node) {
285
+ return {
286
+ level: 4,
287
+ algin: node.style.textAlign || null
288
+ };
289
+ }
290
+ },
291
+ {
292
+ tag: "h5",
293
+ getAttrs(node) {
294
+ return {
295
+ level: 5,
296
+ algin: node.style.textAlign || null
297
+ };
298
+ }
299
+ },
300
+ {
301
+ tag: "h6",
302
+ getAttrs(node) {
303
+ return {
304
+ level: 6,
305
+ algin: node.style.textAlign || null
306
+ };
307
+ }
308
+ }
309
+ ],
310
+ toDOM(node) {
311
+ return [
312
+ "h" + node.attrs.level,
313
+ {
314
+ id: node.textContent.toLowerCase().replace(/\s+/g, "-"),
315
+ style: node.attrs.align ? `text-align: ${node.attrs.align};` : null
316
+ },
317
+ 0
318
+ ];
319
+ }
320
+ },
321
+ horizontal_rule: {
322
+ group: "block",
323
+ parseDOM: [{ tag: "hr" }],
324
+ toDOM() {
325
+ return ["hr"];
326
+ }
327
+ },
328
+ image: {
329
+ attrs: {
330
+ src: { validate: "string" },
331
+ alt: { default: null, validate: "string|null" },
332
+ title: {
333
+ default: null,
334
+ validate: "string|null"
335
+ },
336
+ width: {
337
+ default: null,
338
+ validate: "number|null"
339
+ },
340
+ height: {
341
+ default: null,
342
+ validate: "number|null"
343
+ }
344
+ },
345
+ inline: false,
346
+ group: "block",
347
+ draggable: true,
348
+ parseDOM: [
349
+ {
350
+ tag: "img",
351
+ getAttrs(dom) {
352
+ return {
353
+ src: dom.getAttribute("src"),
354
+ alt: dom.getAttribute("alt"),
355
+ width: dom.getAttribute("width"),
356
+ height: dom.getAttribute("height")
357
+ };
358
+ }
359
+ }
360
+ ],
361
+ toDOM(node) {
362
+ const { src, alt, srcSet, sizes, width, height } = node.attrs;
363
+ return [
364
+ "img",
365
+ {
366
+ src,
367
+ alt,
368
+ srcSet,
369
+ sizes,
370
+ width,
371
+ height
372
+ }
373
+ ];
374
+ }
375
+ }
376
+ },
377
+ marks: {
378
+ link: {
379
+ attrs: {
380
+ href: { default: "" },
381
+ title: { default: null }
382
+ },
383
+ inclusive: false,
384
+ parseDOM: [
385
+ {
386
+ tag: "a[href]",
387
+ getAttrs(dom) {
388
+ return {
389
+ href: dom.getAttribute("href"),
390
+ title: dom.getAttribute("title")
391
+ };
392
+ }
393
+ }
394
+ ],
395
+ toDOM(node) {
396
+ const { href, title } = node.attrs;
397
+ const target = "_blank";
398
+ const rel = "noopener noreferrer";
399
+ return [
400
+ "a",
401
+ { href, title: title || href, target, rel },
402
+ 0
403
+ ];
404
+ }
405
+ },
406
+ bold: {
407
+ parseDOM: [
408
+ { tag: "strong" },
409
+ {
410
+ tag: "b",
411
+ getAttrs: (node) => node.style.fontWeight != "normal" && null
412
+ },
413
+ {
414
+ style: "font-weight=400",
415
+ clearMark: (m) => m.type.name == "strong"
416
+ },
417
+ {
418
+ style: "font-weight",
419
+ getAttrs: (value) => /^(bold(er)?|[5-9]\d{2,})$/.test(value) && null
420
+ }
421
+ ],
422
+ toDOM() {
423
+ return ["strong", 0];
424
+ }
425
+ },
426
+ italic: {
427
+ parseDOM: [
428
+ { tag: "em" },
429
+ { tag: "i" },
430
+ { style: "font-style=italic" },
431
+ {
432
+ style: "font-style=normal",
433
+ clearMark: (m) => m.type.name == "em"
434
+ }
435
+ ],
436
+ toDOM() {
437
+ return ["em", 0];
438
+ }
439
+ },
440
+ underline: {
441
+ parseDOM: [
442
+ { tag: "u" },
443
+ {
444
+ style: "text-decoration",
445
+ getAttrs: (value) => value === "underline" && null
446
+ }
447
+ ],
448
+ toDOM() {
449
+ return ["u", 0];
450
+ }
451
+ }
452
+ }
453
+ });
454
+ const prosemirrorSchema = new import_prosemirror_model2.Schema({
455
+ nodes: (0, import_prosemirror_schema_list2.addListNodes)(
456
+ customSchema.spec.nodes,
457
+ "paragraph block*",
458
+ "block"
459
+ ),
460
+ marks: customSchema.spec.marks
461
+ });
462
+ return prosemirrorSchema;
463
+ }
464
+
465
+ // src/text_editor.tsx
466
+ var import_rxjs = require("rxjs");
467
+
468
+ // src/cn.ts
469
+ function cn(...classes) {
470
+ return classes.filter(Boolean).join(" ").trim();
471
+ }
472
+
473
+ // src/attach_file.tsx
474
+ var import_prosemirror_view3 = require("prosemirror-view");
475
+ function createAttachFile({
476
+ schema,
477
+ generateMetadata,
478
+ uploadFile = (file) => {
479
+ return {
480
+ src: URL.createObjectURL(file),
481
+ alt: file.name
482
+ };
483
+ }
484
+ }) {
485
+ const attachEachFile = async (view, file, pos) => {
486
+ const metadata = generateMetadata ? await generateMetadata(file) : {};
487
+ const id = {};
488
+ view.focus();
489
+ const tr = view.state.tr;
490
+ if (!tr.selection.empty) {
491
+ tr.deleteSelection();
492
+ }
493
+ tr.setMeta(uploadPlaceholderPlugin, {
494
+ add: {
495
+ id,
496
+ pos: pos ?? tr.selection.from,
497
+ type: file.type,
498
+ ...metadata
499
+ }
500
+ });
501
+ view.dispatch(tr);
502
+ const $pos = findPlaceholder(view.state, id);
503
+ if (!$pos) {
504
+ return;
505
+ }
506
+ try {
507
+ const { src, alt } = await uploadFile(file);
508
+ const tr2 = view.state.tr.setMeta(uploadPlaceholderPlugin, {
509
+ remove: { id }
510
+ });
511
+ const createNode = () => {
512
+ if (file.type.startsWith("image/")) {
513
+ return schema.nodes.image.create({
514
+ src,
515
+ alt,
516
+ width: metadata.width,
517
+ height: metadata.height
518
+ });
519
+ }
520
+ if (file.type.startsWith("video/")) {
521
+ return schema.nodes.video.create({
522
+ src,
523
+ width: metadata.width,
524
+ height: metadata.height,
525
+ poster: metadata.poster
526
+ });
527
+ }
528
+ };
529
+ const node = createNode();
530
+ if (!node) {
531
+ return;
532
+ }
533
+ view.dispatch(tr2.replaceWith($pos, $pos, node));
534
+ } catch (e) {
535
+ view.dispatch(
536
+ tr.setMeta(uploadPlaceholderPlugin, { remove: { id } })
537
+ );
538
+ }
539
+ };
540
+ return async (view, files, pos) => {
541
+ for (let i = 0; i < files.length; i++) {
542
+ const file = files[i];
543
+ await attachEachFile(view, file, pos);
544
+ }
545
+ };
546
+ }
547
+
548
+ // src/text_editor.tsx
549
+ function createTextEditor(options = {}) {
550
+ const schema = createSchema();
551
+ const prosemirrorParser = import_prosemirror_model3.DOMParser.fromSchema(schema);
552
+ const prosemirrorSerializer = import_prosemirror_model3.DOMSerializer.fromSchema(schema);
553
+ const attachFile = createAttachFile({
554
+ schema,
555
+ generateMetadata: options.attachFile?.generateMetadata,
556
+ uploadFile: options.attachFile?.uploadFile
557
+ });
558
+ function Component({
559
+ ref,
560
+ state,
561
+ editor,
562
+ mode = "html",
563
+ container,
564
+ autoFocus,
565
+ name,
566
+ placeholder,
567
+ className,
568
+ defaultValue,
569
+ onClick,
570
+ onChange,
571
+ updateDelay = 0,
572
+ ...props
573
+ } = {}) {
574
+ const containerRef = (0, import_react2.useRef)(null);
575
+ const inputRef = (0, import_react2.useRef)(null);
576
+ (0, import_react2.useEffect)(() => {
577
+ const element = containerRef.current;
578
+ if (!element) {
579
+ return;
580
+ }
581
+ const subject = new import_rxjs.Subject();
582
+ const wrapper = document.createElement("div");
583
+ const toInnerHTML = (value) => {
584
+ if (mode === "html") {
585
+ return value;
586
+ }
587
+ return value.split("\n").map((line) => `<p>${line}</p>`).join("");
588
+ };
589
+ wrapper.innerHTML = toInnerHTML(
590
+ defaultValue ? String(defaultValue) : ""
591
+ );
592
+ const view = new import_prosemirror_view4.EditorView(element, {
593
+ ...editor,
594
+ attributes: (state2) => {
595
+ const propsAttributes = (() => {
596
+ if (typeof editor?.attributes === "function") {
597
+ return editor.attributes(state2);
598
+ }
599
+ return editor?.attributes;
600
+ })();
601
+ return {
602
+ ...propsAttributes,
603
+ class: cn(propsAttributes?.class, className),
604
+ spellcheck: propsAttributes?.spellcheck || "false"
605
+ };
606
+ },
607
+ state: import_prosemirror_state4.EditorState.create({
608
+ ...state,
609
+ schema: state?.schema || schema,
610
+ doc: state?.doc || prosemirrorParser.parse(wrapper),
611
+ plugins: [
612
+ ...state?.plugins || [],
613
+ (0, import_prosemirror_history2.history)({
614
+ newGroupDelay: updateDelay
615
+ }),
616
+ (0, import_prosemirror_keymap.keymap)(buildKeymap(schema)),
617
+ (0, import_prosemirror_keymap.keymap)(import_prosemirror_commands2.baseKeymap),
618
+ uploadPlaceholderPlugin,
619
+ attachFile ? dragAndDropPlugin({
620
+ attachFile
621
+ }) : null,
622
+ placeholder && placeholderPlugin(placeholder)
623
+ ].filter((e) => !!e)
624
+ }),
625
+ dispatchTransaction(tr) {
626
+ let result;
627
+ if (editor?.dispatchTransaction) {
628
+ result = editor.dispatchTransaction(tr);
629
+ } else {
630
+ view.updateState(view.state.apply(tr));
631
+ }
632
+ subject.next(tr);
633
+ return result;
634
+ }
635
+ });
636
+ function setValue(value) {
637
+ const wrap = document.createElement("div");
638
+ wrap.innerHTML = toInnerHTML(value);
639
+ const doc = prosemirrorParser.parse(wrap);
640
+ const tr = view.state.tr.replaceWith(
641
+ 0,
642
+ view.state.doc.content.size,
643
+ doc.content
644
+ );
645
+ view.dispatch(tr);
646
+ }
647
+ function clear() {
648
+ const tr = view.state.tr.replaceWith(
649
+ 0,
650
+ view.state.doc.content.size,
651
+ schema.nodes.doc.createAndFill()
652
+ );
653
+ view.dispatch(tr);
654
+ }
655
+ function toHTML() {
656
+ const fragment = prosemirrorSerializer.serializeFragment(
657
+ view.state.doc.content
658
+ );
659
+ const container2 = document.createElement("div");
660
+ container2.appendChild(fragment);
661
+ return container2.innerHTML;
662
+ }
663
+ function toTextContent() {
664
+ const state2 = view.state;
665
+ return state2.doc.textBetween(0, state2.doc.content.size, "\n");
666
+ }
667
+ const sub = subject.pipe(
668
+ (0, import_rxjs.filter)((tr) => tr.docChanged),
669
+ (0, import_rxjs.debounceTime)(updateDelay)
670
+ ).subscribe(() => {
671
+ if (inputRef.current) {
672
+ switch (mode) {
673
+ case "text":
674
+ inputRef.current.value = toTextContent();
675
+ break;
676
+ default:
677
+ inputRef.current.value = toHTML();
678
+ break;
679
+ }
680
+ const event = new Event("input", { bubbles: true });
681
+ inputRef.current.dispatchEvent(event);
682
+ }
683
+ });
684
+ if (autoFocus) {
685
+ view.focus();
686
+ }
687
+ const textEditorController = {
688
+ view,
689
+ subject,
690
+ set value(value) {
691
+ setValue(value);
692
+ },
693
+ get value() {
694
+ switch (mode) {
695
+ case "text":
696
+ return toTextContent();
697
+ default:
698
+ return toHTML();
699
+ }
700
+ },
701
+ clear
702
+ };
703
+ if (typeof ref === "function") {
704
+ ref(textEditorController);
705
+ } else if (ref) {
706
+ ref.current = textEditorController;
707
+ }
708
+ return () => {
709
+ sub.unsubscribe();
710
+ view.destroy();
711
+ element.innerHTML = "";
712
+ };
713
+ }, []);
714
+ return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { ref: containerRef, className: container, ...props }), /* @__PURE__ */ import_react.default.createElement(
715
+ "input",
716
+ {
717
+ ref: inputRef,
718
+ type: "hidden",
719
+ name,
720
+ onInput: onChange
721
+ }
722
+ ));
723
+ }
724
+ return {
725
+ schema,
726
+ attachFile,
727
+ Component
728
+ };
729
+ }
730
+ // Annotate the CommonJS export names for ESM import in node:
731
+ 0 && (module.exports = {
732
+ createAttachFile,
733
+ createSchema,
734
+ createTextEditor
735
+ });