From 74b36e63c210ffdf137e9198d65718ee570cdf75 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Thu, 19 Sep 2024 16:45:06 +0800 Subject: [PATCH] chore(core): cancel block not found alert --- .../src/modules/editor/entities/editor.ts | 39 +++++++++++------ .../modules/editor/utils/scroll-anchoring.ts | 43 ------------------- packages/frontend/i18n/src/resources/en.json | 2 - 3 files changed, 27 insertions(+), 57 deletions(-) delete mode 100644 packages/frontend/core/src/modules/editor/utils/scroll-anchoring.ts diff --git a/packages/frontend/core/src/modules/editor/entities/editor.ts b/packages/frontend/core/src/modules/editor/entities/editor.ts index 5d6be817c704d..46b524130972c 100644 --- a/packages/frontend/core/src/modules/editor/entities/editor.ts +++ b/packages/frontend/core/src/modules/editor/entities/editor.ts @@ -13,7 +13,6 @@ import { paramsParseOptions, preprocessParams } from '../../navigation/utils'; import type { WorkbenchView } from '../../workbench'; import { EditorScope } from '../scopes/editor'; import type { EditorSelector } from '../types'; -import { scrollAnchoring } from '../utils/scroll-anchoring'; export class Editor extends Entity { readonly scope = this.framework.createScope(EditorScope, { @@ -159,14 +158,20 @@ export class Editor extends Entity { const focusAt$ = LiveData.computed(get => { const selector = get(this.selector$); - const id = - get(this.mode$) === 'edgeless' && selector?.elementIds?.length - ? selector?.elementIds?.[0] - : selector?.blockIds?.[0]; + let id = selector?.blockIds?.[0]; + let key = 'blockIds'; + + if (get(this.mode$) === 'edgeless') { + const elementId = selector?.elementIds?.[0]; + if (elementId) { + id = elementId; + key = 'elementIds'; + } + } if (!id) return null; - return { id, refreshKey: selector?.refreshKey }; + return { id, key, refreshKey: selector?.refreshKey }; }); if (focusAt$.value === null && docTitle) { const title = docTitle.querySelector< @@ -177,15 +182,25 @@ export class Editor extends Entity { const subscription = focusAt$ .distinctUntilChanged( - (a, b) => a?.id === b?.id && a?.refreshKey === b?.refreshKey + (a, b) => + a?.id === b?.id && + a?.key === b?.key && + a?.refreshKey === b?.refreshKey ) - .subscribe(params => { - if (!params?.id) return; + .subscribe(anchor => { + if (!anchor) return; + + const selection = editorContainer.host?.std.selection; + if (!selection) return; - const std = editorContainer.host?.std; - if (!std) return; + const { id, key } = anchor; - scrollAnchoring(std, this.mode$.value, params.id); + selection.setGroup('scene', [ + selection.create('highlight', { + mode, + [key]: [id], + }), + ]); }); unsubs.push(subscription.unsubscribe.bind(subscription)); diff --git a/packages/frontend/core/src/modules/editor/utils/scroll-anchoring.ts b/packages/frontend/core/src/modules/editor/utils/scroll-anchoring.ts deleted file mode 100644 index 32a87cb79535e..0000000000000 --- a/packages/frontend/core/src/modules/editor/utils/scroll-anchoring.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { notify } from '@affine/component'; -import { I18n } from '@affine/i18n'; -import type { BlockStdScope } from '@blocksuite/block-std'; -import { - GfxControllerIdentifier, - type GfxModel, -} from '@blocksuite/block-std/gfx'; -import type { DocMode } from '@blocksuite/blocks'; - -// TODO(@fundon): it should be a command -export function scrollAnchoring(std: BlockStdScope, mode: DocMode, id: string) { - let key = 'blockIds'; - let exists = false; - - if (mode === 'page') { - exists = std.doc.hasBlock(id); - } else { - const controller = std.getOptional(GfxControllerIdentifier); - if (!controller) return; - - exists = !!controller.getElementById(id)?.xywh; - if (controller.surface?.hasElementById(id)) { - key = 'elementIds'; - } - } - - if (!exists) { - notify.error({ - title: I18n['Block not found'](), - message: I18n['Block not found description'](), - }); - return; - } - - const selection = std.selection; - - selection.setGroup('scene', [ - selection.create('highlight', { - mode, - [key]: [id], - }), - ]); -} diff --git a/packages/frontend/i18n/src/resources/en.json b/packages/frontend/i18n/src/resources/en.json index 95a16b7ffacb0..372a39582db98 100644 --- a/packages/frontend/i18n/src/resources/en.json +++ b/packages/frontend/i18n/src/resources/en.json @@ -26,8 +26,6 @@ "Back Home": "Back home", "Back to Quick Search": "Back to quick search", "Back to all": "Back to all", - "Block not found": "Block not found", - "Block not found description": "The block you are trying to access does not exist or has been removed.", "Body text": "Body text", "Bold": "Bold", "Cancel": "Cancel",