Skip to content

Commit

Permalink
Add option to change container of all bookmarks in a folder (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgssa committed Jan 28, 2020
1 parent 1a64359 commit 47973de
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 31 deletions.
42 changes: 33 additions & 9 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,42 @@ async function onMessageReceived(message) {
async function onBookmarkCreated(id, bookmark, isEdit) {
if (wasInternallyCreated) {
wasInternallyCreated = false;
} else if (bookmark.type === 'bookmark') {
info.id = id;
info.name = bookmark.title;
info.url = bookmark.url;
info.parentId = bookmark.parentId;
} else if (bookmark.type === 'bookmark' || (bookmark.type === 'folder' && isEdit)) {
info.bookmark = {};

info.bookmark.id = id;
info.bookmark.name = bookmark.title;
info.bookmark.url = bookmark.url || '';
info.bookmark.parentId = bookmark.parentId;
info.bookmark.children = [];

info.bookmark.isFolder = bookmark.type === 'folder';
info.bookmark.isEdit = isEdit;

if (info.bookmark.isFolder) {
const children = (await browser.bookmarks.getChildren(info.bookmark.id))
.filter(child => child.type === 'bookmark');
for (const child of children) {
info.bookmark.children.push({
id: child.id,
name: child.title,
url: child.url,
parentId: child.parentId,

isFolder: false,
isEdit: true,
});
}

const matches = info.url.match(new RegExp(`#${info.preferences['redirect-key'].value}-(.*)`));
info.containerId = matches ? matches[1] : 'none';
if (info.bookmark.children.length > 0) {
info.bookmark.url = info.bookmark.children[0].url;
}
}

info.isEdit = isEdit;
const matches = info.bookmark.url.match(new RegExp(`#${info.preferences['redirect-key'].value}-(.*)`));
info.bookmark.containerId = matches ? matches[1] : 'none';

info.windowId = (await browser.windows.create({
info.bookmark.windowId = (await browser.windows.create({
url: `${browser.runtime.getURL('./popup/popup.html')}`,
type: 'popup',
width: 375,
Expand Down
70 changes: 48 additions & 22 deletions src/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,29 @@ async function getInfo() {
action: 'get-info',
});

const title = info.isEdit ? 'Edit Container Bookmark' : 'New Container Bookmark';
let title = '';
if (info.bookmark.isFolder) {
title = 'Edit Container Folder';
} else if (info.bookmark.isEdit) {
title = 'Edit Container Bookmark';
} else {
title = 'New Container Bookmark';
}
document.title = title;
titleHeader.textContent = title;

nameField.value = info.name;
urlField.value = info.url;
nameField.value = info.bookmark.name;
if (info.bookmark.isFolder) {
urlField.setAttribute('disabled', 'true');
} else {
urlField.value = info.bookmark.url;
}

addOptions(folderDropdown, info.folders);
addOptions(containerDropdown, info.containers);

folderDropdown.value = info.parentId;
containerDropdown.value = info.containerId;
folderDropdown.value = info.bookmark.parentId;
containerDropdown.value = info.bookmark.containerId;

onContainerDropdownChanged();
} catch (error) {
Expand Down Expand Up @@ -62,7 +73,7 @@ async function onContainerDropdownChanged() {
try {
await browser.runtime.sendMessage({
action: 'resize-window',
windowId: info.windowId,
windowId: info.bookmark.windowId,
width: 375,
height: height,
});
Expand All @@ -74,10 +85,10 @@ async function onContainerDropdownChanged() {

async function onCancelButtonClicked() {
try {
if (!info.isEdit) {
if (!info.bookmark.isEdit) {
await browser.runtime.sendMessage({
action: 'remove-bookmark',
id: info.id,
id: info.bookmark.id,
});
}

Expand All @@ -90,20 +101,12 @@ async function onCancelButtonClicked() {

async function onDoneButtonClicked() {
try {
await browser.runtime.sendMessage({
action: 'add-bookmark',
id: info.id,
old: {
parentId: info.parentId,
title: info.name,
url: info.url,
},
new: {
parentId: folderDropdown.value,
title: nameField.value,
url: containerDropdown.value === 'none' ? urlField.value : `${urlField.value.replace(new RegExp(`#${info.preferences['redirect-key'].value}-(.*)`), '')}#${info.preferences['redirect-key'].value}-${containerDropdown.value}`,
},
});
if (info.bookmark.isFolder) {
for (const child of info.bookmark.children) {
await addBookmark(child, child.name, child.url, child.parentId, containerDropdown.value);
}
}
await addBookmark(info.bookmark, nameField.value, urlField.value, folderDropdown.value, containerDropdown.value);

window.close();
} catch (error) {
Expand All @@ -112,6 +115,29 @@ async function onDoneButtonClicked() {
}
}

async function addBookmark(bookmarkInfo, title, url, parentId, containerId) {
const oldInfo = {
parentId: bookmarkInfo.parentId,
title: bookmarkInfo.name,
};
const newInfo = {
parentId,
title,
};

if (!bookmarkInfo.isFolder) {
oldInfo.url = bookmarkInfo.url;
newInfo.url = containerId === 'none' ? url : `${url.replace(new RegExp(`#${info.preferences['redirect-key'].value}-(.*)`), '')}#${info.preferences['redirect-key'].value}-${containerId}`;
}

await browser.runtime.sendMessage({
action: 'add-bookmark',
id: bookmarkInfo.id,
old: oldInfo,
new: newInfo,
});
}

/**
* @param {HTMLSelectElement} dropdown
* @param {OptionDetails[]} options
Expand Down

0 comments on commit 47973de

Please sign in to comment.