You can use replaceWithFragment. To build the fragment you want to insert, first convert the string to HTML blocks with the utility convertFromHTML
const htmlContent = convertFromHTML(text);
then, use another utility to pass the array of content blocks to a map
const htmlContentMap = BlockMapBuilder.createFromArray(
htmlContent.contentBlocks
);
and call replaceWithFragment the same way you were calling insertText, but with the map of HTML content you built
const newContent = Modifier.replaceWithFragment(
currentContent,
currentSelection,
htmlContentMap
);
UPDATE
DraftJS doesn't support the button tag by default but you can add it as a custom block render
first, create a map of types by merging the default supported tags with the button tag
import {
...
DefaultDraftBlockRenderMap,
...
} from "draft-js";
...
const mapWithButton = Map({
button: {
element: "button"
}
}).merge(DefaultDraftBlockRenderMap);
...
and pass it as the third argument to the convertFromHTML function
const htmlContent = convertFromHTML(
text,
undefined,
mapWithButton
);
create a custom block renderer function and pass it to the Editor
function myBlockRenderer(contentBlock: ContentBlock) {
const type = contentBlock.getType();
if (type === "button") {
return {
component: () => {
return (
<button onClick={() => console.log("doesn't seem to work :(")}>
{contentBlock.getText()}
</button>
);
},
editable: false,
};
}
}
...
<Editor
...
customBlockRenderFunc={myBlockRenderer}
/>
It kind of works because it shows the button, although sometimes when there is text before the point when you insert it, it merges the previous text with the button text without showing the button. The click doesn't work either probably because of this
If your custom block renderer requires mouse interaction, it is often
wise to temporarily set your Editor to readOnly={true} during this
interaction. In this way, the user does not trigger any selection
changes within the editor while interacting with the custom block.
https://codesandbox.io/s/insert-html-draftjs-73rmc?file=/src/Editor.tsx