feat(editor): unify block props api (#10888)

Closes: [BS-2707](https://linear.app/affine-design/issue/BS-2707/统一使用props获取和更新block-prop)
This commit is contained in:
Saul-Mirone
2025-03-16 05:48:34 +00:00
parent 8f9e5bf0aa
commit 26285f7dcb
193 changed files with 1019 additions and 891 deletions
@@ -25,7 +25,7 @@ const handlePoint = (
if (isRootDraftModel(model)) {
if (length === 0) return;
(snapshot.props.title as Record<string, unknown>).delta =
model.title.sliceToDelta(index, length + index);
model.props.title.sliceToDelta(index, length + index);
return;
}
@@ -485,7 +485,7 @@ class PasteTr {
if (this.firstSnapshot!.props.type) {
this.firstSnapshot!.props.type = (
this.pointState.model as ParagraphBlockModel
).type;
).props.type;
}
}
@@ -19,10 +19,10 @@ export const replaceIdMiddleware =
payload.snapshot.flavour === 'affine:database'
) {
const model = payload.model as DatabaseBlockModel;
Object.keys(model.cells).forEach(cellId => {
Object.keys(model.props.cells).forEach(cellId => {
if (idMap.has(cellId)) {
model.cells[idMap.get(cellId)!] = model.cells[cellId];
delete model.cells[cellId];
model.props.cells[idMap.get(cellId)!] = model.props.cells[cellId];
delete model.props.cells[cellId];
}
});
}
@@ -35,7 +35,7 @@ export const replaceIdMiddleware =
const model = payload.model as ParagraphBlockModel | ListBlockModel;
let prev = 0;
const delta: DeltaOperation[] = [];
for (const d of model.text.toDelta()) {
for (const d of model.props.text.toDelta()) {
if (d.attributes?.reference?.pageId) {
const newId = idMap.get(d.attributes.reference.pageId);
if (!newId) {
@@ -62,7 +62,7 @@ export const replaceIdMiddleware =
}
}
if (delta.length > 0) {
model.text.applyDelta(delta);
model.props.text.applyDelta(delta);
}
}
@@ -71,20 +71,20 @@ export const replaceIdMiddleware =
payload.snapshot.flavour === 'affine:surface-ref'
) {
const model = payload.model as SurfaceRefBlockModel;
const original = model.reference;
const original = model.props.reference;
// If there exists a replacement, replace the reference with the new id.
// Otherwise,
// 1. If the reference is an affine:frame not in doc, generate a new id.
// 2. If the reference is graph, keep the original id.
if (idMap.has(original)) {
model.reference = idMap.get(original)!;
model.props.reference = idMap.get(original)!;
} else if (
model.refFlavour === 'affine:frame' &&
model.props.refFlavour === 'affine:frame' &&
!model.doc.hasBlock(original)
) {
const newId = idGenerator();
idMap.set(original, newId);
model.reference = newId;
model.props.reference = newId;
}
}
@@ -98,16 +98,16 @@ export const replaceIdMiddleware =
const model = payload.model as
| EmbedLinkedDocModel
| EmbedSyncedDocModel;
const original = model.pageId;
const original = model.props.pageId;
// If the pageId is not in the doc, generate a new id.
// If we already have a replacement, use it.
if (!docCRUD.get(original)) {
if (idMap.has(original)) {
model.pageId = idMap.get(original)!;
model.props.pageId = idMap.get(original)!;
} else {
const newId = idGenerator();
idMap.set(original, newId);
model.pageId = newId;
model.props.pageId = newId;
}
}
}
@@ -59,13 +59,6 @@ export class BlockMetaService extends StoreExtension {
const now = getNow();
this.store.withoutTransact(() => {
const isFlatModel = model.schema.model.isFlatData;
if (!isFlatModel) {
model['meta:createdAt'] = now;
model['meta:createdBy'] = writer.id;
return;
}
model.props['meta:createdAt'] = now;
model.props['meta:createdBy'] = writer.id;
});
@@ -81,19 +74,6 @@ export class BlockMetaService extends StoreExtension {
const now = getNow();
this.store.withoutTransact(() => {
const isFlatModel = model.schema.model.isFlatData;
if (!isFlatModel) {
model['meta:updatedAt'] = now;
model['meta:updatedBy'] = writer.id;
if (!model['meta:createdAt']) {
model['meta:createdAt'] = now;
}
if (!model['meta:createdBy']) {
model['meta:createdBy'] = writer.id;
}
return;
}
model.props['meta:updatedAt'] = now;
model.props['meta:updatedBy'] = writer.id;
if (!model.props['meta:createdAt']) {
@@ -16,10 +16,10 @@ export function calculateCollapsedSiblings(
if (
i > index &&
matchModels(child, [ParagraphBlockModel]) &&
child.type.startsWith('h')
child.props.type.startsWith('h')
) {
const modelLevel = parseInt(model.type.slice(1));
const childLevel = parseInt(child.type.slice(1));
const modelLevel = parseInt(model.props.type.slice(1));
const childLevel = parseInt(child.props.type.slice(1));
return childLevel <= modelLevel;
}
return false;
@@ -47,7 +47,7 @@ export function getNearestHeadingBefore(
const sibling = parent.children[i];
if (
matchModels(sibling, [ParagraphBlockModel]) &&
sibling.type.startsWith('h')
sibling.props.type.startsWith('h')
) {
return sibling;
}
@@ -49,7 +49,7 @@ export function getLastNoteBlock(doc: Store) {
const child = children[i];
if (
matchModels(child, [NoteBlockModel]) &&
child.displayMode !== NoteDisplayMode.EdgelessOnly
child.props.displayMode !== NoteDisplayMode.EdgelessOnly
) {
note = child as NoteBlockModel;
break;
@@ -65,7 +65,7 @@ export function getFirstNoteBlock(doc: Store) {
for (const child of children) {
if (
matchModels(child, [NoteBlockModel]) &&
child.displayMode !== NoteDisplayMode.EdgelessOnly
child.props.displayMode !== NoteDisplayMode.EdgelessOnly
) {
note = child as NoteBlockModel;
break;
@@ -23,7 +23,7 @@ export function getNextContinuousNumberedLists(
const firstNotNumberedListIndex = parent.children.findIndex(
(model, i) =>
i > modelIndex &&
(!matchModels(model, [ListBlockModel]) || model.type !== 'numbered')
(!matchModels(model, [ListBlockModel]) || model.props.type !== 'numbered')
);
const newContinuousLists = parent.children.slice(
modelIndex + 1,
@@ -31,7 +31,8 @@ export function getNextContinuousNumberedLists(
);
if (
!newContinuousLists.every(
model => matchModels(model, [ListBlockModel]) && model.type === 'numbered'
model =>
matchModels(model, [ListBlockModel]) && model.props.type === 'numbered'
)
)
return [];
@@ -56,11 +57,11 @@ export function toNumberedList(
if (
prevSibling &&
matchModels(prevSibling, [ListBlockModel]) &&
prevSibling.type === 'numbered'
prevSibling.props.type === 'numbered'
) {
doc.transact(() => {
if (!prevSibling.order) prevSibling.order = 1;
realOrder = prevSibling.order + 1;
if (!prevSibling.props.order) prevSibling.props.order = 1;
realOrder = prevSibling.props.order + 1;
});
}
@@ -93,7 +94,7 @@ export function toNumberedList(
let base = realOrder + 1;
nextContinuousNumberedLists.forEach(list => {
doc.transact(() => {
list.order = base;
list.props.order = base;
});
base += 1;
});