Compare commits
3 Commits
9696b78728
...
2ee0dcb0ed
Author | SHA1 | Date |
---|---|---|
Bauke | 2ee0dcb0ed | |
Bauke | 1bf8f519c6 | |
Bauke | ce7fbfe349 |
|
@ -6,7 +6,7 @@ export default function createManifest(
|
|||
const manifest: Record<string, unknown> = {
|
||||
name: 'Queue',
|
||||
description: 'A WebExtension for queueing links.',
|
||||
version: '0.3.1',
|
||||
version: '0.3.2',
|
||||
permissions: ['contextMenus', 'storage'],
|
||||
options_ui: {
|
||||
page: 'options/index.html',
|
||||
|
|
|
@ -39,10 +39,12 @@ export class Settings {
|
|||
|
||||
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: id,
|
||||
sortIndex,
|
||||
text,
|
||||
url,
|
||||
};
|
||||
|
@ -52,7 +54,7 @@ export class Settings {
|
|||
[`qi${id}`]: {
|
||||
added: item.added.toISOString(),
|
||||
id,
|
||||
sortIndex: id,
|
||||
sortIndex,
|
||||
text,
|
||||
url,
|
||||
},
|
||||
|
@ -68,19 +70,13 @@ export class Settings {
|
|||
throw new Error(`Failed to move item with ID: ${id}`);
|
||||
}
|
||||
|
||||
const previousIndex = targetItem.sortIndex;
|
||||
let targetIndex = previousIndex;
|
||||
if (direction === 'down') {
|
||||
targetIndex += 1;
|
||||
} else if (direction === 'up') {
|
||||
targetIndex -= 1;
|
||||
}
|
||||
|
||||
const currentIndex = targetItem.sortIndex;
|
||||
const targetIndex = this.nextQueueItemSortIndex(currentIndex, direction);
|
||||
const existingItem = this.queue.find(
|
||||
(item) => item.sortIndex === targetIndex,
|
||||
);
|
||||
if (existingItem !== undefined) {
|
||||
existingItem.sortIndex = previousIndex;
|
||||
if (existingItem !== undefined && targetIndex !== undefined) {
|
||||
existingItem.sortIndex = currentIndex;
|
||||
targetItem.sortIndex = targetIndex;
|
||||
await this.save();
|
||||
}
|
||||
|
@ -91,10 +87,37 @@ export class Settings {
|
|||
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) {
|
||||
|
|
Loading…
Reference in New Issue