Skip to content

Commit 879cfb7

Browse files
committed
fix: refactor pc editor events
1 parent fd6b4df commit 879cfb7

File tree

6 files changed

+63
-95
lines changed

6 files changed

+63
-95
lines changed

common/types/actions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ export type EventMessageTypes = {
1010
ON_MOUSE_MOVE: {x: number; y: number};
1111
ON_HOVER_BLOCK: {rect?: DOMRect; position?: string};
1212
ON_CLICK_BLOCK: {path: number[]};
13-
ON_RESIZE_BLOCK: {rect: DOMRect};
14-
ON_UPDATE_SELECTED_BLOCK: {path: number[]; rect: DOMRect};
13+
ON_UPDATE_BLOCK_SELECTION: {rect: DOMRect};
1514
ON_SUPPORTED_BLOCKS: Pick<EditorState, 'blocks' | 'subBlocks' | 'global'>;
1615
ON_INITIAL_CONTENT: PageContentWithNavigation;
1716
};
1817

1918
export type ActionMessageTypes = {
2019
GET_SUPPORTED_BLOCKS: {};
2120
GET_INITIAL_CONTENT: {};
22-
UPDATE_SELECTED_BLOCK: {path: number[]};
2321
};

editor-v2/containers/Overlay/Overlay.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as React from 'react';
44

55
import {usePostMessageAPIListener} from '../../../common/postMessage';
66
import {useMainEditorStore} from '../../hooks/useMainEditorStore';
7-
import {useSelectedBlockBorders} from '../../hooks/useSelectedBlockBorders';
87
import {editorCn} from '../../utils/cn';
98

109
import './Overlay.scss';
@@ -37,9 +36,34 @@ const Overlay = ({className, canvasElement}: OverlayProps) => {
3736
undefined,
3837
);
3938
const [hoverBorders, setHoverBorders] = React.useState<DOMRect | null>(null);
39+
const [blockBorders, setBlockBorders] = React.useState<DOMRect | null>(null);
4040

41-
// Use the hook to get blockBorders and handle auto-scrolling
42-
const blockBorders = useSelectedBlockBorders(selectedBlock, canvasElement);
41+
// Listen for updates to the selected block's position
42+
usePostMessageAPIListener('ON_UPDATE_BLOCK_SELECTION', ({rect}) => {
43+
setBlockBorders(rect || null);
44+
});
45+
46+
// Update blockBorders when selectedBlock changes
47+
React.useEffect(() => {
48+
if (!selectedBlock) {
49+
setBlockBorders(null);
50+
}
51+
}, [selectedBlock]);
52+
53+
// Auto scroll to the selected block when blockBorders changes
54+
React.useEffect(() => {
55+
if (blockBorders && canvasElement) {
56+
// Calculate the scroll position to center the block in the viewport
57+
const canvasHeight = canvasElement.clientHeight;
58+
const scrollPosition = blockBorders.top - canvasHeight / 2 + blockBorders.height / 2;
59+
60+
// Scroll the canvas element to the calculated position with smooth behavior
61+
canvasElement.scrollTo({
62+
top: Math.max(0, scrollPosition),
63+
behavior: 'smooth',
64+
});
65+
}
66+
}, [blockBorders, canvasElement]);
4367

4468
const margin = 0;
4569

editor-v2/hooks/useSelectedBlockBorders.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/hooks/usePCEditorBlockMouseEvents.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import _ from 'lodash';
44

55
import {getCursorPositionOverElement} from '../utils/editor';
66

7-
import {usePCEditorStore} from './usePCEditorStore';
8-
import {sendEventPostMessage, useInternalPostMessageAPIListener} from './usePostMessageAPI';
7+
import {sendEventPostMessage} from './usePostMessageAPI';
98

