Skip to content

Commit d03684e

Browse files
committed
fix: selected block after blocks reorder
1 parent 89efc64 commit d03684e

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

editor-v2/containers/Overlay/Overlay.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,13 @@ const Overlay = ({className, canvasElement}: OverlayProps) => {
6565
const destination = [...selectedBlock];
6666
destination[destination.length - 1] = destination[destination.length - 1] - 1;
6767
reorderBlock(selectedBlock, destination, 'prepend');
68-
setSelectedBlock(undefined);
6968
};
7069

7170
const handleMoveDown = () => {
7271
if (!selectedBlock) return;
7372
const destination = [...selectedBlock];
7473
destination[destination.length - 1] = destination[destination.length - 1] + 1;
7574
reorderBlock(selectedBlock, destination, 'append');
76-
setSelectedBlock(undefined);
7775
};
7876

7977
return (

editor-v2/hooks/useMainEditorInitialize.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const useMainEditorInitialize = () => {
1313
disableMode,
1414
insertBlock,
1515
reorderBlock,
16-
setSelectedBlock,
1716
preInsertBlockType,
1817
preReorderBlockPath,
1918
} = useMainEditorStore();
@@ -52,7 +51,6 @@ const useMainEditorInitialize = () => {
5251
path,
5352
['left', 'top'].includes(position) ? 'prepend' : 'append',
5453
);
55-
setSelectedBlock(path);
5654
}
5755
disableMode();
5856
},

editor-v2/store.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export const createEditorStore = initializeStore<EditorState, EditorMethods>(
109109
...state,
110110
content: {...state.content, blocks: newBlocksConfig},
111111
}));
112+
113+
// Set the inserted block as selected
114+
get().setSelectedBlock(arrayPath);
112115
},
113116
enableInsertMode(blockType: string) {
114117
set((state) => ({
@@ -180,6 +183,9 @@ export const createEditorStore = initializeStore<EditorState, EditorMethods>(
180183
}));
181184
},
182185
reorderBlock: (arrayPath, destination, position = 'append') => {
186+
// Create a copy of the destination array before any modifications
187+
let finalDestinationPath: number[] = _.cloneDeep(destination);
188+
183189
if (position === 'append') {
184190
// TODO: fix
185191
// eslint-disable-next-line no-not-accumulator-reassign/no-not-accumulator-reassign, no-param-reassign
@@ -199,8 +205,17 @@ export const createEditorStore = initializeStore<EditorState, EditorMethods>(
199205
destination[destination.length - 1],
200206
);
201207
});
208+
209+
if (
210+
position === 'append' &&
211+
destination[destination.length - 1] < arrayPath[arrayPath.length - 1]
212+
) {
213+
finalDestinationPath[finalDestinationPath.length - 1] =
214+
finalDestinationPath[finalDestinationPath.length - 1] + 1;
215+
}
202216
} else {
203217
const arrayDest = getDestinationShiftBeforeReorder(arrayPath, destination);
218+
finalDestinationPath = _.cloneDeep(arrayDest);
204219

205220
// Delete
206221
const blocksConfigWithoutBlock = modifyObjectByPath(
@@ -220,6 +235,8 @@ export const createEditorStore = initializeStore<EditorState, EditorMethods>(
220235
...state,
221236
content: {...state.content, blocks: newBlocksConfig},
222237
}));
238+
239+
get().setSelectedBlock(finalDestinationPath);
223240
},
224241
resetInitialize: () => {
225242
set((state) => ({

0 commit comments

Comments
 (0)