Skip to content

Commit d0ff5e3

Browse files
committed
fix: add media would crash if there is no text entered yet
1 parent 515282c commit d0ff5e3

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

admin/src/components/ReactMdEditor/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,24 @@ const Editor = ({
7474
let newValue = value ? value : "";
7575
assets.map((asset) => {
7676
if (asset.mime.includes("image")) {
77-
const imgTag = ` ![${asset.alt}](${asset.url}) `;
77+
const imgTag = `![${asset.alt}](${asset.url})`;
7878
if (mediaLibSelection > -1) {
79-
newValue =
80-
value.substring(0, mediaLibSelection) +
81-
imgTag +
82-
value.substring(mediaLibSelection);
79+
const preValue = value?.substring(0, mediaLibSelection) ?? "";
80+
const postValue = value?.substring(mediaLibSelection) ?? "";
81+
newValue = `${
82+
preValue && !preValue.endsWith(" ") ? preValue + " " : preValue
83+
}${imgTag}${
84+
postValue && !postValue.startsWith(" ")
85+
? " " + postValue
86+
: postValue
87+
}`;
8388
} else {
8489
newValue = `${newValue}${imgTag}`;
8590
}
8691
}
8792
// Handle videos and other type of files by adding some code
8893
});
89-
onChange({ target: { name, value: newValue || "" } });
94+
onChange({ target: { name, value: newValue ?? "" } });
9095
handleToggleMediaLib();
9196
};
9297

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strapi-plugin-wysiwyg-react-md-editor",
3-
"version": "4.4.0",
3+
"version": "4.4.1",
44
"description": "Replaces the default Strapi WYSIWYG editor with react md editor.",
55
"strapi": {
66
"name": "wysiwyg-react-md-editor",

0 commit comments

Comments
 (0)