From 152c858c1d31671f5917968753a2164459cfa56a Mon Sep 17 00:00:00 2001 From: Anna No Date: Mon, 2 Oct 2023 22:18:11 +0700 Subject: [PATCH] Add isEmpty extension to remove on:content event from collaborator editor (#3774) * Add validation extension to remove on content event from collaborator editor Signed-off-by: Anna No * Add validation extension to remove on content event from collaborator editor Signed-off-by: Anna No * Add validation extension to remove on content event from collaborator editor Signed-off-by: Anna No * Add isEmpty extension to remove on:content event from collaborator editor Signed-off-by: Anna No * Add isEmpty extension to remove on:content event from collaborator editor Signed-off-by: Anna No * Add isEmpty extension to remove on:content event from collaborator editor Signed-off-by: Anna No * Add isEmpty extension to remove on:content event from collaborator editor Signed-off-by: Anna No --------- Signed-off-by: Anna No --- .../components/extension/isEmptyContent.ts | 34 +++++++++++++++++++ packages/text-editor/src/index.ts | 5 +++ 2 files changed, 39 insertions(+) create mode 100644 packages/text-editor/src/components/extension/isEmptyContent.ts 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,