Compare commits

..

3 Commits

2 changed files with 36 additions and 13 deletions

View File

@ -6,7 +6,7 @@ export default function createManifest(
const manifest: Record<string, unknown> = { const manifest: Record<string, unknown> = {
name: 'Queue', name: 'Queue',
description: 'A WebExtension for queueing links.', description: 'A WebExtension for queueing links.',
version: '0.3.1', version: '0.3.2',
permissions: ['contextMenus', 'storage'], permissions: ['contextMenus', 'storage'],
options_ui: { options_ui: {
page: 'options/index.html', page: 'options/index.html',

View File

@ -39,10 +39,12 @@ export class Settings {
public async insertQueueItem(text: string, url: string): Promise<void> { public async insertQueueItem(text: string, url: string): Promise<void> {
const id = this.newQueueItemId(); const id = this.newQueueItemId();
const sortIndex = this.newQueueItemSortIndex();
const item: Queue.Item = { const item: Queue.Item = {
added: new Date(), added: new Date(),
id, id,
sortIndex: id, sortIndex,
text, text,
url, url,
}; };
@ -52,7 +54,7 @@ export class Settings {
[`qi${id}`]: { [`qi${id}`]: {
added: item.added.toISOString(), added: item.added.toISOString(),
id, id,
sortIndex: id, sortIndex,
text, text,
url, url,
}, },
@ -68,19 +70,13 @@ export class Settings {
throw new Error(`Failed to move item with ID: ${id}`); throw new Error(`Failed to move item with ID: ${id}`);
} }
const previousIndex = targetItem.sortIndex; const currentIndex = targetItem.sortIndex;
let targetIndex = previousIndex; const targetIndex = this.nextQueueItemSortIndex(currentIndex, direction);
if (direction === 'down') {
targetIndex += 1;
} else if (direction === 'up') {
targetIndex -= 1;
}
const existingItem = this.queue.find( const existingItem = this.queue.find(
(item) => item.sortIndex === targetIndex, (item) => item.sortIndex === targetIndex,
); );
if (existingItem !== undefined) { if (existingItem !== undefined && targetIndex !== undefined) {
existingItem.sortIndex = previousIndex; existingItem.sortIndex = currentIndex;
targetItem.sortIndex = targetIndex; targetItem.sortIndex = targetIndex;
await this.save(); await this.save();
} }
@ -91,10 +87,37 @@ export class Settings {
return item === undefined ? 1 : item.id + 1; 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 { public nextQueueItem(): Queue.Item | undefined {
return this.queue.sort((a, b) => a.sortIndex - b.sortIndex)[0]; 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> { public async removeQueueItem(id: number): Promise<void> {
const itemIndex = this.queue.findIndex((item) => item.id === id); const itemIndex = this.queue.findIndex((item) => item.id === id);
if (itemIndex === -1) { if (itemIndex === -1) {