Skip to content

Commit

Permalink
feat: selection extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Sep 25, 2024
1 parent 5739eea commit 2935ecf
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 70 deletions.
2 changes: 2 additions & 0 deletions packages/affine/block-surface/src/surface-spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HighlightSelectionExtension } from '@blocksuite/affine-shared/selection';
import {
BlockViewExtension,
CommandExtension,
Expand All @@ -13,6 +14,7 @@ const CommonSurfaceBlockSpec: ExtensionType[] = [
FlavourExtension('affine:surface'),
SurfaceBlockService,
CommandExtension(commands),
HighlightSelectionExtension,
];

export const PageSurfaceBlockSpec: ExtensionType[] = [
Expand Down
4 changes: 3 additions & 1 deletion packages/affine/data-view/src/core/common/selection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseSelection } from '@blocksuite/block-std';
import { BaseSelection, SelectionExtension } from '@blocksuite/block-std';
import { z } from 'zod';

import type { DataViewSelection, GetDataViewSelection } from '../types.js';
Expand Down Expand Up @@ -129,3 +129,5 @@ declare global {
}
}
}

export const DatabaseSelectionExtension = SelectionExtension(DatabaseSelection);
5 changes: 4 additions & 1 deletion packages/affine/shared/src/selection/hightlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type ReferenceParams,
ReferenceParamsSchema,
} from '@blocksuite/affine-model';
import { BaseSelection } from '@blocksuite/block-std';
import { BaseSelection, SelectionExtension } from '@blocksuite/block-std';

export class HighlightSelection extends BaseSelection {
static override group = 'scene';
Expand Down Expand Up @@ -57,3 +57,6 @@ declare global {
}
}
}

export const HighlightSelectionExtension =
SelectionExtension(HighlightSelection);
4 changes: 3 additions & 1 deletion packages/affine/shared/src/selection/image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseSelection } from '@blocksuite/block-std';
import { BaseSelection, SelectionExtension } from '@blocksuite/block-std';
import z from 'zod';

const ImageSelectionSchema = z.object({
Expand Down Expand Up @@ -37,3 +37,5 @@ declare global {
}
}
}

export const ImageSelectionExtension = SelectionExtension(ImageSelection);
7 changes: 5 additions & 2 deletions packages/affine/shared/src/selection/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { HighlightSelection } from './hightlight.js';
export { ImageSelection } from './image.js';
export {
HighlightSelection,
HighlightSelectionExtension,
} from './hightlight.js';
export { ImageSelection, ImageSelectionExtension } from './image.js';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DocMode } from '@blocksuite/affine-model';

