mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 01:09:54 +08:00
feat(editor): improve block meta updated event handler (#10815)
This commit is contained in:
@@ -5,6 +5,8 @@ import {
|
|||||||
type Text,
|
type Text,
|
||||||
} from '@blocksuite/store';
|
} from '@blocksuite/store';
|
||||||
|
|
||||||
|
import type { BlockMeta } from '../../utils/types';
|
||||||
|
|
||||||
export type ParagraphType =
|
export type ParagraphType =
|
||||||
| 'text'
|
| 'text'
|
||||||
| 'quote'
|
| 'quote'
|
||||||
@@ -19,7 +21,7 @@ export type ParagraphProps = {
|
|||||||
type: ParagraphType;
|
type: ParagraphType;
|
||||||
text: Text;
|
text: Text;
|
||||||
collapsed: boolean;
|
collapsed: boolean;
|
||||||
};
|
} & BlockMeta;
|
||||||
|
|
||||||
export const ParagraphBlockSchema = defineBlockSchema({
|
export const ParagraphBlockSchema = defineBlockSchema({
|
||||||
flavour: 'affine:paragraph',
|
flavour: 'affine:paragraph',
|
||||||
@@ -27,6 +29,10 @@ export const ParagraphBlockSchema = defineBlockSchema({
|
|||||||
type: 'text',
|
type: 'text',
|
||||||
text: internal.Text(),
|
text: internal.Text(),
|
||||||
collapsed: false,
|
collapsed: false,
|
||||||
|
'meta:createdAt': undefined,
|
||||||
|
'meta:createdBy': undefined,
|
||||||
|
'meta:updatedAt': undefined,
|
||||||
|
'meta:updatedBy': undefined,
|
||||||
}),
|
}),
|
||||||
metadata: {
|
metadata: {
|
||||||
version: 1,
|
version: 1,
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
import type { BlockMeta } from '@blocksuite/affine-model';
|
import type { BlockMeta } from '@blocksuite/affine-model';
|
||||||
import { type BlockModel, StoreExtension } from '@blocksuite/store';
|
import { type BlockModel, StoreExtension } from '@blocksuite/store';
|
||||||
|
import { filter, groupBy, mergeMap, throttleTime } from 'rxjs/operators';
|
||||||
|
|
||||||
import { FeatureFlagService } from './feature-flag-service';
|
import { FeatureFlagService } from './feature-flag-service';
|
||||||
import { WriterInfoProvider } from './user-service';
|
import { WriterInfoProvider } from './user-service';
|
||||||
|
|
||||||
|
// 30 seconds
|
||||||
|
const BLOCK_META_THROTTLE_TIME = 30 * 1000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The service is used to add following info to the block.
|
* The service is used to add following info to the block.
|
||||||
* - createdAt: The time when the block is created.
|
* - createdAt: The time when the block is created.
|
||||||
@@ -21,20 +25,27 @@ export class BlockMetaService extends StoreExtension {
|
|||||||
|
|
||||||
override loaded() {
|
override loaded() {
|
||||||
this.store.disposableGroup.add(
|
this.store.disposableGroup.add(
|
||||||
this.store.slots.blockUpdated.subscribe(({ type, id }) => {
|
this.store.slots.blockUpdated
|
||||||
if (!this.isBlockMetaEnabled) return;
|
.pipe(
|
||||||
|
filter(payload => payload.isLocal),
|
||||||
|
groupBy(payload => `${payload.type}-${payload.id}`),
|
||||||
|
mergeMap(group => group.pipe(throttleTime(BLOCK_META_THROTTLE_TIME)))
|
||||||
|
)
|
||||||
|
.subscribe(payload => {
|
||||||
|
const { type, id } = payload;
|
||||||
|
if (!this.isBlockMetaEnabled) return;
|
||||||
|
|
||||||
const model = this.store.getBlock(id)?.model;
|
const model = this.store.getBlock(id)?.model;
|
||||||
if (!model) return;
|
if (!model) return;
|
||||||
|
|
||||||
if (type === 'add') {
|
if (type === 'add') {
|
||||||
return this._onBlockCreated(model);
|
return this._onBlockCreated(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'update') {
|
if (type === 'update') {
|
||||||
return this._onBlockUpdated(model);
|
return this._onBlockUpdated(model);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,6 +87,8 @@ export class BlockMetaService extends StoreExtension {
|
|||||||
model['meta:updatedBy'] = writer.id;
|
model['meta:updatedBy'] = writer.id;
|
||||||
if (!model['meta:createdAt']) {
|
if (!model['meta:createdAt']) {
|
||||||
model['meta:createdAt'] = now;
|
model['meta:createdAt'] = now;
|
||||||
|
}
|
||||||
|
if (!model['meta:createdBy']) {
|
||||||
model['meta:createdBy'] = writer.id;
|
model['meta:createdBy'] = writer.id;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -85,6 +98,8 @@ export class BlockMetaService extends StoreExtension {
|
|||||||
model.props['meta:updatedBy'] = writer.id;
|
model.props['meta:updatedBy'] = writer.id;
|
||||||
if (!model.props['meta:createdAt']) {
|
if (!model.props['meta:createdAt']) {
|
||||||
model.props['meta:createdAt'] = now;
|
model.props['meta:createdAt'] = now;
|
||||||
|
}
|
||||||
|
if (!model.props['meta:createdBy']) {
|
||||||
model.props['meta:createdBy'] = writer.id;
|
model.props['meta:createdBy'] = writer.id;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user