refactor(editor): remove readonly in awareness (#9597)

This commit is contained in:
Saul-Mirone
2025-01-09 05:15:35 +00:00
parent d21ef47ae8
commit 422bac6cbe
17 changed files with 71 additions and 179 deletions

View File

@@ -1,5 +1,5 @@
import { SpecProvider } from '@blocksuite/affine/blocks';
import { type Disposable, Slot } from '@blocksuite/affine/global/utils';
import { Slot } from '@blocksuite/affine/global/utils';
import {
type AwarenessStore,
type Doc,
@@ -20,8 +20,6 @@ type DocOptions = {
};
export class DocImpl implements Doc {
private _awarenessUpdateDisposable: Disposable | null = null;
private readonly _canRedo = signal(false);
private readonly _canUndo = signal(false);
@@ -89,8 +87,8 @@ export class DocImpl implements Doc {
private _shouldTransact = true;
private readonly _updateCanUndoRedoSignals = () => {
const canRedo = this.readonly ? false : this._history.canRedo();
const canUndo = this.readonly ? false : this._history.canUndo();
const canRedo = this._history.canRedo();
const canUndo = this._history.canUndo();
if (this._canRedo.peek() !== canRedo) {
this._canRedo.value = canRedo;
}
@@ -159,10 +157,6 @@ export class DocImpl implements Doc {
return this.workspace.meta.getDocMeta(this.id);
}
get readonly(): boolean {
return this.awarenessStore.isReadonly(this);
}
get ready() {
return this._ready;
}
@@ -267,7 +261,6 @@ export class DocImpl implements Doc {
dispose() {
this.slots.historyUpdated.dispose();
this._awarenessUpdateDisposable?.dispose();
if (this.ready) {
this._yBlocks.unobserveDeep(this._handleYEvents);
@@ -320,13 +313,6 @@ export class DocImpl implements Doc {
this._handleYBlockAdd(id);
});
this._awarenessUpdateDisposable = this.awarenessStore.slots.update.on(
() => {
// change readonly state will affect the undo/redo state
this._updateCanUndoRedoSignals();
}
);
initFn?.();
this._ready = true;
@@ -335,18 +321,10 @@ export class DocImpl implements Doc {
}
redo() {
if (this.readonly) {
console.error('cannot modify data in readonly mode');
return;
}
this._history.redo();
}
undo() {
if (this.readonly) {
console.error('cannot modify data in readonly mode');
return;
}
this._history.undo();
}

View File

@@ -2,7 +2,6 @@ import {
BlockSuiteError,
ErrorCode,
} from '@blocksuite/affine/global/exceptions';
import type { BlockSuiteFlags } from '@blocksuite/affine/global/types';
import { NoopLogger, Slot } from '@blocksuite/affine/global/utils';
import {
AwarenessStore,
@@ -33,10 +32,6 @@ type WorkspaceOptions = {
blobSource?: BlobSource;
};
const FLAGS_PRESET = {
readonly: {},
} satisfies BlockSuiteFlags;
export class WorkspaceImpl implements Workspace {
protected readonly _schema: Schema;
@@ -73,10 +68,7 @@ export class WorkspaceImpl implements Workspace {
this.id = id || '';
this.doc = new Y.Doc({ guid: id });
this.awarenessStore = new AwarenessStore(new Awareness(this.doc), {
...FLAGS_PRESET,
readonly: {},
});
this.awarenessStore = new AwarenessStore(new Awareness(this.doc));
blobSource = blobSource ?? new MemoryBlobSource();
const logger = new NoopLogger();