When I'm clicking on the button the text from the clipboard is pasted without style, but keeps the current style inline, but that's not what I need.
My code
async function getClipboardContents() {
try {
const textFromClipboard = await navigator.clipboard.readText();
const newContentState = Modifier.replaceText(
editorState.getCurrentContent(),
editorState.getSelection(),
textFromClipboard,
editorState.getCurrentInlineStyle(),
);
const newPushState = Draft.EditorState.push(editorState, newContentState, 'change-block-data');
handleOnChange(newPushState);
} catch (err) {
console.error('error', err);
}
}
What needs to be done so that the text is inserted with formatting?
Text formatting loses after using clipboard.readText(). You should use clipboard.read() instead. It will return array of ClipBoardItem.
You can read your initial formatting from this object that way:
const [clipboardItem] = await navigator.clipboard.read();
const blob = await clipboardItem.getType('text/html');
// Here's your formmated text
const formattedText = await new Response(blob).text();