Skip to content

Commit 42e237c

Browse files
committed
fix: pc item path id array for editor
1 parent 7b9e3a0 commit 42e237c

File tree

7 files changed

+40
-40
lines changed

7 files changed

+40
-40
lines changed

src/components/editor/ChildrenWrap/ChildrenWrap.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import React, {PropsWithChildren, ReactNode, useCallback, useContext, useState} from 'react';
1+
import React, {PropsWithChildren, ReactNode} from 'react';
22

3-
import {BlockIdContext} from '../../../context/blockIdContext';
4-
import usePCEditorBlockMouseEvents from '../../../hooks/usePCEditorBlockMouseEvents';
3+
import {usePCEditorItemWrap} from '../../../hooks/usePCEditorItemWrap';
54
import {block} from '../../../utils';
65

76
import './ChildrenWrap.scss';
@@ -12,16 +11,11 @@ export interface ChildrenWrapProps extends PropsWithChildren {
1211
checkChildren?: ReactNode;
1312
}
1413

15-
const ChildrenWrap = (props: ChildrenWrapProps) => {
16-
const {children} = props;
17-
const [element, setElement] = useState<HTMLElement | undefined>();
18-
const blockRef = useCallback((node: HTMLElement | null) => {
19-
if (node !== null) {
20-
setElement(node);
21-
}
22-
}, []);
23-
const parentBlockId = useContext(BlockIdContext);
24-
const {onMouseUp, onMouseMove} = usePCEditorBlockMouseEvents([parentBlockId, 0], element);
14+
const ChildrenWrap = ({children}: ChildrenWrapProps) => {
15+
const {
16+
blockRef,
17+
adminBlockMouseEvents: {onMouseMove, onMouseUp},
18+
} = usePCEditorItemWrap();
2519

2620
return (
2721
// eslint-disable-next-line jsx-a11y/no-static-element-interactions

src/components/editor/ItemWrap/ItemWrap.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import React, {PropsWithChildren, useCallback, useContext, useState} from 'react';
1+
import React, {PropsWithChildren} from 'react';
22

3-
import {BlockIdContext} from '../../../context/blockIdContext';
4-
import usePCEditorBlockMouseEvents from '../../../hooks/usePCEditorBlockMouseEvents';
3+
import {usePCEditorItemWrap} from '../../../hooks/usePCEditorItemWrap';
54
import {block} from '../../../utils';
65

76
import './ItemWrap.scss';
@@ -12,17 +11,8 @@ export interface ItemWrapProps extends PropsWithChildren {
1211
index: number;
1312
}
1413

15-
const ItemWrap = (props: ItemWrapProps) => {
16-
const [element, setElement] = useState<HTMLElement | undefined>();
17-
const blockRef = useCallback((node: HTMLElement | null) => {
18-
if (node !== null) {
19-
setElement(node);
20-
}
21-
}, []);
22-
const {children, index} = props;
23-
const parentBlockId = useContext(BlockIdContext);
24-
const adminBlockMouseEvents = usePCEditorBlockMouseEvents([parentBlockId, index], element);
25-
14+
const ItemWrap = ({index, children}: ItemWrapProps) => {
15+
const {blockRef, adminBlockMouseEvents} = usePCEditorItemWrap(index);
2616
return (
2717
<div ref={blockRef} className={b()} {...adminBlockMouseEvents}>
2818
{children}

src/containers/PageConstructor/components/ConstructorBlock/ConstructorBlock.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React, {useCallback, useMemo, useState} from 'react';
1+
import React, {useMemo} from 'react';
22

33
import pick from 'lodash/pick';
44

55
import BlockBase from '../../../../components/BlockBase/BlockBase';
66
import {BlockDecoration} from '../../../../customization/BlockDecoration';
7-
import usePCEditorBlockMouseEvents from '../../../../hooks/usePCEditorBlockMouseEvents';
7+
import {usePCEditorItemWrap} from '../../../../hooks/usePCEditorItemWrap';
88
import {BlockDecorationProps, ConstructorBlock as ConstructorBlockType} from '../../../../models';
99
import {block} from '../../../../utils';
1010

@@ -21,13 +21,7 @@ export const ConstructorBlock = ({
2121
data,
2222
children,
2323
}: React.PropsWithChildren<ConstructorBlockProps>) => {
24-
const [element, setElement] = useState<HTMLElement | undefined>();
25-
const blockRef = useCallback((node: HTMLElement | null) => {
26-
if (node !== null) {
27-
setElement(node);
28-
}
29-
}, []);
30-
const adminBlockMouseEvents = usePCEditorBlockMouseEvents([index], element);
24+
const {blockRef, adminBlockMouseEvents} = usePCEditorItemWrap(index);
3125

3226
const {type} = data;
3327
const blockBaseProps = useMemo(

src/containers/PageConstructor/components/ConstructorItem/ConstructorItem.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ export const ConstructorItem = ({
1616
children,
1717
}: React.PropsWithChildren<ConstructorItemProps>) => {
1818
const {itemMap} = useContext(InnerContext);
19+
const parentId = useContext(BlockIdContext);
1920
const {type, ...rest} = data;
2021

2122
const Component = itemMap[type] as React.ComponentType<
2223
React.ComponentProps<typeof itemMap[typeof type]>
2324
>;
2425

2526
return (
26-
<BlockIdContext.Provider value={blockKey} key={blockKey}>
27+
<BlockIdContext.Provider value={[...parentId, blockKey]} key={blockKey}>
2728
<Component {...rest}>{children}</Component>
2829
</BlockIdContext.Provider>
2930
);

src/containers/PageConstructor/components/ConstructorLoadable/ConstructorLoadable.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export const ConstructorLoadable = (props: ConstructorLoadableProps) => {
1818
const Component = itemMap[type] as React.Component<
1919
React.ComponentProps<typeof itemMap[typeof type]>
2020
>;
21+
const parentId = useContext(BlockIdContext);
2122

2223
return (
23-
<BlockIdContext.Provider value={Number(blockKey)} key={blockKey}>
24+
<BlockIdContext.Provider value={[...parentId, Number(blockKey)]} key={blockKey}>
2425
<Loadable
2526
key={blockKey}
2627
block={block}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
22

3-
export type BlockIdContextProp = number;
3+
export type BlockIdContextProp = number[];
44

5-
export const BlockIdContext = React.createContext<BlockIdContextProp>(NaN);
5+
export const BlockIdContext = React.createContext<BlockIdContextProp>([]);

src/hooks/usePCEditorItemWrap.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {useCallback, useContext, useState} from 'react';
2+
3+
import {BlockIdContext} from '../context/blockIdContext';
4+
5+
import usePCEditorBlockMouseEvents from './usePCEditorBlockMouseEvents';
6+
7+
export function usePCEditorItemWrap(index = 0) {
8+
const [element, setElement] = useState<HTMLElement | undefined>();
9+
10+
const blockRef = useCallback((node: HTMLElement | null) => {
11+
if (node !== null) {
12+
setElement(node);
13+
}
14+
}, []);
15+
16+
const parentBlockId = useContext(BlockIdContext);
17+
const adminBlockMouseEvents = usePCEditorBlockMouseEvents([...parentBlockId, index], element);
18+
19+
return {adminBlockMouseEvents, blockRef};
20+
}

0 commit comments

Comments
 (0)