feat(editor): add page dragging area widget extension (#12045)

Closes: BS-3364

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Introduced a new "Page Dragging Area" widget, enabling enhanced block selection and drag area detection within the user interface.
  - Added utilities for more precise block selection based on rectangular selection areas.

- **Improvements**
  - Integrated the new widget into the view extension system for consistent behavior across supported views.
  - Enhanced clipboard handling with comprehensive adapter configurations for various data types.

- **Refactor**
  - Streamlined widget registration and block selection logic for improved maintainability and modularity.
  - Removed legacy widget exports and registrations to centralize widget management.

- **Chores**
  - Updated workspace and TypeScript configurations to support the new widget module.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Saul-Mirone
2025-04-29 01:44:59 +00:00
parent f177c64ca1
commit d82d37b53d
18 changed files with 305 additions and 161 deletions

View File

@@ -61,6 +61,7 @@
"@blocksuite/affine-widget-frame-title": "workspace:*",
"@blocksuite/affine-widget-keyboard-toolbar": "workspace:*",
"@blocksuite/affine-widget-linked-doc": "workspace:*",
"@blocksuite/affine-widget-page-dragging-area": "workspace:*",
"@blocksuite/affine-widget-remote-selection": "workspace:*",
"@blocksuite/affine-widget-scroll-anchoring": "workspace:*",
"@blocksuite/affine-widget-slash-menu": "workspace:*",
@@ -188,6 +189,8 @@
"./widgets/keyboard-toolbar/view": "./src/widgets/keyboard-toolbar/view.ts",
"./widgets/viewport-overlay": "./src/widgets/viewport-overlay/index.ts",
"./widgets/viewport-overlay/view": "./src/widgets/viewport-overlay/view.ts",
"./widgets/page-dragging-area": "./src/widgets/page-dragging-area/index.ts",
"./widgets/page-dragging-area/view": "./src/widgets/page-dragging-area/view.ts",
"./fragments/doc-title": "./src/fragments/doc-title.ts",
"./fragments/frame-panel": "./src/fragments/frame-panel.ts",
"./fragments/outline": "./src/fragments/outline.ts",

View File

@@ -39,6 +39,7 @@ import { EdgelessZoomToolbarViewExtension } from '@blocksuite/affine-widget-edge
import { FrameTitleViewExtension } from '@blocksuite/affine-widget-frame-title/view';
import { KeyboardToolbarViewExtension } from '@blocksuite/affine-widget-keyboard-toolbar/view';
import { LinkedDocViewExtension } from '@blocksuite/affine-widget-linked-doc/view';
import { PageDraggingAreaViewExtension } from '@blocksuite/affine-widget-page-dragging-area/view';
import { RemoteSelectionViewExtension } from '@blocksuite/affine-widget-remote-selection/view';
import { ScrollAnchoringViewExtension } from '@blocksuite/affine-widget-scroll-anchoring/view';
import { SlashMenuViewExtension } from '@blocksuite/affine-widget-slash-menu/view';
@@ -104,5 +105,6 @@ export function getInternalViewExtensions() {
ToolbarViewExtension,
ViewportOverlayViewExtension,
EdgelessZoomToolbarViewExtension,
PageDraggingAreaViewExtension,
];
}

View File

@@ -0,0 +1 @@
export * from '@blocksuite/affine-widget-page-dragging-area';

View File

@@ -0,0 +1 @@
export * from '@blocksuite/affine-widget-page-dragging-area/view';

View File

@@ -58,6 +58,7 @@
{ "path": "../widgets/frame-title" },
{ "path": "../widgets/keyboard-toolbar" },
{ "path": "../widgets/linked-doc" },
{ "path": "../widgets/page-dragging-area" },
{ "path": "../widgets/remote-selection" },
{ "path": "../widgets/scroll-anchoring" },
{ "path": "../widgets/slash-menu" },

View File

