diff --git a/src/UI/UIDesktop.js b/src/UI/UIDesktop.js index eb7f1f3b..c6715b29 100644 --- a/src/UI/UIDesktop.js +++ b/src/UI/UIDesktop.js @@ -36,6 +36,7 @@ import refresh_item_container from "../helpers/refresh_item_container.js" import changeLanguage from "../i18n/i18nChangeLanguage.js" import UIWindowSettings from "./Settings/UIWindowSettings.js" import UIWindowTaskManager from "./UIWindowTaskManager.js" +import truncate_filename from '../helpers/truncate_filename.js'; async function UIDesktop(options){ let h = ''; @@ -150,7 +151,7 @@ async function UIDesktop(options){ // Update matching items // set new item name - $(`.item[data-uid='${html_encode(item.uid)}'] .item-name`).html(html_encode(window.truncate_filename(item.name, window.TRUNCATE_LENGTH)).replaceAll(' ', ' ')); + $(`.item[data-uid='${html_encode(item.uid)}'] .item-name`).html(html_encode(truncate_filename(item.name)).replaceAll(' ', ' ')); // Set new icon const new_icon = (item.is_dir ? window.icons['folder.svg'] : (await window.item_icon(item)).image); @@ -354,7 +355,7 @@ async function UIDesktop(options){ // Update matching items // Set new item name - $(`.item[data-uid='${html_encode(item.uid)}'] .item-name`).html(html_encode(window.truncate_filename(item.name, window.TRUNCATE_LENGTH)).replaceAll(' ', ' ')); + $(`.item[data-uid='${html_encode(item.uid)}'] .item-name`).html(html_encode(truncate_filename(item.name)).replaceAll(' ', ' ')); // Set new icon const new_icon = (item.is_dir ? window.icons['folder.svg'] : (await window.item_icon(item)).image); diff --git a/src/UI/UIItem.js b/src/UI/UIItem.js index b601df1c..520cff50 100644 --- a/src/UI/UIItem.js +++ b/src/UI/UIItem.js @@ -25,6 +25,7 @@ import UIWindowEmailConfirmationRequired from './UIWindowEmailConfirmationRequir import UIContextMenu from './UIContextMenu.js' import UIAlert from './UIAlert.js' import path from "../lib/path.js" +import truncate_filename from '../helpers/truncate_filename.js'; function UIItem(options){ const matching_appendto_count = $(options.appendTo).length; @@ -158,7 +159,7 @@ function UIItem(options){ h += ``; // name - h += `${options.is_trash ? i18n('trash') : html_encode(window.truncate_filename(options.name, window.TRUNCATE_LENGTH)).replaceAll(' ', ' ')}` + h += `${options.is_trash ? i18n('trash') : html_encode(truncate_filename(options.name)).replaceAll(' ', ' ')}` // name editor h += `` h += ``; @@ -648,7 +649,7 @@ function UIItem(options){ UIAlert(`The name ".." is not allowed, because it is a reserved name. Please choose another name.`) } - $(el_item_name).html(window.truncate_filename(options.name, window.TRUNCATE_LENGTH).replaceAll(' ', ' ')); + $(el_item_name).html(truncate_filename(options.name).replaceAll(' ', ' ')); $(el_item_name).show(); $(el_item_name_editor).val($(el_item).attr('data-name')); $(el_item_name_editor).hide(); diff --git a/src/globals.js b/src/globals.js index 0effd52c..ed5d40fd 100644 --- a/src/globals.js +++ b/src/globals.js @@ -29,8 +29,6 @@ window.app_instance_ids = new Set(); window.download_progress = []; window.download_item_global_id = 0; -window.TRUNCATE_LENGTH = 20; - // This is the minimum width of the window for the sidebar to be shown window.window_width_threshold_for_sidebar = 500; diff --git a/src/helpers.js b/src/helpers.js index f4e3d134..41cd5dec 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -27,6 +27,7 @@ import UIWindowSaveAccount from './UI/UIWindowSaveAccount.js'; import update_username_in_gui from './helpers/update_username_in_gui.js'; import update_title_based_on_uploads from './helpers/update_title_based_on_uploads.js'; import content_type_to_icon from './helpers/content_type_to_icon.js'; +import truncate_filename from './helpers/truncate_filename.js'; import { PROCESS_RUNNING, PortalProcess, PseudoProcess } from "./definitions.js"; import UIWindowProgress from './UI/UIWindowProgress.js'; @@ -113,34 +114,6 @@ window.is_email = (email) => { return re.test(String(email).toLowerCase()); } -/** - * A function that truncates a file name if it exceeds a certain length, while preserving the file extension. - * An ellipsis character '…' is added to indicate the truncation. If the original filename is short enough, - * it is returned unchanged. - * - * @param {string} input - The original filename to be potentially truncated. - * @param {number} max_length - The maximum length for the filename. If the original filename (excluding the extension) exceeds this length, it will be truncated. - * - * @returns {string} The truncated filename with preserved extension if original filename is too long; otherwise, the original filename. - * - * @example - * - * let truncatedFilename = window.truncate_filename('really_long_filename.txt', 10); - * // truncatedFilename would be something like 'really_lo…me.txt' - * - */ -window.truncate_filename = (input, max_length)=>{ - const extname = path.extname('/' + input); - - if ((input.length - 15) > max_length){ - if(extname !== '') - return input.substring(0, max_length) + '…' + input.slice(-1 * (extname.length + 2)); - else - return input.substring(0, max_length) + '…'; - } - return input; -}; - /** * A function that scrolls the parent element so that the child element is in view. * If the child element is already in view, no scrolling occurs. @@ -3146,7 +3119,7 @@ window.rename_file = async(options, new_name, old_name, old_path, el_item, el_it } // Set new item name - $(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name`).html(html_encode(window.truncate_filename(new_name, window.TRUNCATE_LENGTH)).replaceAll(' ', ' ')); + $(`.item[data-uid='${$(el_item).attr('data-uid')}'] .item-name`).html(html_encode(truncate_filename(new_name)).replaceAll(' ', ' ')); $(el_item_name).show(); // Hide item name editor @@ -3213,7 +3186,7 @@ window.rename_file = async(options, new_name, old_name, old_path, el_item, el_it }, error: function (err){ // reset to old name - $(el_item_name).text(window.truncate_filename(options.name, window.TRUNCATE_LENGTH)); + $(el_item_name).text(truncate_filename(options.name)); $(el_item_name).show(); // hide item name editor @@ -3423,7 +3396,7 @@ window.get_html_element_from_options = async function(options){ h += ``; // name - h += `${html_encode(window.truncate_filename(options.name, window.TRUNCATE_LENGTH)).replaceAll(' ', ' ')}` + h += `${html_encode(truncate_filename(options.name)).replaceAll(' ', ' ')}` // name editor h += `` h += ``; @@ -3597,4 +3570,4 @@ window.check_password_strength = (password) => { overallPass: overallPass, report: criteria_report, }; -} \ No newline at end of file +} diff --git a/src/helpers/truncate_filename.js b/src/helpers/truncate_filename.js new file mode 100644 index 00000000..2672eb0a --- /dev/null +++ b/src/helpers/truncate_filename.js @@ -0,0 +1,51 @@ +/** + * Copyright (C) 2024 Puter Technologies Inc. + * + * This file is part of Puter. + * + * Puter is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import path from '../lib/path.js'; + +export const DEFAULT_TRUNCATE_LENGTH = 20; + +/** + * A function that truncates a file name if it exceeds a certain length, while preserving the file extension. + * An ellipsis character '…' is added to indicate the truncation. If the original filename is short enough, + * it is returned unchanged. + * + * @param {string} input - The original filename to be potentially truncated. + * @param {number} max_length - The maximum length for the filename. If the original filename (excluding the extension) exceeds this length, it will be truncated. + * + * @returns {string} The truncated filename with preserved extension if original filename is too long; otherwise, the original filename. + * + * @example + * + * let truncatedFilename = truncate_filename('really_long_filename.txt', 10); + * // truncatedFilename would be something like 'really_lo…me.txt' + * + */ +const truncate_filename = (input, max_length = DEFAULT_TRUNCATE_LENGTH) => { + const extname = path.extname('/' + input); + + if ((input.length - 15) > max_length){ + if(extname !== '') + return input.substring(0, max_length) + '…' + input.slice(-1 * (extname.length + 2)); + else + return input.substring(0, max_length) + '…'; + } + return input; +}; + +export default truncate_filename;