mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
refactor(editor): remove legacy service watcher (#10455)
The main changes in this PR involve replacing the deprecated `BlockServiceWatcher` with the new `LifeCycleWatcher` across multiple files. Here's a detailed breakdown:
1. **Core Architectural Change:**
- Removed `BlockServiceWatcher` class completely (deleted file)
- Migrated to `LifeCycleWatcher` as the new standard for watching component lifecycle events
2. **Key Changes in Implementation:**
- Changed from using `blockService.specSlots` events to using `view.viewUpdated` events
- Replaced `flavour` static property with `key` static property
- Updated event handling to use more specific payload type checking
3. **Major File Changes:**
- Modified multiple block components:
- Embed synced doc block
- Frame preview
- Edgeless root spec
- AI-related components (code, image, paragraph, etc.)
- Quick search service
- Edgeless clipboard
4. **Pattern of Changes:**
The migration follows a consistent pattern:
```typescript
// Old pattern
class SomeWatcher extends BlockServiceWatcher {
static override readonly flavour = 'some:flavour';
mounted() {
this.blockService.specSlots.viewConnected.on(...)
}
}
// New pattern
class SomeWatcher extends LifeCycleWatcher {
static override key = 'some-watcher';
mounted() {
const { view } = this.std;
view.viewUpdated.on(payload => {
if (payload.type !== 'block' || payload.method !== 'add') return;
// Handle event
});
}
}
```
5. **Benefits:**
- More explicit and type-safe event handling
- Cleaner architecture by removing deprecated code
- More consistent approach to lifecycle management
- Better separation of concerns
This appears to be a significant architectural improvement that modernizes the codebase by removing deprecated patterns and standardizing on a more robust lifecycle management system.
This commit is contained in:
@@ -2,16 +2,14 @@ import { Skeleton } from '@affine/component';
|
||||
import type { EditorSettingSchema } from '@affine/core/modules/editor-setting';
|
||||
import { EditorSettingService } from '@affine/core/modules/editor-setting';
|
||||
import type { EditorHost } from '@blocksuite/affine/block-std';
|
||||
import {
|
||||
BlockServiceIdentifier,
|
||||
BlockStdScope,
|
||||
} from '@blocksuite/affine/block-std';
|
||||
import { BlockStdScope } from '@blocksuite/affine/block-std';
|
||||
import {
|
||||
GfxControllerIdentifier,
|
||||
type GfxPrimitiveElementModel,
|
||||
} from '@blocksuite/affine/block-std/gfx';
|
||||
import {
|
||||
EdgelessCRUDIdentifier,
|
||||
type EdgelessRootPreviewBlockComponent,
|
||||
SpecProvider,
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import { Bound } from '@blocksuite/affine/global/utils';
|
||||
@@ -93,14 +91,18 @@ export const EdgelessSnapshot = (props: Props) => {
|
||||
}
|
||||
|
||||
// refresh viewport
|
||||
const edgelessService = editorHost.std.get(
|
||||
BlockServiceIdentifier('affine:page')
|
||||
);
|
||||
const gfx = editorHost.std.get(GfxControllerIdentifier);
|
||||
edgelessService.specSlots.viewConnected.once(({ component }) => {
|
||||
const edgelessBlock = component as any;
|
||||
const disposable = editorHost.std.view.viewUpdated.on(payload => {
|
||||
if (
|
||||
payload.type !== 'block' ||
|
||||
payload.method !== 'add' ||
|
||||
payload.view.model.flavour !== 'affine:page'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const component = payload.view as EdgelessRootPreviewBlockComponent;
|
||||
doc.readonly = false;
|
||||
edgelessBlock.editorViewportSelector = 'ref-viewport';
|
||||
component.editorViewportSelector = 'ref-viewport';
|
||||
const frame = getFrameBlock(doc);
|
||||
if (frame && docName !== 'frame') {
|
||||
// docName with value 'frame' shouldn't be deleted, it is a part of frame settings
|
||||
@@ -110,6 +112,7 @@ export const EdgelessSnapshot = (props: Props) => {
|
||||
const bound = boundMap.get(docName);
|
||||
bound && gfx.viewport.setViewportByBound(bound);
|
||||
doc.readonly = true;
|
||||
disposable.dispose();
|
||||
});
|
||||
|
||||
// append to dom node
|
||||
|
||||
Reference in New Issue
Block a user