Skip to content

Commit

Permalink
fix: assert exists error (#7699)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone authored Jul 20, 2024
1 parent 42035fe commit 422c1e3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PointerEventState } from '@blocksuite/block-std';

import { assertExists, noop } from '@blocksuite/global/utils';
import { noop } from '@blocksuite/global/utils';

import type { SelectionArea } from '../../services/tools-manager.js';
import type { EdgelessTool } from '../../types.js';
Expand Down Expand Up @@ -65,8 +65,7 @@ export class NoteToolController extends EdgelessToolController<NoteTool> {

private _resize(shift = false) {
const { _draggingArea, _draggingNoteOverlay, _edgeless } = this;
assertExists(_draggingArea);
assertExists(_draggingNoteOverlay);
if (!_draggingArea || !_draggingNoteOverlay) return;

const { viewport } = _edgeless.service;
const { zoom } = viewport;
Expand Down Expand Up @@ -171,8 +170,7 @@ export class NoteToolController extends EdgelessToolController<NoteTool> {
}

onContainerDragMove(e: PointerEventState) {
assertExists(this._draggingNoteOverlay);
assertExists(this._draggingArea);
if (!this._draggingNoteOverlay || !this._draggingArea) return;

this._draggingArea.end = new DOMPoint(e.x, e.y);
this._resize(e.keys.shift || this._edgeless.tools.shiftKey);
Expand Down
45 changes: 22 additions & 23 deletions packages/blocks/src/root-block/widgets/drag-handle/drag-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import {
type UIEventHandler,
WidgetElement,
} from '@blocksuite/block-std';
import {
DisposableGroup,
assertExists,
throttle,
} from '@blocksuite/global/utils';
import { DisposableGroup, throttle } from '@blocksuite/global/utils';
import {
type BlockModel,
type BlockSelector,
Expand Down Expand Up @@ -234,7 +230,7 @@ export class AffineDragHandleWidget extends WidgetElement<

// Should select the block if current block is not selected
const blockElement = this.anchorBlockElement;
assertExists(blockElement);
if (!blockElement) return;

if (selectedBlocks.length > 1) {
this._showDragHandleOnHoverBlock(this._anchorBlockPath);
Expand Down Expand Up @@ -433,7 +429,7 @@ export class AffineDragHandleWidget extends WidgetElement<

const offsetParentRect =
this.dragHandleContainerOffsetParent.getBoundingClientRect();
assertExists(offsetParentRect);
if (!offsetParentRect) return new Rect(0, 0, 0, 0);

left -= offsetParentRect.left;
right -= offsetParentRect.left;
Expand Down Expand Up @@ -537,7 +533,7 @@ export class AffineDragHandleWidget extends WidgetElement<
if (!this._isHoverDragHandleVisible || !this._anchorBlockPath) return [];

const hoverBlock = this.anchorBlockElement;
assertExists(hoverBlock);
if (!hoverBlock) return [];

const selections = this.selectedBlocks;
let blockElements: BlockElement[] = [];
Expand Down Expand Up @@ -720,7 +716,7 @@ export class AffineDragHandleWidget extends WidgetElement<
}
);
const newNoteBlock = this.doc.getBlockById(newNoteId) as NoteBlockModel;
assertExists(newNoteBlock);
if (!newNoteBlock) return;

const bound = Bound.deserialize(newNoteBlock.xywh);
bound.h *= this.noteScale;
Expand Down Expand Up @@ -768,14 +764,14 @@ export class AffineDragHandleWidget extends WidgetElement<
}

const targetBlock = this.doc.getBlockById(targetBlockId);
assertExists(targetBlock);
if (!targetBlock) return;

const shouldInsertIn = dropType === 'in';

const parent = shouldInsertIn
? targetBlock
: this.doc.getParent(targetBlockId);
assertExists(parent);
if (!parent) return;

const altKey = state.raw.altKey;

Expand Down Expand Up @@ -810,7 +806,7 @@ export class AffineDragHandleWidget extends WidgetElement<
// In doc page mode, update selected blocks
// In edgeless mode, focus on the first block
setTimeout(() => {
assertExists(parent);
if (!parent) return;
// Need to update selection when moving blocks successfully
// Because the block path may be changed after moving
const parentElement = this._getBlockElementFromViewStore(parent.id);
Expand Down Expand Up @@ -842,8 +838,7 @@ export class AffineDragHandleWidget extends WidgetElement<
const grabber = this._dragHandleGrabber;
if (!container || !grabber) return;

if (this._isHoverDragHandleVisible) {
assertExists(this._anchorBlockPath);
if (this._isHoverDragHandleVisible && this._anchorBlockPath) {
const blockElement = this.anchorBlockElement;
if (!blockElement) return;

Expand Down Expand Up @@ -909,10 +904,13 @@ export class AffineDragHandleWidget extends WidgetElement<
return false;
}

if (!this._isHoverDragHandleVisible) return;
if (
!this._isHoverDragHandleVisible ||
!this._anchorBlockId ||
!this._anchorBlockPath
)
return;
// Get current hover block element by path
assertExists(this._anchorBlockId);
assertExists(this._anchorBlockPath);
const hoverBlockElement = this.anchorBlockElement;
if (!hoverBlockElement) return false;

Expand Down Expand Up @@ -949,8 +947,9 @@ export class AffineDragHandleWidget extends WidgetElement<
)
) {
const blockElement = this.anchorBlockElement;
assertExists(blockElement);
this._setSelectedBlocks([blockElement]);
if (blockElement) {
this._setSelectedBlocks([blockElement]);
}
}

const blockElements = this.selectedBlocks
Expand Down Expand Up @@ -992,7 +991,7 @@ export class AffineDragHandleWidget extends WidgetElement<
}

const blockId = closestBlockElement.getAttribute(this.host.blockIdAttr);
assertExists(blockId);
if (!blockId) return;

this._anchorBlockId = blockId;
this._anchorBlockPath = closestBlockElement.blockId;
Expand Down Expand Up @@ -1196,12 +1195,12 @@ export class AffineDragHandleWidget extends WidgetElement<

if (!this._anchorBlockPath) return;
const blockElement = this.anchorBlockElement;
assertExists(blockElement);
if (!blockElement) return;

const edgelessElement = edgelessRoot.service.getElementById(
blockElement.model.id
);
assertExists(edgelessElement);
if (!edgelessElement) return;

const container = this._dragHandleContainer;
const grabber = this._dragHandleGrabber;
Expand Down Expand Up @@ -1411,7 +1410,7 @@ export class AffineDragHandleWidget extends WidgetElement<
};

private _updateDropResult = (dropResult: DropResult | null) => {
assertExists(this.dropIndicator);
if (!this.dropIndicator) return;
this.dropBlockId = dropResult?.dropBlockId ?? '';
this.dropType = dropResult?.dropType ?? null;
if (dropResult?.rect) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { WithDisposable } from '@blocksuite/block-std';
import { assertExists } from '@blocksuite/global/utils';
import { LitElement, html, nothing } from 'lit';
import { customElement, property } from 'lit/decorators.js';

Expand Down Expand Up @@ -35,7 +34,6 @@ export class EdgelessChangeImageButton extends WithDisposable(LitElement) {
const blockElement = this.edgeless.std.view.getBlock(
blockSelection[0].blockId
) as ImageBlockComponent | null;
assertExists(blockElement);

return blockElement;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { EditorHost } from '@blocksuite/block-std';

import { WithDisposable } from '@blocksuite/block-std';
import { assertExists } from '@blocksuite/global/utils';
import { LitElement, html } from 'lit';
import { customElement, property, query, state } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
Expand Down Expand Up @@ -105,7 +104,11 @@ export class LinkedDocPopover extends WithDisposable(LitElement) {
override connectedCallback() {
super.connectedCallback();
const inlineEditor = this.inlineEditor;
assertExists(inlineEditor, 'RichText InlineEditor not found');

if (!inlineEditor || !inlineEditor.eventSource) {
console.error('inlineEditor or eventSource is not found');
return;
}

// init
this._linkedDocGroup = this._getLinkedDocGroup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ export class SlashMenu extends WithDisposable(LitElement) {
});

const inlineEditor = this.inlineEditor;
assertExists(inlineEditor, 'RichText InlineEditor not found');
if (!inlineEditor || !inlineEditor.eventSource) {
console.error('inlineEditor or eventSource is not found');
return;
}

/**
* Handle arrow key
Expand Down Expand Up @@ -443,7 +446,11 @@ export class InnerSlashMenu extends WithDisposable(LitElement) {
this.context.rootElement.host,
this.context.model
);
assertExists(inlineEditor, 'RichText InlineEditor not found');

if (!inlineEditor || !inlineEditor.eventSource) {
console.error('inlineEditor or eventSource is not found');
return;
}

inlineEditor.eventSource.addEventListener(
'keydown',
Expand Down
3 changes: 1 addition & 2 deletions packages/framework/inline/src/inline-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class InlineEditor<
this.rootElement.contentEditable = value;
}

if (this.eventSource.contentEditable !== value) {
if (this.eventSource && this.eventSource.contentEditable !== value) {
this.eventSource.contentEditable = value;
}

Expand Down Expand Up @@ -343,7 +343,6 @@ export class InlineEditor<
}

get eventSource() {
assertExists(this._eventSource);
return this._eventSource;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/framework/inline/src/services/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ export class EventService<TextAttributes extends BaseTextAttributes> {
);
}

if (!eventSource) {
console.error('Mount inline editor without event source ready');
return;
}

this.editor.disposables.addFromEvent(
eventSource,
'beforeinput',
Expand Down

2 comments on commit 422c1e3

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Size Report

Bundles

Entry Size Gzip Brotli
examples/basic 1.31 kB 747 B (+3 B) 636 B (-1 B)

Packages

Name Size Gzip Brotli
blocks 3.69 MB (+772 B) 1.04 MB (+49 B) 811 kB (+93 B)
editor 84 B 89 B 63 B
store 83 B 88 B 63 B
inline 84 B 88 B 63 B

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Size Report

Bundles

Entry Size Gzip Brotli
examples/basic 1.31 kB 747 B 636 B

Packages

Name Size Gzip Brotli
blocks 3.69 MB 1.04 MB 811 kB
editor 84 B 89 B 63 B
store 83 B 88 B 63 B
inline 84 B 88 B 63 B

Please sign in to comment.