@@ -21,15 +21,10 @@ import {
PageRootBlockComponent,
PreviewRootBlockComponent,
} from './index.js';
import {
AFFINE_PAGE_DRAGGING_AREA_WIDGET,
AffinePageDraggingAreaWidget,
} from './widgets/page-dragging-area/page-dragging-area.js';
export function effects() {
// Register components by category
registerRootComponents();
registerWidgets();
registerEdgelessToolbarComponents();
registerMiscComponents();
}
@@ -44,13 +39,6 @@ function registerRootComponents() {
);
}
function registerWidgets() {
customElements.define(
AFFINE_PAGE_DRAGGING_AREA_WIDGET,
AffinePageDraggingAreaWidget
);
}
function registerEdgelessToolbarComponents() {
// Tool buttons
customElements.define('edgeless-link-tool-button', EdgelessLinkToolButton);

View File

@@ -16,7 +16,6 @@ export * from './preview/preview-root-block.js';
export { RootService } from './root-service.js';
export * from './types.js';
export * from './utils/index.js';
export * from './widgets/index.js';
declare type _GLOBAL_ =
| typeof PointerEffect

View File

@@ -1,23 +1,15 @@
import { ViewportElementExtension } from '@blocksuite/affine-shared/services';
import { BlockViewExtension, WidgetViewExtension } from '@blocksuite/std';
import { BlockViewExtension } from '@blocksuite/std';
import type { ExtensionType } from '@blocksuite/store';
import { literal, unsafeStatic } from 'lit/static-html.js';
import { literal } from 'lit/static-html.js';
import { PageClipboard } from '../clipboard/page-clipboard.js';
import { CommonSpecs } from '../common-specs/index.js';
import { AFFINE_PAGE_DRAGGING_AREA_WIDGET } from '../widgets/page-dragging-area/page-dragging-area.js';
import { PageRootService } from './page-root-service.js';
export const pageDraggingAreaWidget = WidgetViewExtension(
'affine:page',
AFFINE_PAGE_DRAGGING_AREA_WIDGET,
literal`${unsafeStatic(AFFINE_PAGE_DRAGGING_AREA_WIDGET)}`
);
const PageCommonExtension: ExtensionType[] = [
CommonSpecs,
PageRootService,
pageDraggingAreaWidget,
ViewportElementExtension('.affine-page-viewport'),
].flat();

View File

@@ -1 +0,0 @@
export { AffinePageDraggingAreaWidget } from './page-dragging-area/page-dragging-area.js';

View File

@@ -0,0 +1,42 @@
{
"name": "@blocksuite/affine-widget-page-dragging-area",
"description": "Affine page dragging area widget.",
"type": "module",
"scripts": {
"build": "tsc"
},
"sideEffects": false,
"keywords": [],
"author": "toeverything",
"license": "MIT",
"dependencies": {
"@blocksuite/affine-components": "workspace:*",
"@blocksuite/affine-ext-loader": "workspace:*",
"@blocksuite/affine-model": "workspace:*",
"@blocksuite/affine-shared": "workspace:*",
"@blocksuite/global": "workspace:*",
"@blocksuite/icons": "^2.2.12",
"@blocksuite/std": "workspace:*",
"@blocksuite/store": "workspace:*",
"@lit/context": "^1.1.2",
"@preact/signals-core": "^1.8.0",
"@toeverything/theme": "^1.1.12",
"@types/lodash-es": "^4.17.12",
"fflate": "^0.8.2",
"lit": "^3.2.0",
"lodash-es": "^4.17.21",
"rxjs": "^7.8.1"
},
"exports": {
".": "./src/index.ts",
"./effects": "./src/effects.ts",
"./view": "./src/view.ts"
},
"files": [
"src",
"dist",
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.21.0"
}

View File

@@ -0,0 +1,11 @@
import {
AFFINE_PAGE_DRAGGING_AREA_WIDGET,
AffinePageDraggingAreaWidget,
} from './index';
export function effects() {
customElements.define(
AFFINE_PAGE_DRAGGING_AREA_WIDGET,
AffinePageDraggingAreaWidget
);
}

View File

@@ -1,32 +1,27 @@
import { NoteBlockModel, RootBlockModel } from '@blocksuite/affine-model';
import type { RootBlockModel } from '@blocksuite/affine-model';
import { ViewportElementProvider } from '@blocksuite/affine-shared/services';
import {
autoScroll,
getScrollContainer,
matchModels,
} from '@blocksuite/affine-shared/utils';
import {
BLOCK_ID_ATTR,
BlockComponent,
BlockSelection,
type PointerEventState,
WidgetComponent,
WidgetViewExtension,
} from '@blocksuite/std';
import { html, nothing } from 'lit';
import { state } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
import { literal, unsafeStatic } from 'lit/static-html.js';
type Rect = {
left: number;
top: number;
width: number;
height: number;
};
type BlockInfo = {
element: BlockComponent;
rect: Rect;
};
import {
type BlockInfo,
getSelectingBlockPaths,
isDragArea,
type Rect,
} from './utils';
export const AFFINE_PAGE_DRAGGING_AREA_WIDGET =
'affine-page-dragging-area-widget';
@@ -323,129 +318,11 @@ export class AffinePageDraggingAreaWidget extends WidgetComponent<RootBlockModel
accessor rect: Rect | null = null;
}
function rectIntersects(a: Rect, b: Rect) {
return (
a.left < b.left + b.width &&
a.left + a.width > b.left &&
a.top < b.top + b.height &&
a.top + a.height > b.top
);
}
function rectIncludesTopAndBottom(a: Rect, b: Rect) {
return a.top <= b.top && a.top + a.height >= b.top + b.height;
}
function filterBlockInfos(blockInfos: BlockInfo[], userRect: Rect) {
const results: BlockInfo[] = [];
for (const blockInfo of blockInfos) {
const rect = blockInfo.rect;
if (userRect.top + userRect.height < rect.top) break;
results.push(blockInfo);
}
return results;
}
function filterBlockInfosByParent(
parentInfos: BlockInfo,
userRect: Rect,
filteredBlockInfos: BlockInfo[]
) {
const targetBlock = parentInfos.element;
let results = [parentInfos];
if (targetBlock.childElementCount > 0) {
const childBlockInfos = targetBlock.childBlocks
.map(el =>
filteredBlockInfos.find(
blockInfo => blockInfo.element.model.id === el.model.id
)
)
.filter(block => block) as BlockInfo[];
const firstIndex = childBlockInfos.findIndex(
bl => rectIntersects(bl.rect, userRect) && bl.rect.top < userRect.top
);
const lastIndex = childBlockInfos.findIndex(
bl =>
rectIntersects(bl.rect, userRect) &&
bl.rect.top + bl.rect.height > userRect.top + userRect.height
);
if (firstIndex !== -1 && lastIndex !== -1) {
results = childBlockInfos.slice(firstIndex, lastIndex + 1);
}
}
return results;
}
function getSelectingBlockPaths(blockInfos: BlockInfo[], userRect: Rect) {
const filteredBlockInfos = filterBlockInfos(blockInfos, userRect);
const len = filteredBlockInfos.length;
const blockPaths: string[] = [];
let singleTargetParentBlock: BlockInfo | null = null;
let blocks: BlockInfo[] = [];
if (len === 0) return blockPaths;
// To get the single target parent block info
for (const block of filteredBlockInfos) {
const rect = block.rect;
if (
rectIntersects(userRect, rect) &&
rectIncludesTopAndBottom(rect, userRect)
) {
singleTargetParentBlock = block;
}
}
if (singleTargetParentBlock) {
blocks = filterBlockInfosByParent(
singleTargetParentBlock,
userRect,
filteredBlockInfos
);
} else {
// If there is no block contains the top and bottom of the userRect
// Then get all the blocks that intersect with the userRect
for (const block of filteredBlockInfos) {
if (rectIntersects(userRect, block.rect)) {
blocks.push(block);
}
}
}
// Filter out the blocks which parent is in the blocks
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
const parent = blocks[i].element.doc.getParent(block.element.model);
const parentId = parent?.id;
if (parentId) {
const isParentInBlocks = blocks.some(
block => block.element.model.id === parentId
);
if (!isParentInBlocks) {
blockPaths.push(blocks[i].element.blockId);
}
}
}
return blockPaths;
}
function isDragArea(e: PointerEventState) {
const el = e.raw.target;
if (!(el instanceof Element)) {
return false;
}
const block = el.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
if (!block) {
return false;
}
return matchModels(block.model, [RootBlockModel, NoteBlockModel]);
}
export const pageDraggingAreaWidget = WidgetViewExtension(
'affine:page',
AFFINE_PAGE_DRAGGING_AREA_WIDGET,
literal`${unsafeStatic(AFFINE_PAGE_DRAGGING_AREA_WIDGET)}`
);
declare global {
interface HTMLElementTagNameMap {

View File

@@ -0,0 +1,146 @@
import { NoteBlockModel, RootBlockModel } from '@blocksuite/affine-model';
import { matchModels } from '@blocksuite/affine-shared/utils';
import {
BLOCK_ID_ATTR,
type BlockComponent,
type PointerEventState,
} from '@blocksuite/std';
export type Rect = {
left: number;
top: number;
width: number;
height: number;
};
export type BlockInfo = {
element: BlockComponent;
rect: Rect;
};
function rectIntersects(a: Rect, b: Rect) {
return (
a.left < b.left + b.width &&
a.left + a.width > b.left &&
a.top < b.top + b.height &&
a.top + a.height > b.top
);
}
function rectIncludesTopAndBottom(a: Rect, b: Rect) {
return a.top <= b.top && a.top + a.height >= b.top + b.height;
}
function filterBlockInfos(blockInfos: BlockInfo[], userRect: Rect) {
const results: BlockInfo[] = [];
for (const blockInfo of blockInfos) {
const rect = blockInfo.rect;
if (userRect.top + userRect.height < rect.top) break;
results.push(blockInfo);
}
return results;
}
function filterBlockInfosByParent(
parentInfos: BlockInfo,
userRect: Rect,
filteredBlockInfos: BlockInfo[]
) {
const targetBlock = parentInfos.element;
let results = [parentInfos];
if (targetBlock.childElementCount > 0) {
const childBlockInfos = targetBlock.childBlocks
.map(el =>
filteredBlockInfos.find(
blockInfo => blockInfo.element.model.id === el.model.id
)
)
.filter(block => block) as BlockInfo[];
const firstIndex = childBlockInfos.findIndex(
bl => rectIntersects(bl.rect, userRect) && bl.rect.top < userRect.top
);
const lastIndex = childBlockInfos.findIndex(
bl =>
rectIntersects(bl.rect, userRect) &&
bl.rect.top + bl.rect.height > userRect.top + userRect.height
);
if (firstIndex !== -1 && lastIndex !== -1) {
results = childBlockInfos.slice(firstIndex, lastIndex + 1);
}
}
return results;
}
export function getSelectingBlockPaths(
blockInfos: BlockInfo[],
userRect: Rect
) {
const filteredBlockInfos = filterBlockInfos(blockInfos, userRect);
const len = filteredBlockInfos.length;
const blockPaths: string[] = [];
let singleTargetParentBlock: BlockInfo | null = null;
let blocks: BlockInfo[] = [];
if (len === 0) return blockPaths;
// To get the single target parent block info
for (const block of filteredBlockInfos) {
const rect = block.rect;
if (
rectIntersects(userRect, rect) &&
rectIncludesTopAndBottom(rect, userRect)
) {
singleTargetParentBlock = block;
}
}
if (singleTargetParentBlock) {
blocks = filterBlockInfosByParent(
singleTargetParentBlock,
userRect,
filteredBlockInfos
);
} else {
// If there is no block contains the top and bottom of the userRect
// Then get all the blocks that intersect with the userRect
for (const block of filteredBlockInfos) {
if (rectIntersects(userRect, block.rect)) {
blocks.push(block);
}
}
}
// Filter out the blocks which parent is in the blocks
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
const parent = blocks[i].element.doc.getParent(block.element.model);
const parentId = parent?.id;
if (parentId) {
const isParentInBlocks = blocks.some(
block => block.element.model.id === parentId
);
if (!isParentInBlocks) {
blockPaths.push(blocks[i].element.blockId);
}
}
}
return blockPaths;
}
export function isDragArea(e: PointerEventState) {
const el = e.raw.target;
if (!(el instanceof Element)) {
return false;
}
const block = el.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
if (!block) {
return false;
}
return matchModels(block.model, [RootBlockModel, NoteBlockModel]);
}

View File

@@ -0,0 +1,24 @@
import {
type ViewExtensionContext,
ViewExtensionProvider,
} from '@blocksuite/affine-ext-loader';
import { effects } from './effects';
import { pageDraggingAreaWidget } from './index';
export class PageDraggingAreaViewExtension extends ViewExtensionProvider {
override name = 'affine-page-dragging-area-widget';
override effect() {
super.effect();
effects();
}
override setup(context: ViewExtensionContext) {
super.setup(context);
if (this.isEdgeless(context.scope)) {
return;
}
context.register(pageDraggingAreaWidget);
}
}

View File

@@ -0,0 +1,18 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"tsBuildInfoFile": "./dist/tsconfig.tsbuildinfo"
},
"include": ["./src"],
"references": [
{ "path": "../../components" },
{ "path": "../../ext-loader" },
{ "path": "../../model" },
{ "path": "../../shared" },
{ "path": "../../../framework/global" },
{ "path": "../../../framework/std" },
{ "path": "../../../framework/store" }
]
}

