From f38c1799180a7fb7ccb5de77aa6b8698d0332593 Mon Sep 17 00:00:00 2001 From: Petyo Ivanov Date: Thu, 7 Dec 2023 09:43:01 +0200 Subject: [PATCH] fix: source modes trigger onBlur Fixes #222 Fixes #216 --- src/plugins/diff-source/DiffViewer.tsx | 9 ++++++++- src/plugins/diff-source/SourceEditor.tsx | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/plugins/diff-source/DiffViewer.tsx b/src/plugins/diff-source/DiffViewer.tsx index ccc98e48..f1c3a7c2 100644 --- a/src/plugins/diff-source/DiffViewer.tsx +++ b/src/plugins/diff-source/DiffViewer.tsx @@ -24,6 +24,7 @@ interface CmMergeViewProps { const CmMergeView: React.FC = ({ oldMarkdown, newMarkdown, onUpdate }) => { const cmMergeViewRef = React.useRef(null) const [cmExtensions] = diffSourcePluginHooks.useEmitterValues('cmExtensions') + const triggerOnBlur = corePluginHooks.usePublisher('onBlur') const ref = React.useCallback( (el: HTMLDivElement | null) => { @@ -51,6 +52,12 @@ const CmMergeView: React.FC = ({ oldMarkdown, newMarkdown, onU EditorView.updateListener.of(({ state }) => { const md = state.doc.toString() onUpdate(md) + }), + EditorView.focusChangeEffect.of((_, focused) => { + if (!focused) { + triggerOnBlur(new FocusEvent('blur')) + } + return null }) ] } @@ -60,7 +67,7 @@ const CmMergeView: React.FC = ({ oldMarkdown, newMarkdown, onU cmMergeViewRef.current = null } }, - [newMarkdown, oldMarkdown, onUpdate, cmExtensions] + [newMarkdown, oldMarkdown, onUpdate, cmExtensions, triggerOnBlur] ) return
diff --git a/src/plugins/diff-source/SourceEditor.tsx b/src/plugins/diff-source/SourceEditor.tsx index e0186b12..8b408797 100644 --- a/src/plugins/diff-source/SourceEditor.tsx +++ b/src/plugins/diff-source/SourceEditor.tsx @@ -19,6 +19,7 @@ export const SourceEditor = () => { const [markdown, readOnly] = corePluginHooks.useEmitterValues('markdown', 'readOnly') const [cmExtensions] = diffSourcePluginHooks.useEmitterValues('cmExtensions') const updateMarkdown = diffSourcePluginHooks.usePublisher('markdownSourceEditorValue') + const triggerOnBlur = corePluginHooks.usePublisher('onBlur') const editorViewRef = React.useRef(null) const ref = React.useCallback( @@ -30,6 +31,12 @@ export const SourceEditor = () => { ...COMMON_STATE_CONFIG_EXTENSIONS, EditorView.updateListener.of(({ state }) => { updateMarkdown(state.doc.toString()) + }), + EditorView.focusChangeEffect.of((_, focused) => { + if (!focused) { + triggerOnBlur(new FocusEvent('blur')) + } + return null }) ] if (readOnly) { @@ -45,7 +52,7 @@ export const SourceEditor = () => { editorViewRef.current = null } }, - [markdown, readOnly, updateMarkdown, cmExtensions] + [markdown, readOnly, updateMarkdown, cmExtensions, triggerOnBlur] ) return