refactor(editor): set readonly (#9475)

This commit is contained in:
Saul-Mirone
2025-01-02 04:02:15 +00:00
parent d0983696c0
commit be387a6f33
17 changed files with 32 additions and 50 deletions

View File

@@ -188,7 +188,7 @@ async function renderNoteContent(
let parent: string | null = block;
while (parent && !ids.includes(parent)) {
ids.push(parent);
parent = doc.blockCollection.crud.getParent(parent);
parent = doc.getParent(parent)?.id ?? null;
}
});
const query: Query = {

View File

@@ -35,7 +35,7 @@ export class PreviewHelper {
if (!selectedIds.includes(parent)) {
ids.push({ viewType: BlockViewType.Bypass, id: parent });
}
parent = this.widget.doc.blockCollection.crud.getParent(parent);
parent = this.widget.doc.getParent(parent)?.id ?? null;
} while (parent && !ids.map(({ id }) => id).includes(parent));
});

View File

@@ -343,9 +343,7 @@ export class AffineFormatBarWidget extends WidgetComponent {
}
private _shouldDisplay() {
const readonly = this.doc.awarenessStore.isReadonly(
this.doc.blockCollection
);
const readonly = this.doc.readonly;
const active = this.host.event.active;
if (readonly || !active) return false;

View File

@@ -75,7 +75,7 @@ export class SelectionManager extends LifeCycleWatcher {
constructor(std: BlockStdScope) {
super(std);
this._id = `${this.std.doc.blockCollection.id}:${nanoid()}`;
this._id = `${this.std.doc.id}:${nanoid()}`;
this._setupDefaultSelections();
this._store.awareness.on(
'change',
@@ -99,9 +99,7 @@ export class SelectionManager extends LifeCycleWatcher {
if (id === this._store.awareness.clientID) return;
// selection id starts with the same block collection id from others clients would be considered as remote selections
const selection = Object.entries(state.selectionV2)
.filter(([key]) =>
key.startsWith(this.std.doc.blockCollection.id)
)
.filter(([key]) => key.startsWith(this.std.doc.id))
.flatMap(([_, selection]) => selection);
const selections = selection

View File

@@ -163,17 +163,14 @@ describe('basic', () => {
doc.slots.rootAdded.on(rootAddedCallback);
doc.load(() => {
expect(doc.ready).toBe(false);
const rootId = doc.addBlock('affine:page', {
title: new doc.Text(),
});
expect(rootAddedCallback).toBeCalledTimes(1);
expect(doc.ready).toBe(false);
doc.addBlock('affine:note', {}, rootId);
});
expect(doc.ready).toBe(true);
expect(readyCallback).toBeCalledTimes(1);
});

View File

@@ -258,13 +258,13 @@ test('local readonly', () => {
expect(doc2?.readonly).toBeTruthy();
expect(doc3?.readonly).toBeFalsy();
collection.awarenessStore.setReadonly(doc1.blockCollection, true);
doc1.readonly = true;
expect(doc1.readonly).toBeTruthy();
expect(doc2?.readonly).toBeTruthy();
expect(doc3?.readonly).toBeTruthy();
collection.awarenessStore.setReadonly(doc1.blockCollection, false);
doc1.readonly = false;
expect(doc1.readonly).toBeFalsy();
expect(doc2?.readonly).toBeTruthy();

View File

@@ -37,7 +37,7 @@ export class Doc {
mode: 'loose',
};
protected readonly _readonly?: boolean;
protected _readonly?: boolean;
protected readonly _schema: Schema;
@@ -144,10 +144,6 @@ export class Doc {
return this._blockCollection.awarenessStore;
}
get awarenessSync() {
return this.collection.awarenessSync;
}
get blobSync() {
return this.collection.blobSync;
}
@@ -184,10 +180,6 @@ export class Doc {
return this._blockCollection.collection;
}
get docSync() {
return this.collection.docSync;
}
get generateBlockId() {
return this._blockCollection.generateBlockId.bind(this._blockCollection);
}
@@ -219,6 +211,16 @@ export class Doc {
return this._readonly === true;
}
set readonly(value: boolean) {
this._blockCollection.awarenessStore.setReadonly(
this._blockCollection,
value
);
if (this._readonly !== undefined && this._readonly !== value) {
this._readonly = value;
}
}
get ready() {
return this._blockCollection.ready;
}

View File

@@ -668,7 +668,7 @@ export class StarterDebugMenu extends ShadowlessElement {
private _toggleReadonly() {
const doc = this.doc;
doc.awarenessStore.setReadonly(doc.blockCollection, !doc.readonly);
doc.readonly = !doc.readonly;
}
private async _toggleStyleDebugMenu() {

View File

@@ -22,7 +22,6 @@ export function getDocFromUrlParams(collection: DocCollection, url: URL) {
doc.load();
doc.resetHistory();
assertExists(doc.ready, 'Doc is not ready');
assertExists(doc.root, 'Doc root is not ready');
return doc;

View File

@@ -79,7 +79,7 @@ async function initEmptyEditor({
async function waitForMountPageEditor(
doc: ReturnType<typeof collection.createDoc>
) {
if (!doc.ready) doc.load();
doc.load();
if (!doc.root) {
await new Promise(resolve => doc.slots.rootAdded.once(resolve));