UNPKG

@jirkasa/code-box

Version:

Showcase code samples on the web with a container that lets users select and display different samples.

429 lines (428 loc) 20 kB
import CodeView from "../../code-view/CodeView"; import CodeBox, { CodeBoxItemInfo } from "../CodeBox"; import CodeBoxMemento from "../CodeBoxMemento"; import ProjectCodeBoxCodeView from "./ProjectCodeBoxCodeView"; import ProjectCodeBoxFile from "./ProjectCodeBoxFile"; import ProjectCodeBoxOptions from "./ProjectCodeBoxOptions"; /** Project code box. */ declare class ProjectCodeBox extends CodeBox { private static readonly COMMAND_RENAME_PROJECT; private static readonly COMMAND_ADD_FOLDER; private static readonly COMMAND_REMOVE_FOLDER; private static readonly COMMAND_RENAME_FOLDER; private static readonly COMMAND_OPEN_FOLDER; private static readonly COMMAND_CLOSE_FOLDER; private static readonly COMMAND_ADD_PACKAGE; private static readonly COMMAND_REMOVE_PACKAGE; private static readonly COMMAND_RENAME_PACKAGE; private static readonly COMMAND_OPEN_PACKAGE; private static readonly COMMAND_CLOSE_PACKAGE; private static readonly COMMAND_REMOVE_CODE_VIEW; private static readonly COMMAND_RENAME_CODE_VIEW; private static readonly COMMAND_MOVE_CODE_VIEW_TO_FOLDER; private static readonly COMMAND_CHANGE_CODE_VIEW_PACKAGE; private static readonly COMMAND_REMOVE_CODE_VIEW_PACKAGE; private static readonly COMMAND_REMOVE_ALL_CODE_VIEWS; private static readonly COMMAND_ADD_CODE_VIEW_HIGHLIGHT; private static readonly COMMAND_REMOVE_CODE_VIEW_HIGHLIGHT; private static readonly COMMAND_SET_ACTIVE_CODE_VIEW; private static readonly COMMAND_SET_NO_ACTIVE_CODE_VIEW; private static readonly COMMAND_REMOVE_FILE; private static readonly COMMAND_RENAME_FILE; private static readonly COMMAND_MOVE_FILE_TO_FOLDER; private static readonly COMMAND_CHANGE_FILE_PACKAGE; private static readonly COMMAND_REMOVE_FILE_PACKAGE; private static readonly COMMAND_REMOVE_ALL_FILES; /** Manages opening/closing of panel. */ private panelToggle; /** Manages visibility of packages section. */ private packagesSectionToggle; /** Manages folders and its contents (and package folders). */ private foldersManager; /** Reference to parent code box. */ private readonly parentCodeBox; /** Command objects that are processed after initialization of code box. */ private commands; /** Memento created after initialization of code box. */ private initialMemento; /** Stores folder path for packages that was (or will be) set after initialization of code box. */ private initialPackagesFolderPath; /** Event source to set active code view of code box. */ private showCodeViewEventSource; /** Code view entries stored by code view. */ private codeViewEntries; /** File entries stored by code box files. */ private fileEntries; /** Project name. */ private projectName; /** Determines whether active code view folder should be opened on initialization. */ private readonly openActiveCodeViewFolderOnInit; /** Determines whether active code view package should be opened on initialization. */ private readonly openActiveCodeViewPackageOnInit; /** Determines whether the folder and its parent folders containing the active code view should not be opened on initialization when the code view is within a package. This option overrides openActiveCodeViewFolderOnInit when set to true and the active code view is within a package. */ private readonly preventActiveCodeViewFolderOpenOnInitIfPackage; /** Determines whether panel should be closed when code view is selected by clicking on its button. */ private readonly closePanelOnCodeViewSelect; /** * Creates new project code box. * @param element Code box root element. * @param options Code box options. * @param parentCodeBox Parent code box or null if code box does not have parent code box. */ constructor(element: HTMLElement, options?: ProjectCodeBoxOptions, parentCodeBox?: ProjectCodeBox | null); addCodeView(identifier: string, codeView: CodeView): boolean; /** * Adds new code view to code box without making copy of code view. * @param identifier Identifier under which the code view should be added to code box. * @param codeView Code view. * @returns Indicates whether code view has been successfully added. */ private _addCodeView; getCodeViews(): ProjectCodeBoxCodeView[]; /** * Returns code views in folder. * @param folderPath Folder path. * @param includeSubfolders Determines whether code views in subfolders should also be included. * @returns Code views. */ getCodeViewsByFolderPath(folderPath: string, includeSubfolders?: boolean): ProjectCodeBoxCodeView[]; /** * Returns code views in package. * @param packageName Package name (null for default package). * @returns Code views. */ getCodeViewsByPackage(packageName: string | null): ProjectCodeBoxCodeView[]; getCodeView(identifier: string): ProjectCodeBoxCodeView | null; /** * Returns code view based on folder path and name. * @param folderPath Folder path. * @param fileName Name of code view. * @returns Code view (or null if code view wasn't found). */ getCodeViewByFolderPath(folderPath: string, fileName: string): ProjectCodeBoxCodeView | null; /** * Returns code view based on package and name. * @param packageName Package name (null for default package). * @param fileName Name of code view. * @returns Code view (or null if code view wasn't found). */ getCodeViewByPackage(packageName: string | null, fileName: string): ProjectCodeBoxCodeView | null; removeCodeView(identifier: string): boolean; removeAllCodeViews(): void; /** * Changes identifier of code view in code box. (It can change folder path and name of code view but it never changes package of code view.) * @param identifier Identifier of code view whose identifier should be changed. * @param newIdentifier New identifier. * @returns Indicates whether change has been successfully completed (if passed new identifier already belongs to some other code view in code box, it returns false). */ changeCodeViewIdentifier(identifier: string, newIdentifier: string): boolean; /** * Changes package of code view. * @param identifier Identifier of code view whose package should be changed. * @param packageName Package name (null for default package). If package does not exist, it is created. * @param keepFolderPath Determines whether code view should stay in the same folder (if false, code view can be moved to different folder based on package). * @returns Indicates whether change has been successfully completed. */ changeCodeViewPackage(identifier: string, packageName: string | null, keepFolderPath: boolean): boolean; /** * Removes code view from package. * @param identifier Identifier of code view that should be removed from package. * @returns Indicates whether code view has been successfully removed from package. */ removeCodeViewPackage(identifier: string): boolean; /** * Returns package of code view. * @param identifier Identifier of code view. * @returns Package of code view. If null is returned, code view belongs to default package. If undefined is returned, code view doesn't belong to any package or does not event exist. */ getCodeViewPackage(identifier: string): string | null | undefined; setActiveCodeView(identifier: string): boolean; setNoActiveCodeView(): void; getActiveCodeView(): ProjectCodeBoxCodeView | null; addFile(identifier: string, downloadLink?: string | null): boolean; getFiles(): ProjectCodeBoxFile[]; /** * Returns files in folder. * @param folderPath Folder path. * @param includeSubfolders Determines whether files in subfolders should also be included. * @returns Files. */ getFilesByFolderPath(folderPath: string, includeSubfolders?: boolean): ProjectCodeBoxFile[]; /** * Returns files in package. * @param packageName Package name (null for default package). * @returns Files. */ getFilesByPackage(packageName: string | null): ProjectCodeBoxFile[]; getFile(identifier: string): ProjectCodeBoxFile | null; /** * Returns file based on folder path and name. * @param folderPath Folder path. * @param fileName Name of file. * @returns File (or null if file wasn't found). */ getFileByFolderPath(folderPath: string, fileName: string): ProjectCodeBoxFile | null; /** * Returns file based on package and name. * @param packageName Package name (null for default package). * @param fileName Name of file. * @returns File (or null if file wasn't found). */ getFileByPackage(packageName: string | null, fileName: string): ProjectCodeBoxFile | null; removeFile(identifier: string): boolean; removeAllFiles(): void; /** * Changes identifier of file in code box. (It can change folder path and name of file but it never changes package of file.) * @param identifier Indentifier of file whose identifier should be changed. * @param newIdentifier New identifier. * @returns Indicates whether change has been successfully completed (if passed new identifier already belongs to some other file in code box, it returns false). */ changeFileIdentifier(identifier: string, newIdentifier: string): boolean; /** * Changes package of file. * @param identifier Identifier of file whose package should be changed. * @param packageName Package name (null for default package). If package does not exist, it is created. * @param keepFolderPath Determines whether file should stay in the same folder (if false, file can be moved to different folder based on package). * @returns Indicates whether change has been successfully completed. */ changeFilePackage(identifier: string, packageName: string | null, keepFolderPath: boolean): boolean; /** * Removes file from package. * @param identifier Identifier of file that should be removed from package. * @returns Indicates whether file has been successfully removed from package. */ removeFilePackage(identifier: string): boolean; /** * Returns package of file. * @param identifier Identifier of file. * @returns Package of file. If null is returned, file belongs to default package. If undefined is returned, file doesn't belong to any package or does not even exist. */ getFilePackage(identifier: string): string | null | undefined; changeFileDownloadLink(identifier: string, newDownloadLink: string | null): boolean; /** * Creates new folder(s) (if not created yet). * @param folderPath Folder path. */ addFolder(folderPath: string): void; /** * Removes folder and all its contents. This might also remove packages if their folders are removed (if generation of folders for packages is enabled via createFoldersForPackages option). * @param folderPath Path to folder that should be removed. * @returns Indicates whether folder has been successfully removed. */ removeFolder(folderPath: string): boolean; /** * Renames folder. This can also change (rename) folder for packages and rename packages. * @param folderPath Path to folder that should be renamed. * @param newName New name for folder. * @returns Indicates whether folder has been successfully renamed. */ renameFolder(folderPath: string, newName: string): boolean; /** * Opens folder. * @param folderPath Path to folder that should be opened. * @param openParentFolders Determines whether parent folders should also be opened. * @param animate Determines whether animation should be used. */ openFolder(folderPath: string, openParentFolders?: boolean, animate?: boolean): void; /** * Closes folder. * @param folderPath Path to folder that should be closed. * @param closeChildFolders Determines whether subfolders should be closed too. * @param animate Determines whether animation should be used. */ closeFolder(folderPath: string, closeChildFolders?: boolean, animate?: boolean): void; /** * Checks whether folder exists. * @param folderPath Path to folder. * @returns Indicates whether folder exists. */ folderExists(folderPath: string): boolean; /** * Checks whether folder is opened. * @param folderPath Path to folder. * @returns Indicates whether folder is opened (false might also be returned if folder does not exist). */ isFolderOpened(folderPath: string): boolean; /** * Returns names of folder subfolders (only direct subfolders). * @param folderPath Path to folder. * @returns Names of subfolders or null if folder does not exist. */ getSubfolderNames(folderPath: string): string[] | null; /** * Creates new package (if it does not exist yet). * @param name Name of package. */ addPackage(name: string): void; /** * Removes package. * @param name Package name. * @param removePackageFoldersAndContents Determines whether package folder and its contents can be removed (if the folder contains some other code views, files or subfolders, it is not removed). * @param removeAllCodeViewsAndFiles Determines whether all code views and files in package should be removed. * @returns Indicates whether package has been successfully removed. */ removePackage(name: string, removePackageFoldersAndContents?: boolean, removeAllCodeViewsAndFiles?: boolean): boolean; /** * Renames package. This can also rename folders for package if generation of folders is enabled via createFoldersForPackages option. * @param name Current package name. * @param newName New package name. * @returns Indicates whether package has been successfully renamed. */ renamePackage(name: string, newName: string): boolean; /** * Opens package. * @param packageName Package name (null for default package). * @param animate Determines whether animation should be used. */ openPackage(packageName: string | null, animate?: boolean): void; /** * Closes package. * @param packageName Package name (null for default package). * @param animate Determines whether animation should be used. */ closePackage(packageName: string | null, animate?: boolean): void; /** * Checks whether package exists. * @param packageName Package name. * @returns Indicates whether package exists. */ packageExists(packageName: string): boolean; /** * Checks whether package is opened. * @param packageName Package name. * @returns Indicates whether package is opened (false might also be returned if package does not exist). */ isPackageOpened(packageName: string | null): boolean; /** * Returns all packages. * @returns Packages. */ getPackages(): string[]; /** * Returns path to folder that is currently used for packages. * @returns Path to folder that is currently used for packages. */ getPackagesFolderPath(): string; /** * Sets new folder path for packages and removes all code views, files, folders and packages (everything needs to be removed, when folder path for packages is changed). * @param newPackagesFolderPath New folder path for packages. */ changePackagesFolderPathAndRemoveAll(newPackagesFolderPath: string): void; /** * Returns project name. * @returns Project name. */ getProjectName(): string; /** * Sets new project name. * @param newName New project name. */ setProjectName(newName: string): void; /** * Opens panel. */ openPanel(): void; /** * Closes panel. */ closePanel(): void; /** * Checks whether panel is opened. * @returns Indicates whether panel is opened. */ isPanelOpened(): boolean; reset(): void; createMemento(): CodeBoxMemento; applyMemento(memento: CodeBoxMemento): void; protected onInit(codeBoxItemInfos: CodeBoxItemInfo[]): void; protected onAfterInit(): void; /** * Creates project code box memento. * @returns Project code box memento. */ private createProjectCodeBoxMemento; /** * Called by folders manager when code view is selected. * @param codeView Code view that should be set as active. */ private onShowCodeView; /** * Called when panel is opened/closed. */ private onPanelToggled; /** * Creates folders based on element with folder structure configuration. * @param element Element with folder structure configuration. * @param parentFolderNames Parent folder names (should not be passed, it is used internally). */ private createFolderStructure; /** * Processes array of commands obtained from script of type application/json. * @param commands Commands. */ private processCommands; private processRenameProjectCommand; private processAddFolderCommand; private processRemoveFolderCommand; private processRenameFolderCommand; private processOpenFolderCommand; private processCloseFolderCommand; private processAddPackageCommand; private processRemovePackageCommand; private processRenamePackageCommand; private processOpenPackageCommand; private processClosePackageCommand; private processRemoveCodeViewCommand; private processRenameCodeViewCommand; private processMoveCodeViewToFolderCommand; private processChangeCodeViewPackageCommand; private processRemoveCodeViewPackageCommand; private processAddCodeViewHighlightCommand; private processRemoveCodeViewHighlightCommand; private processSetActiveCodeViewCommand; private processRemoveFileCommand; private processRenameFileCommand; private processMoveFileToFolderCommand; private processChangeFilePackageCommand; private processRemoveFilePackageCommand; /** * Returns height for lazy initialization placeholder element based on passed code view identifier. * @param codeViewIdentifier Identifier of code view for which should be obtained height for lazy initialization placeholder element. * @param minLinesCount Minimum number of lines. * @param defaultCodeViewOptions Default code view options. * @returns Height for lazy initialization placeholder element or null if height could not be obtained. */ private getHeightForLazyInitPlaceholderElement; /** * Returns folder path from dataset. * @param dataset Dataset. * @returns Folder path or null if folder path is not defined in dataset. */ private static getFolderPathFromDataset; /** * Returns name from dataset. * @param dataset Dataset. * @returns Name or null if name is not defined in dataset. */ private static getNameFromDataset; /** * Returns package name from dataset. * @param dataset Dataset. * @returns Package name or null if package name is not defined in dataset. */ private static getPackageNameFromDataset; /** * Helper method for constructor to get name of icon from code box options. * @param options Code box options. * @param iconName Name (label..) of icon. * @returns Name of icon. */ private static getIconName; /** * Fills code box options by values from dataset. * @param options Code box options. * @param dataset Dataset of root code box element. */ private fillProjectCodeBoxOptionsFromDataset; } export default ProjectCodeBox;