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 b258028b..b1103b52 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) { @@ -837,6 +840,57 @@ window.create_file = async(options)=>{ } } +window.available_templates = async () => { + 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){ + return [] + } + + const hasTemplateFiles = await puter.fs.readdir(baseRoute + "/" + hasTemplateFolder.name) + console.log(hasTemplateFiles) + + if(hasTemplateFiles.length == 0) { + 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..11a091ec 100644 --- a/src/gui/src/helpers/new_context_menu_item.js +++ b/src/gui/src/helpers/new_context_menu_item.js @@ -27,52 +27,80 @@ */ 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}); + }); + } + }, + ]; + + //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: ``, + 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: ``, + // }); + } + + //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 @@ + + + + + + + + + + +