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,4 +1,4 @@
import { type Disposable, Slot } from '@blocksuite/global/utils';
import { Slot } from '@blocksuite/global/utils';
import { signal } from '@preact/signals-core';
import * as Y from 'yjs';
@@ -17,8 +17,6 @@ type DocOptions = {
};
export class TestDoc implements Doc {
private _awarenessUpdateDisposable: Disposable | null = null;
private readonly _canRedo$ = signal(false);
private readonly _canUndo$ = signal(false);
@@ -86,8 +84,8 @@ export class TestDoc 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;
}
@@ -164,10 +162,6 @@ export class TestDoc implements Doc {
return this.workspace.meta.getDocMeta(this.id);
}
get readonly(): boolean {
return this.awarenessStore.isReadonly(this);
}
get ready() {
return this._ready;
}
@@ -272,7 +266,6 @@ export class TestDoc implements Doc {
dispose() {
this.slots.historyUpdated.dispose();
this._awarenessUpdateDisposable?.dispose();
if (this.ready) {
this._yBlocks.unobserveDeep(this._handleYEvents);
@@ -322,13 +315,6 @@ export class TestDoc 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;
@@ -337,18 +323,10 @@ export class TestDoc 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

@@ -1,5 +1,4 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import type { BlockSuiteFlags } from '@blocksuite/global/types';
import { NoopLogger, Slot } from '@blocksuite/global/utils';
import {
AwarenessEngine,
@@ -11,8 +10,6 @@ import {
MemoryBlobSource,
NoopDocSource,
} from '@blocksuite/sync';
import clonedeep from 'lodash.clonedeep';
import merge from 'lodash.merge';
import { Awareness } from 'y-protocols/awareness.js';
import * as Y from 'yjs';
@@ -26,7 +23,7 @@ import type {
} from '../model/index.js';
import type { Schema } from '../schema/index.js';
import { type IdGenerator, nanoid } from '../utils/id-generator.js';
import { AwarenessStore, type RawAwarenessState } from '../yjs/index.js';
import { AwarenessStore } from '../yjs/index.js';
import { TestDoc } from './test-doc.js';
import { TestMeta } from './test-meta.js';
@@ -34,7 +31,6 @@ export type DocCollectionOptions = {
schema: Schema;
id?: string;
idGenerator?: IdGenerator;
defaultFlags?: Partial<BlockSuiteFlags>;
docSources?: {
main: DocSource;
shadows?: DocSource[];
@@ -46,10 +42,6 @@ export type DocCollectionOptions = {
awarenessSources?: AwarenessSource[];
};
const FLAGS_PRESET = {
readonly: {},
} satisfies BlockSuiteFlags;
/**
* Test only
* Do not use this in production
@@ -95,7 +87,6 @@ export class TestWorkspace implements Workspace {
id,
schema,
idGenerator,
defaultFlags,
awarenessSources = [],
docSources = {
main: new NoopDocSource(),
@@ -108,10 +99,7 @@ export class TestWorkspace implements Workspace {
this.id = id || '';
this.doc = new Y.Doc({ guid: id });
this.awarenessStore = new AwarenessStore(
new Awareness<RawAwarenessState>(this.doc),
merge(clonedeep(FLAGS_PRESET), defaultFlags)
);
this.awarenessStore = new AwarenessStore(new Awareness(this.doc));
const logger = new NoopLogger();