Skip to content

Commit

Permalink
feat: favourite node
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuu-Han committed May 26, 2024
1 parent 6d71fbf commit 9ae9d66
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/historymap/hmControllerTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ function createTab(url, callback) {
chrome.windows.update(tab.windowId, {focused: true});
});
}

function handleFavoriteNode(page) {
updatePage(page.pageId, 'toggleTag', { tag: 'favorite' });
}
10 changes: 10 additions & 0 deletions src/historymap/hmControllerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ function updatePage(pageId, type, data=null) {
case 'toggleCollapse': // update page attributes
page.update({ isCollapsed: !page.isCollapsed });
break;
case 'toggleTag':
let tags = page.tags;
const tag = data.tag;
if (tags.includes(tag)) {
tags = tags.filter(d => d !== tag);
} else {
tags.push(tag);
}
page.update({ tags });
break;
case 'beforeReopen': // before page reopened (from hm tree)
page.update({ incomingTabId: data.tabId });
break;
Expand Down
3 changes: 2 additions & 1 deletion src/historymap/hmModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// A opened webpage
class hmPage {
constructor({ pageId, tabId, time, pageObj, parentPageId, docId=null, isOpened=true, isCollapsed=false, forwardBack={forward: 0, back: 0}, highlights=[], incomingTabId=null }) {
constructor({ pageId, tabId, time, pageObj, parentPageId, docId=null, isOpened=true, isCollapsed=false, forwardBack={forward: 0, back: 0}, highlights=[], tags=[], incomingTabId=null }) {
this.pageId = pageId;
this.docId = docId; // a UUID of the document loaded, get from webNavigation API, needed for locating the page to update pageObj after navigation completed
this.tabId = tabId;
Expand All @@ -14,6 +14,7 @@ class hmPage {
// this.clicked = false;
this.forwardBack = forwardBack;
this.highlights = highlights;
this.tags = tags;

// optional
this.incomingTabId = incomingTabId; // the tabId of the incoming page, needed for locating the page that is reopened from hm tree
Expand Down
10 changes: 7 additions & 3 deletions src/historymap/hmTree/nodeMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @fileoverview Draw menu when hovering on a hmTree node
*/

function hmNodeMenu() {
function hmNodeMenu(node) {
var menu,
container;

Expand Down Expand Up @@ -34,8 +34,12 @@ function hmNodeMenu() {
.on('click', (e, d) => {
// prevent event bubbling
e.stopPropagation();

console.log(d.title);
let nodeData = selection.data()[0];
if (d.title === 'favorite') {
const isFav = !nodeData.tags.includes('favorite');
handleFavoriteNode(nodeData);
node.classed('favorite', isFav);
}
});

item.append('xhtml:img')
Expand Down
4 changes: 4 additions & 0 deletions src/historymap/hmTree/tree.css
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@
opacity: 1;
}

.hm-tree-node.favorite {
background-color: #fde047;
}

.btn-group {
display: flex;
flex-direction: row;
Expand Down
9 changes: 5 additions & 4 deletions src/historymap/hmTree/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,15 @@ function hmTreeView({
.attr('width', (d) => d.width)
.attr('height', (d) => d.height)
.html((d) => hmTreeNode(d).node().outerHTML);

// Node Menu
const menu = hmNodeMenu();

// Interaction
node
.style('cursor', 'pointer')
.on('click', (_, d) => handleOpenPage(d))
.on('mouseenter', function () {
// Append menu
const nodeContent = d3.select(this).select('.hm-tree-node');
const menu = hmNodeMenu(nodeContent);
d3.select(this)
.append('g')
.attr('transform', d => `translate(${d.width}, 0)`)
Expand All @@ -159,7 +158,7 @@ function hmTreeView({
.on('mouseleave', function () {
d3.select(this).select('.menu').remove();
});

// Collapse node
node.select('.icon-collapse')
.on('click', function (e, d) {
Expand Down Expand Up @@ -255,6 +254,7 @@ function hmTreeNode(hmPage) {
title: hmPage.pageObj.title,
favIconUrl: hmPage.pageObj.favIconUrl,
highlights: hmPage.highlights,
tags: hmPage.tags,
isLeaf: hmPage.isLeaf,
isCollapsed: hmPage.isCollapsed,
isDescendantOpened: hmPage.isDescendantOpened,
Expand All @@ -273,6 +273,7 @@ function hmTreeNode(hmPage) {
&& !d.isOpened
&& d.isDescendantOpened
);
node.classed('favorite', d => d.tags.includes('favorite'));

appendHeader();
}
Expand Down

0 comments on commit 9ae9d66

Please sign in to comment.