mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-01 06:10:16 +08:00
feat(core): remember backlink open/close state (#9073)
fix AF-1850, AF-1883
This commit is contained in:
@@ -27,6 +27,7 @@ import { configurePermissionsModule } from './permissions';
|
||||
import { configureQuickSearchModule } from './quicksearch';
|
||||
import { configureShareDocsModule } from './share-doc';
|
||||
import { configureShareSettingModule } from './share-setting';
|
||||
import { configureCommonGlobalStorageImpls } from './storage';
|
||||
import { configureSystemFontFamilyModule } from './system-font-family';
|
||||
import { configureTagModule } from './tag';
|
||||
import { configureTelemetryModule } from './telemetry';
|
||||
@@ -71,4 +72,5 @@ export function configureCommonModules(framework: Framework) {
|
||||
configureOpenInApp(framework);
|
||||
configAtMenuConfigModule(framework);
|
||||
configureDndModule(framework);
|
||||
configureCommonGlobalStorageImpls(framework);
|
||||
}
|
||||
|
||||
+30
-13
@@ -1,13 +1,21 @@
|
||||
import type { GlobalCache, GlobalState, Memento } from '@toeverything/infra';
|
||||
import type {
|
||||
GlobalCache,
|
||||
GlobalSessionState,
|
||||
GlobalState,
|
||||
Memento,
|
||||
} from '@toeverything/infra';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
export class LocalStorageMemento implements Memento {
|
||||
constructor(private readonly prefix: string) {}
|
||||
export class StorageMemento implements Memento {
|
||||
constructor(
|
||||
private readonly storage: Storage,
|
||||
private readonly prefix: string
|
||||
) {}
|
||||
|
||||
keys(): string[] {
|
||||
const keys: string[] = [];
|
||||
for (let i = 0; i < localStorage.length; i++) {
|
||||
const key = localStorage.key(i);
|
||||
for (let i = 0; i < this.storage.length; i++) {
|
||||
const key = this.storage.key(i);
|
||||
if (key && key.startsWith(this.prefix)) {
|
||||
keys.push(key.slice(this.prefix.length));
|
||||
}
|
||||
@@ -16,12 +24,12 @@ export class LocalStorageMemento implements Memento {
|
||||
}
|
||||
|
||||
get<T>(key: string): T | undefined {
|
||||
const json = localStorage.getItem(this.prefix + key);
|
||||
const json = this.storage.getItem(this.prefix + key);
|
||||
return json ? JSON.parse(json) : undefined;
|
||||
}
|
||||
watch<T>(key: string): Observable<T | undefined> {
|
||||
return new Observable<T | undefined>(subscriber => {
|
||||
const json = localStorage.getItem(this.prefix + key);
|
||||
const json = this.storage.getItem(this.prefix + key);
|
||||
const first = json ? JSON.parse(json) : undefined;
|
||||
subscriber.next(first);
|
||||
|
||||
@@ -35,14 +43,14 @@ export class LocalStorageMemento implements Memento {
|
||||
});
|
||||
}
|
||||
set<T>(key: string, value: T): void {
|
||||
localStorage.setItem(this.prefix + key, JSON.stringify(value));
|
||||
this.storage.setItem(this.prefix + key, JSON.stringify(value));
|
||||
const channel = new BroadcastChannel(this.prefix + key);
|
||||
channel.postMessage(value);
|
||||
channel.close();
|
||||
}
|
||||
|
||||
del(key: string): void {
|
||||
localStorage.removeItem(this.prefix + key);
|
||||
this.storage.removeItem(this.prefix + key);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
@@ -53,19 +61,28 @@ export class LocalStorageMemento implements Memento {
|
||||
}
|
||||
|
||||
export class LocalStorageGlobalCache
|
||||
extends LocalStorageMemento
|
||||
extends StorageMemento
|
||||
implements GlobalCache
|
||||
{
|
||||
constructor() {
|
||||
super('global-cache:');
|
||||
super(localStorage, 'global-cache:');
|
||||
}
|
||||
}
|
||||
|
||||
export class LocalStorageGlobalState
|
||||
extends LocalStorageMemento
|
||||
extends StorageMemento
|
||||
implements GlobalState
|
||||
{
|
||||
constructor() {
|
||||
super('global-state:');
|
||||
super(localStorage, 'global-state:');
|
||||
}
|
||||
}
|
||||
|
||||
export class SessionStorageGlobalSessionState
|
||||
extends StorageMemento
|
||||
implements GlobalSessionState
|
||||
{
|
||||
constructor() {
|
||||
super(sessionStorage, 'global-session-state:');
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
import { type Framework, GlobalCache, GlobalState } from '@toeverything/infra';
|
||||
import {
|
||||
type Framework,
|
||||
GlobalCache,
|
||||
GlobalSessionState,
|
||||
GlobalState,
|
||||
} from '@toeverything/infra';
|
||||
|
||||
import { DesktopApiService } from '../desktop-api';
|
||||
import { ElectronGlobalCache, ElectronGlobalState } from './impls/electron';
|
||||
import {
|
||||
LocalStorageGlobalCache,
|
||||
LocalStorageGlobalState,
|
||||
} from './impls/local-storage';
|
||||
SessionStorageGlobalSessionState,
|
||||
} from './impls/storage';
|
||||
|
||||
export function configureLocalStorageStateStorageImpls(framework: Framework) {
|
||||
framework.impl(GlobalCache, LocalStorageGlobalCache);
|
||||
@@ -16,3 +22,7 @@ export function configureElectronStateStorageImpls(framework: Framework) {
|
||||
framework.impl(GlobalCache, ElectronGlobalCache, [DesktopApiService]);
|
||||
framework.impl(GlobalState, ElectronGlobalState, [DesktopApiService]);
|
||||
}
|
||||
|
||||
export function configureCommonGlobalStorageImpls(framework: Framework) {
|
||||
framework.impl(GlobalSessionState, SessionStorageGlobalSessionState);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user