Skip to content

Commit 35d9714

Browse files
St1ggymakhnatkin
authored andcommitted
feat(bundle): add submit in preview mode (#411)
1 parent 40351db commit 35d9714

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/bundle/MarkdownEditorView.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import {useEnsuredForwardedRef, useKey, useUpdate} from 'react-use';
77
import {ClassNameProps, cn} from '../classname';
88
import {i18n} from '../i18n/bundle';
99
import {logger} from '../logger';
10-
import {useBooleanState} from '../react-utils/hooks';
11-
import {ToasterContext} from '../react-utils/toaster';
12-
import {useSticky} from '../react-utils/useSticky';
13-
import {isMac} from '../utils/platform';
10+
import {ToasterContext, useBooleanState, useSticky} from '../react-utils';
11+
import {isMac} from '../utils';
1412

1513
import type {Editor, EditorInt, EditorMode} from './Editor';
1614
import {HorizontalDrag} from './HorizontalDrag';
@@ -20,15 +18,13 @@ import {WysiwygEditorView} from './WysiwygEditorView';
2018
import {
2119
MToolbarData,
2220
MToolbarItemData,
23-
mHiddenDataByPreset,
24-
mToolbarConfigByPreset,
25-
} from './config/markup';
26-
import {
2721
WToolbarData,
2822
WToolbarItemData,
23+
mHiddenDataByPreset,
24+
mToolbarConfigByPreset,
2925
wHiddenDataByPreset,
3026
wToolbarConfigByPreset,
31-
} from './config/wysiwyg';
27+
} from './config';
3228
import {useMarkdownEditorContext} from './context';
3329
import {EditorSettings, EditorSettingsProps} from './settings';
3430
import {stickyCn} from './sticky';
@@ -136,6 +132,19 @@ export const MarkdownEditorView = React.forwardRef<HTMLDivElement, MarkdownEdito
136132
const editorWrapperRef = useRef(null);
137133
const splitModeViewWrapperRef = useRef(null);
138134

135+
useEffect(() => {
136+
if (showPreview) {
137+
divRef.current.focus();
138+
}
139+
}, [divRef, showPreview]);
140+
141+
useKey(
142+
(e) => showPreview && isWrapperFocused(divRef) && isSubmitKeyDown(e),
143+
() => editor.emit('submit', null),
144+
{event: 'keydown'},
145+
[showPreview],
146+
);
147+
139148
const settings = useMemo(
140149
() => (
141150
<Settings
@@ -200,6 +209,8 @@ export const MarkdownEditorView = React.forwardRef<HTMLDivElement, MarkdownEdito
200209
},
201210
[className],
202211
)}
212+
role="button"
213+
tabIndex={0}
203214
>
204215
<div className={b('editor-wrapper')} ref={editorWrapperRef}>
205216
{showPreview ? (
@@ -304,3 +315,12 @@ function isPreviewKeyDown(e: KeyboardEvent) {
304315
const modKey = isMac() ? e.metaKey : e.ctrlKey;
305316
return modKey && e.shiftKey && e.code === 'KeyP';
306317
}
318+
319+
function isWrapperFocused(divRef: React.RefObject<HTMLDivElement>) {
320+
return document.activeElement === divRef.current;
321+
}
322+
323+
function isSubmitKeyDown(e: KeyboardEvent) {
324+
const modKey = isMac() ? e.metaKey : e.ctrlKey;
325+
return modKey && e.code === 'Enter';
326+
}

src/bundle/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './wysiwyg';
2+
export * from './markup';

0 commit comments

Comments
 (0)