Skip to content

Commit 2b9eb15

Browse files
committed
feat: collapse all button in the sidebar which closes all the open directories
1 parent 1b63546 commit 2b9eb15

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
define(function (require, exports, module) {
2+
const AppInit = require("utils/AppInit");
3+
const ProjectManager = require("project/ProjectManager");
4+
5+
/**
6+
* This is the main function that handles the closing of all the directories
7+
*/
8+
function handleCollapseBtnClick() {
9+
// this will give us an array of array's
10+
// the root level directories will be at index 0, its next level will be at index 1 and so on
11+
const openNodes = ProjectManager._actionCreator.model.getOpenNodes();
12+
if (!openNodes || openNodes.length === 0) {
13+
return;
14+
}
15+
16+
// traversing from the back because the deepest nested directories should be closed first
17+
// Note: this is an array of all the directories at the deepest level
18+
for (let i = openNodes.length - 1; i >= 0; i--) {
19+
// close all the directories
20+
openNodes[i].forEach(function (folderPath) {
21+
try {
22+
// to close each dir
23+
ProjectManager._actionCreator.setDirectoryOpen(folderPath, false);
24+
} catch (error) {
25+
console.error("Failed to close folder:", folderPath, error);
26+
}
27+
});
28+
}
29+
}
30+
31+
/**
32+
* This function is responsible to create the 'Collapse All' button
33+
* and append it to the sidebar area on the project-files-header
34+
*/
35+
function createCollapseButton() {
36+
const $projectFilesHeader = $("#project-files-header");
37+
// make sure that we were able to get the project-files-header DOM element
38+
if ($projectFilesHeader.length === 0) {
39+
return;
40+
}
41+
42+
// create the collapse btn
43+
const $collapseBtn = $(`
44+
<div id="collapse-folders" class="btn-alt-quiet" title="Collapse All">
45+
<i class="fa-solid fa-chevron-down collapse-icon" aria-hidden="true"></i>
46+
<i class="fa-solid fa-chevron-up collapse-icon" aria-hidden="true"></i>
47+
</div>
48+
`);
49+
50+
$collapseBtn.on("click", handleCollapseBtnClick);
51+
52+
$projectFilesHeader.append($collapseBtn); // append the btn to the project-files-header
53+
54+
_setupHoverBehavior($collapseBtn); // hover functionality to show/hide the button
55+
}
56+
57+
/**
58+
* This function is responsible for the hover behavior to show/hide the collapse button
59+
* we only show the button when the cursor is over the sidebar area
60+
*/
61+
function _setupHoverBehavior($collapseBtn) {
62+
const $sidebar = $("#sidebar");
63+
if ($sidebar.length === 0) {
64+
return;
65+
}
66+
67+
$sidebar.on("mouseenter", function () {
68+
$collapseBtn.addClass("show");
69+
});
70+
71+
$sidebar.on("mouseleave", function () {
72+
$collapseBtn.removeClass("show");
73+
});
74+
}
75+
76+
AppInit.appReady(function () {
77+
createCollapseButton();
78+
});
79+
});

src/extensionsIntegrated/loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ define(function (require, exports, module) {
4545
require("./CSSColorPreview/main");
4646
require("./TabBar/main");
4747
require("./CustomSnippets/main");
48+
require("./CollapseFolders/main");
4849
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#collapse-folders {
2+
display: flex;
3+
flex-direction: column;
4+
align-items: center;
5+
justify-content: center;
6+
padding: 0.2em 0.4em;
7+
position: absolute !important;
8+
right: 0.5em;
9+
opacity: 0;
10+
visibility: hidden;
11+
transition:
12+
opacity 0.2s ease-in-out,
13+
visibility 0.2s ease-in-out;
14+
15+
.collapse-icon {
16+
font-size: 0.5em;
17+
line-height: 1;
18+
}
19+
20+
&.show {
21+
opacity: 1;
22+
visibility: visible;
23+
}
24+
}

src/styles/brackets.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
@import "Extn-DisplayShortcuts.less";
4646
@import "Extn-CSSColorPreview.less";
4747
@import "Extn-CustomSnippets.less";
48+
@import "Extn-CollapseFolders.less";
4849
@import "UserProfile.less";
4950

5051
/* Overall layout */

0 commit comments

Comments
 (0)