diff --git a/packages/text-editor/src/components/extension/isEmptyContent.ts b/packages/text-editor/src/components/extension/isEmptyContent.ts new file mode 100644 index 0000000000..026d88b442 --- /dev/null +++ b/packages/text-editor/src/components/extension/isEmptyContent.ts @@ -0,0 +1,34 @@ +import { Extension } from '@tiptap/core' + +export interface IsEmptyContentOptions { + onChange: (isEmpty: boolean) => void +} + +export interface IsEmptyContentStorage { + isEmpty: boolean +} + +export const IsEmptyContentExtension: Extension = + Extension.create({ + name: 'is-empty-content-extension', + addStorage () { + return { + isEmpty: true + } + }, + onCreate () { + this.parent?.() + + this.storage.isEmpty = this.editor.isEmpty + this.options.onChange(this.storage.isEmpty) + }, + onUpdate () { + this.parent?.() + + if (this.storage.isEmpty !== this.editor.isEmpty) { + this.storage.isEmpty = this.editor.isEmpty + + this.options.onChange(this.storage.isEmpty) + } + } + }) diff --git a/packages/text-editor/src/index.ts b/packages/text-editor/src/index.ts index 82be78b307..2487a7bac2 100644 --- a/packages/text-editor/src/index.ts +++ b/packages/text-editor/src/index.ts @@ -31,6 +31,11 @@ export * from './types' export { default as Collaboration } from './components/Collaboration.svelte' export { default as StyleButton } from './components/StyleButton.svelte' +export { + IsEmptyContentExtension, + IsEmptyContentOptions, + IsEmptyContentStorage +} from './components/extension/isEmptyContent' export { NodeHighlightExtension, NodeHighlightExtensionOptions,