fix(core): read preventClearDocument meta + allow disable core plugins #5490 (#5514)

This commit is contained in:
Alan North 2024-08-20 11:32:44 -03:00 committed by GitHub
parent cac2da16c4
commit 97ea55fe4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 4 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": minor
---
Fixes #5490. The `preventClearDocument` meta tag can now be used to prevent the `clearDocument` plugin in the core keymap extension from modifying transactions that appear to clear the document (but might be clearing it for other reasons).

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": minor
---
An object can now be passed to `enableCoreExtensions` to allow disabling only specific core extensions.

View File

@ -261,7 +261,12 @@ export class Editor extends EventEmitter<EditorEvents> {
FocusEvents,
Keymap,
Tabindex,
] : []
].filter(ext => {
if (typeof this.options.enableCoreExtensions === 'object') {
return this.options.enableCoreExtensions[ext.name as keyof typeof this.options.enableCoreExtensions] !== false
}
return true
}) : []
const allExtensions = [...coreExtensions, ...this.options.extensions].filter(extension => {
return ['extension', 'node', 'mark'].includes(extension?.type)
})

View File

@ -3,6 +3,7 @@ import { Plugin, PluginKey, Selection } from '@tiptap/pm/state'
import { CommandManager } from '../CommandManager.js'
import { Extension } from '../Extension.js'
import { createChainableState } from '../helpers/createChainableState.js'
import { isNodeEmpty } from '../helpers/isNodeEmpty.js'
import { isiOS } from '../utilities/isiOS.js'
import { isMacOS } from '../utilities/isMacOS.js'
@ -106,7 +107,9 @@ export const Keymap = Extension.create({
const docChanges = transactions.some(transaction => transaction.docChanged)
&& !oldState.doc.eq(newState.doc)
if (!docChanges) {
const ignoreTr = transactions.some(transaction => transaction.getMeta('preventClearDocument'))
if (!docChanges || ignoreTr) {
return
}
@ -119,7 +122,7 @@ export const Keymap = Extension.create({
return
}
const isEmpty = newState.doc.textBetween(0, newState.doc.content.size, ' ', ' ').length === 0
const isEmpty = isNodeEmpty(newState.doc)
if (!isEmpty) {
return

View File

@ -83,7 +83,24 @@ export interface EditorOptions {
};
enableInputRules: EnableRules;
enablePasteRules: EnableRules;
enableCoreExtensions: boolean;
/**
* Determines whether core extensions are enabled.
*
* If set to `false`, all core extensions will be disabled.
* To disable specific core extensions, provide an object where the keys are the extension names and the values are `false`.
* Extensions not listed in the object will remain enabled.
*
* @example
* // Disable all core extensions
* enabledCoreExtensions: false
*
* @example
* // Disable only the keymap core extension
* enabledCoreExtensions: { keymap: false }
*
* @default true
*/
enableCoreExtensions: boolean | Record<'editable' | 'clipboardTextSerializer' | 'commands' | 'focusEvents' | 'keymap' | 'tabindex', false>;
/**
* If `true`, the editor will check the content for errors on initialization.
* Emitting the `contentError` event if the content is invalid.