Skip to content

Commit 96a1e2e

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 close config popup on cursor moves
1 parent c2fd415 commit 96a1e2e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {compare, type ITimestampStruct} from '../../../../json-crdt-patch';
1111
import {SliceTypeCon} from '../../../../json-crdt-extensions/peritext/slice/constants';
1212
import {NewSliceConfig} from './NewSliceConfig';
1313
import type {UiLifeCycles} from '../../../web/types';
14-
import type {BufferDetail, PeritextEventDetailMap} from '../../../events/types';
14+
import type {BufferDetail, PeritextCursorEvent, PeritextEventDetailMap} from '../../../events/types';
1515
import type {PeritextSurfaceState} from '../../../web';
1616
import type {MenuItem} from '../types';
1717
import type {ToolbarPluginOpts} from '../ToolbarPlugin';
@@ -43,7 +43,7 @@ export class ToolbarState implements UiLifeCycles {
4343
/** ------------------------------------------- {@link UiLifeCycles} */
4444

4545
public start() {
46-
const {surface, showInlineToolbar} = this;
46+
const {surface, showInlineToolbar, newSliceConfig} = this;
4747
const {dom, events} = surface;
4848
const {et} = events;
4949
const mouseDown = dom!.cursor.mouseDown;
@@ -78,16 +78,27 @@ export class ToolbarState implements UiLifeCycles {
7878
}
7979
}
8080
};
81-
81+
const onCursor = ({detail}: PeritextCursorEvent) => {
82+
// Close config popup on non-focus cursor moves.
83+
if (newSliceConfig.value) {
84+
const isFocusMove = detail.move && detail.move.length === 1 && detail.move[0][0] === 'focus';
85+
if (!isFocusMove) {
86+
this.newSliceConfig.next(void 0);
87+
}
88+
}
89+
};
90+
8291
el.addEventListener('mousedown', mouseDownListener);
8392
el.addEventListener('mouseup', mouseUpListener);
8493
el.addEventListener('keydown', onKeyDown);
94+
et.addEventListener('cursor', onCursor);
8595
return () => {
8696
changeUnsubscribe();
8797
unsubscribeMouseDown?.();
8898
el.removeEventListener('mousedown', mouseDownListener);
8999
el.removeEventListener('mouseup', mouseUpListener);
90100
el.removeEventListener('keydown', onKeyDown);
101+
et.removeEventListener('cursor', onCursor);
91102
};
92103
}
93104

0 commit comments

Comments
 (0)