Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blocks): add scroll anchoring widget #8378

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions .github/snapshot-changeset.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
---
'@blocksuite/global': patch
'@blocksuite/affine-block-embed': patch
'@blocksuite/affine-block-list': patch
'@blocksuite/affine-block-paragraph': patch
'@blocksuite/affine-block-surface': patch
'@blocksuite/affine-components': patch
'@blocksuite/affine-data-view': patch
'@blocksuite/affine-model': patch
'@blocksuite/affine-shared': patch
'@blocksuite/affine-widget-scroll-anchoring': patch
fundon marked this conversation as resolved.
Show resolved Hide resolved
'@blocksuite/blocks': patch
'@blocksuite/block-std': patch
'@blocksuite/store': patch
'@blocksuite/global': patch
'@blocksuite/inline': patch
'@blocksuite/store': patch
'@blocksuite/sync': patch
'@blocksuite/affine-shared': patch
'@blocksuite/affine-model': patch
'@blocksuite/affine-components': patch
'@blocksuite/affine-block-paragraph': patch
'@blocksuite/affine-block-list': patch
'@blocksuite/blocks': patch
'@blocksuite/presets': patch
---

Expand Down
59 changes: 59 additions & 0 deletions packages/affine/shared/src/selection/hightlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
type ReferenceParams,
ReferenceParamsSchema,
} from '@blocksuite/affine-model';
import { BaseSelection } from '@blocksuite/block-std';

export class HighlightSelection extends BaseSelection {
static override group = 'scene';

static override type = 'highlight';

readonly blockIds: string[] = [];

readonly elementIds: string[] = [];

readonly mode: 'page' | 'edgeless' = 'page';

constructor({ mode, blockIds, elementIds }: ReferenceParams) {
super({ blockId: '[scene-highlight]' });

this.mode = mode ?? 'page';
this.blockIds = blockIds ?? [];
this.elementIds = elementIds ?? [];
}

static override fromJSON(json: Record<string, unknown>): HighlightSelection {
const result = ReferenceParamsSchema.parse(json);
return new HighlightSelection(result);
}

override equals(other: HighlightSelection): boolean {
return (
this.mode === other.mode &&
this.blockId === other.blockId &&
this.blockIds.length === other.blockIds.length &&
this.elementIds.length === other.elementIds.length &&
this.blockIds.every((id, n) => id === other.blockIds[n]) &&
this.elementIds.every((id, n) => id === other.elementIds[n])
);
}

override toJSON(): Record<string, unknown> {
return {
type: 'highlight',
mode: this.mode,
blockId: this.blockId,
blockIds: this.blockIds,
elementIds: this.elementIds,
};
}
}

declare global {
namespace BlockSuite {
interface Selection {
highlight: typeof HighlightSelection;
}
}
}
6 changes: 2 additions & 4 deletions packages/affine/shared/src/selection/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ export class ImageSelection extends BaseSelection {
static override type = 'image';

static override fromJSON(json: Record<string, unknown>): ImageSelection {
ImageSelectionSchema.parse(json);
return new ImageSelection({
blockId: json.blockId as string,
});
const result = ImageSelectionSchema.parse(json);
return new ImageSelection(result);
}

override equals(other: BaseSelection): boolean {
Expand Down
1 change: 1 addition & 0 deletions packages/affine/shared/src/selection/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { HighlightSelection } from './hightlight.js';
export { ImageSelection } from './image.js';
51 changes: 51 additions & 0 deletions packages/affine/widget-scroll-anchoring/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@blocksuite/affine-widget-scroll-anchoring",
"version": "0.17.10",
"description": "Affine scroll anchroing widget.",
"type": "module",
"repository": "toeverything/blocksuite",
"scripts": {
"build": "tsc",
"test:unit": "nx vite:test --run --passWithNoTests",
"test:unit:coverage": "nx vite:test --run --coverage",
"test:e2e": "playwright test"
},
"sideEffects": false,
"keywords": [],
"author": "toeverything",
"license": "MPL-2.0",
"dependencies": {
"@blocksuite/affine-model": "workspace:*",
"@blocksuite/affine-shared": "workspace:*",
"@blocksuite/block-std": "workspace:*",
"@blocksuite/global": "workspace:*",
"@preact/signals-core": "^1.8.0",
"@toeverything/theme": "^1.0.8",
"lit": "^3.2.0"
},
"exports": {
".": "./src/index.ts",
"./effects": "./src/effects.ts"
},
"publishConfig": {
"access": "public",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./effects": {
"import": "./dist/effects.js",
"types": "./dist/effects.d.ts"
}
}
},
"files": [
"src",
"dist",
"!src/__tests__",
"!dist/__tests__"
]
}
17 changes: 17 additions & 0 deletions packages/affine/widget-scroll-anchoring/src/effects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
AFFINE_SCROLL_ANCHORING_WIDGET,
AffineScrollAnchoringWidget,
} from './scroll-anchoring.js';

export function effects() {
customElements.define(
AFFINE_SCROLL_ANCHORING_WIDGET,
AffineScrollAnchoringWidget
);
}

declare global {
interface HTMLElementTagNameMap {
[AFFINE_SCROLL_ANCHORING_WIDGET]: AffineScrollAnchoringWidget;
}
}
1 change: 1 addition & 0 deletions packages/affine/widget-scroll-anchoring/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './scroll-anchoring.js';
Loading
Loading