-
Notifications
You must be signed in to change notification settings - Fork 1
/
Folders.svelte
363 lines (269 loc) · 10.7 KB
/
Folders.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<script lang="ts">
import { DataStore } from "../src/Stores/DataStore";
import { GlobalSettings, SettingTypes } from '../src/Lib/Settings';
import { ModuleHandler } from '../src/Lib/ModuleHandler';
import { onMount } from 'svelte';
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const componentID: string = "Folders";
onMount(() => {
ModuleHandler.RegisterModule(componentID, ModuleHandler.ComponentSize.Small,
"A small module that allows you to bind folders to colorful icon buttons.\nThis is useful for quickly opening common used folders.\nHover over an icon to see the folder path and modify it.");
UpdateEmptyFolderCSS();
MinimizeAddFolder(MinimizedAddFolder);
});
type Folder = {
Path: string;
Hash: string;
Color: string;
}
const PopupSideChoiceToRem = (side: string) => {
switch (side) {
case "Left":
return "-17rem";
case "Right":
return "3rem";
}
}
let _Datastore = new DataStore(componentID);
let Folders = _Datastore.Get<Folder[]>("Folders", []);
let MinimizedAddFolder = _Datastore.Get<boolean>("MinimizedAddFolder", false);
let _Settings = GlobalSettings.GetInstance(componentID);
let PromptFolderUponAdd = _Settings.RegisterSetting("Quick Select","Prompts you to select a folder directly when creating a new folder slot", true, SettingTypes.Type.Checkbox);
let PopupSide = _Settings.RegisterSetting("Popup Side", "Side of the folder icon the popup appears on", "Right", SettingTypes.Type.Dropdown, <SettingTypes.Dropdown>{ Options: ["Left", "Right"]});
let SelectLatestFile = _Settings.RegisterSetting("Select Latest File", "Automatically selects the latest modified file in the folder when opening it", true, SettingTypes.Type.Checkbox);
let AddFolder = () => {
let hash: string = require('crypto').randomBytes(16).toString('hex');
_Datastore.Set("Folders", [...Folders, {
Path: "",
//Generate a random hash for each folder
Hash: hash,
Color: '#ffffff'
}]);
Folders = _Datastore.Get<Folder[]>("Folders", []);
UpdateEmptyFolderCSS();
if (PromptFolderUponAdd) {
SetFolder(null, hash);
}
}
let RemoveFolder = (event: MouseEvent) => {
//Button > Popup Bottom > Popup(has ID)
let folder = (event.target as HTMLElement).parentElement.parentElement as HTMLElement;
let folderHash = folder.id;
let newFolders = Folders.filter((folder) => {
return folder.Hash != folderHash;
});
_Datastore.Set("Folders", newFolders);
Folders = _Datastore.Get<Folder[]>("Folders", []);
UpdateEmptyFolderCSS();
}
let UpdateEmptyFolderCSS = () => {
let folders = _Datastore.Get<Folder[]>("Folders", []);
folders.forEach((folder) => {
if (folder.Path == "") {
const element = <HTMLElement>document.getElementById(`folderIcon-${folder.Hash}`);
if (!element) return;
element.classList.add("emptyFolder");
} else {
const element = <HTMLElement>document.getElementById(`folderIcon-${folder.Hash}`);
if (!element) return;
element.classList.remove("emptyFolder");
}
});
}
let SetFolder = (event: Event | null, forceHash?: string) => {
let folderHash = forceHash ?? ((event?.target as HTMLElement).parentElement.parentElement as HTMLElement).id;
//prompt the user to select a folder
/*@ts-ignore*/
let folderPath = require('electron').remote.dialog.showOpenDialogSync({
properties: ['openDirectory']
});
if (!folderPath) return;
let newFolders = Folders.map((folder) => {
if (folder.Hash == folderHash) {
folder.Path = folderPath[0];
}
return folder;
});
_Datastore.Set("Folders", newFolders);
Folders = _Datastore.Get<Folder[]>("Folders", []);
UpdateEmptyFolderCSS();
}
let OpenFolder = (event: Event) => {
let folder = (event.target as HTMLElement).parentElement.parentElement as HTMLElement;
let folderHash = folder.id;
let folderPath = Folders.find((folder) => {
return folder.Hash == folderHash;
})?.Path;
if (!folderPath) return;
if (!SelectLatestFile) {
const fileManager = process.platform === 'win32' ? 'explorer' : 'open';
const folderPathNormalized = path.normalize(folderPath);
spawn(fileManager, [folderPathNormalized]);
return;
}
fs.readdir(folderPath, (err, files) => {
if (err) {
console.log(err);
return;
}
// Sort files by modification time
const fileDetails = files.map((file) => {
const filePath = `${folderPath}/${file}`;
const stats = fs.statSync(filePath);
return {
filePath,
stats,
};
});
fileDetails.sort((a, b) => b.stats.mtime.getTime() - a.stats.mtime.getTime());
// Open folder with default selected file
const defaultSelectedFile = fileDetails[0].filePath.replace(/\//g, path.sep);
const fileManager = process.platform === 'win32' ? 'explorer' : 'open';
const folderPathNormalized = path.normalize(folderPath);
spawn(fileManager, ['/select,', defaultSelectedFile], {
cwd: folderPathNormalized,
});
});
}
let FolderColorChange = (event: Event) => {
_Datastore.Set("Folders", Folders);
const Hash = ((event?.target as HTMLElement).parentElement.parentElement as HTMLElement).id;
const element = <HTMLElement>document.getElementById(`folderIcon-${Hash}`);
if (!element) return;
element.setAttribute('style', `color: ${(event.target as HTMLInputElement).value}`);
}
let MinimizeAddFolder = (forceStatus?: boolean) => {
const element = <HTMLElement>document.getElementById("addFolder");
if (!element) return;
MinimizedAddFolder = forceStatus ?? !MinimizedAddFolder;
if (MinimizedAddFolder) {
element.classList.add("minimized");
} else {
element.classList.remove("minimized");
}
_Datastore.Set("MinimizedAddFolder", MinimizedAddFolder);
}
</script>
<main id={componentID}>
{#each Folders as folder}
<div class="folder" id={folder.Hash}>
<div class="folderIcon" id={`folderIcon-${folder.Hash}`}>
<svg
on:click={OpenFolder} on:keydown={OpenFolder} style="color: {folder.Color};"
id={folder.Hash} xmlns="http://www.w3.org/2000/svg" height="52" viewBox="0 96 960 960" width="52" fill="currentColor">
<path d="M141 896q-24 0-42-18.5T81 836V316q0-23 18-41.5t42-18.5h280l60 60h340q23 0 41.5 18.5T881 376v460q0 23-18.5 41.5T821 896H141Zm0-580v520h680V376H456l-60-60H141Zm0 0v520-520Z"/>
</svg>
</div>
<div class="folderPath" id={folder.Hash} style="--popupside: {PopupSideChoiceToRem(PopupSide)}">
<input type="text" id={`folderInput-${folder.Hash}`} bind:value={folder.Path} on:change={UpdateEmptyFolderCSS} placeholder="Use the 'Set Path' Button">
<div class=folderPathBottom>
<button on:click={SetFolder}>Set Path</button>
<button on:click={RemoveFolder}>Remove</button>
<input type="color" id={`color-${folder.Hash}`} bind:value={folder.Color} on:change={FolderColorChange}>
</div>
</div>
</div>
{/each}
<div id="addButton">
<div id="thinLine" on:click={() => MinimizeAddFolder()} on:keydown={() => MinimizeAddFolder()}> </div>
<svg id="addFolder" on:click={AddFolder} on:keydown={AddFolder} xmlns="http://www.w3.org/2000/svg" height="48" viewBox="0 96 960 960" width="48" fill="currentColor"><path d="M570 736h60v-90h90v-60h-90v-90h-60v90h-90v60h90v90ZM141 896q-24 0-42-18.5T81 836V316q0-23 18-41.5t42-18.5h280l60 60h340q23 0 41.5 18.5T881 376v460q0 23-18.5 41.5T821 896H141Zm0-580v520h680V376H456l-60-60H141Zm0 0v520-520Z"/></svg>
</div>
</main>
<style lang="scss">
@use '../src/scss/Colors';
svg {
cursor: pointer;
transition: opacity 0.2s, transform 0.2s;
&:hover {
opacity: 0.8;
}
&:active {
opacity: 0.6;
transform: scale(0.9);
}
}
main {
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
width: 100%;
height: 100%;
}
button {
background-color: Colors.$ColumnColor;
color: Colors.$TextColor;
border-color: Colors.$BackgroundColor;
border-radius: 0.5rem;
padding: 0.4rem;
margin: 0.5rem 0.5rem 0 0.5rem;
outline: none;
cursor: pointer;
&:hover {
background-color: darken(Colors.$ColumnColor, 3%);
}
&:active {
background-color: darken(Colors.$ColumnColor, 5%);
}
transition: background-color 0.2s;
}
input {
@extend button;
}
.folder {
position: relative;
height: 3rem;
width: 3rem;
}
.folderPath {
display: none;
position: absolute;
top: /*2.75rem*/ 0;
left: var(--popupside);
background-color: darken(#222222, 5%);
color: Colors.$TextColor;
border-radius: 0.5rem;
padding: 0.5rem;
z-index: 55;
input[type=text] {
width: 14rem;
}
}
.folder:hover .folderPath {
display: block;
}
.folderPathBottom {
display: flex;
justify-content: center;
flex-direction: row;
}
#addButton {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
#thinLine {
width: 60%;
height: 0.2rem;
background-color: Colors.$BackgroundColor;
cursor: pointer;
user-select: none;
margin: 0.5rem 0 0.5rem 0;
&:hover {
background-color: darken(Colors.$BackgroundColor, 5%);
}
transition: background-color 0.1s;
}
:global(.minimized) {
display: none;
}
:global(.emptyFolder) {
& > svg {
opacity: 0.3;
}
}
</style>