mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-08 01:53:04 +08:00
Merge pull request #6411 from ueberdosis/feature/emit-content-error-when-content-check-is-disabled
feat: add config option to emit content error when content check is disabled
This commit is contained in:
parent
e2f23d8474
commit
d0fda30635
5
.changeset/fresh-ads-nail.md
Normal file
5
.changeset/fresh-ads-nail.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@tiptap/core": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add config option to emit content error when content check is disabled
|
@ -92,6 +92,7 @@ export class Editor extends EventEmitter<EditorEvents> {
|
|||||||
enablePasteRules: true,
|
enablePasteRules: true,
|
||||||
enableCoreExtensions: true,
|
enableCoreExtensions: true,
|
||||||
enableContentCheck: false,
|
enableContentCheck: false,
|
||||||
|
emitContentError: false,
|
||||||
onBeforeCreate: () => null,
|
onBeforeCreate: () => null,
|
||||||
onCreate: () => null,
|
onCreate: () => null,
|
||||||
onUpdate: () => null,
|
onUpdate: () => null,
|
||||||
|
@ -76,18 +76,10 @@ export const insertContentAt: RawCommands['insertContentAt'] =
|
|||||||
let content: Fragment | ProseMirrorNode
|
let content: Fragment | ProseMirrorNode
|
||||||
const { selection } = editor.state
|
const { selection } = editor.state
|
||||||
|
|
||||||
try {
|
const emitContentError = (error: Error) => {
|
||||||
content = createNodeFromContent(value, editor.schema, {
|
|
||||||
parseOptions: {
|
|
||||||
preserveWhitespace: 'full',
|
|
||||||
...options.parseOptions,
|
|
||||||
},
|
|
||||||
errorOnInvalidContent: options.errorOnInvalidContent ?? editor.options.enableContentCheck,
|
|
||||||
})
|
|
||||||
} catch (e) {
|
|
||||||
editor.emit('contentError', {
|
editor.emit('contentError', {
|
||||||
editor,
|
editor,
|
||||||
error: e as Error,
|
error,
|
||||||
disableCollaboration: () => {
|
disableCollaboration: () => {
|
||||||
if (
|
if (
|
||||||
'collaboration' in editor.storage &&
|
'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
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -354,6 +354,15 @@ export interface EditorOptions {
|
|||||||
* @default false
|
* @default false
|
||||||
*/
|
*/
|
||||||
enableContentCheck: boolean
|
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.
|
* Called before the editor is constructed.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user