@mbs-dev/react-editor 1.1.0 → 1.3.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.
- package/dist/Editor.js +28 -4
- package/package.json +1 -1
- package/src/Editor.tsx +28 -5
package/dist/Editor.js
CHANGED
|
@@ -57,6 +57,18 @@ var ReactEditor = function (_a) {
|
|
|
57
57
|
var onChange = _a.onChange, onBlur = _a.onBlur, value = _a.value, config = _a.config;
|
|
58
58
|
return (react_1.default.createElement(jodit_react_1.default, { value: value, config: config, onBlur: onBlur, onChange: onChange }));
|
|
59
59
|
};
|
|
60
|
+
var isImageByExtension = function (filename, imageExts) {
|
|
61
|
+
var _a, _b;
|
|
62
|
+
var clean = (_b = (_a = (filename || '').split('?')[0]) === null || _a === void 0 ? void 0 : _a.split('#')[0]) !== null && _b !== void 0 ? _b : '';
|
|
63
|
+
var ext = (clean.split('.').pop() || '').toLowerCase();
|
|
64
|
+
return !!ext && imageExts.includes(ext);
|
|
65
|
+
};
|
|
66
|
+
var getDisplayNameFromPath = function (filename) {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
var clean = (_b = (_a = (filename || '').split('?')[0]) === null || _a === void 0 ? void 0 : _a.split('#')[0]) !== null && _b !== void 0 ? _b : '';
|
|
69
|
+
var last = clean.split('/').pop();
|
|
70
|
+
return last ? decodeURIComponent(last) : filename;
|
|
71
|
+
};
|
|
60
72
|
var uploaderConfig = function (apiUrl, imageUrl) { return ({
|
|
61
73
|
imagesExtensions: ['jpg', 'png', 'jpeg', 'gif', 'webp'],
|
|
62
74
|
filesVariableName: function (t) {
|
|
@@ -70,15 +82,27 @@ var uploaderConfig = function (apiUrl, imageUrl) { return ({
|
|
|
70
82
|
return formdata;
|
|
71
83
|
},
|
|
72
84
|
isSuccess: function (e) {
|
|
85
|
+
var _this = this;
|
|
73
86
|
var _a;
|
|
74
87
|
var fn = this.jodit;
|
|
75
88
|
if (((_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.files) && e.data.files.length) {
|
|
76
|
-
var tagName_1 = 'img';
|
|
77
89
|
e.data.files.forEach(function (filename) {
|
|
78
|
-
var elm = fn.createInside.element(tagName_1);
|
|
79
90
|
var src = imageUrl ? "".concat(imageUrl, "/").concat(filename) : filename;
|
|
80
|
-
|
|
81
|
-
|
|
91
|
+
if (isImageByExtension(filename, _this.imagesExtensions || ['jpg', 'png', 'jpeg', 'gif', 'webp'])) {
|
|
92
|
+
var tagName = 'img';
|
|
93
|
+
var elm = fn.createInside.element(tagName);
|
|
94
|
+
elm.setAttribute('src', src);
|
|
95
|
+
fn.s.insertImage(elm, null, fn.o.imageDefaultWidth);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
var tagName = 'a';
|
|
99
|
+
var elm = fn.createInside.element(tagName);
|
|
100
|
+
elm.setAttribute('href', src);
|
|
101
|
+
elm.setAttribute('target', '_blank');
|
|
102
|
+
elm.setAttribute('rel', 'noopener noreferrer');
|
|
103
|
+
elm.textContent = getDisplayNameFromPath(filename);
|
|
104
|
+
fn.s.insertNode(elm);
|
|
105
|
+
}
|
|
82
106
|
});
|
|
83
107
|
}
|
|
84
108
|
return !!(e === null || e === void 0 ? void 0 : e.success);
|
package/package.json
CHANGED
package/src/Editor.tsx
CHANGED
|
@@ -13,6 +13,18 @@ const ReactEditor: React.FC<EditorProps> = ({ onChange, onBlur, value, config })
|
|
|
13
13
|
);
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
const isImageByExtension = (filename: string, imageExts: string[]): boolean => {
|
|
17
|
+
const clean = (filename || '').split('?')[0]?.split('#')[0] ?? '';
|
|
18
|
+
const ext = (clean.split('.').pop() || '').toLowerCase();
|
|
19
|
+
return !!ext && imageExts.includes(ext);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const getDisplayNameFromPath = (filename: string): string => {
|
|
23
|
+
const clean = (filename || '').split('?')[0]?.split('#')[0] ?? '';
|
|
24
|
+
const last = clean.split('/').pop();
|
|
25
|
+
return last ? decodeURIComponent(last) : filename;
|
|
26
|
+
};
|
|
27
|
+
|
|
16
28
|
/**
|
|
17
29
|
* Uploader configuration for Jodit
|
|
18
30
|
* Handles image upload + insertion in the editor
|
|
@@ -36,13 +48,24 @@ export const uploaderConfig = (
|
|
|
36
48
|
const fn = this.jodit;
|
|
37
49
|
|
|
38
50
|
if (e?.data?.files && e.data.files.length) {
|
|
39
|
-
const tagName = 'img';
|
|
40
|
-
|
|
41
51
|
e.data.files.forEach((filename: string) => {
|
|
42
|
-
const elm = fn.createInside.element(tagName);
|
|
43
52
|
const src = imageUrl ? `${imageUrl}/${filename}` : filename;
|
|
44
|
-
|
|
45
|
-
|
|
53
|
+
|
|
54
|
+
// ✅ If it's an image => insert <img>, otherwise insert <a href="...">
|
|
55
|
+
if (isImageByExtension(filename, this.imagesExtensions || ['jpg', 'png', 'jpeg', 'gif', 'webp'])) {
|
|
56
|
+
const tagName = 'img';
|
|
57
|
+
const elm = fn.createInside.element(tagName);
|
|
58
|
+
elm.setAttribute('src', src);
|
|
59
|
+
fn.s.insertImage(elm as HTMLImageElement, null, fn.o.imageDefaultWidth);
|
|
60
|
+
} else {
|
|
61
|
+
const tagName = 'a';
|
|
62
|
+
const elm = fn.createInside.element(tagName);
|
|
63
|
+
elm.setAttribute('href', src);
|
|
64
|
+
elm.setAttribute('target', '_blank');
|
|
65
|
+
elm.setAttribute('rel', 'noopener noreferrer');
|
|
66
|
+
elm.textContent = getDisplayNameFromPath(filename);
|
|
67
|
+
fn.s.insertNode(elm);
|
|
68
|
+
}
|
|
46
69
|
});
|
|
47
70
|
}
|
|
48
71
|
|