2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2021-01-23 10:50:07 +05:00
|
|
|
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";
|
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-04-07 20:17:55 +05:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 13:48:19 +05:00
|
|
|
async init() {
|
2021-09-26 11:47:13 +05:00
|
|
|
var settings = await this._db.storage.read("settings");
|
2021-07-16 18:46:45 +05:00
|
|
|
this._initSettings(settings);
|
2021-12-15 15:23:13 +05:00
|
|
|
await this._saveSettings(false);
|
2021-07-12 21:07:03 +05:00
|
|
|
|
2022-07-20 18:29:42 +05:00
|
|
|
EV.subscribe(EVENTS.userLoggedOut, async () => {
|
2021-07-12 13:48:19 +05:00
|
|
|
this._initSettings();
|
2022-07-20 18:29:42 +05:00
|
|
|
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())) {
|
2022-07-20 19:55:00 +05:00
|
|
|
this._settings.id = item.id;
|
2021-07-12 13:48:19 +05:00
|
|
|
this._settings.groupOptions = {
|
|
|
|
|
...this._settings.groupOptions,
|
2022-08-31 06:33:37 +05:00
|
|
|
...item.groupOptions
|
2021-10-29 14:05:11 +05:00
|
|
|
};
|
2022-06-29 11:42:00 +05:00
|
|
|
this._settings.toolbarConfig = {
|
|
|
|
|
...this._settings.toolbarConfig,
|
2022-08-31 06:33:37 +05:00
|
|
|
...item.toolbarConfig
|
2022-06-29 11:42:00 +05:00
|
|
|
};
|
2021-10-29 14:05:11 +05:00
|
|
|
this._settings.aliases = {
|
|
|
|
|
...this._settings.aliases,
|
2022-08-31 06:33:37 +05:00
|
|
|
...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 {
|
2021-07-16 18:46:45 +05:00
|
|
|
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) {
|
2021-07-12 20:53:13 +05:00
|
|
|
return (
|
|
|
|
|
this._settings.groupOptions[key] || {
|
2021-07-16 18:46:45 +05:00
|
|
|
groupBy: "default",
|
2021-12-25 14:06:18 +05:00
|
|
|
sortBy:
|
|
|
|
|
key === "trash"
|
|
|
|
|
? "dateDeleted"
|
|
|
|
|
: key === "tags"
|
|
|
|
|
? "dateCreated"
|
|
|
|
|
: "dateEdited",
|
2022-08-31 06:33:37 +05:00
|
|
|
sortDirection: "desc"
|
2021-07-12 20:53:13 +05:00
|
|
|
}
|
|
|
|
|
);
|
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];
|
|
|
|
|
}
|
2023-02-24 11:01:35 +05:00
|
|
|
/**
|
|
|
|
|
* Setting to -1 means never clear trash.
|
|
|
|
|
* @param {7 | 30 | 365 | -1} time
|
|
|
|
|
*/
|
|
|
|
|
async setTrashCleanupInterval(time) {
|
|
|
|
|
this._settings.trashCleanupInterval = time;
|
|
|
|
|
await this._saveSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 08:06:34 +05:00
|
|
|
/**
|
|
|
|
|
* @returns {7 | 30 | 365 | -1}
|
|
|
|
|
*/
|
2023-02-24 11:01:35 +05:00
|
|
|
getTrashCleanupInterval() {
|
|
|
|
|
return this._settings.trashCleanupInterval || 7;
|
|
|
|
|
}
|
2021-09-03 12:20:58 +05:00
|
|
|
|
2021-07-16 18:46:45 +05:00
|
|
|
_initSettings(settings) {
|
2021-07-12 13:48:19 +05:00
|
|
|
this._settings = {
|
|
|
|
|
type: "settings",
|
|
|
|
|
id: id(),
|
|
|
|
|
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,
|
2023-02-24 11:01:35 +05:00
|
|
|
trashCleanupInterval: 7,
|
2022-08-31 06:33:37 +05:00
|
|
|
...(settings || {})
|
2021-07-12 13:48:19 +05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 14:37:06 +05:00
|
|
|
async _saveSettings(updateDateModified = true) {
|
2022-06-06 10:19:57 +05:00
|
|
|
if (updateDateModified) {
|
|
|
|
|
this._settings.dateModified = Date.now();
|
|
|
|
|
this._settings.synced = false;
|
|
|
|
|
}
|
2021-12-20 14:37:06 +05:00
|
|
|
|
2021-09-26 11:47:13 +05:00
|
|
|
await this._db.storage.write("settings", this._settings);
|
2022-07-20 17:50:12 +05:00
|
|
|
this._db.eventManager.publish(EVENTS.databaseUpdated, this._settings);
|
2021-07-12 13:48:19 +05:00
|
|
|
}
|
2020-11-25 15:04:39 +05:00
|
|
|
}
|
2020-11-25 15:18:57 +05:00
|
|
|
export default Settings;
|