dn-react-text-editor 0.1.2 → 0.2.1

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 (55) hide show
  1. package/README.md +19 -0
  2. package/dist/attach_file.d.mts +20 -22
  3. package/dist/attach_file.d.ts +20 -22
  4. package/dist/attach_file.js +5 -5
  5. package/dist/attach_file.mjs +5 -4
  6. package/dist/base64_file_uploader.d.mts +6 -0
  7. package/dist/base64_file_uploader.d.ts +6 -0
  8. package/dist/{plugins/trailing_paragraph.js → base64_file_uploader.js} +19 -22
  9. package/dist/base64_file_uploader.mjs +18 -0
  10. package/dist/commands.d.mts +23 -0
  11. package/dist/commands.d.ts +23 -0
  12. package/dist/commands.js +110 -0
  13. package/dist/commands.mjs +75 -0
  14. package/dist/create_text_editor.d.mts +28 -0
  15. package/dist/create_text_editor.d.ts +28 -0
  16. package/dist/create_text_editor.js +1093 -0
  17. package/dist/create_text_editor.mjs +1064 -0
  18. package/dist/html.d.mts +8 -0
  19. package/dist/html.d.ts +8 -0
  20. package/dist/html.js +136 -0
  21. package/dist/html.mjs +98 -0
  22. package/dist/index.d.mts +7 -4
  23. package/dist/index.d.ts +7 -4
  24. package/dist/index.js +782 -365
  25. package/dist/index.mjs +777 -360
  26. package/dist/input.d.mts +21 -0
  27. package/dist/input.d.ts +21 -0
  28. package/dist/input.js +70 -0
  29. package/dist/input.mjs +37 -0
  30. package/dist/plugins/drag_and_drop.d.mts +1 -1
  31. package/dist/plugins/drag_and_drop.d.ts +1 -1
  32. package/dist/plugins/drag_and_drop.js +3 -0
  33. package/dist/plugins/drag_and_drop.mjs +3 -0
  34. package/dist/plugins/highlighter.d.mts +6 -0
  35. package/dist/plugins/highlighter.d.ts +6 -0
  36. package/dist/plugins/highlighter.js +105 -0
  37. package/dist/plugins/highlighter.mjs +69 -0
  38. package/dist/plugins/keymap.js +17 -0
  39. package/dist/plugins/keymap.mjs +17 -0
  40. package/dist/schema.d.mts +2 -2
  41. package/dist/schema.d.ts +2 -2
  42. package/dist/schema.js +255 -14
  43. package/dist/schema.mjs +245 -14
  44. package/dist/text_editor_controller.d.mts +46 -0
  45. package/dist/text_editor_controller.d.ts +46 -0
  46. package/dist/text_editor_controller.js +503 -0
  47. package/dist/text_editor_controller.mjs +470 -0
  48. package/package.json +3 -1
  49. package/dist/plugins/trailing_paragraph.d.mts +0 -5
  50. package/dist/plugins/trailing_paragraph.d.ts +0 -5
  51. package/dist/plugins/trailing_paragraph.mjs +0 -21
  52. package/dist/text_editor.d.mts +0 -37
  53. package/dist/text_editor.d.ts +0 -37
  54. package/dist/text_editor.js +0 -722
  55. package/dist/text_editor.mjs +0 -692