import { HighlightSelection } from '@blocksuite/affine-shared/selection';
import '@blocksuite/affine-shared/selection';
import { WidgetComponent } from '@blocksuite/block-std';
import {
GfxControllerIdentifier,
Expand Down Expand Up @@ -181,8 +181,6 @@ export class AffineScrollAnchoringWidget extends WidgetComponent {
override connectedCallback() {
super.connectedCallback();

this.std.selection.register(HighlightSelection);

this.#resizeObserver.observe(this.offsetParent!);
this.handleEvent('wheel', this.#requestUpdateFn);
this.disposables.addFromEvent(window, 'resize', this.#requestUpdateFn);
Expand Down
6 changes: 0 additions & 6 deletions packages/blocks/src/database-block/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
DatabaseBlockSchema,
} from '@blocksuite/affine-model';
import { BlockService } from '@blocksuite/block-std';
import { DatabaseSelection } from '@blocksuite/data-view';
import { viewPresets } from '@blocksuite/data-view/view-presets';

import {
Expand Down Expand Up @@ -58,9 +57,4 @@ export class DatabaseBlockService extends BlockService {
}
applyPropertyUpdate(blockModel);
}

override mounted(): void {
super.mounted();
this.selectionManager.register(DatabaseSelection);
}
}
2 changes: 2 additions & 0 deletions packages/blocks/src/database-block/database-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type ExtensionType,
FlavourExtension,
} from '@blocksuite/block-std';
import { DatabaseSelectionExtension } from '@blocksuite/data-view';
import { literal } from 'lit/static-html.js';

import { DatabaseDragHandleOption } from './config.js';
Expand All @@ -13,4 +14,5 @@ export const DatabaseBlockSpec: ExtensionType[] = [
DatabaseBlockService,
BlockViewExtension('affine:database', literal`affine-database`),
DatabaseDragHandleOption,
DatabaseSelectionExtension,
];
3 changes: 0 additions & 3 deletions packages/blocks/src/image-block/image-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ImageBlockSchema } from '@blocksuite/affine-model';
import { ImageSelection } from '@blocksuite/affine-shared/selection';
import {
DragHandleConfigExtension,
TelemetryProvider,
Expand Down Expand Up @@ -79,8 +78,6 @@ export class ImageBlockService extends BlockService {
override mounted(): void {
super.mounted();

this.selectionManager.register(ImageSelection);

this.fileDropManager = new FileDropManager(this, this._fileDropOptions);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/blocks/src/image-block/image-spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ImageSelectionExtension } from '@blocksuite/affine-shared/selection';
import {
BlockViewExtension,
CommandExtension,
Expand Down Expand Up @@ -27,4 +28,5 @@ export const ImageBlockSpec: ExtensionType[] = [
imageToolbar: literal`affine-image-toolbar-widget`,
}),
ImageDragHandleOption,
ImageSelectionExtension,
];
1 change: 1 addition & 0 deletions packages/framework/block-std/src/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './extension.js';
export * from './flavour.js';
export * from './keymap.js';
export * from './lifecycle-watcher.js';
export * from './selection.js';
export * from './service.js';
export * from './service-watcher.js';
export * from './widget-view-map.js';
14 changes: 14 additions & 0 deletions packages/framework/block-std/src/extension/selection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { SelectionConstructor } from '../selection/index.js';
import type { ExtensionType } from './extension.js';

import { SelectionIdentifier } from '../identifier.js';

export function SelectionExtension(
selectionCtor: SelectionConstructor
): ExtensionType {
return {
setup: di => {
di.addImpl(SelectionIdentifier(selectionCtor.type), () => selectionCtor);
},
};
}
4 changes: 4 additions & 0 deletions packages/framework/block-std/src/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Command } from './command/index.js';
import type { EventOptions, UIEventHandler } from './event/index.js';
import type { BlockService, LifeCycleWatcher } from './extension/index.js';
import type { BlockStdScope } from './scope/index.js';
import type { SelectionConstructor } from './selection/index.js';
import type { BlockViewType, WidgetViewMapType } from './spec/type.js';

export const BlockServiceIdentifier =
Expand Down Expand Up @@ -32,3 +33,6 @@ export const KeymapIdentifier = createIdentifier<{
getter: (std: BlockStdScope) => Record<string, UIEventHandler>;
options?: EventOptions;
}>('Keymap');

export const SelectionIdentifier =
createIdentifier<SelectionConstructor>('Selection');
12 changes: 11 additions & 1 deletion packages/framework/block-std/src/scope/block-std-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import {
StdIdentifier,
} from '../identifier.js';
import { RangeManager } from '../range/index.js';
import { SelectionManager } from '../selection/index.js';
import {
BlockSelectionExtension,
CursorSelectionExtension,
SelectionManager,
SurfaceSelectionExtension,
TextSelectionExtension,
} from '../selection/index.js';
import { ServiceManager } from '../service/index.js';
import { EditorHost } from '../view/element/index.js';
import { ViewStore } from '../view/view-store.js';
Expand All @@ -37,6 +43,10 @@ const internalExtensions = [
ViewStore,
Clipboard,
GfxController,
BlockSelectionExtension,
TextSelectionExtension,
SurfaceSelectionExtension,
CursorSelectionExtension,
];

export class BlockStdScope {
Expand Down
24 changes: 24 additions & 0 deletions packages/framework/block-std/src/selection/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
import type {
BlockSelection,
CursorSelection,
SurfaceSelection,
TextSelection,
} from './variants/index.js';

export * from './base.js';
export * from './manager.js';
export * from './variants/index.js';

declare global {
namespace BlockSuite {
interface Selection {
block: typeof BlockSelection;
cursor: typeof CursorSelection;
surface: typeof SurfaceSelection;
text: typeof TextSelection;
}

type SelectionType = keyof Selection;

type SelectionInstance = {
[P in SelectionType]: InstanceType<Selection[P]>;
};
}
}
20 changes: 6 additions & 14 deletions packages/framework/block-std/src/selection/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ import type { BlockStdScope } from '../scope/index.js';
import type { BaseSelection } from './base.js';

import { LifeCycleWatcher } from '../extension/index.js';
import {
BlockSelection,
CursorSelection,
SurfaceSelection,
TextSelection,
} from './variants/index.js';

interface SelectionConstructor {
import { SelectionIdentifier } from '../identifier.js';

export interface SelectionConstructor {
type: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new (...args: any[]): BaseSelection;
Expand Down Expand Up @@ -133,12 +128,9 @@ export class SelectionManager extends LifeCycleWatcher {
}

private _setupDefaultSelections() {
this.register([
TextSelection,
BlockSelection,
SurfaceSelection,
CursorSelection,
]);
this.std.provider.getAll(SelectionIdentifier).forEach(ctor => {
this.register(ctor);
});
}

clear(types?: string[]) {
Expand Down
9 changes: 2 additions & 7 deletions packages/framework/block-std/src/selection/variants/block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import z from 'zod';

import { SelectionExtension } from '../../extension/selection.js';
import { BaseSelection } from '../base.js';

const BlockSelectionSchema = z.object({
Expand Down Expand Up @@ -31,10 +32,4 @@ export class BlockSelection extends BaseSelection {
}
}

declare global {
namespace BlockSuite {
interface Selection {
block: typeof BlockSelection;
}
}
}
export const BlockSelectionExtension = SelectionExtension(BlockSelection);
9 changes: 2 additions & 7 deletions packages/framework/block-std/src/selection/variants/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import z from 'zod';

import { SelectionExtension } from '../../extension/selection.js';
import { BaseSelection } from '../base.js';

const CursorSelectionSchema = z.object({
Expand Down Expand Up @@ -43,10 +44,4 @@ export class CursorSelection extends BaseSelection {
}
}

declare global {
namespace BlockSuite {
interface Selection {
cursor: typeof CursorSelection;
}
}
}
export const CursorSelectionExtension = SelectionExtension(CursorSelection);
10 changes: 0 additions & 10 deletions packages/framework/block-std/src/selection/variants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,3 @@ export * from './block.js';
export * from './cursor.js';
export * from './surface.js';
export * from './text.js';

declare global {
namespace BlockSuite {
type SelectionType = keyof Selection;

type SelectionInstance = {
[P in SelectionType]: InstanceType<Selection[P]>;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import z from 'zod';

import { SelectionExtension } from '../../extension/selection.js';
import { BaseSelection } from '../base.js';

const SurfaceSelectionSchema = z.object({
Expand Down Expand Up @@ -68,10 +69,4 @@ export class SurfaceSelection extends BaseSelection {
}
}

declare global {
namespace BlockSuite {
interface Selection {
surface: typeof SurfaceSelection;
}
}
}
export const SurfaceSelectionExtension = SelectionExtension(SurfaceSelection);
9 changes: 2 additions & 7 deletions packages/framework/block-std/src/selection/variants/text.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import z from 'zod';

import { SelectionExtension } from '../../extension/selection.js';
import { BaseSelection } from '../base.js';

export type TextRangePoint = {
Expand Down Expand Up @@ -113,10 +114,4 @@ export class TextSelection extends BaseSelection {
}
}

declare global {
namespace BlockSuite {
interface Selection {
text: typeof TextSelection;
}
}
}
export const TextSelectionExtension = SelectionExtension(TextSelection);

0 comments on commit 2935ecf

Please sign in to comment.