🐛 Fix crash when pasting image into text editor

When pasting an image (with no text content) into the text editor,
Draft.js calls handlePastedText with null/empty text. The previous fix
guarded splitTextIntoTextBlocks against null, but insertText still
attempted to build a fragment from an empty block array, causing
Modifier.replaceWithFragment to crash with 'Cannot read properties of
undefined (reading getLength)'.

Fix insertText to return the original state unchanged when there are no
text blocks to insert. Also guard handle-pasted-text in the ClojureScript
editor to skip the insert-text call entirely when text is nil or empty.

Signed-off-by: Andrey Antukh <niwi@niwi.nz>
This commit is contained in:
Andrey Antukh 2026-03-24 13:00:28 +00:00
parent d863c7065f
commit 56f1fcdb53
2 changed files with 11 additions and 6 deletions

View File

@ -375,6 +375,10 @@ function splitTextIntoTextBlocks(text) {
export function insertText(state, text, attrs, inlineStyles) { export function insertText(state, text, attrs, inlineStyles) {
const blocks = splitTextIntoTextBlocks(text); const blocks = splitTextIntoTextBlocks(text);
if (blocks.length === 0) {
return state;
}
const character = CharacterMetadata.create({style: OrderedSet(inlineStyles)}); const character = CharacterMetadata.create({style: OrderedSet(inlineStyles)});
let blockArray = DraftPasteProcessor.processText( let blockArray = DraftPasteProcessor.processText(

View File

@ -221,12 +221,13 @@
handle-pasted-text handle-pasted-text
(fn [text _ _] (fn [text _ _]
(let [current-block-styles (ted/get-editor-current-block-data state) (when (seq text)
inline-styles (ted/get-editor-current-inline-styles state) (let [current-block-styles (ted/get-editor-current-block-data state)
style (merge current-block-styles inline-styles) inline-styles (ted/get-editor-current-inline-styles state)
state (-> (ted/insert-text state text style) style (merge current-block-styles inline-styles)
(handle-change))] state (-> (ted/insert-text state text style)
(st/emit! (dwt/update-editor-state shape state))) (handle-change))]
(st/emit! (dwt/update-editor-state shape state))))
"handled")] "handled")]
(mf/use-layout-effect on-mount) (mf/use-layout-effect on-mount)