Files
standardnotes-app-web/app/assets/javascripts/web_device_interface.ts
Mo 50c92619ce refactor: migrate remaining angular components to react (#833)
* refactor: menuRow directive to MenuRow component

* refactor: migrate footer to react

* refactor: migrate actions menu to react

* refactor: migrate history menu to react

* fix: click outside handler use capture to trigger event before re-render occurs which would otherwise cause node.contains to return incorrect result (specifically for the account menu)

* refactor: migrate revision preview modal to react

* refactor: migrate permissions modal to react

* refactor: migrate password wizard to react

* refactor: remove unused input modal directive

* refactor: remove unused delay hide component

* refactor: remove unused filechange directive

* refactor: remove unused elemReady directive

* refactor: remove unused sn-enter directive

* refactor: remove unused lowercase directive

* refactor: remove unused autofocus directive

* refactor(wip): note view to react

* refactor: use mutation observer to deinit textarea listeners

* refactor: migrate challenge modal to react

* refactor: migrate note group view to react

* refactor(wip): migrate remaining classes

* fix: navigation parent ref

* refactor: fully remove angular assets

* fix: account switcher

* fix: application view state

* refactor: remove unused password wizard type

* fix: revision preview and permissions modal

* fix: remove angular comment

* refactor: react panel resizers for editor

* feat: simple panel resizer

* fix: use simple panel resizer everywhere

* fix: simplify panel resizer state

* chore: rename simple panel resizer to panel resizer

* refactor: simplify column layout

* fix: editor mount safety check

* fix: use inline onLoad callback for iframe, as setting onload after it loads will never call it

* chore: fix note view test

* chore(deps): upgrade snjs
2022-01-30 19:01:30 -06:00

164 lines
3.9 KiB
TypeScript

import {
DeviceInterface,
getGlobalScope,
SNApplication,
ApplicationIdentifier,
} from '@standardnotes/snjs';
import { Database } from '@/database';
import { Bridge } from './services/bridge';
export class WebDeviceInterface extends DeviceInterface {
private databases: Database[] = [];
constructor(private bridge: Bridge) {
super(
setTimeout.bind(getGlobalScope()),
setInterval.bind(getGlobalScope())
);
}
setApplication(application: SNApplication) {
const database = new Database(
application.identifier,
application.alertService
);
this.databases.push(database);
}
private databaseForIdentifier(identifier: ApplicationIdentifier) {
return this.databases.find(
(database) => database.databaseName === identifier
)!;
}
deinit() {
super.deinit();
for (const database of this.databases) {
database.deinit();
}
this.databases = [];
}
async getRawStorageValue(key: string) {
return localStorage.getItem(key) as any;
}
async getAllRawStorageKeyValues() {
const results = [];
for (const key of Object.keys(localStorage)) {
results.push({
key: key,
value: localStorage[key],
});
}
return results;
}
async setRawStorageValue(key: string, value: any) {
localStorage.setItem(key, value);
}
async removeRawStorageValue(key: string) {
localStorage.removeItem(key);
}
async removeAllRawStorageValues() {
localStorage.clear();
}
async openDatabase(identifier: ApplicationIdentifier) {
this.databaseForIdentifier(identifier).unlock();
return new Promise((resolve, reject) => {
this.databaseForIdentifier(identifier)
.openDatabase(() => {
resolve({ isNewDatabase: true });
})
.then(() => {
resolve({ isNewDatabase: false });
})
.catch((error) => {
reject(error);
});
}) as Promise<{ isNewDatabase?: boolean } | undefined>;
}
async getAllRawDatabasePayloads(identifier: ApplicationIdentifier) {
return this.databaseForIdentifier(identifier).getAllPayloads();
}
async saveRawDatabasePayload(
payload: any,
identifier: ApplicationIdentifier
) {
return this.databaseForIdentifier(identifier).savePayload(payload);
}
async saveRawDatabasePayloads(
payloads: any[],
identifier: ApplicationIdentifier
) {
return this.databaseForIdentifier(identifier).savePayloads(payloads);
}
async removeRawDatabasePayloadWithId(
id: string,
identifier: ApplicationIdentifier
) {
return this.databaseForIdentifier(identifier).deletePayload(id);
}
async removeAllRawDatabasePayloads(identifier: ApplicationIdentifier) {
return this.databaseForIdentifier(identifier).clearAllPayloads();
}
async getNamespacedKeychainValue(identifier: ApplicationIdentifier) {
const keychain = await this.getRawKeychainValue();
if (!keychain) {
return;
}
return keychain[identifier];
}
async setNamespacedKeychainValue(
value: any,
identifier: ApplicationIdentifier
) {
let keychain = await this.getRawKeychainValue();
if (!keychain) {
keychain = {};
}
return this.bridge.setKeychainValue({
...keychain,
[identifier]: value,
});
}
async clearNamespacedKeychainValue(identifier: ApplicationIdentifier) {
const keychain = await this.getRawKeychainValue();
if (!keychain) {
return;
}
delete keychain[identifier];
return this.bridge.setKeychainValue(keychain);
}
getRawKeychainValue(): Promise<any> {
return this.bridge.getKeychainValue();
}
legacy_setRawKeychainValue(value: unknown): Promise<any> {
return this.bridge.setKeychainValue(value);
}
clearRawKeychainValue() {
return this.bridge.clearKeychainValue();
}
openUrl(url: string) {
const win = window.open(url, '_blank');
if (win) {
win.focus();
}
}
}