109
const usePCEditorBlockMouseEvents = (arrayIndex: number[], element?: HTMLElement) => {
11-
const {selectedBlock} = usePCEditorStore();
12-
1310
const onMouseUp = React.useCallback(
1411
(e: React.MouseEvent) => {
1512
e.stopPropagation();
@@ -57,41 +54,6 @@ const usePCEditorBlockMouseEvents = (arrayIndex: number[], element?: HTMLElement
5754
[arrayIndex, element],
5855
);
5956

60-
const onResize = React.useCallback(() => {
61-
if (element && _.isEqual(selectedBlock, arrayIndex)) {
62-
const rect = element.getClientRects().item(0);
63-
if (rect) {
64-
sendEventPostMessage('ON_RESIZE_BLOCK', {rect});
65-
}
66-
}
67-
}, [arrayIndex, element, selectedBlock]);
68-
69-
React.useEffect(() => {
70-
window.addEventListener('resize', onResize);
71-
72-
return () => {
73-
window.removeEventListener('resize', onResize);
74-
};
75-
}, [element, onResize]);
76-
77-
// Listen for the UPDATE_SELECTED_BLOCK action and handle it directly
78-
const handleUpdateSelectedBlock = React.useCallback(
79-
(data: {path?: number[]}) => {
80-
if (data && data.path && Array.isArray(data.path) && _.isEqual(data.path, arrayIndex)) {
81-
// Only proceed if the path matches arrayIndex
82-
if (element) {
83-
const rect = element.getClientRects().item(0);
84-
if (rect) {
85-
sendEventPostMessage('ON_UPDATE_SELECTED_BLOCK', {rect, path: arrayIndex});
86-
}
87-
}
88-
}
89-
},
90-
[arrayIndex, element],
91-
);
92-
93-
useInternalPostMessageAPIListener('UPDATE_SELECTED_BLOCK', handleUpdateSelectedBlock);
94-
9557
return {
9658
onClick,
9759
onMouseMove,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as React from 'react';
2+
import _ from 'lodash';
3+
4+
import {usePCEditorStore} from './usePCEditorStore';
5+
import {sendEventPostMessage} from './usePostMessageAPI';
6+
7+
const usePCEditorBlockSelection = (arrayIndex: number[], element?: HTMLElement) => {
8+
const {selectedBlock} = usePCEditorStore();
9+
10+
const onResize = React.useCallback(() => {
11+
if (element && _.isEqual(selectedBlock, arrayIndex)) {
12+
const rect = element.getClientRects().item(0);
13+
if (rect) {
14+
sendEventPostMessage('ON_UPDATE_BLOCK_SELECTION', {rect});
15+
}
16+
}
17+
}, [JSON.stringify(arrayIndex), element, JSON.stringify(selectedBlock)]);
18+
19+
React.useEffect(() => {
20+
window.addEventListener('resize', onResize);
21+
22+
return () => {
23+
window.removeEventListener('resize', onResize);
24+
};
25+
}, [element, onResize]);
26+
27+
React.useEffect(() => {
28+
onResize();
29+
}, [onResize]);
30+
};
31+
32+
export default usePCEditorBlockSelection;

src/hooks/usePCEditorItemWrap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as React from 'react';
33
import {BlockIdContext} from '../context/blockIdContext';
44

55
import usePCEditorBlockMouseEvents from './usePCEditorBlockMouseEvents';
6+
import usePCEditorBlockSelection from './usePCEditorBlockSelection';
67

78
export function usePCEditorItemWrap(index = 0) {
89
const [element, setElement] = React.useState<HTMLElement | undefined>();
@@ -15,6 +16,7 @@ export function usePCEditorItemWrap(index = 0) {
1516

1617
const parentBlockId = React.useContext(BlockIdContext);
1718
const adminBlockMouseEvents = usePCEditorBlockMouseEvents([...parentBlockId, index], element);
19+
usePCEditorBlockSelection([...parentBlockId, index], element);
1820

1921
return {adminBlockMouseEvents, blockRef};
2022
}

0 commit comments

Comments
 (0)