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