Skip to content

Commit

Permalink
chore(core): cancel block not found alert
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Sep 19, 2024
1 parent 0450fce commit c65668e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
27 changes: 18 additions & 9 deletions packages/frontend/core/src/modules/editor/entities/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,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<
Expand All @@ -177,15 +183,18 @@ 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 std = editorContainer.host?.std;
if (!std) return;

scrollAnchoring(std, this.mode$.value, params.id);
scrollAnchoring(std, this.mode$.value, anchor);
});
unsubs.push(subscription.unsubscribe.bind(subscription));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
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<GfxModel>(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;
}

export function scrollAnchoring(
std: BlockStdScope,
mode: DocMode,
{ id, key }: Partial<{ id: string; key: string }>
) {
const selection = std.selection;

selection.setGroup('scene', [
Expand Down

0 comments on commit c65668e

Please sign in to comment.