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
@@ -82,7 +82,7 @@ describe('DatabaseManager', () => {
noteBlockId
);
const databaseModel = doc.getBlockById(
const databaseModel = doc.getModelById(
databaseBlockId
) as DatabaseBlockModel;
db = databaseModel;
@@ -187,7 +187,7 @@ describe('DatabaseManager', () => {
addProperty(db, 'end', column);
updateCell(db, modelId, cell);
const model = doc.getBlockById(modelId);
const model = doc.getModelById(modelId);
expect(model).not.toBeNull();
@@ -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];
@@ -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,
@@ -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);
}
@@ -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));
}),
];
@@ -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;
}
@@ -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({
@@ -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
);
@@ -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;
@@ -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(() => {
@@ -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;
}
@@ -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);
@@ -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);
@@ -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;
@@ -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;
@@ -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)) ||
@@ -78,7 +78,7 @@ export class FrameBlockModel
for (const key of this.childIds) {
const element =
this.surface.getElementById(key) ||
(this.surface.doc.getBlockById(key) as GfxBlockElementModel);
(this.surface.doc.getModelById(key) as GfxBlockElementModel);
element && elements.push(element);
}
@@ -15,7 +15,7 @@ export class RootBlockModel extends BlockModel<RootBlockProps> {
const createdSubscription = this.created.subscribe(() => {
createdSubscription.unsubscribe();
this.doc.slots.rootAdded.subscribe(id => {
const model = this.doc.getBlockById(id);
const model = this.doc.getModelById(id);
if (model instanceof RootBlockModel) {
const newDocMeta = this.doc.workspace.meta.getDocMeta(model.doc.id);
if (
@@ -45,7 +45,7 @@ export function calcDropTarget(
*/
allowSublist: boolean = true
): DropTarget | null {
const schema = model.doc.getSchemaByFlavour('affine:database');
const schema = model.doc.schema.get('affine:database');
const children = schema?.model.children ?? [];
let shouldAppendToDatabase = true;
@@ -62,7 +62,7 @@ export class EdgelessWatcher {
};
private readonly _showDragHandle = async () => {
const surfaceModel = this.widget.doc.getBlockByFlavour('affine:surface');
const surfaceModel = this.widget.doc.getModelsByFlavour('affine:surface');
const surface = this.widget.std.view.getBlock(
surfaceModel[0]!.id
) as SurfaceBlockComponent;