Skip to content

Commit

Permalink
refactor: selection.getInstance -> selection.create (#5859)
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind authored Dec 26, 2023
1 parent 8b72dc1 commit 3822a25
Show file tree
Hide file tree
Showing 30 changed files with 55 additions and 59 deletions.
2 changes: 1 addition & 1 deletion packages/block-std/src/selection/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class SelectionManager {
return ctor.fromJSON(json);
};

getInstance<T extends BlockSuite.SelectionType>(
create<T extends BlockSuite.SelectionType>(
type: T,
...args: ConstructorParameters<BlockSuite.Selection[T]>
): BlockSuite.SelectionInstance[T] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
selection.update(selList => {
return selList.map(sel => {
if (PathFinder.equals(sel.path, blockElement.path)) {
return selection.getInstance('block', { path: blockElement.path });
return selection.create('block', { path: blockElement.path });
}
return sel;
});
Expand All @@ -55,7 +55,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
selection.update(selList => {
return selList.map(sel => {
if (PathFinder.equals(sel.path, blockElement.path)) {
return selection.getInstance('text', {
return selection.create('text', {
from: {
path: blockElement.path,
index: start ? 0 : blockElement.model.text?.length ?? 0,
Expand Down Expand Up @@ -102,7 +102,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
if (!PathFinder.equals(sel.path, blockElement.path)) {
return sel;
}
return selection.getInstance('text', {
return selection.create('text', {
from: {
path: blockElement.path,
index: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function addSpace(element: BlockElement, index: number) {
element.model.text?.insert(' ', index);
const currentText = element.selection.find('text');
element.selection.setGroup('note', [
element.selection.getInstance('text', {
element.selection.create('text', {
from: {
path: element.path,
index: (currentText?.from.index ?? 0) + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class EmbedBlockElement<
const isInSurface = blockComponent.isInSurface;
if (!isInSurface) {
this.host.selection.setGroup('note', [
this.host.selection.getInstance('block', {
this.host.selection.create('block', {
path: blockComponent.path,
}),
]);
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/attachment-block/attachment-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class AttachmentBlockComponent extends BlockElement<AttachmentBlockModel>
// If start dragging from the attachment element
// Set selection and take over dragStart event to start dragging
this.host.selection.set([
this.host.selection.getInstance('block', {
this.host.selection.create('block', {
path: attachmentBlock.path,
}),
]);
Expand Down Expand Up @@ -180,7 +180,7 @@ export class AttachmentBlockComponent extends BlockElement<AttachmentBlockModel>

private _focusAttachment = () => {
const selectionManager = this.host.selection;
const blockSelection = selectionManager.getInstance('block', {
const blockSelection = selectionManager.create('block', {
path: this.path,
});
selectionManager.setGroup('note', [blockSelection]);
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/bookmark-block/bookmark-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class BookmarkBlockComponent extends BlockElement<
const isInSurface = blockComponent.isInSurface;
if (!isInSurface) {
this.host.selection.setGroup('note', [
this.host.selection.getInstance('block', {
this.host.selection.create('block', {
path: blockComponent.path,
}),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class BookmarkCard extends WithDisposable(ShadowlessElement) {

private _onCardClick() {
const selectionManager = this.bookmark.host.selection;
const blockSelection = selectionManager.getInstance('block', {
const blockSelection = selectionManager.create('block', {
path: this.bookmark.path,
});
selectionManager.setGroup('note', [blockSelection]);
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/code-block/code-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export class CodeBlockComponent extends BlockElement<CodeBlockModel> {
if (from.index === 0 && from.length === 0) {
state.raw.preventDefault();
selectionManager.setGroup('note', [
selectionManager.getInstance('block', { path: this.path }),
selectionManager.create('block', { path: this.path }),
]);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/divider-block/divider-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class DividerBlockComponent extends BlockElement<DividerBlockModel> {

this.handleEvent('click', () => {
this.host.selection.set([
this.host.selection.getInstance('block', {
this.host.selection.create('block', {
path: this.path,
}),
]);
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/image-block/doc-image-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class ImageBlockPageComponent extends BlockElement<ImageBlockModel> {
.filter(sel => {
return !['text', 'block', 'image'].includes(sel.type);
})
.concat(selection.getInstance('image', { path: this.path }));
.concat(selection.create('image', { path: this.path }));
});
return true;
});
Expand Down Expand Up @@ -238,7 +238,7 @@ export class ImageBlockPageComponent extends BlockElement<ImageBlockModel> {
return selList
.filter(sel => !sel.is('image'))
.concat(
selection.getInstance('text', {
selection.create('text', {
from: {
path: this.parentPath.concat(blockId),
index: 0,
Expand All @@ -258,7 +258,7 @@ export class ImageBlockPageComponent extends BlockElement<ImageBlockModel> {
const current =
sel.is('image') && PathFinder.equals(sel.path, this.path);
if (current) {
return selection.getInstance('block', { path: this.path });
return selection.create('block', { path: this.path });
}
return sel;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/image-block/image-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class ImageBlockComponent extends BlockElement<ImageBlockModel> {

const selection = this.host.selection;
selection.setGroup('note', [
selection.getInstance('block', { path: this.path }),
selection.create('block', { path: this.path }),
]);
}

Expand All @@ -133,7 +133,7 @@ export class ImageBlockComponent extends BlockElement<ImageBlockModel> {
const isInSurface = blockComponent.isInSurface;
if (!isInSurface) {
this.std.selection.setGroup('note', [
this.std.selection.getInstance('block', {
this.std.selection.create('block', {
path: blockComponent.path,
}),
]);
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/list-block/list-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ListBlockComponent extends BlockElement<ListBlockModel> {
selection.update(selList => {
return selList
.filter(sel => !sel.is('text') && !sel.is('block'))
.concat(selection.getInstance('block', { path: this.path }));
.concat(selection.create('block', { path: this.path }));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const selectBlockTextBySide: Command<
const { selection } = ctx.std;

selection.setGroup('note', [
selection.getInstance('text', {
selection.create('text', {
from: {
path,
index: tail ? focusBlock.model.text?.length ?? 0 : 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/note-block/commands/select-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const selectBlock: Command<'focusBlock'> = (ctx, next) => {
const { selection } = std;

selection.setGroup('note', [
selection.getInstance('block', { path: focusBlock.path }),
selection.create('block', { path: focusBlock.path }),
]);

return next();
Expand Down
10 changes: 3 additions & 7 deletions packages/blocks/src/note-block/commands/select-blocks-between.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ export const selectBlocksBetween: Command<
// In same block
if (PathFinder.equals(anchorBlock.path, focusBlock.path)) {
const path = focusBlock.path;
selection.setGroup('note', [selection.getInstance('block', { path })]);
selection.setGroup('note', [selection.create('block', { path })]);
return next();
}

// In different blocks
const selections = [...selection.value];
if (selections.every(sel => !PathFinder.equals(sel.path, focusBlock.path))) {
if (tail) {
selections.push(
selection.getInstance('block', { path: focusBlock.path })
);
selections.push(selection.create('block', { path: focusBlock.path }));
} else {
selections.unshift(
selection.getInstance('block', { path: focusBlock.path })
);
selections.unshift(selection.create('block', { path: focusBlock.path }));
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/note-block/keymap-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ export class KeymapController implements ReactiveController {
index + 1
);

const sel = selection.getInstance('text', {
const sel = selection.create('text', {
from: {
path: element.parentPath.concat(blockId),
index: 0,
Expand Down Expand Up @@ -619,7 +619,7 @@ export class KeymapController implements ReactiveController {
)
) {
blocks.push(
selection.getInstance('block', {
selection.create('block', {
path: nodeView.path,
})
);
Expand Down Expand Up @@ -689,7 +689,7 @@ export class KeymapController implements ReactiveController {
);
assertExists(codeElement);
this._std.selection.setGroup('note', [
this._std.selection.getInstance('text', {
this._std.selection.create('text', {
from: {
path: codeElement.path,
index: 0,
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/page-block/clipboard/middlewares/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,19 @@ class PasteTr {
assertExists(target);
if (!lastModel.text) {
if (matchFlavours(lastModel, ['affine:image'])) {
const selection = this.std.selection.getInstance('image', {
const selection = this.std.selection.create('image', {
path: target.path,
});
this.std.selection.setGroup('note', [selection]);
return;
}
const selection = this.std.selection.getInstance('block', {
const selection = this.std.selection.create('block', {
path: target.path,
});
this.std.selection.setGroup('note', [selection]);
return;
}
const selection = this.std.selection.getInstance('text', {
const selection = this.std.selection.create('text', {
from: {
path: target.path,
index:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const deleteTextCommand: Command<
if (!to) {
fromText.delete(from.index, from.length);
ctx.std.selection.setGroup('note', [
ctx.std.selection.getInstance('text', {
ctx.std.selection.create('text', {
from: {
path: from.path,
index: from.index,
Expand Down Expand Up @@ -81,7 +81,7 @@ export const deleteTextCommand: Command<
});

ctx.std.selection.setGroup('note', [
ctx.std.selection.getInstance('text', {
ctx.std.selection.create('text', {
from: {
path: to.path,
index: to.index,
Expand Down
10 changes: 5 additions & 5 deletions packages/blocks/src/page-block/doc/doc-page-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ export class DocPageBlockComponent extends BlockElement<
.filter(sel => !sel.is('text') && !sel.is('block'))
.concat([
sel.is('text')
? selection.getInstance('text', {
? selection.create('text', {
from: {
path: lastNoteChild.path,
index: lastNoteChild.model.text?.length ?? 0,
length: 0,
},
to: null,
})
: selection.getInstance('block', {
: selection.create('block', {
path: lastNoteChild.path,
}),
])
Expand Down Expand Up @@ -319,15 +319,15 @@ export class DocPageBlockComponent extends BlockElement<
.filter(sel => !sel.is('text') && !sel.is('block'))
.concat([
sel.is('text')
? selection.getInstance('text', {
? selection.create('text', {
from: {
path: firstNoteChild.path,
index: 0,
length: 0,
},
to: null,
})
: selection.getInstance('block', {
: selection.create('block', {
path: firstNoteChild.path,
}),
])
Expand Down Expand Up @@ -400,7 +400,7 @@ export class DocPageBlockComponent extends BlockElement<

requestAnimationFrame(() => {
this.host.selection.setGroup('note', [
this.host.selection.getInstance('text', {
this.host.selection.create('text', {
from: {
path: [this.model.id, noteId, paragraphId],
index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export class EdgelessSelectionManager {

if (elements.length > 0) {
instances.push(
this._selection.getInstance(
this._selection.create(
'surface',
this.container.surface.path,
elements,
Expand All @@ -267,7 +267,7 @@ export class EdgelessSelectionManager {
if (blocks.length > 0) {
instances = instances.concat(
blocks.map(blockId =>
this._selection.getInstance(
this._selection.create(
'surface',
[this.container.page.root!.id, blockId],
[blockId],
Expand Down Expand Up @@ -303,7 +303,7 @@ export class EdgelessSelectionManager {
}

setCursor(cursor: CursorSelection | CursorSelectionState) {
const instance = this._selection.getInstance('cursor', cursor.x, cursor.y);
const instance = this._selection.create('cursor', cursor.x, cursor.y);

this._selection.setGroup('edgeless', [...this.selections, instance]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class PageKeyboardManager {

if (selection) {
this._selection.set([
this._selection.getInstance('text', {
this._selection.create('text', {
from: {
index: 0,
length: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function updateBlockElementType(
const blockSelections = selectionManager.filter('block');

if (textSelection) {
const newTextSelection = selectionManager.getInstance('text', {
const newTextSelection = selectionManager.create('text', {
from: {
path: textSelection.from.path.slice(0, -1).concat(firstNewModel.id),
index: textSelection.from.index,
Expand All @@ -120,7 +120,7 @@ export function updateBlockElementType(
if (blockSelections.length !== 0) {
requestAnimationFrame(() => {
const selections = newModels.map(model => {
return selectionManager.getInstance('block', {
return selectionManager.create('block', {
path: blockSelections[0].path.slice(0, -1).concat(model.id),
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class AffineDocDraggingAreaWidget extends WidgetElement<DocPageBlockCompo
this._allBlocksWithRect,
userRect
).map(blockPath => {
return this.host.selection.getInstance('block', {
return this.host.selection.create('block', {
path: blockPath,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ export class AffineDragHandleWidget extends WidgetElement<
) => {
const { selection } = this.host;
const selections = blockElements.map(blockElement =>
selection.getInstance('block', {
selection.create('block', {
path: blockElement.path,
})
);
Expand All @@ -788,7 +788,7 @@ export class AffineDragHandleWidget extends WidgetElement<
// We need to remain surface selection and set editing as true
if (isInsideEdgelessEditor(this.host)) {
const surfaceElementId = noteId ? noteId : getNoteId(blockElements[0]);
const surfaceSelection = selection.getInstance(
const surfaceSelection = selection.create(
'surface',
blockElements[0]!.path,
[surfaceElementId],
Expand Down
Loading

2 comments on commit 3822a25

@vercel
Copy link

@vercel vercel bot commented on 3822a25 Dec 26, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 3822a25 Dec 26, 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

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

Please sign in to comment.