Files
notesnook/packages/core/api/settings.js

184 lines
4.3 KiB
JavaScript
Raw Normal View History

import { EV, EVENTS } from "../common";
2020-12-05 11:26:02 +05:00
import id from "../utils/id";
2021-07-12 13:48:19 +05:00
import "../types";
import setManipulator from "../utils/set";
2020-12-05 11:26:02 +05:00
2020-11-25 15:04:39 +05:00
class Settings {
/**
*
* @param {import("./index").default} db
*/
constructor(db) {
this._db = db;
}
2021-07-12 13:48:19 +05:00
async init() {
var settings = await this._db.storage.read("settings");
this._initSettings(settings);
await this._saveSettings(false);
EV.subscribe(EVENTS.userLoggedOut, async () => {
2021-07-12 13:48:19 +05:00
this._initSettings();
await this._saveSettings(false);
2021-07-12 13:48:19 +05:00
});
2020-12-05 11:26:02 +05:00
}
get raw() {
return this._settings;
}
2020-12-10 14:23:33 +05:00
async merge(item) {
2021-12-20 14:37:06 +05:00
if (this._settings.dateModified > (await this._db.lastSynced())) {
this._settings.id = item.id;
2021-07-12 13:48:19 +05:00
this._settings.pins = setManipulator.union(
this._settings.pins,
2021-10-29 14:05:11 +05:00
item.pins,
(p) => p.data.id
2021-07-12 13:48:19 +05:00
);
this._settings.groupOptions = {
...this._settings.groupOptions,
2021-10-29 14:05:11 +05:00
...item.groupOptions,
};
2022-06-29 11:42:00 +05:00
this._settings.toolbarConfig = {
...this._settings.toolbarConfig,
...item.toolbarConfig,
};
2021-10-29 14:05:11 +05:00
this._settings.aliases = {
...this._settings.aliases,
...item.aliases,
2021-07-12 13:48:19 +05:00
};
2021-12-20 14:37:06 +05:00
this._settings.dateModified = Date.now();
2021-07-12 13:48:19 +05:00
} else {
this._initSettings(item);
2021-07-12 13:48:19 +05:00
}
2021-10-29 14:05:11 +05:00
await this._saveSettings(false);
2020-11-25 15:04:39 +05:00
}
2021-07-12 13:48:19 +05:00
/**
*
2021-07-12 14:03:59 +05:00
* @param {GroupingKey} key
2021-07-12 13:48:19 +05:00
* @param {GroupOptions} groupOptions
*/
async setGroupOptions(key, groupOptions) {
this._settings.groupOptions[key] = groupOptions;
await this._saveSettings();
}
/**
*
2021-07-12 14:03:59 +05:00
* @param {GroupingKey} key
2021-07-12 13:48:19 +05:00
* @returns {GroupOptions}
*/
getGroupOptions(key) {
return (
this._settings.groupOptions[key] || {
groupBy: "default",
sortBy:
key === "trash"
? "dateDeleted"
: key === "tags"
? "dateCreated"
: "dateEdited",
sortDirection: "desc",
}
);
2020-11-25 15:04:39 +05:00
}
2022-06-29 11:42:00 +05:00
/**
*
* @param {string} key
* @param {{preset: string, config?: any[]}} config
*/
async setToolbarConfig(key, config) {
this._settings.toolbarConfig[key] = config;
await this._saveSettings();
}
/**
*
* @param {string} key
* @returns {{preset: string, config: any[]}}
*/
getToolbarConfig(key) {
return this._settings.toolbarConfig[key];
}
2021-09-03 12:20:58 +05:00
async setAlias(id, name) {
this._settings.aliases[id] = name;
await this._saveSettings();
}
getAlias(id) {
return this._settings.aliases[id];
}
2020-11-25 15:04:39 +05:00
async pin(type, data) {
2020-11-25 20:49:29 +05:00
if (type !== "notebook" && type !== "topic" && type !== "tag")
2020-11-25 15:04:39 +05:00
throw new Error("This item cannot be pinned.");
2020-11-25 20:52:54 +05:00
if (this.isPinned(data.id)) return;
2020-11-25 20:49:29 +05:00
this._settings.pins.push({ type, data });
2021-07-12 13:48:19 +05:00
await this._saveSettings();
2020-11-25 15:04:39 +05:00
}
async unpin(id) {
2020-11-25 20:49:29 +05:00
const index = this._settings.pins.findIndex((i) => i.data.id === id);
2020-11-25 15:04:39 +05:00
if (index <= -1) return;
this._settings.pins.splice(index, 1);
2021-07-12 13:48:19 +05:00
await this._saveSettings();
2020-11-25 15:04:39 +05:00
}
2020-11-25 20:52:54 +05:00
isPinned(id) {
2020-11-25 20:59:27 +05:00
return this._settings.pins.findIndex((v) => v.data.id === id) > -1;
2020-11-25 20:52:54 +05:00
}
2020-11-25 15:04:39 +05:00
get pins() {
2021-01-10 10:54:47 +05:00
return this._settings.pins.reduce((prev, pin) => {
if (!pin || !pin.data) return;
let item = null;
2020-11-25 15:04:39 +05:00
if (pin.type === "notebook") {
const notebook = this._db.notebooks.notebook(pin.data.id);
item = notebook ? notebook.data : null;
2020-11-25 15:04:39 +05:00
} else if (pin.type === "topic") {
const notebook = this._db.notebooks.notebook(pin.data.notebookId);
if (notebook) {
const topic = notebook.topics.topic(pin.data.id);
if (topic) item = topic._topic;
}
2020-11-25 15:04:39 +05:00
} else if (pin.type === "tag") {
item = this._db.tags.tag(pin.data.id);
2020-11-25 15:04:39 +05:00
}
if (item) prev.push(item);
else this.unpin(pin.data.id); // TODO risky.
2021-01-10 10:54:47 +05:00
return prev;
}, []);
2020-11-25 15:04:39 +05:00
}
2021-07-12 13:48:19 +05:00
_initSettings(settings) {
2021-07-12 13:48:19 +05:00
this._settings = {
type: "settings",
id: id(),
pins: [],
groupOptions: {},
2022-06-29 11:42:00 +05:00
toolbarConfig: {},
2021-09-03 12:20:58 +05:00
aliases: {},
2021-12-20 14:37:06 +05:00
dateModified: 0,
2021-07-12 13:48:19 +05:00
dateCreated: 0,
...(settings || {}),
2021-07-12 13:48:19 +05:00
};
}
2021-12-20 14:37:06 +05:00
async _saveSettings(updateDateModified = true) {
if (updateDateModified) {
this._settings.dateModified = Date.now();
this._settings.synced = false;
}
2021-12-20 14:37:06 +05:00
await this._db.storage.write("settings", this._settings);
this._db.eventManager.publish(EVENTS.databaseUpdated, this._settings);
2021-07-12 13:48:19 +05:00
}
2020-11-25 15:04:39 +05:00
}
export default Settings;