mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
refactor(core): separate editor & doc mode (#7873)
doc.mode -> primaryMode
(*new) editor.mode
New Service:
editor service
Change Mode:
```
const editor = useService(EditorService).editor;
editor.setMode('page')
```
Change primary mode
```
const editor = useService(EditorService).editor;
editor.doc.setPrimaryMode('page')
```
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import type { RootBlockModel } from '@blocksuite/blocks';
|
||||
|
||||
import { Entity } from '../../../framework';
|
||||
import type { DocScope } from '../scopes/doc';
|
||||
import type { DocsStore } from '../stores/docs';
|
||||
@@ -19,24 +21,22 @@ export class Doc extends Entity {
|
||||
public readonly record = this.scope.props.record;
|
||||
|
||||
readonly meta$ = this.record.meta$;
|
||||
readonly mode$ = this.record.mode$;
|
||||
readonly primaryMode$ = this.record.primaryMode$;
|
||||
readonly title$ = this.record.title$;
|
||||
readonly trash$ = this.record.trash$;
|
||||
|
||||
setMode(mode: DocMode) {
|
||||
return this.record.setMode(mode);
|
||||
setPrimaryMode(mode: DocMode) {
|
||||
return this.record.setPrimaryMode(mode);
|
||||
}
|
||||
|
||||
getMode() {
|
||||
return this.record.getMode();
|
||||
getPrimaryMode() {
|
||||
return this.record.getPrimaryMode();
|
||||
}
|
||||
|
||||
toggleMode() {
|
||||
return this.record.toggleMode();
|
||||
}
|
||||
|
||||
observeMode() {
|
||||
return this.record.observeMode();
|
||||
togglePrimaryMode() {
|
||||
this.setPrimaryMode(
|
||||
this.getPrimaryMode() === 'edgeless' ? 'page' : 'edgeless'
|
||||
);
|
||||
}
|
||||
|
||||
moveToTrash() {
|
||||
@@ -54,4 +54,16 @@ export class Doc extends Entity {
|
||||
setPriorityLoad(priority: number) {
|
||||
return this.store.setPriorityLoad(this.id, priority);
|
||||
}
|
||||
|
||||
changeDocTitle(newTitle: string) {
|
||||
const pageBlock = this.blockSuiteDoc.getBlocksByFlavour('affine:page').at(0)
|
||||
?.model as RootBlockModel | undefined;
|
||||
if (pageBlock) {
|
||||
this.blockSuiteDoc.transact(() => {
|
||||
pageBlock.title.delete(0, pageBlock.title.length);
|
||||
pageBlock.title.insert(newTitle, 0);
|
||||
});
|
||||
this.record.setMeta({ title: newTitle });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,21 +55,24 @@ export class DocRecordList extends Entity {
|
||||
return this.docs$.map(record => record.find(record => record.id === id));
|
||||
}
|
||||
|
||||
public setMode(id: string, mode: DocMode) {
|
||||
return this.store.setDocModeSetting(id, mode);
|
||||
public setPrimaryMode(id: string, mode: DocMode) {
|
||||
return this.store.setDocPrimaryModeSetting(id, mode);
|
||||
}
|
||||
|
||||
public getMode(id: string) {
|
||||
return this.store.getDocModeSetting(id);
|
||||
public getPrimaryMode(id: string) {
|
||||
return this.store.getDocPrimaryModeSetting(id);
|
||||
}
|
||||
|
||||
public toggleMode(id: string) {
|
||||
const mode = this.getMode(id) === 'edgeless' ? 'page' : 'edgeless';
|
||||
this.setMode(id, mode);
|
||||
return this.getMode(id);
|
||||
public togglePrimaryMode(id: string) {
|
||||
const mode = this.getPrimaryMode(id) === 'edgeless' ? 'page' : 'edgeless';
|
||||
this.setPrimaryMode(id, mode);
|
||||
return this.getPrimaryMode(id);
|
||||
}
|
||||
|
||||
public observeMode(id: string) {
|
||||
return this.store.watchDocModeSetting(id);
|
||||
public primaryMode$(id: string) {
|
||||
return LiveData.from(
|
||||
this.store.watchDocPrimaryModeSetting(id),
|
||||
this.getPrimaryMode(id)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,27 +26,17 @@ export class DocRecord extends Entity<{ id: string }> {
|
||||
this.docsStore.setDocMeta(this.id, meta);
|
||||
}
|
||||
|
||||
mode$: LiveData<DocMode> = LiveData.from(
|
||||
this.docsStore.watchDocModeSetting(this.id),
|
||||
primaryMode$: LiveData<DocMode> = LiveData.from(
|
||||
this.docsStore.watchDocPrimaryModeSetting(this.id),
|
||||
'page'
|
||||
).map(mode => (mode === 'edgeless' ? 'edgeless' : 'page'));
|
||||
|
||||
setMode(mode: DocMode) {
|
||||
return this.docsStore.setDocModeSetting(this.id, mode);
|
||||
setPrimaryMode(mode: DocMode) {
|
||||
return this.docsStore.setDocPrimaryModeSetting(this.id, mode);
|
||||
}
|
||||
|
||||
getMode() {
|
||||
return this.docsStore.getDocModeSetting(this.id);
|
||||
}
|
||||
|
||||
toggleMode() {
|
||||
const mode = this.getMode() === 'edgeless' ? 'page' : 'edgeless';
|
||||
this.setMode(mode);
|
||||
return this.getMode();
|
||||
}
|
||||
|
||||
observeMode() {
|
||||
return this.docsStore.watchDocModeSetting(this.id);
|
||||
getPrimaryMode() {
|
||||
return this.docsStore.getDocPrimaryModeSetting(this.id);
|
||||
}
|
||||
|
||||
moveToTrash() {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Unreachable } from '@affine/env/constant';
|
||||
import type { RootBlockModel } from '@blocksuite/blocks';
|
||||
|
||||
import { Service } from '../../../framework';
|
||||
import { initEmptyPage } from '../../../initialization';
|
||||
@@ -54,7 +53,7 @@ export class DocsService extends Service {
|
||||
|
||||
createDoc(
|
||||
options: {
|
||||
mode?: DocMode;
|
||||
primaryMode?: DocMode;
|
||||
title?: string;
|
||||
} = {}
|
||||
) {
|
||||
@@ -65,8 +64,8 @@ export class DocsService extends Service {
|
||||
if (!docRecord) {
|
||||
throw new Unreachable();
|
||||
}
|
||||
if (options.mode) {
|
||||
docRecord.setMode(options.mode);
|
||||
if (options.primaryMode) {
|
||||
docRecord.setPrimaryMode(options.primaryMode);
|
||||
}
|
||||
return docRecord;
|
||||
}
|
||||
@@ -100,15 +99,7 @@ export class DocsService extends Service {
|
||||
const { doc, release } = this.open(docId);
|
||||
doc.setPriorityLoad(10);
|
||||
await doc.waitForSyncReady();
|
||||
const pageBlock = doc.blockSuiteDoc.getBlocksByFlavour('affine:page').at(0)
|
||||
?.model as RootBlockModel | undefined;
|
||||
if (pageBlock) {
|
||||
doc.blockSuiteDoc.transact(() => {
|
||||
pageBlock.title.delete(0, pageBlock.title.length);
|
||||
pageBlock.title.insert(newTitle, 0);
|
||||
});
|
||||
doc.record.setMeta({ title: newTitle });
|
||||
}
|
||||
doc.changeDocTitle(newTitle);
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,15 +101,15 @@ export class DocsStore extends Store {
|
||||
this.workspaceService.workspace.docCollection.setDocMeta(id, meta);
|
||||
}
|
||||
|
||||
setDocModeSetting(id: string, mode: DocMode) {
|
||||
setDocPrimaryModeSetting(id: string, mode: DocMode) {
|
||||
return this.localState.set(`page:${id}:mode`, mode);
|
||||
}
|
||||
|
||||
getDocModeSetting(id: string) {
|
||||
getDocPrimaryModeSetting(id: string) {
|
||||
return this.localState.get<DocMode>(`page:${id}:mode`);
|
||||
}
|
||||
|
||||
watchDocModeSetting(id: string) {
|
||||
watchDocPrimaryModeSetting(id: string) {
|
||||
return this.localState.watch<DocMode>(`page:${id}:mode`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user