refactor(editor): reduce dependency to doc collection (#9492)

This commit is contained in:
Saul-Mirone
2025-01-03 01:59:25 +00:00
parent eb15b3cb39
commit 8b6c81f76d
70 changed files with 185 additions and 210 deletions
@@ -6,8 +6,8 @@ import {
NoteDisplayMode,
} from '@blocksuite/blocks';
import { assertExists } from '@blocksuite/global/utils';
import { DocCollection } from '@blocksuite/store';
import { beforeEach, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
import { wait } from '../utils/common.js';
import { addNote, getDocRootBlock } from '../utils/edgeless.js';
@@ -24,7 +24,7 @@ describe('group', () => {
});
test('group with no children will be removed automatically', () => {
const map = new DocCollection.Y.Map<boolean>();
const map = new Y.Map<boolean>();
const ids = Array.from({ length: 2 })
.map(() => {
const id = service.crud.addElement('shape', {
@@ -62,7 +62,7 @@ describe('group', () => {
});
test('remove group should remove its children at the same time', () => {
const map = new DocCollection.Y.Map<boolean>();
const map = new Y.Map<boolean>();
const doc = service.doc;
const noteId = addNote(doc);
const shapeId = service.crud.addElement('shape', {
@@ -112,7 +112,7 @@ describe('group', () => {
collapsedHeight: 100,
},
});
const children = new DocCollection.Y.Map<boolean>();
const children = new Y.Map<boolean>();
children.set(shape1, true);
children.set(shape2, true);
@@ -187,7 +187,7 @@ describe('group', () => {
});
test('empty group should have all zero xywh', () => {
const map = new DocCollection.Y.Map<boolean>();
const map = new Y.Map<boolean>();
const groupId = service.crud.addElement('group', { children: map });
assertExists(groupId);
const group = service.crud.getElementById(groupId) as GroupElementModel;
@@ -201,7 +201,7 @@ describe('group', () => {
test('descendant of group should not contain itself', () => {
const groupIds = [1, 2, 3].map(_ => {
return service.crud.addElement('group', {
children: new DocCollection.Y.Map<boolean>(),
children: new Y.Map<boolean>(),
}) as string;
});
const groups = groupIds.map(
@@ -5,8 +5,9 @@ import type {
GroupElementModel,
NoteBlockModel,
} from '@blocksuite/blocks';
import { type BlockModel, type Doc, DocCollection } from '@blocksuite/store';
import { type BlockModel, type Doc } from '@blocksuite/store';
import { beforeEach, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
import { wait } from '../utils/common.js';
import {
@@ -409,7 +410,7 @@ describe('group related functionality', () => {
service: EdgelessRootBlockComponent['service'],
childIds: string[]
) => {
const children = new DocCollection.Y.Map<boolean>();
const children = new Y.Map<boolean>();
childIds.forEach(id => children.set(id, true));
return service.crud.addElement('group', {
@@ -574,7 +575,7 @@ describe('compare function', () => {
childIds: string[]
// eslint-disable-next-line sonarjs/no-identical-functions
) => {
const children = new DocCollection.Y.Map<boolean>();
const children = new Y.Map<boolean>();
childIds.forEach(id => children.set(id, true));
return service.crud.addElement('group', {
@@ -2,9 +2,9 @@ import type { TextSelection } from '@blocksuite/block-std';
import { ShadowlessElement } from '@blocksuite/block-std';
import type { RichText } from '@blocksuite/blocks';
import { WithDisposable } from '@blocksuite/global/utils';
import { DocCollection } from '@blocksuite/store';
import { css, html, nothing } from 'lit';
import { property, query } from 'lit/decorators.js';
import * as Y from 'yjs';
import type { Comment, CommentManager } from './comment-manager.js';
@@ -52,7 +52,7 @@ export class CommentInput extends WithDisposable(ShadowlessElement) {
return;
}
const yText = new DocCollection.Y.Text();
const yText = new Y.Text();
yText.applyDelta(deltas);
const comment = this.manager.addComment(textSelection, {
author: 'Anonymous',
@@ -82,7 +82,7 @@ export class CommentInput extends WithDisposable(ShadowlessElement) {
const { quote } = parseResult;
const tmpYDoc = new DocCollection.Y.Doc();
const tmpYDoc = new Y.Doc();
const tmpYText = tmpYDoc.getText('comment');
return html`<div class="comment-input-container">
@@ -1,5 +1,5 @@
import type { EditorHost, TextSelection } from '@blocksuite/block-std';
import { DocCollection, type Y } from '@blocksuite/store';
import * as Y from 'yjs';
export interface CommentMeta {
id: string;
@@ -53,10 +53,7 @@ export class CommentManager {
quote,
...payload,
};
this.commentsMap.set(
id,
new DocCollection.Y.Map<unknown>(Object.entries(comment))
);
this.commentsMap.set(id, new Y.Map<unknown>(Object.entries(comment)));
return comment;
}
@@ -66,17 +63,15 @@ export class CommentManager {
const start = comment.get('start') as Comment['start'];
const end = comment.get('end') as Comment['end'];
const startIndex =
DocCollection.Y.createAbsolutePositionFromRelativePosition(
start.index,
this.host.doc.spaceDoc
);
const startIndex = Y.createAbsolutePositionFromRelativePosition(
start.index,
this.host.doc.spaceDoc
);
const startBlock = this.host.view.getBlock(start.id);
const endIndex =
DocCollection.Y.createAbsolutePositionFromRelativePosition(
end.index,
this.host.doc.spaceDoc
);
const endIndex = Y.createAbsolutePositionFromRelativePosition(
end.index,
this.host.doc.spaceDoc
);
const endBlock = this.host.view.getBlock(end.id);
if (!startIndex || !startBlock || !endIndex || !endBlock) {
@@ -122,11 +117,11 @@ export class CommentManager {
const toBlockId = toBlock.model.id;
if (!fromBlockText || !toBlockText) return null;
const startIndex = DocCollection.Y.createRelativePositionFromTypeIndex(
const startIndex = Y.createRelativePositionFromTypeIndex(
fromBlockText.yText,
from.index
);
const endIndex = DocCollection.Y.createRelativePositionFromTypeIndex(
const endIndex = Y.createRelativePositionFromTypeIndex(
toBlockText.yText,
to ? to.index + to.length : from.index + from.length
);
@@ -84,7 +84,7 @@ export class DocTitle extends WithDisposable(ShadowlessElement) {
};
private readonly _updateTitleInMeta = () => {
this.doc.collection.setDocMeta(this.doc.id, {
this.doc.collection.meta.setDocMeta(this.doc.id, {
title: this._rootModel.title.toString(),
});
};
@@ -1,9 +1,9 @@
import { ShadowlessElement } from '@blocksuite/block-std';
import type { FrameBlockModel } from '@blocksuite/blocks';
import { DisposableGroup, WithDisposable } from '@blocksuite/global/utils';
import type { Y } from '@blocksuite/store';
import { css, html, type PropertyValues } from 'lit';
import { property, query } from 'lit/decorators.js';
import type * as Y from 'yjs';
import { FrameCardTitleEditor } from './frame-card-title-editor.js';