mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-08 20:57:08 +08:00
refactor(editor): rename doc to store on block components (#12173)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Unified internal data access by replacing all references from `doc` to `store` across all components, blocks, widgets, and utilities. This affects how readonly state, block operations, and service retrieval are handled throughout the application. - **Tests** - Updated all test utilities and test cases to use `store` instead of `doc` for document-related operations. - **Chores** - Updated context providers and property names to reflect the change from `doc` to `store` for improved consistency and maintainability. No user-facing features or behaviors have changed. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
+9
-9
@@ -174,7 +174,7 @@ export class EdgelessAutoCompletePanel extends WithDisposable(LitElement) {
|
||||
presentationIndex: frameMgr.generatePresentationIndex(),
|
||||
});
|
||||
const id = this.crud.addBlock('affine:frame', props, surfaceBlockModel);
|
||||
edgeless.doc.captureSync();
|
||||
edgeless.store.captureSync();
|
||||
const frame = this.crud.getElementById(id);
|
||||
if (!frame) return;
|
||||
|
||||
@@ -190,7 +190,7 @@ export class EdgelessAutoCompletePanel extends WithDisposable(LitElement) {
|
||||
}
|
||||
|
||||
private _addNote() {
|
||||
const { doc } = this.edgeless;
|
||||
const { store } = this.edgeless;
|
||||
const target = this._getTargetXYWH(
|
||||
DEFAULT_NOTE_WIDTH,
|
||||
DEFAULT_NOTE_OVERLAY_HEIGHT
|
||||
@@ -203,13 +203,13 @@ export class EdgelessAutoCompletePanel extends WithDisposable(LitElement) {
|
||||
{
|
||||
xywh: serializeXYWH(...xywh),
|
||||
},
|
||||
doc.root?.id
|
||||
store.root?.id
|
||||
);
|
||||
const note = doc.getBlock(id)?.model;
|
||||
const note = store.getBlock(id)?.model;
|
||||
if (!matchModels(note, [NoteBlockModel])) {
|
||||
return;
|
||||
}
|
||||
doc.addBlock('affine:paragraph', { type: 'text' }, id);
|
||||
store.addBlock('affine:paragraph', { type: 'text' }, id);
|
||||
const group = this.currentSource.group;
|
||||
|
||||
if (group instanceof GroupElementModel) {
|
||||
@@ -251,7 +251,7 @@ export class EdgelessAutoCompletePanel extends WithDisposable(LitElement) {
|
||||
elements: [id],
|
||||
editing: true,
|
||||
});
|
||||
edgeless.doc.captureSync();
|
||||
edgeless.store.captureSync();
|
||||
}
|
||||
|
||||
private _addText() {
|
||||
@@ -260,7 +260,7 @@ export class EdgelessAutoCompletePanel extends WithDisposable(LitElement) {
|
||||
const { xywh, position } = target;
|
||||
const bound = Bound.fromXYWH(xywh);
|
||||
|
||||
const textFlag = this.edgeless.doc
|
||||
const textFlag = this.edgeless.store
|
||||
.get(FeatureFlagService)
|
||||
.getFlag('enable_edgeless_text');
|
||||
if (textFlag) {
|
||||
@@ -287,7 +287,7 @@ export class EdgelessAutoCompletePanel extends WithDisposable(LitElement) {
|
||||
elements: [textId],
|
||||
editing: false,
|
||||
});
|
||||
this.edgeless.doc.captureSync();
|
||||
this.edgeless.store.captureSync();
|
||||
} else {
|
||||
const textId = this.crud.addElement(CanvasElementType.TEXT, {
|
||||
xywh: bound.serialize(),
|
||||
@@ -316,7 +316,7 @@ export class EdgelessAutoCompletePanel extends WithDisposable(LitElement) {
|
||||
elements: [textId],
|
||||
editing: false,
|
||||
});
|
||||
this.edgeless.doc.captureSync();
|
||||
this.edgeless.store.captureSync();
|
||||
|
||||
mountTextElementEditor(textElement, this.edgeless);
|
||||
}
|
||||
|
||||
+2
-2
@@ -371,7 +371,7 @@ export class EdgelessAutoComplete extends WithDisposable(LitElement) {
|
||||
}
|
||||
|
||||
private _generateElementOnClick(type: Direction) {
|
||||
const { doc } = this.edgeless;
|
||||
const { store } = this.edgeless;
|
||||
const bound = this._computeNextBound(type);
|
||||
const id = createEdgelessElement(this.edgeless, this.current, bound);
|
||||
if (!id) return;
|
||||
@@ -393,7 +393,7 @@ export class EdgelessAutoComplete extends WithDisposable(LitElement) {
|
||||
this.edgeless
|
||||
);
|
||||
} else {
|
||||
const model = doc.getModelById(id);
|
||||
const model = store.getModelById(id);
|
||||
if (!model) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -288,8 +288,8 @@ export function createEdgelessElement(
|
||||
if (!id) return null;
|
||||
element = crud.getElementById(id);
|
||||
} else {
|
||||
const { doc } = edgeless;
|
||||
id = doc.addBlock(
|
||||
const { store } = edgeless;
|
||||
id = store.addBlock(
|
||||
'affine:note',
|
||||
{
|
||||
background: current.props.background,
|
||||
@@ -299,7 +299,7 @@ export function createEdgelessElement(
|
||||
},
|
||||
edgeless.model.id
|
||||
);
|
||||
const note = doc.getBlock(id)?.model;
|
||||
const note = store.getBlock(id)?.model;
|
||||
if (!note) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.GfxBlockElementError,
|
||||
@@ -307,10 +307,10 @@ export function createEdgelessElement(
|
||||
);
|
||||
}
|
||||
assertType<NoteBlockModel>(note);
|
||||
doc.updateBlock(note, () => {
|
||||
store.updateBlock(note, () => {
|
||||
note.props.edgeless.collapse = true;
|
||||
});
|
||||
doc.addBlock('affine:paragraph', {}, note.id);
|
||||
store.addBlock('affine:paragraph', {}, note.id);
|
||||
|
||||
element = note;
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ export class NoteSlicer extends WidgetComponent<RootBlockModel> {
|
||||
|
||||
private _sliceNote() {
|
||||
if (!this._anchorNote || !this._noteBlockIds.length) return;
|
||||
const doc = this.doc;
|
||||
const doc = this.store;
|
||||
|
||||
const {
|
||||
index: originalIndex,
|
||||
@@ -177,7 +177,7 @@ export class NoteSlicer extends WidgetComponent<RootBlockModel> {
|
||||
const sliceVerticalPos =
|
||||
this._divingLinePositions[this._activeSlicerIndex].y;
|
||||
const newY = this.gfx.viewport.toModelCoord(x, sliceVerticalPos)[1];
|
||||
const newNoteId = this.doc.addBlock(
|
||||
const newNoteId = this.store.addBlock(
|
||||
'affine:note',
|
||||
{
|
||||
background,
|
||||
@@ -364,7 +364,7 @@ export class NoteSlicer extends WidgetComponent<RootBlockModel> {
|
||||
|
||||
override render() {
|
||||
if (
|
||||
this.doc.readonly ||
|
||||
this.store.readonly ||
|
||||
this._hidden ||
|
||||
this._isResizing ||
|
||||
!this._anchorNote ||
|
||||
|
||||
+6
-6
@@ -477,7 +477,7 @@ export class EdgelessSelectedRectWidget extends WidgetComponent<
|
||||
private readonly _onDragEnd = () => {
|
||||
this.slots.dragEnd.next();
|
||||
|
||||
this.doc.transact(() => {
|
||||
this.store.transact(() => {
|
||||
this._dragEndCallback.forEach(cb => cb());
|
||||
});
|
||||
|
||||
@@ -1210,7 +1210,7 @@ export class EdgelessSelectedRectWidget extends WidgetComponent<
|
||||
this._isHeightLimit = bound.h === NOTE_MIN_HEIGHT * scale;
|
||||
|
||||
if (bound.h > NOTE_MIN_HEIGHT * scale) {
|
||||
this.doc.updateBlock(element, () => {
|
||||
this.store.updateBlock(element, () => {
|
||||
element.props.edgeless.collapse = true;
|
||||
element.props.edgeless.collapsedHeight = bound.h / scale;
|
||||
});
|
||||
@@ -1393,7 +1393,7 @@ export class EdgelessSelectedRectWidget extends WidgetComponent<
|
||||
}
|
||||
|
||||
_disposables.add(
|
||||
this.doc.slots.blockUpdated.subscribe(this._updateOnElementChange)
|
||||
this.store.slots.blockUpdated.subscribe(this._updateOnElementChange)
|
||||
);
|
||||
|
||||
_disposables.add(
|
||||
@@ -1464,14 +1464,14 @@ export class EdgelessSelectedRectWidget extends WidgetComponent<
|
||||
const {
|
||||
block,
|
||||
gfx,
|
||||
doc,
|
||||
store,
|
||||
resizeMode,
|
||||
_resizeManager,
|
||||
_selectedRect,
|
||||
_updateCursor,
|
||||
} = this;
|
||||
|
||||
const hasResizeHandles = !selection.editing && !doc.readonly;
|
||||
const hasResizeHandles = !selection.editing && !store.readonly;
|
||||
const inoperable = selection.inoperable;
|
||||
const hasElementLocked = elements.some(element => element.isLocked());
|
||||
const handlers = [];
|
||||
@@ -1578,7 +1578,7 @@ export class EdgelessSelectedRectWidget extends WidgetComponent<
|
||||
}
|
||||
</style>
|
||||
|
||||
${!doc.readonly && !inoperable && this._canAutoComplete()
|
||||
${!store.readonly && !inoperable && this._canAutoComplete()
|
||||
? html`<edgeless-auto-complete
|
||||
.current=${this.selection.selectedElements[0]}
|
||||
.edgeless=${block}
|
||||
|
||||
@@ -52,8 +52,8 @@ export function createLinkedDocFromEdgelessElements(
|
||||
elements: GfxModel[],
|
||||
docTitle?: string
|
||||
) {
|
||||
const _doc = host.doc.workspace.createDoc();
|
||||
const transformer = host.doc.getTransformer();
|
||||
const _doc = host.store.workspace.createDoc();
|
||||
const transformer = host.store.getTransformer();
|
||||
const linkedDoc = _doc.getStore();
|
||||
linkedDoc.load(() => {
|
||||
const rootId = linkedDoc.addBlock('affine:page', {
|
||||
|
||||
@@ -465,7 +465,7 @@ export class EdgelessPageKeyboardManager extends PageKeyboardManager {
|
||||
!event.metaKey
|
||||
) {
|
||||
const elements = selection.selectedElements;
|
||||
const doc = this.rootComponent.doc;
|
||||
const doc = this.rootComponent.store;
|
||||
|
||||
if (isSingleMindMapNode(elements)) {
|
||||
const target = gfx.getElementById(
|
||||
|
||||
@@ -407,7 +407,7 @@ export class EdgelessRootBlockComponent extends BlockComponent<
|
||||
elements[0].id
|
||||
) as ShapeElementModel;
|
||||
if (target.text) {
|
||||
this.doc.transact(() => {
|
||||
this.store.transact(() => {
|
||||
target.text!.delete(0, target.text!.length);
|
||||
target.text!.insert(0, key);
|
||||
});
|
||||
@@ -467,7 +467,7 @@ export class EdgelessRootBlockComponent extends BlockComponent<
|
||||
this._initPanEvent();
|
||||
this._initPinchEvent();
|
||||
|
||||
if (this.doc.readonly) {
|
||||
if (this.store.readonly) {
|
||||
this.gfx.tool.setTool(PanTool, { panning: true });
|
||||
} else {
|
||||
this.gfx.tool.setTool(DefaultTool);
|
||||
|
||||
@@ -25,10 +25,10 @@ export function deleteElements(
|
||||
|
||||
set.forEach(element => {
|
||||
if (isNoteBlock(element)) {
|
||||
const children = edgeless.doc.root?.children ?? [];
|
||||
const children = edgeless.store.root?.children ?? [];
|
||||
// FIXME: should always keep at least 1 note
|
||||
if (children.length > 1) {
|
||||
edgeless.doc.deleteBlock(element);
|
||||
edgeless.store.deleteBlock(element);
|
||||
}
|
||||
} else {
|
||||
service.removeElement(element.id);
|
||||
|
||||
@@ -74,7 +74,7 @@ export class PageKeyboardManager {
|
||||
}
|
||||
|
||||
private get _doc() {
|
||||
return this.rootComponent.doc;
|
||||
return this.rootComponent.store;
|
||||
}
|
||||
|
||||
private get _selection() {
|
||||
@@ -143,7 +143,7 @@ export class PageKeyboardManager {
|
||||
return;
|
||||
}
|
||||
|
||||
const doc = rootComponent.host.doc;
|
||||
const doc = rootComponent.host.store;
|
||||
const autofill = getTitleFromSelectedModels(
|
||||
selectedModels.map(toDraftModel)
|
||||
);
|
||||
|
||||
@@ -117,7 +117,7 @@ export class PageRootBlockComponent extends BlockComponent<RootBlockModel> {
|
||||
focusTextModel(this.std, firstText.id);
|
||||
return { id: firstText.id, created: false };
|
||||
} else {
|
||||
const newFirstParagraphId = this.doc.addBlock(
|
||||
const newFirstParagraphId = this.store.addBlock(
|
||||
'affine:paragraph',
|
||||
{},
|
||||
defaultNote,
|
||||
@@ -131,7 +131,7 @@ export class PageRootBlockComponent extends BlockComponent<RootBlockModel> {
|
||||
keyboardManager: PageKeyboardManager | null = null;
|
||||
|
||||
prependParagraphWithText = (text: Text) => {
|
||||
const newFirstParagraphId = this.doc.addBlock(
|
||||
const newFirstParagraphId = this.store.addBlock(
|
||||
'affine:paragraph',
|
||||
{ text },
|
||||
this._getDefaultNoteBlock(),
|
||||
@@ -157,16 +157,17 @@ export class PageRootBlockComponent extends BlockComponent<RootBlockModel> {
|
||||
}
|
||||
|
||||
private _createDefaultNoteBlock() {
|
||||
const { doc } = this;
|
||||
const { store } = this;
|
||||
|
||||
const noteId = doc.addBlock('affine:note', {}, doc.root?.id);
|
||||
return doc.getModelById(noteId) as NoteBlockModel;
|
||||
const noteId = store.addBlock('affine:note', {}, store.root?.id);
|
||||
return store.getModelById(noteId) as NoteBlockModel;
|
||||
}
|
||||
|
||||
private _getDefaultNoteBlock() {
|
||||
return (
|
||||
this.doc.root?.children.find(child => child.flavour === 'affine:note') ??
|
||||
this._createDefaultNoteBlock()
|
||||
this.store.root?.children.find(
|
||||
child => child.flavour === 'affine:note'
|
||||
) ?? this._createDefaultNoteBlock()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -230,16 +231,16 @@ export class PageRootBlockComponent extends BlockComponent<RootBlockModel> {
|
||||
);
|
||||
if (!sel) return;
|
||||
let model: BlockModel | null = null;
|
||||
let current = this.doc.getModelById(sel.blockId);
|
||||
let current = this.store.getModelById(sel.blockId);
|
||||
while (current && !model) {
|
||||
if (current.flavour === 'affine:note') {
|
||||
model = current;
|
||||
} else {
|
||||
current = this.doc.getParent(current);
|
||||
current = this.store.getParent(current);
|
||||
}
|
||||
}
|
||||
if (!model) return;
|
||||
const prevNote = this.doc.getPrev(model);
|
||||
const prevNote = this.store.getPrev(model);
|
||||
if (!prevNote || prevNote.flavour !== 'affine:note') {
|
||||
const isFirstText = sel.is(TextSelection) && sel.start.index === 0;
|
||||
const isBlock = sel.is(BlockSelection);
|
||||
@@ -248,7 +249,7 @@ export class PageRootBlockComponent extends BlockComponent<RootBlockModel> {
|
||||
}
|
||||
return;
|
||||
}
|
||||
const notes = this.doc.getModelsByFlavour('affine:note');
|
||||
const notes = this.store.getModelsByFlavour('affine:note');
|
||||
const index = notes.indexOf(prevNote);
|
||||
if (index !== 0) return;
|
||||
|
||||
@@ -409,7 +410,7 @@ export class PageRootBlockComponent extends BlockComponent<RootBlockModel> {
|
||||
return !(isNote && displayOnEdgeless);
|
||||
});
|
||||
|
||||
this.contentEditable = String(!this.doc.readonly$.value);
|
||||
this.contentEditable = String(!this.store.readonly$.value);
|
||||
|
||||
return html`
|
||||
<div class="affine-page-root-block-container">${children} ${widgets}</div>
|
||||
|
||||
Reference in New Issue
Block a user