Skip to content

Commit

Permalink
fix: improve code block performance (toeverything#6236)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flrande authored Feb 20, 2024
1 parent 134cc0b commit bc2769f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/blocks/src/_common/components/rich-text/rich-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ export class RichText extends WithDisposable(ShadowlessElement) {
.catch(console.error);
}

override updated() {
if (this._inlineEditor) {
override updated(changedProperties: Map<string | number | symbol, unknown>) {
if (this._inlineEditor && changedProperties.has('readonly')) {
this._inlineEditor.setReadonly(this.readonly);
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/blocks/src/note-block/note-block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="vite/client" />
import { BlockElement, RangeManager } from '@blocksuite/lit';
import { BlockElement } from '@blocksuite/lit';
import { css, html } from 'lit';
import { customElement } from 'lit/decorators.js';

Expand All @@ -26,8 +26,6 @@ export class NoteBlockComponent extends BlockElement<
override connectedCallback() {
super.connectedCallback();

this.setAttribute(RangeManager.rangeSyncExcludeAttr, 'true');

this.keymapController.bind();
}

Expand Down
5 changes: 4 additions & 1 deletion packages/framework/inline/src/services/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export class RangeService<TextAttributes extends BaseTextAttributes> {
if (this.editor.mounted) {
// range change may happen before the editor is prepared
await this.editor.waitForUpdate();
this.editor.requestUpdate(false);
// improve performance
requestIdleCallback(() => {
this.editor.requestUpdate(false);
});
}

if (!sync) {
Expand Down
38 changes: 25 additions & 13 deletions packages/framework/lit/src/utils/range-binding.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { BaseSelection, TextSelection } from '@blocksuite/block-std';
import { PathFinder } from '@blocksuite/block-std';
import { assertExists } from '@blocksuite/global/utils';
import { assertExists, throttle } from '@blocksuite/global/utils';

import { BlockElement } from '../element/block-element.js';
import type { RangeManager } from './range-manager.js';
import { RangeManager } from './range-manager.js';

/**
* Two-way binding between native range and text selection
Expand All @@ -28,9 +28,12 @@ export class RangeBinding {
);

this.host.disposables.add(
this.host.event.add('selectionChange', () => {
this._onNativeSelectionChanged().catch(console.error);
})
this.host.event.add(
'selectionChange',
throttle(() => {
this._onNativeSelectionChanged().catch(console.error);
}, 10)
)
);

this.host.disposables.add(
Expand All @@ -52,7 +55,7 @@ export class RangeBinding {
}

isComposing = false;
private _prevSelection: BaseSelection | null = null;
private _prevTextSelection: TextSelection | null = null;
private _onStdSelectionChanged = (selections: BaseSelection[]) => {
// wait for lit updated
this.host.updateComplete
Expand All @@ -63,14 +66,14 @@ export class RangeBinding {
) ?? null;

const eq =
text && this._prevSelection
? text.equals(this._prevSelection)
: text === this._prevSelection;
text && this._prevTextSelection
? text.equals(this._prevTextSelection)
: text === this._prevTextSelection;
if (eq) {
return;
}

this._prevSelection = text;
this._prevTextSelection = text;
if (text) {
this.rangeManager.syncTextSelectionToRange(text);
} else {
Expand Down Expand Up @@ -100,18 +103,27 @@ export class RangeBinding {
Node.DOCUMENT_POSITION_PRECEDING);

if (range) {
const el =
range.commonAncestorContainer instanceof Element
? range.commonAncestorContainer
: range.commonAncestorContainer.parentElement;
if (!el) return;
const block = el.closest<BlockElement>(`[${this.host.blockIdAttr}]`);
if (block?.getAttribute(RangeManager.rangeSyncExcludeAttr) === 'true')
return;

const inlineEditor = this.rangeManager.getClosestInlineEditor(
range.commonAncestorContainer
);
if (inlineEditor && inlineEditor.isComposing) return;
if (inlineEditor?.isComposing) return;

this._prevSelection = this.rangeManager.rangeToTextSelection(
this._prevTextSelection = this.rangeManager.rangeToTextSelection(
range,
isRangeReversed
);
this.rangeManager.syncRangeToTextSelection(range, isRangeReversed);
} else {
this._prevSelection = null;
this._prevTextSelection = null;
this.selectionManager.clear(['text']);
}
};
Expand Down
3 changes: 3 additions & 0 deletions packages/framework/lit/src/utils/range-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class RangeManager {
if (topContenteditableElement instanceof HTMLElement) {
topContenteditableElement.blur();
}
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
}

set(range: Range) {
Expand Down

3 comments on commit bc2769f

@github-actions
Copy link

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 13.8 MB (+63.1 kB) 2.86 MB (+10.1 kB) 1.78 MB (+3.94 kB)

Packages

Name Size Gzip Brotli
blocks 2.17 MB (+13.7 kB) 506 kB (+629 B) 369 kB (+1.07 kB)
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

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 13.8 MB 2.86 MB 1.78 MB

Packages

Name Size Gzip Brotli
blocks 2.17 MB 506 kB 369 kB
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

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 13.8 MB 2.86 MB 1.78 MB

Packages

Name Size Gzip Brotli
blocks 2.17 MB 506 kB 369 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.