View File

@@ -56,6 +56,7 @@ export const PackageList = [
'blocksuite/affine/widgets/frame-title',
'blocksuite/affine/widgets/keyboard-toolbar',
'blocksuite/affine/widgets/linked-doc',
'blocksuite/affine/widgets/page-dragging-area',
'blocksuite/affine/widgets/remote-selection',
'blocksuite/affine/widgets/scroll-anchoring',
'blocksuite/affine/widgets/slash-menu',
@@ -899,6 +900,19 @@ export const PackageList = [
'blocksuite/framework/store',
],
},
{
location: 'blocksuite/affine/widgets/page-dragging-area',
name: '@blocksuite/affine-widget-page-dragging-area',
workspaceDependencies: [
'blocksuite/affine/components',
'blocksuite/affine/ext-loader',
'blocksuite/affine/model',
'blocksuite/affine/shared',
'blocksuite/framework/global',
'blocksuite/framework/std',
'blocksuite/framework/store',
],
},
{
location: 'blocksuite/affine/widgets/remote-selection',
name: '@blocksuite/affine-widget-remote-selection',
@@ -1386,6 +1400,7 @@ export type PackageName =
| '@blocksuite/affine-widget-frame-title'
| '@blocksuite/affine-widget-keyboard-toolbar'
| '@blocksuite/affine-widget-linked-doc'
| '@blocksuite/affine-widget-page-dragging-area'
| '@blocksuite/affine-widget-remote-selection'
| '@blocksuite/affine-widget-scroll-anchoring'
| '@blocksuite/affine-widget-slash-menu'

View File

@@ -103,6 +103,7 @@
{ "path": "./blocksuite/affine/widgets/frame-title" },
{ "path": "./blocksuite/affine/widgets/keyboard-toolbar" },
{ "path": "./blocksuite/affine/widgets/linked-doc" },
{ "path": "./blocksuite/affine/widgets/page-dragging-area" },
{ "path": "./blocksuite/affine/widgets/remote-selection" },
{ "path": "./blocksuite/affine/widgets/scroll-anchoring" },
{ "path": "./blocksuite/affine/widgets/slash-menu" },

View File

@@ -3861,6 +3861,29 @@ __metadata:
languageName: unknown
linkType: soft
"@blocksuite/affine-widget-page-dragging-area@workspace:*, @blocksuite/affine-widget-page-dragging-area@workspace:blocksuite/affine/widgets/page-dragging-area":
version: 0.0.0-use.local
resolution: "@blocksuite/affine-widget-page-dragging-area@workspace:blocksuite/affine/widgets/page-dragging-area"
dependencies:
"@blocksuite/affine-components": "workspace:*"
"@blocksuite/affine-ext-loader": "workspace:*"
"@blocksuite/affine-model": "workspace:*"
"@blocksuite/affine-shared": "workspace:*"
"@blocksuite/global": "workspace:*"
"@blocksuite/icons": "npm:^2.2.12"
"@blocksuite/std": "workspace:*"
"@blocksuite/store": "workspace:*"
"@lit/context": "npm:^1.1.2"
"@preact/signals-core": "npm:^1.8.0"
"@toeverything/theme": "npm:^1.1.12"
"@types/lodash-es": "npm:^4.17.12"
fflate: "npm:^0.8.2"
lit: "npm:^3.2.0"
lodash-es: "npm:^4.17.21"
rxjs: "npm:^7.8.1"
languageName: unknown
linkType: soft
"@blocksuite/affine-widget-remote-selection@workspace:*, @blocksuite/affine-widget-remote-selection@workspace:blocksuite/affine/widgets/remote-selection":
version: 0.0.0-use.local
resolution: "@blocksuite/affine-widget-remote-selection@workspace:blocksuite/affine/widgets/remote-selection"
@@ -4020,6 +4043,7 @@ __metadata:
"@blocksuite/affine-widget-frame-title": "workspace:*"
"@blocksuite/affine-widget-keyboard-toolbar": "workspace:*"
"@blocksuite/affine-widget-linked-doc": "workspace:*"
"@blocksuite/affine-widget-page-dragging-area": "workspace:*"
"@blocksuite/affine-widget-remote-selection": "workspace:*"
"@blocksuite/affine-widget-scroll-anchoring": "workspace:*"
"@blocksuite/affine-widget-slash-menu": "workspace:*"