Merge pull request #6422 from ueberdosis/feature/emit-content-error-when-content-check-is-disabled-cherrypick-next
Some checks are pending
build / build (20) (push) Waiting to run
build / test (20, map[name:Demos/Commands spec:./demos/src/Commands/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / test (20, map[name:Demos/Examples spec:./demos/src/Examples/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / test (20, map[name:Demos/Experiments spec:./demos/src/Experiments/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / test (20, map[name:Demos/Extensions spec:./demos/src/Extensions/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / test (20, map[name:Demos/GuideContent spec:./demos/src/GuideContent/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / test (20, map[name:Demos/GuideGettingStarted spec:./demos/src/GuideGettingStarted/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / test (20, map[name:Demos/Marks spec:./demos/src/Marks/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / test (20, map[name:Demos/Nodes spec:./demos/src/Nodes/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / test (20, map[name:Integration spec:./tests/cypress/integration/**/*.spec.{js,ts}]) (push) Blocked by required conditions
build / release (20) (push) Blocked by required conditions
Publish / Release (20) (push) Waiting to run

Merge pull request #6411 from ueberdosis/feature/emit-content-error-when-content-check-is-disabled
This commit is contained in:
Arnau Gómez Farell 2025-06-05 15:22:06 +02:00 committed by GitHub
commit a035719174
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 10 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": minor
---
Add config option to emit content error when content check is disabled

View File

@ -92,6 +92,7 @@ export class Editor extends EventEmitter<EditorEvents> {
enablePasteRules: true,
enableCoreExtensions: true,
enableContentCheck: false,
emitContentError: false,
onBeforeCreate: () => null,
onCreate: () => null,
onUpdate: () => null,

View File

@ -76,18 +76,10 @@ export const insertContentAt: RawCommands['insertContentAt'] =
let content: Fragment | ProseMirrorNode
const { selection } = editor.state
try {
content = createNodeFromContent(value, editor.schema, {
parseOptions: {
preserveWhitespace: 'full',
...options.parseOptions,
},
errorOnInvalidContent: options.errorOnInvalidContent ?? editor.options.enableContentCheck,
})
} catch (e) {
const emitContentError = (error: Error) => {
editor.emit('contentError', {
editor,
error: e as Error,
error,
disableCollaboration: () => {
if (
'collaboration' in editor.storage &&
@ -98,6 +90,33 @@ export const insertContentAt: RawCommands['insertContentAt'] =
}
},
})
}
const parseOptions: ParseOptions = {
preserveWhitespace: 'full',
...options.parseOptions,
}
// If `emitContentError` is enabled, we want to check the content for errors
// but ignore them (do not remove the invalid content from the document)
if (!options.errorOnInvalidContent && !editor.options.enableContentCheck && editor.options.emitContentError) {
try {
createNodeFromContent(value, editor.schema, {
parseOptions,
errorOnInvalidContent: true,
})
} catch (e) {
emitContentError(e as Error)
}
}
try {
content = createNodeFromContent(value, editor.schema, {
parseOptions,
errorOnInvalidContent: options.errorOnInvalidContent ?? editor.options.enableContentCheck,
})
} catch (e) {
emitContentError(e as Error)
return false
}

View File

@ -354,6 +354,15 @@ export interface EditorOptions {
* @default false
*/
enableContentCheck: boolean
/**
* If `true`, the editor will emit the `contentError` event if invalid content is
* encountered but `enableContentCheck` is `false`. This lets you preserve the
* invalid editor content while still showing a warning or error message to
* the user.
*
* @default false
*/
emitContentError: boolean
/**
* Called before the editor is constructed.
*/