mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-29 16:19:43 +08:00
refactor(editor): set readonly (#9475)
This commit is contained in:
@@ -188,7 +188,7 @@ async function renderNoteContent(
|
|||||||
let parent: string | null = block;
|
let parent: string | null = block;
|
||||||
while (parent && !ids.includes(parent)) {
|
while (parent && !ids.includes(parent)) {
|
||||||
ids.push(parent);
|
ids.push(parent);
|
||||||
parent = doc.blockCollection.crud.getParent(parent);
|
parent = doc.getParent(parent)?.id ?? null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const query: Query = {
|
const query: Query = {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export class PreviewHelper {
|
|||||||
if (!selectedIds.includes(parent)) {
|
if (!selectedIds.includes(parent)) {
|
||||||
ids.push({ viewType: BlockViewType.Bypass, id: 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));
|
} while (parent && !ids.map(({ id }) => id).includes(parent));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -343,9 +343,7 @@ export class AffineFormatBarWidget extends WidgetComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _shouldDisplay() {
|
private _shouldDisplay() {
|
||||||
const readonly = this.doc.awarenessStore.isReadonly(
|
const readonly = this.doc.readonly;
|
||||||
this.doc.blockCollection
|
|
||||||
);
|
|
||||||
const active = this.host.event.active;
|
const active = this.host.event.active;
|
||||||
if (readonly || !active) return false;
|
if (readonly || !active) return false;
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ export class SelectionManager extends LifeCycleWatcher {
|
|||||||
|
|
||||||
constructor(std: BlockStdScope) {
|
constructor(std: BlockStdScope) {
|
||||||
super(std);
|
super(std);
|
||||||
this._id = `${this.std.doc.blockCollection.id}:${nanoid()}`;
|
this._id = `${this.std.doc.id}:${nanoid()}`;
|
||||||
this._setupDefaultSelections();
|
this._setupDefaultSelections();
|
||||||
this._store.awareness.on(
|
this._store.awareness.on(
|
||||||
'change',
|
'change',
|
||||||
@@ -99,9 +99,7 @@ export class SelectionManager extends LifeCycleWatcher {
|
|||||||
if (id === this._store.awareness.clientID) return;
|
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
|
// selection id starts with the same block collection id from others clients would be considered as remote selections
|
||||||
const selection = Object.entries(state.selectionV2)
|
const selection = Object.entries(state.selectionV2)
|
||||||
.filter(([key]) =>
|
.filter(([key]) => key.startsWith(this.std.doc.id))
|
||||||
key.startsWith(this.std.doc.blockCollection.id)
|
|
||||||
)
|
|
||||||
.flatMap(([_, selection]) => selection);
|
.flatMap(([_, selection]) => selection);
|
||||||
|
|
||||||
const selections = selection
|
const selections = selection
|
||||||
|
|||||||
@@ -163,17 +163,14 @@ describe('basic', () => {
|
|||||||
doc.slots.rootAdded.on(rootAddedCallback);
|
doc.slots.rootAdded.on(rootAddedCallback);
|
||||||
|
|
||||||
doc.load(() => {
|
doc.load(() => {
|
||||||
expect(doc.ready).toBe(false);
|
|
||||||
const rootId = doc.addBlock('affine:page', {
|
const rootId = doc.addBlock('affine:page', {
|
||||||
title: new doc.Text(),
|
title: new doc.Text(),
|
||||||
});
|
});
|
||||||
expect(rootAddedCallback).toBeCalledTimes(1);
|
expect(rootAddedCallback).toBeCalledTimes(1);
|
||||||
expect(doc.ready).toBe(false);
|
|
||||||
|
|
||||||
doc.addBlock('affine:note', {}, rootId);
|
doc.addBlock('affine:note', {}, rootId);
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(doc.ready).toBe(true);
|
|
||||||
expect(readyCallback).toBeCalledTimes(1);
|
expect(readyCallback).toBeCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -258,13 +258,13 @@ test('local readonly', () => {
|
|||||||
expect(doc2?.readonly).toBeTruthy();
|
expect(doc2?.readonly).toBeTruthy();
|
||||||
expect(doc3?.readonly).toBeFalsy();
|
expect(doc3?.readonly).toBeFalsy();
|
||||||
|
|
||||||
collection.awarenessStore.setReadonly(doc1.blockCollection, true);
|
doc1.readonly = true;
|
||||||
|
|
||||||
expect(doc1.readonly).toBeTruthy();
|
expect(doc1.readonly).toBeTruthy();
|
||||||
expect(doc2?.readonly).toBeTruthy();
|
expect(doc2?.readonly).toBeTruthy();
|
||||||
expect(doc3?.readonly).toBeTruthy();
|
expect(doc3?.readonly).toBeTruthy();
|
||||||
|
|
||||||
collection.awarenessStore.setReadonly(doc1.blockCollection, false);
|
doc1.readonly = false;
|
||||||
|
|
||||||
expect(doc1.readonly).toBeFalsy();
|
expect(doc1.readonly).toBeFalsy();
|
||||||
expect(doc2?.readonly).toBeTruthy();
|
expect(doc2?.readonly).toBeTruthy();
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export class Doc {
|
|||||||
mode: 'loose',
|
mode: 'loose',
|
||||||
};
|
};
|
||||||
|
|
||||||
protected readonly _readonly?: boolean;
|
protected _readonly?: boolean;
|
||||||
|
|
||||||
protected readonly _schema: Schema;
|
protected readonly _schema: Schema;
|
||||||
|
|
||||||
@@ -144,10 +144,6 @@ export class Doc {
|
|||||||
return this._blockCollection.awarenessStore;
|
return this._blockCollection.awarenessStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
get awarenessSync() {
|
|
||||||
return this.collection.awarenessSync;
|
|
||||||
}
|
|
||||||
|
|
||||||
get blobSync() {
|
get blobSync() {
|
||||||
return this.collection.blobSync;
|
return this.collection.blobSync;
|
||||||
}
|
}
|
||||||
@@ -184,10 +180,6 @@ export class Doc {
|
|||||||
return this._blockCollection.collection;
|
return this._blockCollection.collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
get docSync() {
|
|
||||||
return this.collection.docSync;
|
|
||||||
}
|
|
||||||
|
|
||||||
get generateBlockId() {
|
get generateBlockId() {
|
||||||
return this._blockCollection.generateBlockId.bind(this._blockCollection);
|
return this._blockCollection.generateBlockId.bind(this._blockCollection);
|
||||||
}
|
}
|
||||||
@@ -219,6 +211,16 @@ export class Doc {
|
|||||||
return this._readonly === true;
|
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() {
|
get ready() {
|
||||||
return this._blockCollection.ready;
|
return this._blockCollection.ready;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -668,7 +668,7 @@ export class StarterDebugMenu extends ShadowlessElement {
|
|||||||
|
|
||||||
private _toggleReadonly() {
|
private _toggleReadonly() {
|
||||||
const doc = this.doc;
|
const doc = this.doc;
|
||||||
doc.awarenessStore.setReadonly(doc.blockCollection, !doc.readonly);
|
doc.readonly = !doc.readonly;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _toggleStyleDebugMenu() {
|
private async _toggleStyleDebugMenu() {
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ export function getDocFromUrlParams(collection: DocCollection, url: URL) {
|
|||||||
doc.load();
|
doc.load();
|
||||||
doc.resetHistory();
|
doc.resetHistory();
|
||||||
|
|
||||||
assertExists(doc.ready, 'Doc is not ready');
|
|
||||||
assertExists(doc.root, 'Doc root is not ready');
|
assertExists(doc.root, 'Doc root is not ready');
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ async function initEmptyEditor({
|
|||||||
async function waitForMountPageEditor(
|
async function waitForMountPageEditor(
|
||||||
doc: ReturnType<typeof collection.createDoc>
|
doc: ReturnType<typeof collection.createDoc>
|
||||||
) {
|
) {
|
||||||
if (!doc.ready) doc.load();
|
doc.load();
|
||||||
|
|
||||||
if (!doc.root) {
|
if (!doc.root) {
|
||||||
await new Promise(resolve => doc.slots.rootAdded.once(resolve));
|
await new Promise(resolve => doc.slots.rootAdded.once(resolve));
|
||||||
|
|||||||
@@ -226,10 +226,7 @@ export class TextRenderer extends WithDisposable(ShadowlessElement) {
|
|||||||
this.disposables.add(() => {
|
this.disposables.add(() => {
|
||||||
doc.blockCollection.clearQuery(this._query);
|
doc.blockCollection.clearQuery(this._query);
|
||||||
});
|
});
|
||||||
this._doc.awarenessStore.setReadonly(
|
this._doc.readonly = true;
|
||||||
this._doc.blockCollection,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
this.requestUpdate();
|
this.requestUpdate();
|
||||||
if (this.state !== 'generating') {
|
if (this.state !== 'generating') {
|
||||||
this._clearTimer();
|
this._clearTimer();
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ export const useSnapshotPage = (
|
|||||||
page = historyShellWorkspace.createDoc({
|
page = historyShellWorkspace.createDoc({
|
||||||
id: pageId,
|
id: pageId,
|
||||||
});
|
});
|
||||||
page.awarenessStore.setReadonly(page.blockCollection, true);
|
page.readonly = true;
|
||||||
const spaceDoc = page.spaceDoc;
|
const spaceDoc = page.spaceDoc;
|
||||||
page.load(() => {
|
page.load(() => {
|
||||||
applyUpdate(spaceDoc, new Uint8Array(snapshot));
|
applyUpdate(spaceDoc, new Uint8Array(snapshot));
|
||||||
|
|||||||
@@ -56,11 +56,8 @@ export function useDocMetaHelper() {
|
|||||||
const setDocReadonly = useCallback(
|
const setDocReadonly = useCallback(
|
||||||
(docId: string, readonly: boolean) => {
|
(docId: string, readonly: boolean) => {
|
||||||
const doc = workspaceService.workspace.docCollection.getDoc(docId);
|
const doc = workspaceService.workspace.docCollection.getDoc(docId);
|
||||||
if (doc?.blockCollection) {
|
if (doc) {
|
||||||
workspaceService.workspace.docCollection.awarenessStore.setReadonly(
|
doc.readonly = readonly;
|
||||||
doc.blockCollection,
|
|
||||||
readonly
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[workspaceService]
|
[workspaceService]
|
||||||
|
|||||||
+2
-2
@@ -359,7 +359,7 @@ export const ShapeSettings = () => {
|
|||||||
) as EdgelessRootService;
|
) as EdgelessRootService;
|
||||||
const surface = getSurfaceBlock(doc);
|
const surface = getSurfaceBlock(doc);
|
||||||
if (!surface) return;
|
if (!surface) return;
|
||||||
doc.awarenessStore.setReadonly(doc.blockCollection, false);
|
doc.readonly = false;
|
||||||
surface.getElementsByType('shape').forEach(node => {
|
surface.getElementsByType('shape').forEach(node => {
|
||||||
const shape = node as ShapeElementModel;
|
const shape = node as ShapeElementModel;
|
||||||
const { shapeType, radius } = shape;
|
const { shapeType, radius } = shape;
|
||||||
@@ -367,7 +367,7 @@ export const ShapeSettings = () => {
|
|||||||
const props = editorSetting.get(`shape:${shapeName}`);
|
const props = editorSetting.get(`shape:${shapeName}`);
|
||||||
edgelessService.crud.updateElement(shape.id, props);
|
edgelessService.crud.updateElement(shape.id, props);
|
||||||
});
|
});
|
||||||
doc.awarenessStore.setReadonly(doc.blockCollection, true);
|
doc.readonly = true;
|
||||||
},
|
},
|
||||||
[editorSetting]
|
[editorSetting]
|
||||||
);
|
);
|
||||||
|
|||||||
+4
-4
@@ -77,11 +77,11 @@ export const EdgelessSnapshot = (props: Props) => {
|
|||||||
) as EdgelessRootService;
|
) as EdgelessRootService;
|
||||||
const elements = getElements(doc);
|
const elements = getElements(doc);
|
||||||
const props = editorSetting.get(keyName) as any;
|
const props = editorSetting.get(keyName) as any;
|
||||||
doc.awarenessStore.setReadonly(doc.blockCollection, false);
|
doc.readonly = false;
|
||||||
elements.forEach(element => {
|
elements.forEach(element => {
|
||||||
edgelessService.crud.updateElement(element.id, props);
|
edgelessService.crud.updateElement(element.id, props);
|
||||||
});
|
});
|
||||||
doc.awarenessStore.setReadonly(doc.blockCollection, true);
|
doc.readonly = true;
|
||||||
}, [editorSetting, getElements, keyName]);
|
}, [editorSetting, getElements, keyName]);
|
||||||
|
|
||||||
const renderEditor = useCallback(async () => {
|
const renderEditor = useCallback(async () => {
|
||||||
@@ -112,7 +112,7 @@ export const EdgelessSnapshot = (props: Props) => {
|
|||||||
) as EdgelessRootService;
|
) as EdgelessRootService;
|
||||||
edgelessService.specSlots.viewConnected.once(({ component }) => {
|
edgelessService.specSlots.viewConnected.once(({ component }) => {
|
||||||
const edgelessBlock = component as any;
|
const edgelessBlock = component as any;
|
||||||
doc.awarenessStore.setReadonly(doc.blockCollection, false);
|
doc.readonly = false;
|
||||||
edgelessBlock.editorViewportSelector = 'ref-viewport';
|
edgelessBlock.editorViewportSelector = 'ref-viewport';
|
||||||
const frame = getFrameBlock(doc);
|
const frame = getFrameBlock(doc);
|
||||||
if (frame) {
|
if (frame) {
|
||||||
@@ -121,7 +121,7 @@ export const EdgelessSnapshot = (props: Props) => {
|
|||||||
}
|
}
|
||||||
const bound = boundMap.get(docName);
|
const bound = boundMap.get(docName);
|
||||||
bound && edgelessService.viewport.setViewportByBound(bound);
|
bound && edgelessService.viewport.setViewportByBound(bound);
|
||||||
doc.awarenessStore.setReadonly(doc.blockCollection, true);
|
doc.readonly = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
// append to dom node
|
// append to dom node
|
||||||
|
|||||||
+1
-4
@@ -51,10 +51,7 @@ const useLoadDoc = (pageId: string) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (doc && isInTrash) {
|
if (doc && isInTrash) {
|
||||||
currentWorkspace.docCollection.awarenessStore.setReadonly(
|
doc.blockSuiteDoc.readonly = true;
|
||||||
doc.blockSuiteDoc.blockCollection,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}, [currentWorkspace.docCollection.awarenessStore, doc, isInTrash]);
|
}, [currentWorkspace.docCollection.awarenessStore, doc, isInTrash]);
|
||||||
|
|
||||||
|
|||||||
@@ -211,10 +211,7 @@ const SharePageInner = ({
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
const { doc } = workspace.scope.get(DocsService).open(docId);
|
const { doc } = workspace.scope.get(DocsService).open(docId);
|
||||||
|
|
||||||
workspace.docCollection.awarenessStore.setReadonly(
|
doc.blockSuiteDoc.readonly = true;
|
||||||
doc.blockSuiteDoc.blockCollection,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
setPage(doc);
|
setPage(doc);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user