@@ -0,0 +1,503 @@
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/text_editor_controller.tsx
31
+ var text_editor_controller_exports = {};
32
+ __export(text_editor_controller_exports, {
33
+ createTextEditorController: () => createTextEditorController
34
+ });
35
+ module.exports = __toCommonJS(text_editor_controller_exports);
36
+ var import_prosemirror_state5 = require("prosemirror-state");
37
+ var import_prosemirror_view4 = require("prosemirror-view");
38
+ var commands2 = __toESM(require("prosemirror-commands"));
39
+ var import_prosemirror_keymap = require("prosemirror-keymap");
40
+
41
+ // src/plugins/drag_and_drop.tsx
42
+ var import_prosemirror_state = require("prosemirror-state");
43
+ function dragAndDropPlugin({ attachFile }) {
44
+ return new import_prosemirror_state.Plugin({
45
+ props: {
46
+ handleDOMEvents: {
47
+ drop(view, event) {
48
+ if (!attachFile) {
49
+ return;
50
+ }
51
+ const files = event.dataTransfer?.files;
52
+ if (!files || files.length === 0) {
53
+ return;
54
+ }
55
+ event.preventDefault();
56
+ const pos = view.state.selection.$from.pos || view.posAtCoords({
57
+ left: event.clientX,
58
+ top: event.clientY
59
+ })?.pos || null;
60
+ if (pos === null) {
61
+ return;
62
+ }
63
+ const medias = Array.from(files).filter(
64
+ (file) => file.type.startsWith("image/") || file.type.startsWith("video/")
65
+ );
66
+ attachFile(view, medias);
67
+ return true;
68
+ }
69
+ }
70
+ }
71
+ });
72
+ }
73
+
74
+ // src/plugins/upload_placeholder.tsx
75
+ var import_prosemirror_state2 = require("prosemirror-state");
76
+ var import_prosemirror_view = require("prosemirror-view");
77
+ var uploadPlaceholderPlugin = new import_prosemirror_state2.Plugin({
78
+ state: {
79
+ init() {
80
+ return import_prosemirror_view.DecorationSet.empty;
81
+ },
82
+ apply(tr, set) {
83
+ set = set.map(tr.mapping, tr.doc);
84
+ const action = tr.getMeta(this);
85
+ if (action && action.add) {
86
+ const { type, width, height } = action.add;
87
+ const widget = document.createElement("div");
88
+ widget.className = "upload-placeholder";
89
+ widget.style.width = `100%`;
90
+ if (type.startsWith("image/") || type.startsWith("video/")) {
91
+ widget.style.aspectRatio = `${width} / ${height}`;
92
+ widget.style.maxWidth = `${width}px`;
93
+ } else {
94
+ widget.style.height = "80px";
95
+ }
96
+ const progress = document.createElement("div");
97
+ progress.className = "upload-progress";
98
+ widget.appendChild(progress);
99
+ const deco = import_prosemirror_view.Decoration.widget(action.add.pos, widget, {
100
+ id: action.add.id
101
+ });
102
+ set = set.add(tr.doc, [deco]);
103
+ } else if (action && action.progress) {
104
+ const found = set.find(
105
+ void 0,
106
+ void 0,
107
+ (spec) => spec.id === action.progress.id
108
+ );
109
+ if (found.length) {
110
+ const widget = found[0].type.toDOM;
111
+ const progress = widget.querySelector(".upload-progress");
112
+ if (progress) {
113
+ progress.innerHTML = `${Math.round(action.progress.progress)}%`;
114
+ }
115
+ }
116
+ } else if (action && action.remove) {
117
+ set = set.remove(
118
+ set.find(void 0, void 0, (spec) => spec.id === action.remove.id)
119
+ );
120
+ }
121
+ return set;
122
+ }
123
+ },
124
+ props: {
125
+ decorations(state) {
126
+ return this.getState(state);
127
+ }
128
+ }
129
+ });
130
+ var findPlaceholder = (state, id) => {
131
+ const decos = uploadPlaceholderPlugin.getState(state);
132
+ if (!decos) {
133
+ return null;
134
+ }
135
+ const found = decos.find(void 0, void 0, (spec) => spec.id === id);
136
+ return found.length ? found[0].from : null;
137
+ };
138
+
139
+ // src/plugins/placehoder.tsx
140
+ var import_prosemirror_model = require("prosemirror-model");
141
+ var import_prosemirror_state3 = require("prosemirror-state");
142
+ var import_prosemirror_view2 = require("prosemirror-view");
143
+ var getFirstChildDescendants = (view) => {
144
+ const nodes = [];
145
+ view.state.doc?.descendants((n) => {
146
+ nodes.push(n);
147
+ });
148
+ return nodes;
149
+ };
150
+ function placeholderPlugin(text) {
151
+ const update = (view) => {
152
+ const decos = uploadPlaceholderPlugin.getState(view.state);
153
+ 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) {
154
+ view.dom.removeAttribute("data-placeholder");
155
+ } else {
156
+ view.dom.setAttribute("data-placeholder", text);
157
+ }
158
+ };
159
+ return new import_prosemirror_state3.Plugin({
160
+ view(view) {
161
+ update(view);
162
+ return { update };
163
+ }
164
+ });
165
+ }
166
+
167
+ // src/text_editor_controller.tsx
168
+ var import_prosemirror_history2 = require("prosemirror-history");
169
+
170
+ // src/plugins/keymap.tsx
171
+ var import_prosemirror_state4 = require("prosemirror-state");
172
+ var import_prosemirror_history = require("prosemirror-history");
173
+ var import_prosemirror_commands = require("prosemirror-commands");
174
+ var import_prosemirror_schema_list = require("prosemirror-schema-list");
175
+ function buildKeymap(schema) {
176
+ const keys = {};
177
+ function bind(key, cmd) {
178
+ keys[key] = cmd;
179
+ }
180
+ bind("Mod-z", import_prosemirror_history.undo);
181
+ bind("Shift-Mod-z", import_prosemirror_history.redo);
182
+ bind("Mod-y", import_prosemirror_history.redo);
183
+ const li = schema.nodes.list_item;
184
+ bind(
185
+ "Enter",
186
+ (0, import_prosemirror_commands.chainCommands)((0, import_prosemirror_schema_list.splitListItem)(li), (state, dispatch) => {
187
+ const { $head } = state.selection;
188
+ if ($head.parent.type === state.schema.nodes.paragraph) {
189
+ (0, import_prosemirror_commands.splitBlockAs)((n) => {
190
+ return {
191
+ type: n.type,
192
+ attrs: n.attrs
193
+ };
194
+ })(state, dispatch);
195
+ return true;
196
+ }
197
+ return false;
198
+ })
199
+ );
200
+ bind("ArrowDown", (state, dispatch) => {
201
+ const doc = state.doc;
202
+ const lastNode = doc.lastChild;
203
+ if (lastNode && lastNode.type.name !== "paragraph") {
204
+ const paragraphType = state.schema.nodes.paragraph;
205
+ let tr = state.tr;
206
+ const endPos = doc.content.size;
207
+ tr = tr.insert(endPos, paragraphType.create());
208
+ tr = tr.setSelection(import_prosemirror_state4.TextSelection.create(tr.doc, tr.doc.content.size));
209
+ if (dispatch) {
210
+ dispatch(tr);
211
+ }
212
+ return true;
213
+ }
214
+ return false;
215
+ });
216
+ return keys;
217
+ }
218
+
219
+ // src/cn.ts
220
+ function cn(...classes) {
221
+ return classes.filter(Boolean).join(" ").trim();
222
+ }
223
+
224
+ // src/attach_file.tsx
225
+ var import_prosemirror_view3 = require("prosemirror-view");
226
+
227
+ // src/base64_file_uploader.ts
228
+ var base64FileUploader = async (file) => {
229
+ const base64 = await new Promise((resolve, reject) => {
230
+ const reader = new FileReader();
231
+ reader.onload = () => {
232
+ resolve(reader.result);
233
+ };
234
+ reader.onerror = reject;
235
+ reader.readAsDataURL(file);
236
+ });
237
+ return {
238
+ src: base64,
239
+ alt: file.name
240
+ };
241
+ };
242
+
243
+ // src/attach_file.tsx
244
+ function createAttachFile({
245
+ schema,
246
+ generateMetadata,
247
+ uploadFile = base64FileUploader
248
+ }) {
249
+ const attachEachFile = async (view, file, pos) => {
250
+ const metadata = generateMetadata ? await generateMetadata(file) : {};
251
+ const id = {};
252
+ view.focus();
253
+ const tr = view.state.tr;
254
+ if (!tr.selection.empty) {
255
+ tr.deleteSelection();
256
+ }
257
+ tr.setMeta(uploadPlaceholderPlugin, {
258
+ add: {
259
+ id,
260
+ pos: pos ?? tr.selection.from,
261
+ type: file.type,
262
+ ...metadata
263
+ }
264
+ });
265
+ view.dispatch(tr);
266
+ const $pos = findPlaceholder(view.state, id);
267
+ if (!$pos) {
268
+ return;
269
+ }
270
+ try {
271
+ const { src, alt } = await uploadFile(file);
272
+ const tr2 = view.state.tr.setMeta(uploadPlaceholderPlugin, {
273
+ remove: { id }
274
+ });
275
+ const createNode = () => {
276
+ if (file.type.startsWith("image/")) {
277
+ return schema.nodes.image.create({
278
+ src,
279
+ alt,
280
+ width: metadata.width,
281
+ height: metadata.height
282
+ });
283
+ }
284
+ if (file.type.startsWith("video/")) {
285
+ return schema.nodes.video.create({
286
+ src,
287
+ width: metadata.width,
288
+ height: metadata.height,
289
+ poster: metadata.poster
290
+ });
291
+ }
292
+ };
293
+ const node = createNode();
294
+ if (!node) {
295
+ return;
296
+ }
297
+ view.dispatch(tr2.replaceWith($pos, $pos, node));
298
+ } catch (e) {
299
+ view.dispatch(tr.setMeta(uploadPlaceholderPlugin, { remove: { id } }));
300
+ }
301
+ };
302
+ return async (view, files, pos) => {
303
+ for (let i = 0; i < files.length; i++) {
304
+ const file = files[i];
305
+ await attachEachFile(view, file, pos);
306
+ }
307
+ };
308
+ }
309
+
310
+ // src/commands.tsx
311
+ var commands = __toESM(require("prosemirror-commands"));
312
+ var schemaList = __toESM(require("prosemirror-schema-list"));
313
+ var createCommands = (schema, view, options = {}) => {
314
+ {
315
+ const isBlockTypeActive = (node, attrs, excludes = []) => {
316
+ const state = view.state;
317
+ const ranges = state.selection.ranges;
318
+ let active = false;
319
+ for (const range of ranges) {
320
+ const { $from, $to } = range;
321
+ state.doc.nodesBetween($from.pos, $to.pos, (n) => {
322
+ if (active) {
323
+ return true;
324
+ }
325
+ if (n.type !== node || excludes.includes(n.type)) {
326
+ return;
327
+ }
328
+ if (!attrs || Object.keys(attrs).every((key) => n.attrs[key] === attrs[key])) {
329
+ active = true;
330
+ }
331
+ });
332
+ return active;
333
+ }
334
+ };
335
+ const setBlockType2 = (node, attrs) => {
336
+ view.focus();
337
+ const nodeType = schema.nodes[node];
338
+ const command = commands.setBlockType(nodeType, attrs);
339
+ command(view.state, view.dispatch);
340
+ };
341
+ const toggleBlockType = (node, attrs) => {
342
+ view.focus();
343
+ const nodeType = schema.nodes[node];
344
+ const command = commands.setBlockType(nodeType, attrs);
345
+ if (isBlockTypeActive(nodeType, attrs)) {
346
+ command(view.state, view.dispatch);
347
+ }
348
+ };
349
+ const toggleMark2 = (mark, attrs, options2) => {
350
+ view.focus();
351
+ const markType = schema.marks[mark];
352
+ const command = commands.toggleMark(markType, attrs, options2);
353
+ command(view.state, view.dispatch);
354
+ };
355
+ const wrapInList2 = (listType, attrs) => {
356
+ view.focus();
357
+ const nodeType = schema.nodes[listType];
358
+ const command = schemaList.wrapInList(nodeType, attrs);
359
+ command(view.state, view.dispatch);
360
+ };
361
+ const clear = () => {
362
+ const tr = view.state.tr.replaceWith(
363
+ 0,
364
+ view.state.doc.content.size,
365
+ schema.nodes.doc.createAndFill()
366
+ );
367
+ view.dispatch(tr);
368
+ };
369
+ return {
370
+ isBlockTypeActive,
371
+ setBlockType: setBlockType2,
372
+ toggleBlockType,
373
+ toggleMark: toggleMark2,
374
+ wrapInList: wrapInList2,
375
+ clear,
376
+ attachFile: (files) => {
377
+ options.attachFile?.(view, files);
378
+ }
379
+ };
380
+ }
381
+ };
382
+
383
+ // src/text_editor_controller.tsx
384
+ var import_prosemirror_model2 = require("prosemirror-model");
385
+ var import_rxjs = require("rxjs");
386
+ function createTextEditorController(container, schema, options, {
387
+ mode = "html",
388
+ state,
389
+ editor,
390
+ defaultValue,
391
+ updateDelay = 500,
392
+ placeholder
393
+ }) {
394
+ const subject = new import_rxjs.Subject();
395
+ const prosemirrorParser = import_prosemirror_model2.DOMParser.fromSchema(schema);
396
+ const prosemirrorSerializer = import_prosemirror_model2.DOMSerializer.fromSchema(schema);
397
+ const wrapper = document.createElement("div");
398
+ const toInnerHTML = (value) => {
399
+ if (mode === "text") {
400
+ return value.split("\n").map((line) => `<p>${line}</p>`).join("");
401
+ }
402
+ return value;
403
+ };
404
+ wrapper.innerHTML = toInnerHTML(defaultValue ? String(defaultValue) : "");
405
+ const attachFile = createAttachFile({
406
+ schema,
407
+ generateMetadata: options.attachFile?.generateMetadata,
408
+ uploadFile: options.attachFile?.uploadFile
409
+ });
410
+ const view = new import_prosemirror_view4.EditorView(container, {
411
+ ...editor,
412
+ attributes: (state2) => {
413
+ const propsAttributes = (() => {
414
+ if (typeof editor?.attributes === "function") {
415
+ return editor.attributes(state2);
416
+ }
417
+ return editor?.attributes;
418
+ })();
419
+ return {
420
+ ...propsAttributes,
421
+ class: cn(options?.className, propsAttributes?.class),
422
+ spellcheck: propsAttributes?.spellcheck || "false",
423
+ style: options.style || "width: 100%; height: inherit; outline: none;"
424
+ };
425
+ },
426
+ state: import_prosemirror_state5.EditorState.create({
427
+ ...state,
428
+ schema: state?.schema || schema,
429
+ doc: state?.doc || prosemirrorParser.parse(wrapper),
430
+ plugins: [
431
+ ...state?.plugins || [],
432
+ (0, import_prosemirror_history2.history)({
433
+ newGroupDelay: updateDelay
434
+ }),
435
+ (0, import_prosemirror_keymap.keymap)(buildKeymap(schema)),
436
+ (0, import_prosemirror_keymap.keymap)(commands2.baseKeymap),
437
+ uploadPlaceholderPlugin,
438
+ dragAndDropPlugin({
439
+ attachFile
440
+ }),
441
+ placeholder && placeholderPlugin(placeholder)
442
+ ].filter((e) => !!e)
443
+ }),
444
+ dispatchTransaction(tr) {
445
+ let result;
446
+ if (editor?.dispatchTransaction) {
447
+ result = editor.dispatchTransaction(tr);
448
+ } else {
449
+ view.updateState(view.state.apply(tr));
450
+ }
451
+ subject.next(tr);
452
+ return result;
453
+ }
454
+ });
455
+ function setValue(value) {
456
+ const wrap = document.createElement("div");
457
+ wrap.innerHTML = toInnerHTML(value);
458
+ const doc = prosemirrorParser.parse(wrap);
459
+ const tr = view.state.tr.replaceWith(
460
+ 0,
461
+ view.state.doc.content.size,
462
+ doc.content
463
+ );
464
+ view.dispatch(tr);
465
+ }
466
+ function toHTML() {
467
+ const fragment = prosemirrorSerializer.serializeFragment(
468
+ view.state.doc.content
469
+ );
470
+ const container2 = document.createElement("div");
471
+ container2.appendChild(fragment);
472
+ return container2.innerHTML;
473
+ }
474
+ function toTextContent() {
475
+ const state2 = view.state;
476
+ return state2.doc.textBetween(0, state2.doc.content.size, "\n");
477
+ }
478
+ const textEditorCommands = createCommands(schema, view, {
479
+ attachFile
480
+ });
481
+ const textEditorController = {
482
+ schema,
483
+ view,
484
+ subject,
485
+ set value(value) {
486
+ setValue(value);
487
+ },
488
+ get value() {
489
+ switch (mode) {
490
+ case "text":
491
+ return toTextContent();
492
+ default:
493
+ return toHTML();
494
+ }
495
+ },
496
+ commands: textEditorCommands
497
+ };
498
+ return textEditorController;
499
+ }
500
+ // Annotate the CommonJS export names for ESM import in node:
501
+ 0 && (module.exports = {
502
+ createTextEditorController
503
+ });