From 3e2f384b802a5c2af21bf60273e4042b742d044c Mon Sep 17 00:00:00 2001 From: kudlajz Date: Tue, 17 Sep 2024 16:58:34 +0200 Subject: [PATCH 1/3] Refetch coverage when update event happens --- .../extensions/coverage/components/CoverageElement.tsx | 10 ++++++++++ packages/slate-editor/src/modules/events/types.ts | 3 +++ 2 files changed, 13 insertions(+) diff --git a/packages/slate-editor/src/extensions/coverage/components/CoverageElement.tsx b/packages/slate-editor/src/extensions/coverage/components/CoverageElement.tsx index cc8ad0720..9ba51fd07 100644 --- a/packages/slate-editor/src/extensions/coverage/components/CoverageElement.tsx +++ b/packages/slate-editor/src/extensions/coverage/components/CoverageElement.tsx @@ -60,6 +60,16 @@ export function CoverageElement({ } } + function handleCoverageUpdated({ coverage }: { coverage: Pick}) { + if (element.coverage.id === coverage.id) { + loadCoverage(); + } + } + + useEffect(() => { + EventsEditor.addEventListener(editor, 'coverage-updated', handleCoverageUpdated); + }, []); + return ( ; }; + 'coverage-updated': { + coverage: Pick; + }, 'coverage-removed': { uuid: string; }; From f15d26855d902e99fee1f2fd1d4aeeaa2d77342a Mon Sep 17 00:00:00 2001 From: kudlajz Date: Tue, 17 Sep 2024 17:02:43 +0200 Subject: [PATCH 2/3] Fixed prettier issue --- packages/slate-editor/src/modules/events/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/slate-editor/src/modules/events/types.ts b/packages/slate-editor/src/modules/events/types.ts index c7bada18e..08df06555 100644 --- a/packages/slate-editor/src/modules/events/types.ts +++ b/packages/slate-editor/src/modules/events/types.ts @@ -76,7 +76,7 @@ export type EditorEventMap = { }; 'coverage-updated': { coverage: Pick; - }, + }; 'coverage-removed': { uuid: string; }; From 8d6df46bafe9042e0552dac0e210b90d9cf6e124 Mon Sep 17 00:00:00 2001 From: kudlajz Date: Tue, 17 Sep 2024 17:07:44 +0200 Subject: [PATCH 3/3] Remove the listener when component is unmounted --- .../coverage/components/CoverageElement.tsx | 12 ++++++------ .../slate-editor/src/modules/events/EventsEditor.ts | 8 ++++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/slate-editor/src/extensions/coverage/components/CoverageElement.tsx b/packages/slate-editor/src/extensions/coverage/components/CoverageElement.tsx index 9ba51fd07..79afea370 100644 --- a/packages/slate-editor/src/extensions/coverage/components/CoverageElement.tsx +++ b/packages/slate-editor/src/extensions/coverage/components/CoverageElement.tsx @@ -60,14 +60,14 @@ export function CoverageElement({ } } - function handleCoverageUpdated({ coverage }: { coverage: Pick}) { - if (element.coverage.id === coverage.id) { - loadCoverage(); + useEffect(() => { + function handleCoverageUpdated({ coverage }: { coverage: Pick}) { + if (element.coverage.id === coverage.id) { + loadCoverage(); + } } - } - useEffect(() => { - EventsEditor.addEventListener(editor, 'coverage-updated', handleCoverageUpdated); + return EventsEditor.addEventListener(editor, 'coverage-updated', handleCoverageUpdated); }, []); return ( diff --git a/packages/slate-editor/src/modules/events/EventsEditor.ts b/packages/slate-editor/src/modules/events/EventsEditor.ts index 01096c87d..4045996c6 100644 --- a/packages/slate-editor/src/modules/events/EventsEditor.ts +++ b/packages/slate-editor/src/modules/events/EventsEditor.ts @@ -1,4 +1,6 @@ +import type { RemoveListener } from '@prezly/events'; import { Events } from '@prezly/events'; +import { noop } from '@technically/lodash'; import { Editor } from 'slate'; import { EVENTS_PROPERTY } from './constants'; @@ -10,10 +12,12 @@ export abstract class EventsEditor { editor: Editor, event: Event, listener: EditorEventHandlers[Event], - ): void { + ): RemoveListener { if (EventsEditor.isEventsEditor(editor)) { - editor[EVENTS_PROPERTY].addEventListener(event, listener); + return editor[EVENTS_PROPERTY].addEventListener(event, listener); } + + return noop; } static dispatchEvent(