From 4625d8463ed2d6fa742ee38a30acc99d3b289448 Mon Sep 17 00:00:00 2001 From: Chi Zhang Date: Tue, 6 Sep 2022 18:12:27 +0800 Subject: [PATCH] Fix/group select (#378) * fix(select): group selecet * fix(selection): more robust * refactor(selection): better group Co-authored-by: DarkSky <25152247+darkskygit@users.noreply.github.com> --- .../src/components/text-manage/TextManage.tsx | 34 +++++++++++++++++-- .../src/editor/selection/selection.ts | 1 + .../menu/inline-menu/menu-item/IconItem.tsx | 6 +++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/libs/components/editor-blocks/src/components/text-manage/TextManage.tsx b/libs/components/editor-blocks/src/components/text-manage/TextManage.tsx index 72faaef8a7..4b32a9da43 100644 --- a/libs/components/editor-blocks/src/components/text-manage/TextManage.tsx +++ b/libs/components/editor-blocks/src/components/text-manage/TextManage.tsx @@ -6,7 +6,10 @@ import { } from '@toeverything/components/common'; import { useOnSelectActive } from '@toeverything/components/editor-core'; import { styled } from '@toeverything/components/ui'; -import { ContentColumnValue } from '@toeverything/datasource/db-service'; +import { + ContentColumnValue, + Protocol, +} from '@toeverything/datasource/db-service'; import { AsyncBlock, BlockEditor, @@ -377,9 +380,36 @@ export const TextManage = forwardRef( return false; } }; - const onSelectAll = () => { + const selectGroupBlocks = async (isSelectAll: boolean) => { + if (editor.selectionManager.currentSelectInfo.anchorNode) { + const block = await editor.getBlockById( + editor.selectionManager.currentSelectInfo.anchorNode.id + ); + if (isSelectAll) { + if ( + Protocol.Block.Type.group !== block.type && + Protocol.Block.Type.page !== block.type + ) { + const paths = await editor.getBlockPath(block.id); + paths && + paths[1] && + editor.selectionManager.setSelectedNodesIds([ + paths[1].id, + ]); + return true; + } + } + } + }; + const onSelectAll = async () => { const isSelectAll = textRef.current.isEmpty() || textRef.current.isSelectAll(); + + const ifSelectGroup = await selectGroupBlocks(isSelectAll); + if (ifSelectGroup) { + return false; + } + if (isSelectAll) { editor.selectionManager.selectAllBlocks(); return true; diff --git a/libs/components/editor-core/src/editor/selection/selection.ts b/libs/components/editor-core/src/editor/selection/selection.ts index c33eb6d64c..74005c728b 100644 --- a/libs/components/editor-core/src/editor/selection/selection.ts +++ b/libs/components/editor-core/src/editor/selection/selection.ts @@ -256,6 +256,7 @@ export class SelectionManager implements VirgoSelection { const rootBlockId = this._editor.getRootBlockId(); const rootBlock = await this._editor.getBlockById(rootBlockId); const children = await rootBlock?.children(); + if (children) { this.setSelectedNodesIds(children.map(({ id }) => id)); // blur focused element and blur it diff --git a/libs/components/editor-plugins/src/menu/inline-menu/menu-item/IconItem.tsx b/libs/components/editor-plugins/src/menu/inline-menu/menu-item/IconItem.tsx index bd3a13bf8d..981fcd7418 100644 --- a/libs/components/editor-plugins/src/menu/inline-menu/menu-item/IconItem.tsx +++ b/libs/components/editor-plugins/src/menu/inline-menu/menu-item/IconItem.tsx @@ -2,7 +2,11 @@ import { Tooltip } from '@toeverything/components/ui'; import { uaHelper } from '@toeverything/utils'; import React, { useCallback } from 'react'; import style9 from 'style9'; -import { inlineMenuNamesKeys, WinInlineMenuShortcuts } from '../config'; +import { + inlineMenuNamesKeys, + MacInlineMenuShortcuts, + WinInlineMenuShortcuts, +} from '../config'; import type { IconItemType, WithEditorSelectionType } from '../types'; type MenuIconItemProps = IconItemType & WithEditorSelectionType;