@finema/core 3.9.0 → 3.9.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/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -20,83 +20,100 @@ export const ImagePaste = Extension.create({
|
|
|
20
20
|
new Plugin({
|
|
21
21
|
key: new PluginKey("imagePaste"),
|
|
22
22
|
props: {
|
|
23
|
-
handlePaste:
|
|
23
|
+
handlePaste: (view, event) => {
|
|
24
24
|
const items = Array.from(event.clipboardData?.items || []);
|
|
25
25
|
const imageItems = items.filter((item) => item.type.includes("image"));
|
|
26
26
|
if (imageItems.length === 0) {
|
|
27
27
|
return false;
|
|
28
28
|
}
|
|
29
29
|
event.preventDefault();
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
if (!options.requestOptions) {
|
|
38
|
-
console.warn("ImagePaste: requestOptions is not configured");
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
const {
|
|
42
|
-
schema
|
|
43
|
-
} = view.state;
|
|
44
|
-
const pos = view.state.selection.from;
|
|
45
|
-
if (schema.nodes.imageUpload) {
|
|
46
|
-
const transaction = view.state.tr.insert(pos, schema.nodes.imageUpload.create());
|
|
47
|
-
view.dispatch(transaction);
|
|
48
|
-
}
|
|
49
|
-
const request = {
|
|
50
|
-
requestOptions: options.requestOptions,
|
|
51
|
-
pathURL: options.uploadPathURL
|
|
52
|
-
};
|
|
53
|
-
const uploadLoader = useUploadLoader(request);
|
|
54
|
-
const formData = new FormData();
|
|
55
|
-
const bodyKey = options.bodyKey || "file";
|
|
56
|
-
formData.append(bodyKey, file);
|
|
57
|
-
await uploadLoader.run({
|
|
58
|
-
data: formData
|
|
59
|
-
});
|
|
60
|
-
if (uploadLoader.status.value.isSuccess && uploadLoader.data.value) {
|
|
61
|
-
const responseURL = options.responseURL || "url";
|
|
62
|
-
const url = _get(uploadLoader.data.value, responseURL);
|
|
63
|
-
if (!url) {
|
|
64
|
-
console.error("ImagePaste: Could not find URL in response", uploadLoader.data.value);
|
|
30
|
+
const uploadImages = async () => {
|
|
31
|
+
for (const item of imageItems) {
|
|
32
|
+
const file = item.getAsFile();
|
|
33
|
+
if (!file) continue;
|
|
34
|
+
if (options.maxSize && file.size > options.maxSize * 1024) {
|
|
35
|
+
console.warn(`Image size (${file.size} bytes) exceeds maximum allowed size (${options.maxSize} KB)`);
|
|
65
36
|
continue;
|
|
66
37
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
38
|
+
if (!options.requestOptions) {
|
|
39
|
+
console.warn("ImagePaste: requestOptions is not configured");
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const {
|
|
43
|
+
schema
|
|
44
|
+
} = view.state;
|
|
45
|
+
const pos = view.state.selection.from;
|
|
46
|
+
if (schema.nodes.imageUpload) {
|
|
47
|
+
const transaction = view.state.tr.insert(pos, schema.nodes.imageUpload.create({
|
|
48
|
+
loading: true
|
|
49
|
+
}));
|
|
50
|
+
view.dispatch(transaction);
|
|
51
|
+
}
|
|
52
|
+
const request = {
|
|
53
|
+
requestOptions: options.requestOptions,
|
|
54
|
+
pathURL: options.uploadPathURL
|
|
55
|
+
};
|
|
56
|
+
const uploadLoader = useUploadLoader(request);
|
|
57
|
+
const formData = new FormData();
|
|
58
|
+
const bodyKey = options.bodyKey || "file";
|
|
59
|
+
formData.append(bodyKey, file);
|
|
60
|
+
await uploadLoader.run({
|
|
61
|
+
data: formData
|
|
74
62
|
});
|
|
75
|
-
if (
|
|
76
|
-
const
|
|
77
|
-
|
|
63
|
+
if (uploadLoader.status.value.isSuccess && uploadLoader.data.value) {
|
|
64
|
+
const responseURL = options.responseURL || "url";
|
|
65
|
+
const url = _get(uploadLoader.data.value, responseURL);
|
|
66
|
+
if (!url) {
|
|
67
|
+
console.error("ImagePaste: Could not find URL in response", uploadLoader.data.value);
|
|
68
|
+
const currentState2 = view.state;
|
|
69
|
+
let placeholderPos2 = -1;
|
|
70
|
+
currentState2.doc.descendants((node, nodePos) => {
|
|
71
|
+
if (node.type.name === "imageUpload" && placeholderPos2 === -1) {
|
|
72
|
+
placeholderPos2 = nodePos;
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (placeholderPos2 !== -1) {
|
|
77
|
+
const tr = currentState2.tr.delete(placeholderPos2, placeholderPos2 + 1);
|
|
78
|
+
view.dispatch(tr);
|
|
79
|
+
}
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const currentState = view.state;
|
|
83
|
+
let placeholderPos = -1;
|
|
84
|
+
currentState.doc.descendants((node, nodePos) => {
|
|
85
|
+
if (node.type.name === "imageUpload" && placeholderPos === -1) {
|
|
86
|
+
placeholderPos = nodePos;
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
78
89
|
});
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
if (placeholderPos !== -1 && schema.nodes.image) {
|
|
91
|
+
const imageNode = schema.nodes.image.create({
|
|
92
|
+
src: url
|
|
93
|
+
});
|
|
94
|
+
let tr = currentState.tr.delete(placeholderPos, placeholderPos + 1).insert(placeholderPos, imageNode);
|
|
95
|
+
const resolvedPos = tr.doc.resolve(placeholderPos + imageNode.nodeSize);
|
|
96
|
+
tr = tr.setSelection(TextSelection.near(resolvedPos));
|
|
97
|
+
view.dispatch(tr);
|
|
98
|
+
}
|
|
99
|
+
} else if (uploadLoader.status.value.isError) {
|
|
100
|
+
console.error("ImagePaste: Upload failed", uploadLoader.status.value.errorData);
|
|
101
|
+
const currentState = view.state;
|
|
102
|
+
let placeholderPos = -1;
|
|
103
|
+
currentState.doc.descendants((node, nodePos) => {
|
|
104
|
+
if (node.type.name === "imageUpload" && placeholderPos === -1) {
|
|
105
|
+
placeholderPos = nodePos;
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
if (placeholderPos !== -1) {
|
|
110
|
+
const tr = currentState.tr.delete(placeholderPos, placeholderPos + 1);
|
|
111
|
+
view.dispatch(tr);
|
|
92
112
|
}
|
|
93
|
-
});
|
|
94
|
-
if (placeholderPos !== -1) {
|
|
95
|
-
const tr = currentState.tr.delete(placeholderPos, placeholderPos + 1);
|
|
96
|
-
view.dispatch(tr);
|
|
97
113
|
}
|
|
98
114
|
}
|
|
99
|
-
}
|
|
115
|
+
};
|
|
116
|
+
uploadImages();
|
|
100
117
|
return true;
|
|
101
118
|
}
|
|
102
119
|
}
|