Skip to content

Commit

Permalink
refactor: simplify editor.host getter without .value (#5876)
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind authored Dec 27, 2023
1 parent 58d3bf4 commit 214616d
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ The above design ensures that BlockSuite is built for scalability. In addition t
- Incremental updates, real-time collaboration, local-first state management, and even decentralized data synchronization based on the document's [provider](https://blocksuite.io/data-persistence.html#realtime-provider-based-persistence) mechanism.
- State scheduling across multiple documents and simultaneous use of a single document in multiple editors based on an opt-in workspace mechanism.

> 🚧 BlockSuite is currently in beta, with some extension capabilities still under refinement. Hope you can stay tuned, try it out, or share your feedback!
> 🚧 BlockSuite is currently in its early stage, with some extension capabilities still under refinement. Hope you can stay tuned, try it out, or share your feedback!
## Getting Started

Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/bookmark-block/doc-bookmark-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class DocBookmarkBlockComponent extends WithDisposable(
return this.block.model;
}

get root() {
get host() {
return this.block.host;
}

Expand All @@ -43,7 +43,7 @@ export class DocBookmarkBlockComponent extends WithDisposable(
}

this.disposables.add(
this.root.selection.slots.changed.on(() => this.requestUpdate())
this.host.selection.slots.changed.on(() => this.requestUpdate())
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/docs/blocksuite-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ The above design ensures that BlockSuite is built for scalability. In addition t

To try out BlockSuite, refer to the [Quick Start](./quick-start) document and start with the preset editors in `@blocksuite/presets`.

> 🚧 BlockSuite is currently in beta, with some extension capabilities still under refinement. Hope you can stay tuned, try it out, or share your feedback!
> 🚧 BlockSuite is currently in its early stage, with some extension capabilities still under refinement. Hope you can stay tuned, try it out, or share your feedback!
6 changes: 3 additions & 3 deletions packages/playground/apps/starter/components/debug-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export class DebugMenu extends ShadowlessElement {
super.connectedCallback();

const readSelectionFromURL = async () => {
const root = this.editor.root;
const root = this.editor.host;
if (!root) {
await new Promise(resolve => {
setTimeout(resolve, 500);
Expand Down Expand Up @@ -363,7 +363,7 @@ export class DebugMenu extends ShadowlessElement {
children,
};
};
const blocks = getSelectedBlocks(this.editor.root!);
const blocks = getSelectedBlocks(this.editor.host!);
const toTreeNode = (block: BaseBlockModel): TreeNode => {
return {
text: block.text?.toString() ?? '',
Expand Down Expand Up @@ -480,7 +480,7 @@ export class DebugMenu extends ShadowlessElement {
}

private _shareSelection() {
const selection = this.editor.root?.selection.value;
const selection = this.editor.host?.selection.value;
if (!selection || selection.length === 0) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/presets/src/__tests__/utils/edgeless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { AffineEditorContainer } from '../../index.js';
export function getSurface(page: Page, editor: AffineEditorContainer) {
const surfaceModel = page.getBlockByFlavour('affine:surface');

return editor.root!.view.viewFromPath('block', [
return editor.host!.view.viewFromPath('block', [
page.root!.id,
surfaceModel[0]!.id,
]) as SurfaceBlockComponent;
Expand All @@ -33,7 +33,7 @@ export function getPageRootBlock(
editor: AffineEditorContainer,
_?: 'edgeless' | 'page'
) {
return editor.root!.view.viewFromPath('block', [page.root!.id]) as
return editor.host!.view.viewFromPath('block', [page.root!.id]) as
| EdgelessPageBlockComponent
| DocPageBlockComponent;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/presets/src/editors/doc-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export class DocEditor extends WithDisposable(ShadowlessElement) {
@property({ type: Boolean })
hasViewport = true;

host: Ref<EditorHost> = createRef<EditorHost>();
private _host: Ref<EditorHost> = createRef<EditorHost>();

get host() {
return this._host.value;
}

override render() {
return html`
Expand Down Expand Up @@ -59,7 +63,7 @@ export class DocEditor extends WithDisposable(ShadowlessElement) {
: 'doc-editor-container'}
>
<editor-host
${ref(this.host)}
${ref(this._host)}
.page=${this.page}
.specs=${this.specs}
></editor-host>
Expand Down
8 changes: 6 additions & 2 deletions packages/presets/src/editors/edgeless-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export class EdgelessEditor extends WithDisposable(ShadowlessElement) {
@property({ attribute: false })
specs = EdgelessEditorBlockSpecs;

host: Ref<EditorHost> = createRef<EditorHost>();
private _host: Ref<EditorHost> = createRef<EditorHost>();

get host() {
return this._host.value;
}

override render() {
return html`
Expand All @@ -39,7 +43,7 @@ export class EdgelessEditor extends WithDisposable(ShadowlessElement) {
}
</style>
<editor-host
${ref(this.host)}
${ref(this._host)}
.page=${this.page}
.specs=${this.specs}
></editor-host>
Expand Down
16 changes: 10 additions & 6 deletions packages/presets/src/editors/editor-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
ThemeObserver,
} from '@blocksuite/blocks';
import { assertExists, noop, Slot } from '@blocksuite/global/utils';
import { ShadowlessElement, WithDisposable } from '@blocksuite/lit';
import {
type EditorHost,
ShadowlessElement,
WithDisposable,
} from '@blocksuite/lit';
import type { Page } from '@blocksuite/store';
import { html, nothing } from 'lit';
import { customElement, property, query } from 'lit/decorators.js';
Expand Down Expand Up @@ -64,10 +68,10 @@ export class AffineEditorContainer
@query('affine-edgeless-page')
private _edgelessPage?: EdgelessPageBlockComponent;

get root() {
return this.mode === 'page'
? this._docPage?.host
: this._edgelessPage?.host;
get host() {
return (
this.mode === 'page' ? this._docPage?.host : this._edgelessPage?.host
) as EditorHost;
}

readonly themeObserver = new ThemeObserver();
Expand All @@ -85,7 +89,7 @@ export class AffineEditorContainer

override async getUpdateComplete(): Promise<boolean> {
const result = await super.getUpdateComplete();
const root = this.root;
const root = this.host;
if (root) {
await root.updateComplete;
}
Expand Down
28 changes: 12 additions & 16 deletions packages/presets/src/fragments/copilot-panel/copilot-panel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import './chat-with-workspace/chat-with-workspace.js';

import { FrameBlockModel } from '@blocksuite/blocks';
import {
type EditorHost,
ShadowlessElement,
WithDisposable,
} from '@blocksuite/lit';
import { ShadowlessElement, WithDisposable } from '@blocksuite/lit';
import { css, html, nothing, type TemplateResult } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
Expand Down Expand Up @@ -97,8 +93,8 @@ export class CopilotPanel extends WithDisposable(ShadowlessElement) {
return this.editor.page;
}

get root() {
return this.editor.root as EditorHost;
get host() {
return this.editor.host;
}

public override connectedCallback() {
Expand All @@ -113,7 +109,7 @@ export class CopilotPanel extends WithDisposable(ShadowlessElement) {
private async _replace() {
if (!this._result) return;

const selectedBlocks = await getSelectedBlocks(this.root);
const selectedBlocks = await getSelectedBlocks(this.host);
if (!selectedBlocks.length) return;

const firstBlock = selectedBlocks[0];
Expand All @@ -128,7 +124,7 @@ export class CopilotPanel extends WithDisposable(ShadowlessElement) {
});

const models = await insertFromMarkdown(
this.root,
this.host,
this._result,
parentBlock.model.id,
firstIndex
Expand All @@ -137,15 +133,15 @@ export class CopilotPanel extends WithDisposable(ShadowlessElement) {
const parentPath = firstBlock.parentPath;
const selections = models
.map(model => [...parentPath, model.id])
.map(path => this.root.selection.create('block', { path }));
this.root.selection.setGroup('note', selections);
.map(path => this.host.selection.create('block', { path }));
this.host.selection.setGroup('note', selections);
}, 0);
}

private async _insertBelow() {
if (!this._result) return;

const selectedBlocks = await getSelectedBlocks(this.root);
const selectedBlocks = await getSelectedBlocks(this.host);
const blockLength = selectedBlocks.length;
if (!blockLength) return;

Expand All @@ -157,7 +153,7 @@ export class CopilotPanel extends WithDisposable(ShadowlessElement) {
) as number;

const models = await insertFromMarkdown(
this.root,
this.host,
this._result,
parentBlock.model.id,
lastIndex + 1
Expand All @@ -167,8 +163,8 @@ export class CopilotPanel extends WithDisposable(ShadowlessElement) {
const parentPath = lastBlock.parentPath;
const selections = models
.map(model => [...parentPath, model.id])
.map(path => this.root.selection.create('block', { path }));
this.root.selection.setGroup('note', selections);
.map(path => this.host.selection.create('block', { path }));
this.host.selection.setGroup('note', selections);
}, 0);
}

Expand Down Expand Up @@ -263,7 +259,7 @@ export class CopilotPanel extends WithDisposable(ShadowlessElement) {
key: K,
payload: Omit<GPTAPIPayloadMap[K], 'input'>
) => {
const input = await getSelectedTextContent(this.root);
const input = await getSelectedTextContent(this.host);
if (!input) {
alert('Please select some text first');
return;
Expand Down
7 changes: 2 additions & 5 deletions packages/presets/src/fragments/copilot-panel/edgeless/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
FrameBlockModel,
type ImageBlockProps,
} from '@blocksuite/blocks';
import { assertExists } from '@blocksuite/global/utils';
import type { EditorHost } from '@blocksuite/lit';
import type { Workspace } from '@blocksuite/store';

import type { AffineEditorContainer } from '../../../editors/index.js';
Expand Down Expand Up @@ -53,9 +51,8 @@ export class EditorWithAI {
return this.editor.page.workspace;
}

get root(): EditorHost {
assertExists(this.editor.root);
return this.editor.root;
get host() {
return this.editor.host;
}

constructor(private editor: AffineEditorContainer) {}
Expand Down
8 changes: 2 additions & 6 deletions packages/presets/src/fragments/frame-panel/frame-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import './body/frame-panel-body.js';

import { FramePreview } from '@blocksuite/blocks';
import { DisposableGroup } from '@blocksuite/global/utils';
import {
type EditorHost,
ShadowlessElement,
WithDisposable,
} from '@blocksuite/lit';
import { ShadowlessElement, WithDisposable } from '@blocksuite/lit';
import { baseTheme } from '@toeverything/theme';
import { css, html, type PropertyValues, unsafeCSS } from 'lit';
import { property } from 'lit/decorators.js';
Expand Down Expand Up @@ -86,7 +82,7 @@ export class FramePanel extends WithDisposable(ShadowlessElement) {
}

get host() {
return this.editor.root as EditorHost;
return this.editor.host;
}

get edgeless() {
Expand Down
3 changes: 1 addition & 2 deletions packages/presets/src/fragments/toc-panel/toc-panel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DisposableGroup } from '@blocksuite/global/utils';
import type { EditorHost } from '@blocksuite/lit';
import { WithDisposable } from '@blocksuite/lit';
import { baseTheme } from '@toeverything/theme';
import { css, html, LitElement, type PropertyValues, unsafeCSS } from 'lit';
Expand Down Expand Up @@ -57,7 +56,7 @@ export class TOCPanel extends WithDisposable(LitElement) {
}

get host() {
return this.editor.root as EditorHost;
return this.editor.host;
}

get edgeless() {
Expand Down

3 comments on commit 214616d

@vercel
Copy link

@vercel vercel bot commented on 214616d Dec 27, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

blocksuite – ./packages/playground

blocksuite-git-master-toeverything.vercel.app
blocksuite-toeverything.vercel.app
try-blocksuite.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 214616d Dec 27, 2023

Choose a reason for hiding this comment

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

@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 13.8 MB (-2.87 kB) 2.7 MB (-2.84 kB) 1.69 MB (+771 B)

Packages

Name Size Gzip Brotli
blocks 1.99 MB (-471 B) 462 kB (-210 B) 349 kB (+46 B)
editor 84 B 89 B 63 B
store 61.9 kB 17.7 kB 15.8 kB
inline 32.4 kB 8.93 kB 8 kB

Please sign in to comment.