feat(editor): improve api for store, and add docs (#10941)

This commit is contained in:
Saul-Mirone
2025-03-17 16:30:59 +00:00
parent b0aa2c90fd
commit 3de7d85eea
47 changed files with 1212 additions and 210 deletions

View File

@@ -466,7 +466,7 @@ export class DatabaseBlockDataSource extends DataSourceBase {
}
rowMove(rowId: string, position: InsertToPosition): void {
const model = this.doc.getBlockById(rowId);
const model = this.doc.getModelById(rowId);
if (model) {
const index = insertPositionToIndex(position, this._model.children);
const target = this._model.children[index];

View File

@@ -68,7 +68,7 @@ export async function uploadBlobForImage(
} finally {
setImageUploaded(blockId);
const imageModel = doc.getBlockById(blockId) as ImageBlockModel | null;
const imageModel = doc.getModelById(blockId) as ImageBlockModel | null;
if (sourceId && imageModel) {
const props: Partial<ImageBlockProps> = {
sourceId,

View File

@@ -84,7 +84,7 @@ export const updateBlockType: Command<
if (flavour !== 'affine:code') return;
const id = mergeToCodeModel(blockModels);
if (!id) return;
const model = doc.getBlockById(id);
const model = doc.getModelById(id);
if (!model) return;
asyncSetInlineRange(host, model, {
index: model.text?.length ?? 0,
@@ -115,7 +115,7 @@ export const updateBlockType: Command<
nextSiblingId = doc.addBlock('affine:paragraph', {}, parent);
}
focusTextModel(host.std, nextSiblingId);
const newModel = doc.getBlockById(id);
const newModel = doc.getModelById(id);
if (!newModel) {
return next({ updatedBlocks: [] });
}
@@ -217,7 +217,7 @@ export const updateBlockType: Command<
if (!newId) {
return;
}
const newModel = doc.getBlockById(newId);
const newModel = doc.getModelById(newId);
if (newModel) {
newModels.push(newModel);
}

View File

@@ -1091,7 +1091,7 @@ export class EdgelessClipboardController extends PageClipboard {
const newSelected = [
...canvasElementIds,
...blockIds.filter(id => {
return isTopLevelBlock(this.doc.getBlockById(id));
return isTopLevelBlock(this.doc.getModelById(id));
}),
];

View File

@@ -380,7 +380,7 @@ export class EdgelessAutoComplete extends WithDisposable(LitElement) {
this.edgeless
);
} else {
const model = doc.getBlockById(id);
const model = doc.getModelById(id);
if (!model) {
return;
}

View File

@@ -182,7 +182,7 @@ export class NoteSlicer extends WidgetComponent<
doc.root?.id
);
doc.moveBlocks(resetBlocks, doc.getBlockById(newNoteId) as NoteBlockModel);
doc.moveBlocks(resetBlocks, doc.getModelById(newNoteId) as NoteBlockModel);
this._activeSlicerIndex = 0;
this._selection.set({

View File

@@ -188,7 +188,7 @@ export class TemplateJob {
if (isMergeBlock) {
this._mergeProps(
json,
this.model.doc.getBlockById(
this.model.doc.getModelById(
this._getMergeBlockId(json)
) as BlockModel
);

View File

@@ -191,7 +191,7 @@ export class PageRootBlockComponent extends BlockComponent<
const { doc } = this;
const noteId = doc.addBlock('affine:note', {}, doc.root?.id);
return doc.getBlockById(noteId) as NoteBlockModel;
return doc.getModelById(noteId) as NoteBlockModel;
}
private _getDefaultNoteBlock() {
@@ -262,7 +262,7 @@ export class PageRootBlockComponent extends BlockComponent<
);
if (!sel) return;
let model: BlockModel | null = null;
let current = this.doc.getBlockById(sel.blockId);
let current = this.doc.getModelById(sel.blockId);
while (current && !model) {
if (current.flavour === 'affine:note') {
model = current;
@@ -280,7 +280,7 @@ export class PageRootBlockComponent extends BlockComponent<
}
return;
}
const notes = this.doc.getBlockByFlavour('affine:note');
const notes = this.doc.getModelsByFlavour('affine:note');
const index = notes.indexOf(prevNote);
if (index !== 0) return;

View File

@@ -281,7 +281,7 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
flavour: 'affine:paragraph',
},
]);
const model = this.doc.getBlockById(paragraphId);
const model = this.doc.getModelById(paragraphId);
if (!model) return;
requestConnectedFrame(() => {

View File

@@ -129,7 +129,7 @@ export class EdgelessCRUDExtension extends Extension {
return;
}
const block = this.std.store.getBlockById(id);
const block = this.std.store.getModelById(id);
if (block) {
const key = getLastPropsKey(block.flavour, {
...block.yBlock.toJSON(),
@@ -145,7 +145,7 @@ export class EdgelessCRUDExtension extends Extension {
if (!surface) {
return null;
}
const el = surface.getElementById(id) ?? this.std.store.getBlockById(id);
const el = surface.getElementById(id) ?? this.std.store.getModelById(id);
return el as GfxModel | null;
}

View File

@@ -602,7 +602,7 @@ function getNotesInFrameBound(
): NoteBlockModel[] {
const bound = Bound.deserialize(frame.xywh);
return (doc.getBlockByFlavour('affine:note') as NoteBlockModel[]).filter(
return (doc.getModelsByFlavour('affine:note') as NoteBlockModel[]).filter(
ele => {
const xywh = Bound.deserialize(ele.xywh);

View File

@@ -32,7 +32,7 @@ export function mindmap(
const { connector, outdated } = result;
const elementGetter = (id: string) =>
model.surface.getElementById(id) ??
(model.surface.doc.getBlockById(id) as GfxModel);
(model.surface.doc.getModelById(id) as GfxModel);
if (outdated) {
ConnectorPathGenerator.updatePath(connector, null, elementGetter);

View File

@@ -25,7 +25,7 @@ export class SurfaceBlockService extends BlockService {
const disposable = this.doc.slots.blockUpdated.subscribe(payload => {
if (payload.flavour === 'affine:surface') {
disposable.unsubscribe();
const surface = this.doc.getBlockById(
const surface = this.doc.getModelById(
payload.id
) as SurfaceBlockModel | null;
if (!surface) return;

View File

@@ -100,7 +100,7 @@ export function addNote(
noteId
);
if (options.collapse && height > NOTE_MIN_HEIGHT) {
const note = doc.getBlockById(noteId) as NoteBlockModel;
const note = doc.getModelById(noteId) as NoteBlockModel;
doc.updateBlock(note, () => {
note.props.edgeless.collapse = true;
note.props.edgeless.collapsedHeight = height;

View File

@@ -8,9 +8,9 @@ export const connectorWatcher: SurfaceMiddleware = (
surface: SurfaceBlockModel
) => {
const hasElementById = (id: string) =>
surface.hasElementById(id) || surface.doc.hasBlockById(id);
surface.hasElementById(id) || surface.doc.hasBlock(id);
const elementGetter = (id: string) =>
surface.getElementById(id) ?? (surface.doc.getBlockById(id) as GfxModel);
surface.getElementById(id) ?? (surface.doc.getModelById(id) as GfxModel);
const updateConnectorPath = (connector: ConnectorElementModel) => {
if (
((connector.source?.id && hasElementById(connector.source.id)) ||