Skip to content

Commit c2fd415

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 close config popup on Esc press
1 parent ef5cffc commit c2fd415

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import {ContextPane} from 'nice-ui/lib/4-card/ContextMenu/ContextPane';
33
import {NewLinkConfig} from '../config/NewLinkConfig';
4+
import {useToolbarPlugin} from '../context';
45
import type {SliceConfigState} from '../state/types';
56

67
export interface InlineConfigCardProps {
@@ -9,9 +10,19 @@ export interface InlineConfigCardProps {
910
}
1011

1112
export const InlineConfigCard: React.FC<InlineConfigCardProps> = ({config, onSave}) => {
13+
const {toolbar} = useToolbarPlugin();
14+
1215
return (
13-
<ContextPane style={{display: 'block', minWidth: 'calc(min(600px, max(50vw, 260px)))'}}>
14-
<NewLinkConfig config={config} onSave={onSave} />
15-
</ContextPane>
16+
<div onKeyDown={(e) => {
17+
if (e.key === 'Escape') {
18+
e.preventDefault();
19+
e.stopPropagation();
20+
toolbar.newSliceConfig.next(void 0);
21+
}
22+
}}>
23+
<ContextPane style={{display: 'block', minWidth: 'calc(min(600px, max(50vw, 260px)))'}}>
24+
<NewLinkConfig config={config} onSave={onSave} />
25+
</ContextPane>
26+
</div>
1627
);
1728
};

src/json-crdt-peritext-ui/plugins/toolbar/config/NewLinkConfig.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import {parseUrl} from '../../../web/util';
1616
import {ContextPaneHeaderSep} from '../../../components/ContextPaneHeaderSep';
1717
import type {SliceConfigState} from '../state/types';
1818

19+
const blockClass = rule({
20+
maxW: '600px',
21+
});
22+
1923
const headerClass = rule({
2024
d: 'flex',
2125
ai: 'center',
@@ -62,7 +66,7 @@ export const NewLinkConfig: React.FC<NewLinkConfigProps> = ({config, onSave}) =>
6266
const name = config.menu?.name ?? config.def.name;
6367

6468
return (
65-
<form onSubmit={(e) => {
69+
<form className={blockClass} onSubmit={(e) => {
6670
e.preventDefault();
6771
onSave();
6872
}}>

src/json-crdt-peritext-ui/plugins/toolbar/state/ToolbarState.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class ToolbarState implements UiLifeCycles {
4747
const {dom, events} = surface;
4848
const {et} = events;
4949
const mouseDown = dom!.cursor.mouseDown;
50-
const source = dom.el;
50+
const el = dom.el;
5151

5252
const changeUnsubscribe = et.subscribe('change', (ev) => {
5353
const lastEvent = ev.detail.ev;
@@ -67,14 +67,27 @@ export class ToolbarState implements UiLifeCycles {
6767
const mouseUpListener = (event: MouseEvent) => {
6868
if (!showInlineToolbar.value[0]) showInlineToolbar.next([true, Date.now()]);
6969
};
70+
const onKeyDown = (event: KeyboardEvent) => {
71+
const newSliceConfig = this.newSliceConfig;
72+
if (event.key === 'Escape') {
73+
if (newSliceConfig.value) {
74+
event.stopPropagation();
75+
event.preventDefault
76+
newSliceConfig.next(void 0);
77+
return;
78+
}
79+
}
80+
};
7081

71-
source?.addEventListener('mousedown', mouseDownListener);
72-
source?.addEventListener('mouseup', mouseUpListener);
82+
el.addEventListener('mousedown', mouseDownListener);
83+
el.addEventListener('mouseup', mouseUpListener);
84+
el.addEventListener('keydown', onKeyDown);
7385
return () => {
7486
changeUnsubscribe();
7587
unsubscribeMouseDown?.();
76-
source?.removeEventListener('mousedown', mouseDownListener);
77-
source?.removeEventListener('mouseup', mouseUpListener);
88+
el.removeEventListener('mousedown', mouseDownListener);
89+
el.removeEventListener('mouseup', mouseUpListener);
90+
el.removeEventListener('keydown', onKeyDown);
7891
};
7992
}
8093

0 commit comments

Comments
 (0)