mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00: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:
@@ -299,7 +299,7 @@ export class UIEventDispatcher extends LifeCycleWatcher {
|
||||
return (
|
||||
element instanceof HTMLInputElement ||
|
||||
element instanceof HTMLTextAreaElement ||
|
||||
(element instanceof EditorHost && !element.doc.readonly) ||
|
||||
(element instanceof EditorHost && !element.store.readonly) ||
|
||||
(element as HTMLElement).isContentEditable
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ export class RangeBinding {
|
||||
let parent: BlockModel | null = block;
|
||||
while (parent) {
|
||||
path.unshift(parent.id);
|
||||
parent = this.host.doc.getParent(parent);
|
||||
parent = this.host.store.getParent(parent);
|
||||
}
|
||||
|
||||
return path;
|
||||
@@ -54,7 +54,7 @@ export class RangeBinding {
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
this.host.doc.transact(() => {
|
||||
this.host.store.transact(() => {
|
||||
startText.delete(from.index, from.length);
|
||||
startText.insert(event.data ?? '', from.index);
|
||||
endText.delete(0, to.length);
|
||||
@@ -65,9 +65,9 @@ export class RangeBinding {
|
||||
// delete from lowest to highest
|
||||
.reverse()
|
||||
.forEach(block => {
|
||||
const parent = this.host.doc.getParent(block.model);
|
||||
const parent = this.host.store.getParent(block.model);
|
||||
if (!parent) return;
|
||||
this.host.doc.deleteBlock(block.model, {
|
||||
this.host.store.deleteBlock(block.model, {
|
||||
bringChildrenTo: parent,
|
||||
});
|
||||
});
|
||||
@@ -129,9 +129,9 @@ export class RangeBinding {
|
||||
this.host.requestUpdate();
|
||||
await this.host.updateComplete;
|
||||
|
||||
this.host.doc.captureSync();
|
||||
this.host.store.captureSync();
|
||||
|
||||
this.host.doc.transact(() => {
|
||||
this.host.store.transact(() => {
|
||||
endText.delete(0, to.length);
|
||||
startText.delete(from.index, from.length);
|
||||
startText.insert(event.data, from.index);
|
||||
@@ -142,9 +142,9 @@ export class RangeBinding {
|
||||
// delete from lowest to highest
|
||||
.reverse()
|
||||
.forEach(block => {
|
||||
const parent = this.host.doc.getParent(block.model);
|
||||
const parent = this.host.store.getParent(block.model);
|
||||
if (!parent) return;
|
||||
this.host.doc.deleteBlock(block.model, {
|
||||
this.host.store.deleteBlock(block.model, {
|
||||
bringChildrenTo: parent,
|
||||
});
|
||||
});
|
||||
@@ -256,7 +256,7 @@ export class RangeBinding {
|
||||
return;
|
||||
}
|
||||
|
||||
const model = this.host.doc.getModelById(textSelection.blockId);
|
||||
const model = this.host.store.getModelById(textSelection.blockId);
|
||||
// If the model is not found, the selection maybe in another editor
|
||||
if (!model) return;
|
||||
|
||||
|
||||
@@ -152,7 +152,7 @@ export class BlockStdScope {
|
||||
render() {
|
||||
const element = new EditorHost();
|
||||
element.std = this;
|
||||
element.doc = this.store;
|
||||
element.store = this.store;
|
||||
this._host = element;
|
||||
this._lifeCycleWatchers.forEach(watcher => {
|
||||
watcher.rendered();
|
||||
|
||||
@@ -20,12 +20,12 @@ import {
|
||||
modelContext,
|
||||
serviceContext,
|
||||
} from './consts.js';
|
||||
import { docContext, stdContext } from './lit-host.js';
|
||||
import { stdContext, storeContext } from './lit-host.js';
|
||||
import { ShadowlessElement } from './shadowless-element.js';
|
||||
import type { WidgetComponent } from './widget-component.js';
|
||||
|
||||
@requiredProperties({
|
||||
doc: PropTypes.instanceOf(Store),
|
||||
store: PropTypes.instanceOf(Store),
|
||||
std: PropTypes.object,
|
||||
widgets: PropTypes.recordOf(PropTypes.object),
|
||||
})
|
||||
@@ -86,7 +86,7 @@ export class BlockComponent<
|
||||
}
|
||||
|
||||
get isVersionMismatch() {
|
||||
const schema = this.doc.schema.flavourSchemaMap.get(this.model.flavour);
|
||||
const schema = this.store.schema.flavourSchemaMap.get(this.model.flavour);
|
||||
if (!schema) {
|
||||
console.warn(
|
||||
`Schema not found for block ${this.model.id}, flavour ${this.model.flavour}`
|
||||
@@ -109,7 +109,7 @@ export class BlockComponent<
|
||||
if (this._model) {
|
||||
return this._model;
|
||||
}
|
||||
const model = this.doc.getModelById<Model>(this.blockId);
|
||||
const model = this.store.getModelById<Model>(this.blockId);
|
||||
if (!model) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.MissingViewModelError,
|
||||
@@ -131,7 +131,7 @@ export class BlockComponent<
|
||||
}
|
||||
|
||||
get rootComponent(): BlockComponent | null {
|
||||
const rootId = this.doc.root?.id;
|
||||
const rootId = this.store.root?.id;
|
||||
if (!rootId) {
|
||||
return null;
|
||||
}
|
||||
@@ -173,7 +173,9 @@ export class BlockComponent<
|
||||
this.isVersionMismatch,
|
||||
() => {
|
||||
const actualVersion = this.model.version;
|
||||
const schema = this.doc.schema.flavourSchemaMap.get(this.model.flavour);
|
||||
const schema = this.store.schema.flavourSchemaMap.get(
|
||||
this.model.flavour
|
||||
);
|
||||
const expectedVersion = schema?.version ?? -1;
|
||||
return this.renderVersionMismatch(expectedVersion, actualVersion);
|
||||
},
|
||||
@@ -290,8 +292,8 @@ export class BlockComponent<
|
||||
@state()
|
||||
private accessor _service: Service | null = null;
|
||||
|
||||
@consume({ context: docContext })
|
||||
accessor doc!: Store;
|
||||
@consume({ context: storeContext })
|
||||
accessor store!: Store;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor viewType: BlockViewType = 'display';
|
||||
|
||||
@@ -56,7 +56,7 @@ function handleGfxConnection(instance: GfxBlockComponent) {
|
||||
);
|
||||
|
||||
instance.disposables.add(
|
||||
instance.doc.slots.blockUpdated.subscribe(({ type, id }) => {
|
||||
instance.store.slots.blockUpdated.subscribe(({ type, id }) => {
|
||||
if (id === instance.model.id && type === 'update') {
|
||||
updateTransform(instance);
|
||||
}
|
||||
|
||||
@@ -25,11 +25,11 @@ import type { ViewStore } from '../view-store.js';
|
||||
import { BLOCK_ID_ATTR, WIDGET_ID_ATTR } from './consts.js';
|
||||
import { ShadowlessElement } from './shadowless-element.js';
|
||||
|
||||
export const docContext = createContext<Store>('doc');
|
||||
export const storeContext = createContext<Store>('store');
|
||||
export const stdContext = createContext<BlockStdScope>('std');
|
||||
|
||||
@requiredProperties({
|
||||
doc: PropTypes.instanceOf(Store),
|
||||
store: PropTypes.instanceOf(Store),
|
||||
std: PropTypes.object,
|
||||
})
|
||||
export class EditorHost extends SignalWatcher(
|
||||
@@ -46,11 +46,11 @@ export class EditorHost extends SignalWatcher(
|
||||
|
||||
private readonly _renderModel = (model: BlockModel): TemplateResult => {
|
||||
const { flavour } = model;
|
||||
const block = this.doc.getBlock(model.id);
|
||||
const block = this.store.getBlock(model.id);
|
||||
if (!block || block.blockViewType === 'hidden') {
|
||||
return html`${nothing}`;
|
||||
}
|
||||
const schema = this.doc.schema.flavourSchemaMap.get(flavour);
|
||||
const schema = this.store.schema.flavourSchemaMap.get(flavour);
|
||||
const view = this.std.getView(flavour);
|
||||
if (!schema || !view) {
|
||||
console.warn(`Cannot find render flavour ${flavour}.`);
|
||||
@@ -112,7 +112,7 @@ export class EditorHost extends SignalWatcher(
|
||||
override connectedCallback() {
|
||||
super.connectedCallback();
|
||||
|
||||
if (!this.doc.root) {
|
||||
if (!this.store.root) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.NoRootModelError,
|
||||
'This doc is missing root block. Please initialize the default block structure before connecting the editor to DOM.'
|
||||
@@ -131,7 +131,7 @@ export class EditorHost extends SignalWatcher(
|
||||
override async getUpdateComplete(): Promise<boolean> {
|
||||
try {
|
||||
const result = await super.getUpdateComplete();
|
||||
const rootModel = this.doc.root;
|
||||
const rootModel = this.store.root;
|
||||
if (!rootModel) return result;
|
||||
|
||||
const view = this.std.getView(rootModel.flavour);
|
||||
@@ -175,15 +175,15 @@ export class EditorHost extends SignalWatcher(
|
||||
}
|
||||
|
||||
override render() {
|
||||
const { root } = this.doc;
|
||||
const { root } = this.store;
|
||||
if (!root) return nothing;
|
||||
|
||||
return this._renderModel(root);
|
||||
}
|
||||
|
||||
@provide({ context: docContext })
|
||||
@provide({ context: storeContext })
|
||||
@property({ attribute: false })
|
||||
accessor doc!: Store;
|
||||
accessor store!: Store;
|
||||
|
||||
@provide({ context: stdContext })
|
||||
@property({ attribute: false })
|
||||
|
||||
@@ -8,7 +8,7 @@ import type { BlockService } from '../../extension/index.js';
|
||||
import type { BlockStdScope } from '../../scope/index.js';
|
||||
import type { BlockComponent } from './block-component.js';
|
||||
import { modelContext, serviceContext } from './consts.js';
|
||||
import { docContext, stdContext } from './lit-host.js';
|
||||
import { stdContext, storeContext } from './lit-host.js';
|
||||
|
||||
export class WidgetComponent<
|
||||
Model extends BlockModel = BlockModel,
|
||||
@@ -31,8 +31,8 @@ export class WidgetComponent<
|
||||
return this.std.view.getBlock(this.model.id) as B | null;
|
||||
}
|
||||
|
||||
get doc() {
|
||||
return this._doc;
|
||||
get store() {
|
||||
return this._store;
|
||||
}
|
||||
|
||||
get flavour(): string {
|
||||
@@ -84,8 +84,8 @@ export class WidgetComponent<
|
||||
return null;
|
||||
}
|
||||
|
||||
@consume({ context: docContext })
|
||||
private accessor _doc!: Store;
|
||||
@consume({ context: storeContext })
|
||||
private accessor _store!: Store;
|
||||
|
||||
@consume({ context: modelContext })
|
||||
private accessor _model!: Model;
|
||||
|
||||
Reference in New Issue
Block a user