diff --git a/src/gui/src/globals.js b/src/gui/src/globals.js index 4f0c2d98..ae544148 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 a85be51b..3d15fb02 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 6c2ea49e..602a8ae2 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 00000000..2fd3284b --- /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 cd7bf0b3..a28b1a65 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