Remove History and Settings files.
This commit is contained in:
parent
091da70bea
commit
fbd8de8b03
|
@ -1,141 +0,0 @@
|
||||||
import browser from 'webextension-polyfill';
|
|
||||||
|
|
||||||
import {migrate} from '@holllo/migration-helper';
|
|
||||||
|
|
||||||
import {History} from '../utilities/history.js';
|
|
||||||
import {
|
|
||||||
dataMigrations,
|
|
||||||
deserializeQueue,
|
|
||||||
serializeQueue,
|
|
||||||
} from './migrations.js';
|
|
||||||
|
|
||||||
export class Settings {
|
|
||||||
public static async fromSyncStorage(): Promise<Settings> {
|
|
||||||
const settings = new Settings();
|
|
||||||
|
|
||||||
const sync = await browser.storage.sync.get(null);
|
|
||||||
const migrated = (await migrate(
|
|
||||||
sync,
|
|
||||||
sync.version ?? settings.version,
|
|
||||||
dataMigrations,
|
|
||||||
)) as Record<string, any>;
|
|
||||||
|
|
||||||
settings.queue = deserializeQueue(migrated);
|
|
||||||
settings.version = migrated.version as string;
|
|
||||||
|
|
||||||
await settings.save();
|
|
||||||
return settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public manifest: browser.Manifest.ManifestBase;
|
|
||||||
public queue: Queue.Item[];
|
|
||||||
public version: string;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.manifest = browser.runtime.getManifest();
|
|
||||||
this.queue = [];
|
|
||||||
this.version = '0.0.0';
|
|
||||||
}
|
|
||||||
|
|
||||||
public async insertQueueItem(text: string, url: string): Promise<void> {
|
|
||||||
const id = this.newQueueItemId();
|
|
||||||
const sortIndex = this.newQueueItemSortIndex();
|
|
||||||
|
|
||||||
const item: Queue.Item = {
|
|
||||||
added: new Date(),
|
|
||||||
id,
|
|
||||||
sortIndex,
|
|
||||||
text,
|
|
||||||
url,
|
|
||||||
};
|
|
||||||
this.queue.push(item);
|
|
||||||
|
|
||||||
await browser.storage.sync.set({
|
|
||||||
[`qi${id}`]: {
|
|
||||||
added: item.added.toISOString(),
|
|
||||||
id,
|
|
||||||
sortIndex,
|
|
||||||
text,
|
|
||||||
url,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async moveQueueItem(
|
|
||||||
id: number,
|
|
||||||
direction: Queue.MoveDirection,
|
|
||||||
): Promise<void> {
|
|
||||||
const targetItem = this.queue.find((item) => item.id === id);
|
|
||||||
if (targetItem === undefined) {
|
|
||||||
throw new Error(`Failed to move item with ID: ${id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentIndex = targetItem.sortIndex;
|
|
||||||
const targetIndex = this.nextQueueItemSortIndex(currentIndex, direction);
|
|
||||||
const existingItem = this.queue.find(
|
|
||||||
(item) => item.sortIndex === targetIndex,
|
|
||||||
);
|
|
||||||
if (existingItem !== undefined && targetIndex !== undefined) {
|
|
||||||
existingItem.sortIndex = currentIndex;
|
|
||||||
targetItem.sortIndex = targetIndex;
|
|
||||||
await this.save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public newQueueItemId(): number {
|
|
||||||
const item = this.queue.sort((a, b) => b.id - a.id)[0];
|
|
||||||
return item === undefined ? 1 : item.id + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public newQueueItemSortIndex(): number {
|
|
||||||
const item = this.queue.sort((a, b) => b.sortIndex - a.sortIndex)[0];
|
|
||||||
return item === undefined ? 1 : item.sortIndex + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public nextQueueItem(): Queue.Item | undefined {
|
|
||||||
return this.queue.sort((a, b) => a.sortIndex - b.sortIndex)[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
public nextQueueItemSortIndex(
|
|
||||||
currentIndex: number,
|
|
||||||
direction: Queue.MoveDirection,
|
|
||||||
): number | undefined {
|
|
||||||
this.queue.sort((a, b) => {
|
|
||||||
return direction === 'up'
|
|
||||||
? b.sortIndex - a.sortIndex
|
|
||||||
: a.sortIndex - b.sortIndex;
|
|
||||||
});
|
|
||||||
|
|
||||||
let foundCurrent = false;
|
|
||||||
for (const item of this.queue) {
|
|
||||||
if (foundCurrent) {
|
|
||||||
return item.sortIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.sortIndex === currentIndex) {
|
|
||||||
foundCurrent = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async removeQueueItem(id: number): Promise<void> {
|
|
||||||
const itemIndex = this.queue.findIndex((item) => item.id === id);
|
|
||||||
if (itemIndex === -1) {
|
|
||||||
console.error(`Tried to remove an item with unknown ID: ${id}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const removedItems = this.queue.splice(itemIndex, 1);
|
|
||||||
await browser.storage.sync.remove(removedItems.map(({id}) => `qi${id}`));
|
|
||||||
|
|
||||||
const history = await History.fromLocalStorage();
|
|
||||||
await history.insertItems(removedItems);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async save(): Promise<void> {
|
|
||||||
await browser.storage.sync.set({
|
|
||||||
...serializeQueue(this.queue),
|
|
||||||
version: this.version,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,44 +0,0 @@
|
||||||
import browser from 'webextension-polyfill';
|
|
||||||
|
|
||||||
import {deserializeQueue, serializeQueue} from '../settings/migrations.js';
|
|
||||||
|
|
||||||
export class History {
|
|
||||||
public static async clear(): Promise<void> {
|
|
||||||
await browser.storage.local.remove('history');
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async fromLocalStorage(): Promise<History> {
|
|
||||||
const history = new History();
|
|
||||||
|
|
||||||
const stored = await browser.storage.local.get({history: []});
|
|
||||||
history.queue = deserializeQueue(stored.history);
|
|
||||||
|
|
||||||
return history;
|
|
||||||
}
|
|
||||||
|
|
||||||
public queue: Queue.Item[];
|
|
||||||
|
|
||||||
private constructor() {
|
|
||||||
this.queue = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
public async clear(): Promise<void> {
|
|
||||||
await History.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async insertItems(items: Queue.Item[]): Promise<void> {
|
|
||||||
this.queue = this.queue.concat(items);
|
|
||||||
|
|
||||||
for (const [index, item] of this.queue.entries()) {
|
|
||||||
item.id = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async save(): Promise<void> {
|
|
||||||
await browser.storage.local.set({
|
|
||||||
history: serializeQueue(this.queue),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue