From f897e844989083b0b369ba0ce4d2c5a9f3db5ad8 Mon Sep 17 00:00:00 2001 From: Koppeks Date: Fri, 19 Jul 2024 19:42:47 -0300 Subject: [PATCH 1/4] Temporal commit for issue #432 --- src/gui/src/globals.js | 2 + src/gui/src/helpers.js | 54 +++++++++ src/gui/src/helpers/new_context_menu_item.js | 118 ++++++++++++------- src/gui/src/icons/file-template.svg | 11 ++ src/gui/src/initgui.js | 2 + 5 files changed, 143 insertions(+), 44 deletions(-) create mode 100644 src/gui/src/icons/file-template.svg diff --git a/src/gui/src/globals.js b/src/gui/src/globals.js index 4f0c2d98a..ae5441480 100644 --- a/src/gui/src/globals.js +++ b/src/gui/src/globals.js @@ -231,6 +231,8 @@ window.is_auto_arrange_enabled = true; window.desktop_item_positions = {}; window.reset_item_positions = true; // The variable decides if the item positions should be reset when the user enabled auto arrange +window.file_templates = [] + // default language window.locale = 'en'; diff --git a/src/gui/src/helpers.js b/src/gui/src/helpers.js index a85be51b5..3d15fb029 100644 --- a/src/gui/src/helpers.js +++ b/src/gui/src/helpers.js @@ -837,6 +837,60 @@ window.create_file = async(options)=>{ } } +window.available_templates = async () => { + console.log(window.user.username) + const baseRoute = `/${window.user.username}` + const keywords = ["template", "templates", i18n('template')] + //make sure all its lowercase + const lowerCaseKeywords = keywords.map(keywords => keywords.toLowerCase()) + + //create file + try{ + // search the folder name i18n("template"), "template" or "templates" + const files = await puter.fs.readdir(baseRoute) + + const hasTemplateFolder = files.find(file => lowerCaseKeywords.includes(file.name.toLowerCase())) + console.log(hasTemplateFolder) + + if(!hasTemplateFolder){ + console.log("No template folder") + return [] + } + + const hasTemplateFiles = await puter.fs.readdir(baseRoute + "/" + hasTemplateFolder.name) + console.log(hasTemplateFiles) + + if(hasTemplateFiles.length == 0) { + console.log("There are no templates") + return [] + } + + let result = [] + + hasTemplateFiles.forEach(element => { + console.log(element) + const elementInformation = element.name.split(".") + const name = elementInformation[0] + let extension = elementInformation[1] + console.log(extension) + if(extension == "txt") extension = "text" + const itemStructure = { + html: `${extension.toUpperCase()} ${name}`, + extension:extension, + name: element.name + } + console.log(extension) + result.push(itemStructure) + }); + + // return result + return result + + } catch (err) { + console.log(err) + } +} + window.create_shortcut = async(filename, is_dir, basedir, appendto_element, shortcut_to, shortcut_to_path)=>{ let dirname = basedir; const extname = path.extname(filename); diff --git a/src/gui/src/helpers/new_context_menu_item.js b/src/gui/src/helpers/new_context_menu_item.js index 6c2ea49e1..602a8ae2b 100644 --- a/src/gui/src/helpers/new_context_menu_item.js +++ b/src/gui/src/helpers/new_context_menu_item.js @@ -27,52 +27,82 @@ */ const new_context_menu_item = function(dirname, append_to_element){ + + const baseItems = [ + // New Folder + { + html: i18n('new_folder'), + icon: ``, + onClick: function() { + window.create_folder(dirname, append_to_element); + }, + }, + // divider + '-', + // Text Document + { + html: i18n('text_document'), + icon: ``, + onClick: async function() { + window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New File.txt'}); + } + }, + // HTML Document + { + html: i18n('html_document'), + icon: ``, + onClick: async function() { + window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New File.html'}); + } + }, + // JPG Image + { + html: i18n('jpeg_image'), + icon: ``, + onClick: async function() { + var canvas = document.createElement("canvas"); + + canvas.width = 800; + canvas.height = 600; + + canvas.toBlob((blob) => { + window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New Image.jpg', content: blob}); + }); + } + }, + // divider + '-' + ]; + + //Show file_templates on the lower part of "New" + if (window.file_templates.length > 0) { + baseItems.push({ + html: "User templates", + icon: ``, + items: window.file_templates.map(template => ({ + html: template.html, + icon: ``, + onClick: function() { + window.create_file({dirname: dirname, append_to_element: append_to_element, name: template.name}); + } + })) + }); + } else { + baseItems.push({ + html: "No templates found", + icon: ``, + //Add function to ask user to create new template folder + // onClick: function() { + // window.create_file({dirname: dirname, append_to_element: append_to_element, name: template.name}); + // } + }); + } + + //Conditional rendering for the templates return { html: i18n('new'), - items: [ - // New Folder - { - html: i18n('new_folder'), - icon: ``, - onClick: function(){ - window.create_folder(dirname, append_to_element); - } - }, - // divider - '-', - // Text Document - { - html: i18n('text_document'), - icon: ``, - onClick: async function(){ - window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New File.txt'}); - } - }, - // HTML Document - { - html: i18n('html_document'), - icon: ``, - onClick: async function(){ - window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New File.html'}); - } - }, - // JPG Image - { - html: i18n('jpeg_image'), - icon: ``, - onClick: async function(){ - var canvas = document.createElement("canvas"); - - canvas.width = 800; - canvas.height = 600; - - canvas.toBlob((blob) =>{ - window.create_file({dirname: dirname, append_to_element: append_to_element, name: 'New Image.jpg', content: blob}); - }); - } - }, - ] - } + items: baseItems + }; } export default new_context_menu_item; \ No newline at end of file diff --git a/src/gui/src/icons/file-template.svg b/src/gui/src/icons/file-template.svg new file mode 100644 index 000000000..2fd3284be --- /dev/null +++ b/src/gui/src/icons/file-template.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/src/gui/src/initgui.js b/src/gui/src/initgui.js index cd7bf0b33..a28b1a659 100644 --- a/src/gui/src/initgui.js +++ b/src/gui/src/initgui.js @@ -108,6 +108,8 @@ const launch_services = async function (options) { const svc_process = globalThis.services.get('process'); svc_process.get_init().chstatus(PROCESS_RUNNING); } + // Search and store user templates + window.file_templates = await window.available_templates() }; // This code snippet addresses the issue flagged by Lighthouse regarding the use of From a0113788f94c6b018fd1b9087091a2be108f1cca Mon Sep 17 00:00:00 2001 From: Koppeks Date: Sun, 21 Jul 2024 17:32:33 -0300 Subject: [PATCH 2/4] remove extra comments --- src/gui/src/helpers/new_context_menu_item.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/gui/src/helpers/new_context_menu_item.js b/src/gui/src/helpers/new_context_menu_item.js index 602a8ae2b..16e9fb469 100644 --- a/src/gui/src/helpers/new_context_menu_item.js +++ b/src/gui/src/helpers/new_context_menu_item.js @@ -91,10 +91,6 @@ const new_context_menu_item = function(dirname, append_to_element){ baseItems.push({ html: "No templates found", icon: ``, - //Add function to ask user to create new template folder - // onClick: function() { - // window.create_file({dirname: dirname, append_to_element: append_to_element, name: template.name}); - // } }); } From a9801fc10db3db3317bad60486c613a4d7dbfc07 Mon Sep 17 00:00:00 2001 From: Nariman Jelveh Date: Mon, 22 Jul 2024 11:07:57 -0700 Subject: [PATCH 3/4] place `available_templates` call in `update_auth_data` to guarantee that `user` object exists --- src/gui/src/helpers.js | 6 ++++-- src/gui/src/initgui.js | 2 -- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gui/src/helpers.js b/src/gui/src/helpers.js index 3d15fb029..2fcfb46c9 100644 --- a/src/gui/src/helpers.js +++ b/src/gui/src/helpers.js @@ -432,7 +432,7 @@ window.refresh_user_data = async (auth_token)=>{ } } -window.update_auth_data = (auth_token, user)=>{ +window.update_auth_data = async (auth_token, user)=>{ window.auth_token = auth_token; localStorage.setItem('auth_token', auth_token); @@ -493,6 +493,9 @@ window.update_auth_data = (auth_token, user)=>{ $('.user-options-login-btn, .user-options-create-account-btn').hide(); $('.user-options-menu-btn').show(); } + + // Search and store user templates + window.file_templates = await window.available_templates() } window.mutate_user_preferences = function(user_preferences_delta) { @@ -838,7 +841,6 @@ window.create_file = async(options)=>{ } window.available_templates = async () => { - console.log(window.user.username) const baseRoute = `/${window.user.username}` const keywords = ["template", "templates", i18n('template')] //make sure all its lowercase diff --git a/src/gui/src/initgui.js b/src/gui/src/initgui.js index a28b1a659..cd7bf0b33 100644 --- a/src/gui/src/initgui.js +++ b/src/gui/src/initgui.js @@ -108,8 +108,6 @@ const launch_services = async function (options) { const svc_process = globalThis.services.get('process'); svc_process.get_init().chstatus(PROCESS_RUNNING); } - // Search and store user templates - window.file_templates = await window.available_templates() }; // This code snippet addresses the issue flagged by Lighthouse regarding the use of From d8dd2180895656e2d7d1b135fc126b06908c77bc Mon Sep 17 00:00:00 2001 From: Nariman Jelveh Date: Mon, 22 Jul 2024 11:31:01 -0700 Subject: [PATCH 4/4] Don't show `No templates found` for now. --- src/gui/src/helpers.js | 2 -- src/gui/src/helpers/new_context_menu_item.js | 14 ++++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/gui/src/helpers.js b/src/gui/src/helpers.js index 2fcfb46c9..331a4b2dd 100644 --- a/src/gui/src/helpers.js +++ b/src/gui/src/helpers.js @@ -855,7 +855,6 @@ window.available_templates = async () => { console.log(hasTemplateFolder) if(!hasTemplateFolder){ - console.log("No template folder") return [] } @@ -863,7 +862,6 @@ window.available_templates = async () => { console.log(hasTemplateFiles) if(hasTemplateFiles.length == 0) { - console.log("There are no templates") return [] } diff --git a/src/gui/src/helpers/new_context_menu_item.js b/src/gui/src/helpers/new_context_menu_item.js index 16e9fb469..11a091ecc 100644 --- a/src/gui/src/helpers/new_context_menu_item.js +++ b/src/gui/src/helpers/new_context_menu_item.js @@ -70,12 +70,14 @@ const new_context_menu_item = function(dirname, append_to_element){ }); } }, - // divider - '-' ]; //Show file_templates on the lower part of "New" if (window.file_templates.length > 0) { + // divider + baseItems.push('-'); + + // User templates baseItems.push({ html: "User templates", icon: ``, @@ -88,10 +90,10 @@ const new_context_menu_item = function(dirname, append_to_element){ })) }); } else { - baseItems.push({ - html: "No templates found", - icon: ``, - }); + // baseItems.push({ + // html: "No templates found", + // icon: ``, + // }); } //Conditional rendering for the templates