@acransac/filetree 中文文档教程
Introduction
filetree 将典型文件系统树中的文件条目 映射到具有句柄 的句柄,可以是与应用程序相关的任何内容。 使用选择 导航树。 此外,该库公开了一些内部工具以允许重新实现其逻辑。
How To Use Filetree
将 filetree 添加到项目中:
$ npm install @acransac/filetree
并导入所需的功能:
const { branches, entryName, insertInFileTree, isDirectoryEntry, isFileSelected, makeFileEntry, makeFileTree, makeSelectionInFileTree, parseFilePath, refreshSelectedFileTree, root, selectedBranch, selectedEntry, selectedEntryBranchName, selectedEntryHandle, selectedEntryLeafName, selectedEntryName, selectNext, selectPrevious, visitChildBranch, visitParentBranch } = require('@acransac/filetree');
Make a File Tree And Selection
创建一个空的文件树并使用 makeFileTree
和 makeSelectionInFileTree。 然后,使用
insertInFileTree
添加使用 makeFileEntry
创建的条目。 每次插入后,都会使用 refreshSelectedFileTree
更新选择。
可以使用 root
和 branches
检查文件树,以及使用 entryName
或 isDirectoryEntry
的条目,尽管这是首选检查选择。 这是通过 selectedBranch
、selectedEntry
、selectedEntryBranchName
、selectedEntryHandle
、selectedEntryLeafName
、< code>selectedEntryName 或 isFileSelected
:
makeFileTree:: (Maybe
, Maybe ) -> 文件树 Parameter Type Description root Maybe\ The path to the root of the file tree, written /path/to/root
. Default: the empty string, it is deduced when inserting files. Specify if implementing new behaviourbranches Maybe\ The initial branches of the tree. Default: the empty branch. Specify if implementing new behaviour makeSelectionInFileTree:: (FileTree, Maybe
, Maybe ) -> 选择 Parameter Type Description fileTree FileTree The file tree on which the selection is made. It has to be the empty file tree unless implementing new behaviour selectedBranch Maybe\ Not used when the file tree is empty. When defining new behaviour, it is the branch to which the selected entry belongs selectedEntry Maybe\ Not used when the file tree is empty. When defining new behaviour, it is the selected entry insertInFileTree:: (FileTree, String, FileEntry) -> 文件树
Parameter Type Description fileTree FileTree The file tree to insert to path String The path to the file without the file name, written /some/path
file FileEntry The file entry to insert makeFileEntry:: (String, Any) -> 文件入口
Parameter Type Description name String The file name read in /path/name
handle Any The handle can be anything relevant in the application's context such as a file id, a callback, etc refreshSelectedFileTree:: (Selection, FileTree) -> 选择
Parameter Type Description selectionInFileTree Selection The selection with an outdated referenced tree newFileTree FileTree The referenced tree after insertion parseFilePath:: 字符串 -> (字符串,字符串)
Parameter / Returned Type Description fullPath String The path to a file with its name included, written /path/to/file
returned (String, String) An array of two strings. The first one is the path to the file /path/to
and the second is the file nameroot:: 文件树 -> 字符串
Parameter / Returned Type Description fileTree FileTree A file tree returned String The path to the root, written /path/to/root
<代码>分支:: FileTree -> 分支机构
Parameter / Returned Type Description fileTree FileTree A file tree returned Branches The branches of the tree entryName:: 条目 -> 字符串
Parameter / Returned Type Description entry Entry An entry, file or directory returned String The name of the entry, which is the last element of the absolute path isDirectoryEntry:: 条目 -> 布尔值
Parameter Type Description entry Entry The entry checked selectedBranch:: 选择 -> 分支机构
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Branches The branch to which the selection belongs. Use if implementing new behavior selectedEntry:: 选择 -> 选定项
Parameter Type Description selectionInFileTree Selection A selection <代码>selectedEntryBranchName::SelectedEntry -> 字符串
Parameter / Returned Type Description selectedEntry SelectedEntry A selected entry returned String The path to the entry without its name and without the selected file tree's root path, written /pathFromRoot/justBeforeTheEntry
selectedEntryHandle:: SelectedEntry -> 任何
Parameter / Returned Type Description selectedEntry SelectedEntry A selected file returned Any The associated handle selectedEntryLeafName:: SelectedEntry -> 字符串
Parameter / Returned Type Description selectedEntry SelectedEntry A selected entry returned String The name of the entry, which is the last element of the absolute path selectedEntryName:: SelectedEntry -> 字符串
Parameter / Returned Type Description selectedEntry SelectedEntry A selected entry returned String The path to the entry with its name but without the selected file tree's root path, written /pathFromRoot/to/entry
<代码>isFileSelected::SelectedEntry -> Boolean
Parameter Type Description selectedEntry SelectedEntry A selected entry Example: const { insertInFileTree, isFileSelected, makeFileEntry, makeFileTree, makeSelectionInFileTree, parseFilePath, refreshSelectedFileTree, selectedEntry, selectedEntryBranchName, selectedEntryHandle, selectedEntryLeafName, selectedEntryName } = require('@acransac/filetree'); const emptyFileTree = makeFileTree(); const emptySelection = makeSelectionInFileTree(emptyFileTree); const [filePath, fileName] = parseFilePath("/root/file"); const selection = refreshSelectedFileTree(emptySelection, insertInFileTree(emptyFileTree, filePath, makeFileEntry(fileName, () => "File handled"))); reportOnSelection(selection); function reportOnSelection(selection) { console.log(`Is the selection a file? ${isFileSelected(selectedEntry(selection)) ? "Yes" : "No"}`); console.log(`Full entry name: \"${selectedEntryName(selectedEntry(selection))}\"`); console.log(`Entry branch name: \"${selectedEntryBranchName(selectedEntry(selection))}\"`); console.log(`Entry leaf name: \"${selectedEntryLeafName(selectedEntry(selection))}\"`); console.log(`Using the handle: ${selectedEntryHandle(selectedEntry(selection))()}`); }
$ node example.js Is the selection a file? Yes Full entry name: "/file" Entry branch name: "" Entry leaf name: "file" Using the handle: File handled
Navigate The File Tree
具有多个条目的文件树可以通过选择进行导航。 后者可以访问目录的内容或检查父目录或嵌套目录。 在任何时候,选择都是指所选条目所属的所选分支。 导航是通过
selectNext
、selectPrevious
、visitChildBranch
和visitParentBranch
实现的:selectNext:: 选择 -> 选择
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Selection A new selection set on the next entry in the selected branch. If the input selection is the last entry of the branch, it is returned selectPrevious:: 选择 -> 选择
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Selection A new selection set on the previous entry in the selected branch. If the input selection is the first entry of the branch, it is returned visitChildBranch:: 选择 -> 选择
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Selection If the input selection is a directory, the new selection has its selected branch changed to the content of this directory and is set on its first entry. If the input selection is a file, it is returned visitParentBranch:: 选择 -> 选择
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Selection If the input selection has a parent directory, the new selection has its selected branch changed to the content of this directory and is set on its first entry. Otherwise, the selection is set on the first entry in the current directory, and the selected branch remains unchanged Example: const { insertInFileTree, isFileSelected, makeFileEntry, makeFileTree, makeSelectionInFileTree, parseFilePath, refreshSelectedFileTree, selectedEntry, selectedEntryBranchName, selectedEntryHandle, selectedEntryLeafName, selectedEntryName, selectNext, selectPrevious, visitChildBranch, visitParentBranch } = require('@acransac/filetree'); const [fileTree, selection] = insertFile(makeFileTree(), makeSelectionInFileTree(makeFileTree()), ...parseFilePath("/root/fileA")); const [newFileTree, newSelection] = insertFile(fileTree, selection, ...parseFilePath("/root/dir/fileB")); reportOnSelection(selectNext(newSelection)); reportOnSelection(selectPrevious(selectNext(newSelection))); reportOnSelection(visitChildBranch(selectNext(newSelection))); reportOnSelection(visitParentBranch(visitChildBranch(selectNext(newSelection)))); function insertFile(fileTree, selection, filePath, fileName) { return (fileTree => [fileTree, refreshSelectedFileTree(selection, fileTree)]) (insertInFileTree(fileTree, filePath, makeFileEntry(fileName, () => `${fileName} handled`))); } function reportOnSelection(selection) { console.log(`Is the selection a file? ${isFileSelected(selectedEntry(selection)) ? "Yes" : "No"}`); console.log(`Full entry name: \"${selectedEntryName(selectedEntry(selection))}\"`); console.log(`Entry branch name: \"${selectedEntryBranchName(selectedEntry(selection))}\"`); console.log(`Entry leaf name: \"${selectedEntryLeafName(selectedEntry(selection))}\"`); if (isFileSelected(selectedEntry(selection))) { console.log(`Using the handle: ${selectedEntryHandle(selectedEntry(selection))()}`); } console.log("\n"); }
$ node example.js Is the selection a file? No Full entry name: "/dir" Entry branch name: "" Entry leaf name: "dir" Is the selection a file? Yes Full entry name: "/fileA" Entry branch name: "" Entry leaf name: "fileA" Using the handle: fileA handled Is the selection a file? Yes Full entry name: "/dir/fileB" Entry branch name: "/dir" Entry leaf name: "fileB" Using the handle: fileB handled Is the selection a file? Yes Full entry name: "/fileA" Entry branch name: "" Entry leaf name: "fileA" Using the handle: fileA handled
Introduction
filetree maps file entries in a typical filesystem tree with handles which can be anything relevant to an application. The tree is navigated with a selection. Also, this library exposes some of its internal tooling to allow reimplementation of its logic.
How To Use Filetree
Add filetree to a project with:
$ npm install @acransac/filetree
and import the needed functionalities:
const { branches, entryName, insertInFileTree, isDirectoryEntry, isFileSelected, makeFileEntry, makeFileTree, makeSelectionInFileTree, parseFilePath, refreshSelectedFileTree, root, selectedBranch, selectedEntry, selectedEntryBranchName, selectedEntryHandle, selectedEntryLeafName, selectedEntryName, selectNext, selectPrevious, visitChildBranch, visitParentBranch } = require('@acransac/filetree');
Make a File Tree And Selection
Create an empty file tree and selection with makeFileTree
and makeSelectionInFileTree
. Then, use insertInFileTree
to add entries which are created with makeFileEntry
. After each insertion, the selection is updated with refreshSelectedFileTree
.
A file tree can be inspected with root
and branches
, and an entry with entryName
or isDirectoryEntry
though it is preferred to inspect the selection. This is done with selectedBranch
, selectedEntry
, selectedEntryBranchName
, selectedEntryHandle
, selectedEntryLeafName
, selectedEntryName
or isFileSelected
:
makeFileTree:: (Maybe<String>, Maybe<Branches>) -> FileTree
Parameter Type Description root Maybe\ The path to the root of the file tree, written /path/to/root
. Default: the empty string, it is deduced when inserting files. Specify if implementing new behaviourbranches Maybe\ The initial branches of the tree. Default: the empty branch. Specify if implementing new behaviour makeSelectionInFileTree:: (FileTree, Maybe<Branches>, Maybe<SelectedEntry>) -> Selection
Parameter Type Description fileTree FileTree The file tree on which the selection is made. It has to be the empty file tree unless implementing new behaviour selectedBranch Maybe\ Not used when the file tree is empty. When defining new behaviour, it is the branch to which the selected entry belongs selectedEntry Maybe\ Not used when the file tree is empty. When defining new behaviour, it is the selected entry insertInFileTree:: (FileTree, String, FileEntry) -> FileTree
Parameter Type Description fileTree FileTree The file tree to insert to path String The path to the file without the file name, written /some/path
file FileEntry The file entry to insert makeFileEntry:: (String, Any) -> FileEntry
Parameter Type Description name String The file name read in /path/name
handle Any The handle can be anything relevant in the application's context such as a file id, a callback, etc refreshSelectedFileTree:: (Selection, FileTree) -> Selection
Parameter Type Description selectionInFileTree Selection The selection with an outdated referenced tree newFileTree FileTree The referenced tree after insertion parseFilePath:: String -> (String, String)
Parameter / Returned Type Description fullPath String The path to a file with its name included, written /path/to/file
returned (String, String) An array of two strings. The first one is the path to the file /path/to
and the second is the file nameroot:: FileTree -> String
Parameter / Returned Type Description fileTree FileTree A file tree returned String The path to the root, written /path/to/root
branches:: FileTree -> Branches
Parameter / Returned Type Description fileTree FileTree A file tree returned Branches The branches of the tree entryName:: Entry -> String
Parameter / Returned Type Description entry Entry An entry, file or directory returned String The name of the entry, which is the last element of the absolute path isDirectoryEntry:: Entry -> Boolean
Parameter Type Description entry Entry The entry checked selectedBranch:: Selection -> Branches
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Branches The branch to which the selection belongs. Use if implementing new behavior selectedEntry:: Selection -> SelectedEntry
Parameter Type Description selectionInFileTree Selection A selection selectedEntryBranchName:: SelectedEntry -> String
Parameter / Returned Type Description selectedEntry SelectedEntry A selected entry returned String The path to the entry without its name and without the selected file tree's root path, written /pathFromRoot/justBeforeTheEntry
selectedEntryHandle:: SelectedEntry -> Any
Parameter / Returned Type Description selectedEntry SelectedEntry A selected file returned Any The associated handle selectedEntryLeafName:: SelectedEntry -> String
Parameter / Returned Type Description selectedEntry SelectedEntry A selected entry returned String The name of the entry, which is the last element of the absolute path selectedEntryName:: SelectedEntry -> String
Parameter / Returned Type Description selectedEntry SelectedEntry A selected entry returned String The path to the entry with its name but without the selected file tree's root path, written /pathFromRoot/to/entry
isFileSelected:: SelectedEntry -> Boolean
Parameter Type Description selectedEntry SelectedEntry A selected entry
const { insertInFileTree, isFileSelected, makeFileEntry, makeFileTree, makeSelectionInFileTree, parseFilePath, refreshSelectedFileTree, selectedEntry, selectedEntryBranchName, selectedEntryHandle, selectedEntryLeafName, selectedEntryName } = require('@acransac/filetree');
const emptyFileTree = makeFileTree();
const emptySelection = makeSelectionInFileTree(emptyFileTree);
const [filePath, fileName] = parseFilePath("/root/file");
const selection = refreshSelectedFileTree(emptySelection,
insertInFileTree(emptyFileTree,
filePath,
makeFileEntry(fileName, () => "File handled")));
reportOnSelection(selection);
function reportOnSelection(selection) {
console.log(`Is the selection a file? ${isFileSelected(selectedEntry(selection)) ? "Yes" : "No"}`);
console.log(`Full entry name: \"${selectedEntryName(selectedEntry(selection))}\"`);
console.log(`Entry branch name: \"${selectedEntryBranchName(selectedEntry(selection))}\"`);
console.log(`Entry leaf name: \"${selectedEntryLeafName(selectedEntry(selection))}\"`);
console.log(`Using the handle: ${selectedEntryHandle(selectedEntry(selection))()}`);
}
$ node example.js
Is the selection a file? Yes
Full entry name: "/file"
Entry branch name: ""
Entry leaf name: "file"
Using the handle: File handled
Navigate The File Tree
A file tree which has multiple entries can be navigated with a selection. The latter can visit the content of a directory or inspect parent or nested directories. At any point, the selection refers to a selected branch to which the selected entry belongs. The navigation is realized with selectNext
, selectPrevious
, visitChildBranch
and visitParentBranch
:
selectNext:: Selection -> Selection
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Selection A new selection set on the next entry in the selected branch. If the input selection is the last entry of the branch, it is returned selectPrevious:: Selection -> Selection
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Selection A new selection set on the previous entry in the selected branch. If the input selection is the first entry of the branch, it is returned visitChildBranch:: Selection -> Selection
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Selection If the input selection is a directory, the new selection has its selected branch changed to the content of this directory and is set on its first entry. If the input selection is a file, it is returned visitParentBranch:: Selection -> Selection
Parameter / Returned Type Description selectionInFileTree Selection A selection returned Selection If the input selection has a parent directory, the new selection has its selected branch changed to the content of this directory and is set on its first entry. Otherwise, the selection is set on the first entry in the current directory, and the selected branch remains unchanged
const { insertInFileTree, isFileSelected, makeFileEntry, makeFileTree, makeSelectionInFileTree, parseFilePath, refreshSelectedFileTree, selectedEntry, selectedEntryBranchName, selectedEntryHandle, selectedEntryLeafName, selectedEntryName, selectNext, selectPrevious, visitChildBranch, visitParentBranch } = require('@acransac/filetree');
const [fileTree, selection] = insertFile(makeFileTree(),
makeSelectionInFileTree(makeFileTree()),
...parseFilePath("/root/fileA"));
const [newFileTree, newSelection] = insertFile(fileTree, selection, ...parseFilePath("/root/dir/fileB"));
reportOnSelection(selectNext(newSelection));
reportOnSelection(selectPrevious(selectNext(newSelection)));
reportOnSelection(visitChildBranch(selectNext(newSelection)));
reportOnSelection(visitParentBranch(visitChildBranch(selectNext(newSelection))));
function insertFile(fileTree, selection, filePath, fileName) {
return (fileTree => [fileTree, refreshSelectedFileTree(selection, fileTree)])
(insertInFileTree(fileTree, filePath, makeFileEntry(fileName, () => `${fileName} handled`)));
}
function reportOnSelection(selection) {
console.log(`Is the selection a file? ${isFileSelected(selectedEntry(selection)) ? "Yes" : "No"}`);
console.log(`Full entry name: \"${selectedEntryName(selectedEntry(selection))}\"`);
console.log(`Entry branch name: \"${selectedEntryBranchName(selectedEntry(selection))}\"`);
console.log(`Entry leaf name: \"${selectedEntryLeafName(selectedEntry(selection))}\"`);
if (isFileSelected(selectedEntry(selection))) {
console.log(`Using the handle: ${selectedEntryHandle(selectedEntry(selection))()}`);
}
console.log("\n");
}
$ node example.js
Is the selection a file? No
Full entry name: "/dir"
Entry branch name: ""
Entry leaf name: "dir"
Is the selection a file? Yes
Full entry name: "/fileA"
Entry branch name: ""
Entry leaf name: "fileA"
Using the handle: fileA handled
Is the selection a file? Yes
Full entry name: "/dir/fileB"
Entry branch name: "/dir"
Entry leaf name: "fileB"
Using the handle: fileB handled
Is the selection a file? Yes
Full entry name: "/fileA"
Entry branch name: ""
Entry leaf name: "fileA"
Using the handle: fileA handled