From 55d0e67422d2a98674b7059be498140ff1f5402c Mon Sep 17 00:00:00 2001 From: castroCrea Date: Wed, 4 Aug 2021 12:14:12 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Add=20CreateNodeFromContentOpti?= =?UTF-8?q?ons=20=20to=20insertContent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/commands/insertContent.ts | 7 ++++--- packages/core/src/commands/insertContentAt.ts | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/core/src/commands/insertContent.ts b/packages/core/src/commands/insertContent.ts index 9b8db9130..5acd40b79 100644 --- a/packages/core/src/commands/insertContent.ts +++ b/packages/core/src/commands/insertContent.ts @@ -1,3 +1,4 @@ +import { CreateNodeFromContentOptions } from '../helpers/createNodeFromContent' import { RawCommands, Content } from '../types' declare module '@tiptap/core' { @@ -6,11 +7,11 @@ declare module '@tiptap/core' { /** * Insert a node or string of HTML at the current position. */ - insertContent: (value: Content) => ReturnType, + insertContent: (value: Content, options?: CreateNodeFromContentOptions) => ReturnType, } } } -export const insertContent: RawCommands['insertContent'] = value => ({ tr, commands }) => { - return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value) +export const insertContent: RawCommands['insertContent'] = (value, options) => ({ tr, commands }) => { + return commands.insertContentAt({ from: tr.selection.from, to: tr.selection.to }, value, options) } diff --git a/packages/core/src/commands/insertContentAt.ts b/packages/core/src/commands/insertContentAt.ts index 22e6fc7df..f5a1a4f1f 100644 --- a/packages/core/src/commands/insertContentAt.ts +++ b/packages/core/src/commands/insertContentAt.ts @@ -1,4 +1,4 @@ -import createNodeFromContent from '../helpers/createNodeFromContent' +import createNodeFromContent, { CreateNodeFromContentOptions } from '../helpers/createNodeFromContent' import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd' import { RawCommands, @@ -12,17 +12,18 @@ declare module '@tiptap/core' { /** * Insert a node or string of HTML at a specific position. */ - insertContentAt: (position: number | Range, value: Content) => ReturnType, + insertContentAt: (position: number | Range, value: Content, options?: CreateNodeFromContentOptions) => ReturnType, } } } -export const insertContentAt: RawCommands['insertContentAt'] = (position, value) => ({ tr, dispatch, editor }) => { +export const insertContentAt: RawCommands['insertContentAt'] = (position, value, options) => ({ tr, dispatch, editor }) => { if (dispatch) { const content = createNodeFromContent(value, editor.schema, { parseOptions: { preserveWhitespace: 'full', }, + ...(options || {}) }) // don’t dispatch an empty fragment because this can lead to strange errors From 68fe31b2e42238b785e0569c1e3902315227d724 Mon Sep 17 00:00:00 2001 From: castroCrea Date: Wed, 4 Aug 2021 12:36:12 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9D=20Add=20it=20to=20the=20doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/demos/Examples/Tables/React/index.jsx | 32 +++++++++++++++++++ .../docPages/api/commands/insert-content.md | 8 +++++ packages/core/src/commands/insertContentAt.ts | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/docs/src/demos/Examples/Tables/React/index.jsx b/docs/src/demos/Examples/Tables/React/index.jsx index 15702130d..179c71cc1 100644 --- a/docs/src/demos/Examples/Tables/React/index.jsx +++ b/docs/src/demos/Examples/Tables/React/index.jsx @@ -32,6 +32,31 @@ const CustomTableCell = TableCell.extend({ }, }) +export const tableHTML = ` + + + + + + + + + + + + + + + + + + + + + +
FirstnameLastnameAge
JillSmith50
EveJackson94
JohnDoe80
+` + const MenuBar = ({ editor }) => { if (!editor) { return null @@ -42,6 +67,13 @@ const MenuBar = ({ editor }) => { + diff --git a/docs/src/docPages/api/commands/insert-content.md b/docs/src/docPages/api/commands/insert-content.md index 147230a82..24059b374 100644 --- a/docs/src/docPages/api/commands/insert-content.md +++ b/docs/src/docPages/api/commands/insert-content.md @@ -16,6 +16,14 @@ editor.commands.insertContent('Example Text') // HTML editor.commands.insertContent('

Example Text

') +// HTML with trim white space +editor.commands.insertContent('

Example Text

', +{ + parseOptions: { + preserveWhitespace: false, + } +}) + // JSON/Nodes editor.commands.insertContent({ type: 'heading', diff --git a/packages/core/src/commands/insertContentAt.ts b/packages/core/src/commands/insertContentAt.ts index f5a1a4f1f..db971232e 100644 --- a/packages/core/src/commands/insertContentAt.ts +++ b/packages/core/src/commands/insertContentAt.ts @@ -23,7 +23,7 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, parseOptions: { preserveWhitespace: 'full', }, - ...(options || {}) + ...(options || {}), }) // don’t dispatch an empty fragment because this can lead to strange errors