mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-07 01:12:56 +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
1577ec0d47
commit
f8788798b4
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
|
@ -81,6 +81,7 @@ export class Editor extends EventEmitter<EditorEvents> {
|
||||
enablePasteRules: true,
|
||||
enableCoreExtensions: true,
|
||||
enableContentCheck: false,
|
||||
emitContentError: false,
|
||||
onBeforeCreate: () => null,
|
||||
onCreate: () => null,
|
||||
onUpdate: () => null,
|
||||
|
@ -72,24 +72,43 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value,
|
||||
|
||||
let content: Fragment | ProseMirrorNode
|
||||
|
||||
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 (editor.storage.collaboration) {
|
||||
editor.storage.collaboration.isDisabled = true
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -127,6 +127,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;
|
||||
onBeforeCreate: (props: EditorEvents['beforeCreate']) => void;
|
||||
onCreate: (props: EditorEvents['create']) => void;
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user