fix: preserve whitespace when using insertContent command

This commit is contained in:
Philipp Kühn 2021-05-19 15:15:56 +02:00
parent c0cbd513f6
commit 8f101810fe
2 changed files with 9 additions and 2 deletions

View File

@ -20,7 +20,11 @@ declare module '@tiptap/core' {
export const insertContentAt: RawCommands['insertContentAt'] = (position, value) => ({ tr, dispatch, editor }) => {
if (dispatch) {
const content = createNodeFromContent(value, editor.schema)
const content = createNodeFromContent(value, editor.schema, {
parseOptions: {
preserveWhitespace: 'full',
},
})
const { from, to } = typeof position === 'number'
? { from: position, to: position }
: position

View File

@ -1,3 +1,6 @@
export default function elementFromString(value: string): HTMLElement {
return new window.DOMParser().parseFromString(value, 'text/html').body
// add a wrapper to preserve leading and trailing whitespace
const wrappedValue = `<body>${value}</body>`
return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body
}