Skip to content

Commit

Permalink
Merge branch 'develop' into markviews
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 authored Oct 25, 2024
2 parents 7938d42 + f95b13e commit d3e43dc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 27 deletions.
21 changes: 21 additions & 0 deletions demos/src/Commands/SetContent/React/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ context('/src/Commands/SetContent/React/', () => {
})
})

it('should insert raw JSON content', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent({ type: 'paragraph', content: [{ type: 'text', text: 'Hello World.' }] })
cy.get('.tiptap').should('contain.html', '<p>Hello World.</p>')
})
})

it('should insert a Prosemirror Node as content', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent(editor.schema.node('paragraph', null, editor.schema.text('Hello World.')))
cy.get('.tiptap').should('contain.html', '<p>Hello World.</p>')
})
})

it('should insert a Prosemirror Fragment as content', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent(editor.schema.node('doc', null, editor.schema.node('paragraph', null, editor.schema.text('Hello World.'))).content)
cy.get('.tiptap').should('contain.html', '<p>Hello World.</p>')
})
})

it('should emit updates', () => {
cy.get('.tiptap').then(([{ editor }]) => {
let updateCount = 0
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/commands/insertContent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParseOptions } from '@tiptap/pm/model'
import { Fragment, Node as ProseMirrorNode, ParseOptions } from '@tiptap/pm/model'

import { Content, RawCommands } from '../types.js'

Expand All @@ -14,7 +14,7 @@ declare module '@tiptap/core' {
/**
* The ProseMirror content to insert.
*/
value: Content,
value: Content | ProseMirrorNode | Fragment,

/**
* Optional options
Expand All @@ -23,17 +23,17 @@ declare module '@tiptap/core' {
/**
* Options for parsing the content.
*/
parseOptions?: ParseOptions
parseOptions?: ParseOptions;

/**
* Whether to update the selection after inserting the content.
*/
updateSelection?: boolean
applyInputRules?: boolean
applyPasteRules?: boolean
},
) => ReturnType
}
updateSelection?: boolean;
applyInputRules?: boolean;
applyPasteRules?: boolean;
}
) => ReturnType;
};
}
}

Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/commands/insertContentAt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare module '@tiptap/core' {
/**
* The ProseMirror content to insert.
*/
value: Content,
value: Content | ProseMirrorNode | Fragment,

/**
* Optional options
Expand Down Expand Up @@ -132,6 +132,16 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value,
// otherwise if it is an array, we have to join it
if (Array.isArray(value)) {
newContent = value.map(v => v.text || '').join('')
} else if (value instanceof Fragment) {
let text = ''

value.forEach(node => {
if (node.text) {
text += node.text
}
})

newContent = text
} else if (typeof value === 'object' && !!value && !!value.text) {
newContent = value.text
} else {
Expand Down
24 changes: 10 additions & 14 deletions packages/core/src/commands/setContent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParseOptions } from '@tiptap/pm/model'
import { Fragment, Node as ProseMirrorNode, ParseOptions } from '@tiptap/pm/model'

import { createDocument } from '../helpers/createDocument.js'
import { Content, RawCommands } from '../types.js'
Expand All @@ -17,7 +17,7 @@ declare module '@tiptap/core' {
/**
* The new content.
*/
content: Content,
content: Content | Fragment | ProseMirrorNode,

/**
* Whether to emit an update event.
Expand All @@ -37,10 +37,10 @@ declare module '@tiptap/core' {
/**
* Whether to throw an error if the content is invalid.
*/
errorOnInvalidContent?: boolean
},
) => ReturnType
}
errorOnInvalidContent?: boolean;
}
) => ReturnType;
};
}
}

Expand All @@ -66,12 +66,8 @@ export const setContent: RawCommands['setContent'] = (content, emitUpdate = fals
tr.setMeta('preventUpdate', !emitUpdate)
}

return commands.insertContentAt(
{ from: 0, to: doc.content.size },
content,
{
parseOptions,
errorOnInvalidContent: options.errorOnInvalidContent ?? editor.options.enableContentCheck,
},
)
return commands.insertContentAt({ from: 0, to: doc.content.size }, content, {
parseOptions,
errorOnInvalidContent: options.errorOnInvalidContent ?? editor.options.enableContentCheck,
})
}
6 changes: 4 additions & 2 deletions packages/core/src/helpers/createDocument.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Node as ProseMirrorNode, ParseOptions, Schema } from '@tiptap/pm/model'
import {
Fragment, Node as ProseMirrorNode, ParseOptions, Schema,
} from '@tiptap/pm/model'

import { Content } from '../types.js'
import { createNodeFromContent } from './createNodeFromContent.js'
Expand All @@ -11,7 +13,7 @@ import { createNodeFromContent } from './createNodeFromContent.js'
* @returns The created Prosemirror document node
*/
export function createDocument(
content: Content,
content: Content | ProseMirrorNode | Fragment,
schema: Schema,
parseOptions: ParseOptions = {},
options: { errorOnInvalidContent?: boolean } = {},
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/helpers/createNodeFromContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ export type CreateNodeFromContentOptions = {
* @returns The created Prosemirror node or fragment
*/
export function createNodeFromContent(
content: Content,
content: Content | ProseMirrorNode | Fragment,
schema: Schema,
options?: CreateNodeFromContentOptions,
): ProseMirrorNode | Fragment {
if (content instanceof ProseMirrorNode || content instanceof Fragment) {
return content
}
options = {
slice: true,
parseOptions: {},
Expand Down

0 comments on commit d3e43dc

Please sign in to comment.