console-gui-tools
Version:
A simple library to draw option menu, text popup or other widgets and layout on a Node.js console.
182 lines • 7.72 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from "events";
import { ConsoleManager, KeyListenerArgs } from "../../ConsoleGui.js";
import { PhisicalValues } from "../Utils.js";
/**
* @description The configuration for the FileSelectorPopup class.
* @typedef {Object} FileSelectorPopupConfig
*
* @prop {string} id - The id of the file selector popup.
* @prop {string} title - The title of the file selector popup.
* @prop {string} basePath - The base path of the file selector popup.
* @prop {boolean} [selectDirectory] - If the file selector popup can select directories.
* @prop {Array<string>} [allowedExtensions] - The allowed extensions. If not set, all extensions are allowed.
* @prop {boolean} [limitToPath] - If true, the user can select a directory. Otherwise, only files are selectable. When true, to enter a directory, the user must press the space key instead of the enter key.
* @prop {boolean} [visible] - If the file selector popup is visible.
*
* @export
* @interface FileSelectorPopupConfig
*/
export interface FileSelectorPopupConfig {
id: string;
title: string;
basePath: string;
selectDirectory?: boolean;
allowedExtensions?: string[];
limitToPath?: boolean;
visible?: boolean;
}
/**
* @description The file descriptions for the file selector popup.
* @typedef {Object} FileItemObject
* @prop {string} name - The name of the file.
* @prop {string} path - The path to the file.
* @prop {"dir" | "file"} type - The type of the file.
* @prop {string} text - The display text of the file.
*
* @interface FileItemObject
*/
interface FileItemObject {
text: string;
name: string;
type: "dir" | "file";
path: string;
}
/**
* @class FileSelectorPopup
* @extends EventEmitter
* @description This class is used to create a popup with a file input to select a file or a directory.
* It will run a promise with fs.readdir to get the list of files and directories.
* The user can select a file or a directory and the popup will be closed.
*
* 
*
* Emits the following events:
* - "confirm" when the user confirm the file or directory selection. The file or directory path is passed as parameter like this: {path: "path/to/file", name: "file.ext"}
* - "cancel" when the user cancel the file or directory selection.
* - "exit" when the user exit
* @param {FileSelectorPopupConfig} config - The configuration for the FileSelectorPopup class.
*
* @example ```ts
* const popup = new FileSelectorPopup({
* id: "popup1",
* title: "Choose the file",
* basePath: "./examples"
* }).show().on("confirm", (selected) => {
* console.log(selected)
* }) // show the popup and wait for the user to confirm
*/
export declare class FileSelectorPopup extends EventEmitter {
readonly CM: ConsoleManager;
readonly id: string;
title: string;
private basePath;
currentPath: string;
private selectDirectory;
private allowedExtensions;
private limitToPath;
private visible;
private marginTop;
private startIndex;
private selected;
private options;
private parsingMouseFrame;
/** @var {number} x - The x offset of the popup to be drown. If 0 it will be placed on the center */
private offsetX;
/** @var {number} y - The y offset of the popup to be drown. If 0 it will be placed on the center */
private offsetY;
private absoluteValues;
private dragging;
private dragStart;
private focused;
constructor(config: FileSelectorPopupConfig);
/**
* @description This function is used to load the list of files and directories in the current path.
it return a promise with the list of files and directories. The list is an array of objects like this:
[{text: "📄 file.ext", name: "file.ext", type: "file", path: "path/to/file.ext"}, {text: "📁 dir/", name: "dir", type: "dir", path: "path/to/dir"}]
* @param {string} dir - The path to load the list.
* @returns {Promise<Array<object>>} The list of files and directories.
* @memberof FileSelectorPopup
*/
private listDir;
/**
* @description This function calls the updateList function and store the result to this.options, it also refresh the list of files and directories.
* @param {string} _path - The path to load the list.
* @memberof FileSelectorPopup
*/
private updateList;
private adaptOptions;
/**
* @description This function is used to make the ConsoleManager handle the key events when the popup is showed.
* Inside this function are defined all the keys that can be pressed and the actions to do when they are pressed.
* @param {string} str - The string of the input.
* @param {Object} key - The key object.
* @memberof FileSelectorPopup
*/
keyListener(_str: string, key: KeyListenerArgs): void;
/**
* @description This function is used to get the selected option.
* @returns {FileItemObject} The selected value of the popup.
* @memberof FileSelectorPopup
*/
getSelected(): FileItemObject;
/**
* @description This function is used to change the selection of the popup. It also refresh the ConsoleManager.
* @param {FileItemObject} selected - The new value of the selection.
* @memberof FileSelectorPopup
* @returns {FileSelectorPopup} The instance of the FileSelectorPopup.
*/
private setSelected;
/**
* @description This function is used to show the popup. It also register the key events and refresh the ConsoleManager.
* @returns {FileSelectorPopup} The instance of the FileSelectorPopup.
* @memberof FileSelectorPopup
*/
show(): FileSelectorPopup;
/**
* @description This function is used to hide the popup. It also unregister the key events and refresh the ConsoleManager.
* @returns {FileSelectorPopup} The instance of the FileSelectorPopup.
* @memberof FileSelectorPopup
*/
hide(): FileSelectorPopup;
/**
* @description This function is used to get the visibility of the popup.
* @returns {boolean} The visibility of the popup.
* @memberof FileSelectorPopup
*/
isVisible(): boolean;
/**
* @description This function is used to return the PhisicalValues of the popup (x, y, width, height).
* @memberof FileSelectorPopup
* @private
* @returns {FileSelectorPopup} The instance of the FileSelectorPopup.
* @memberof FileSelectorPopup
*/
getPosition(): PhisicalValues;
/**
* @description This function is used to add the FileSelectorPopup key listener callback to te ConsoleManager.
* @returns {FileSelectorPopup} The instance of the FileSelectorPopup.
* @memberof FileSelectorPopup
*/
private manageInput;
/**
* @description This function is used to remove the FileSelectorPopup key listener callback to te ConsoleManager.
* @returns {FileSelectorPopup} The instance of the FileSelectorPopup.
* @memberof FileSelectorPopup
*/
private unManageInput;
/**
* @description This function is used to manage the mouse events on the OptionPopup.
* @param {MouseEvent} event - The string of the input.
* @memberof OptionPopup
*/
private mouseListener;
/**
* @description This function is used to draw the FileSelectorPopup to the screen in the middle.
* @returns {FileSelectorPopup} The instance of the FileSelectorPopup.
* @memberof FileSelectorPopup
*/
draw(): FileSelectorPopup;
}
export default FileSelectorPopup;
//# sourceMappingURL=FileSelectorPopup.d.ts.map