Skip to content

Commit

Permalink
fix: preserve whitespace when using insertContent command
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed May 19, 2021
1 parent c0cbd51 commit 8f10181
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/core/src/commands/insertContentAt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/utilities/elementFromString.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8f10181

Please sign in to comment.