Solution for applying HTML to drafts that does not nest incoming nodes

This commit is contained in:
Ben Gotow 2020-04-21 23:31:04 -05:00
parent 2dd6104b20
commit c3577f579f

View file

@ -1,5 +1,5 @@
import MailspringStore from 'mailspring-store'; import MailspringStore from 'mailspring-store';
import { Editor, Value } from 'slate'; import { Editor, Value, Block } from 'slate';
import RegExpUtils from '../../regexp-utils'; import RegExpUtils from '../../regexp-utils';
import { localized } from '../../intl'; import { localized } from '../../intl';
@ -79,15 +79,11 @@ function hotwireDraftBodyState(draft: any, session: DraftEditingSession): Messag
} }
} }
const [newFirst, ...newRest] = inHTMLEditorValue.document.nodes.toArray(); _bodyEditorValue = edits
.replaceNodeByKey(first.key, Block.create({ type: 'div' }))
// replace the first node in the document with the first node of the new .moveToRangeOfDocument()
// document, and then "insert" the remaining new nodes at the end. .insertFragment(inHTMLEditorValue.document)
edits = edits.replaceNodeByKey(first.key, newFirst).moveToEndOfDocument(); .moveToStart().value;
for (const block of newRest) {
edits = edits.insertBlock(block);
}
_bodyEditorValue = edits.moveToStart().value;
} catch (err) { } catch (err) {
// deleting and re-inserting the whole document seems to push Slate pretty hard and it // deleting and re-inserting the whole document seems to push Slate pretty hard and it
// sometimes fails with odd schema issues (undefined node, invalid range.) Just fall // sometimes fails with odd schema issues (undefined node, invalid range.) Just fall