Skip to content

Commit 1f7f094

Browse files
authored
Merge pull request #627 from Koppeks/koppeks/templates_folder_contextmenu
Templates folder feature
2 parents 2d5a0bb + d8dd218 commit 1f7f094

File tree

4 files changed

+138
-43
lines changed

4 files changed

+138
-43
lines changed

src/gui/src/globals.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ window.is_auto_arrange_enabled = true;
231231
window.desktop_item_positions = {};
232232
window.reset_item_positions = true; // The variable decides if the item positions should be reset when the user enabled auto arrange
233233

234+
window.file_templates = []
235+
234236
// default language
235237
window.locale = 'en';
236238

src/gui/src/helpers.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ window.refresh_user_data = async (auth_token)=>{
432432
}
433433
}
434434

435-
window.update_auth_data = (auth_token, user)=>{
435+
window.update_auth_data = async (auth_token, user)=>{
436436
window.auth_token = auth_token;
437437
localStorage.setItem('auth_token', auth_token);
438438

@@ -493,6 +493,9 @@ window.update_auth_data = (auth_token, user)=>{
493493
$('.user-options-login-btn, .user-options-create-account-btn').hide();
494494
$('.user-options-menu-btn').show();
495495
}
496+
497+
// Search and store user templates
498+
window.file_templates = await window.available_templates()
496499
}
497500

498501
window.mutate_user_preferences = function(user_preferences_delta) {
@@ -837,6 +840,57 @@ window.create_file = async(options)=>{
837840
}
838841
}
839842

843+
window.available_templates = async () => {
844+
const baseRoute = `/${window.user.username}`
845+
const keywords = ["template", "templates", i18n('template')]
846+
//make sure all its lowercase
847+
const lowerCaseKeywords = keywords.map(keywords => keywords.toLowerCase())
848+
849+
//create file
850+
try{
851+
// search the folder name i18n("template"), "template" or "templates"
852+
const files = await puter.fs.readdir(baseRoute)
853+
854+
const hasTemplateFolder = files.find(file => lowerCaseKeywords.includes(file.name.toLowerCase()))
855+
console.log(hasTemplateFolder)
856+
857+
if(!hasTemplateFolder){
858+
return []
859+
}
860+
861+
const hasTemplateFiles = await puter.fs.readdir(baseRoute + "/" + hasTemplateFolder.name)
862+
console.log(hasTemplateFiles)
863+
864+
if(hasTemplateFiles.length == 0) {
865+
return []
866+
}
867+
868+
let result = []
869+
870+
hasTemplateFiles.forEach(element => {
871+
console.log(element)
872+
const elementInformation = element.name.split(".")
873+
const name = elementInformation[0]
874+
let extension = elementInformation[1]
875+
console.log(extension)
876+
if(extension == "txt") extension = "text"
877+
const itemStructure = {
878+
html: `${extension.toUpperCase()} ${name}`,
879+
extension:extension,
880+
name: element.name
881+
}
882+
console.log(extension)
883+
result.push(itemStructure)
884+
});
885+
886+
// return result
887+
return result
888+
889+
} catch (err) {
890+
console.log(err)
891+
}
892+
}
893+
840894
window.create_shortcut = async(filename, is_dir, basedir, appendto_element, shortcut_to, shortcut_to_path)=>{
841895
let dirname = basedir;
842896
const extname = path.extname(filename);

src/gui/src/helpers/new_context_menu_item.js

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,80 @@
2727
*/
2828

2929
const new_context_menu_item = function(dirname, append_to_element){
30-
return {
31-
html: i18n('new'),
32-
items: [
33-
// New Folder
34-
{
35-
html: i18n('new_folder'),
36-
icon: `<img src="${html_encode(window.icons['folder.svg'])}" class="ctx-item-icon">`,
37-
onClick: function(){
38-
window.create_folder(dirname, append_to_element);
39-
}
40-
},
41-
// divider
42-
'-',
43-
// Text Document
44-
{
45-
html: i18n('text_document'),
46-
icon: `<img src="${html_encode(window.icons['file-text.svg'])}" class="ctx-item-icon">`,
47-
onClick: async function(){
48-
window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New File.txt'});
49-
}
50-
},
51-
// HTML Document
52-
{
53-
html: i18n('html_document'),
54-
icon: `<img src="${html_encode(window.icons['file-html.svg'])}" class="ctx-item-icon">`,
55-
onClick: async function(){
56-
window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New File.html'});
57-
}
30+
31+
const baseItems = [
32+
// New Folder
33+
{
34+
html: i18n('new_folder'),
35+
icon: `<img src="${html_encode(window.icons['folder.svg'])}" class="ctx-item-icon">`,
36+
onClick: function() {
37+
window.create_folder(dirname, append_to_element);
5838
},
59-
// JPG Image
60-
{
61-
html: i18n('jpeg_image'),
62-
icon: `<img src="${html_encode(window.icons['file-image.svg'])}" class="ctx-item-icon">`,
63-
onClick: async function(){
64-
var canvas = document.createElement("canvas");
39+
},
40+
// divider
41+
'-',
42+
// Text Document
43+
{
44+
html: i18n('text_document'),
45+
icon: `<img src="${html_encode(window.icons['file-text.svg'])}" class="ctx-item-icon">`,
46+
onClick: async function() {
47+
window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New File.txt'});
48+
}
49+
},
50+
// HTML Document
51+
{
52+
html: i18n('html_document'),
53+
icon: `<img src="${html_encode(window.icons['file-html.svg'])}" class="ctx-item-icon">`,
54+
onClick: async function() {
55+
window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New File.html'});
56+
}
57+
},
58+
// JPG Image
59+
{
60+
html: i18n('jpeg_image'),
61+
icon: `<img src="${html_encode(window.icons['file-image.svg'])}" class="ctx-item-icon">`,
62+
onClick: async function() {
63+
var canvas = document.createElement("canvas");
6564

66-
canvas.width = 800;
67-
canvas.height = 600;
68-
69-
canvas.toBlob((blob) =>{
70-
window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New Image.jpg', content: blob});
71-
});
65+
canvas.width = 800;
66+
canvas.height = 600;
67+
68+
canvas.toBlob((blob) => {
69+
window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New Image.jpg', content: blob});
70+
});
71+
}
72+
},
73+
];
74+
75+
//Show file_templates on the lower part of "New"
76+
if (window.file_templates.length > 0) {
77+
// divider
78+
baseItems.push('-');
79+
80+
// User templates
81+
baseItems.push({
82+
html: "User templates",
83+
icon: `<img src="${html_encode(window.icons['file-template.svg'])}" class="ctx-item-icon">`,
84+
items: window.file_templates.map(template => ({
85+
html: template.html,
86+
icon: `<img src="${html_encode(window.icons[`file-${template.extension}.svg`])}" class="ctx-item-icon">`,
87+
onClick: function() {
88+
window.create_file({dirname: dirname, append_to_element: append_to_element, name: template.name});
7289
}
73-
},
74-
]
90+
}))
91+
});
92+
} else {
93+
// baseItems.push({
94+
// html: "No templates found",
95+
// icon: `<img src="${html_encode(window.icons['file-template.svg'])}" class="ctx-item-icon">`,
96+
// });
7597
}
98+
99+
//Conditional rendering for the templates
100+
return {
101+
html: i18n('new'),
102+
items: baseItems
103+
};
76104
}
77105

78106
export default new_context_menu_item;
Lines changed: 11 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)