@acransac/filetree 中文文档教程

发布于 4年前 浏览 11 项目主页 更新于 3年前

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

创建一个空的文件树并使用 makeFileTreemakeSelectionInFileTree。 然后,使用 insertInFileTree 添加使用 makeFileEntry 创建的条目。 每次插入后,都会使用 refreshSelectedFileTree 更新选择。

可以使用 rootbranches 检查文件树,以及使用 entryNameisDirectoryEntry 的条目,尽管这是首选检查选择。 这是通过 selectedBranchselectedEntryselectedEntryBranchNameselectedEntryHandleselectedEntryLeafName、< code>selectedEntryName 或 isFileSelected:

  • makeFileTree:: (Maybe, Maybe) -> 文件树

    ParameterTypeDescription
    rootMaybe\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 behaviour
    branchesMaybe\The initial branches of the tree. Default: the empty branch. Specify if implementing new behaviour

  • makeSelectionInFileTree:: (FileTree, Maybe, Maybe) -> 选择

    ParameterTypeDescription
    fileTreeFileTreeThe file tree on which the selection is made. It has to be the empty file tree unless implementing new behaviour
    selectedBranchMaybe\Not used when the file tree is empty. When defining new behaviour, it is the branch to which the selected entry belongs
    selectedEntryMaybe\Not used when the file tree is empty. When defining new behaviour, it is the selected entry

  • insertInFileTree:: (FileTree, String, FileEntry) -> 文件树

    ParameterTypeDescription
    fileTreeFileTreeThe file tree to insert to
    pathStringThe path to the file without the file name, written /some/path
    fileFileEntryThe file entry to insert

  • makeFileEntry:: (String, Any) -> 文件入口

    ParameterTypeDescription
    nameStringThe file name read in /path/name
    handleAnyThe handle can be anything relevant in the application's context such as a file id, a callback, etc

  • refreshSelectedFileTree:: (Selection, FileTree) -> 选择

    ParameterTypeDescription
    selectionInFileTreeSelectionThe selection with an outdated referenced tree
    newFileTreeFileTreeThe referenced tree after insertion

  • parseFilePath:: 字符串 -> (字符串,字符串)

    Parameter / ReturnedTypeDescription
    fullPathStringThe 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 name

  • root:: 文件树 -> 字符串

    Parameter / ReturnedTypeDescription
    fileTreeFileTreeA file tree
    returnedStringThe path to the root, written /path/to/root

  • <代码>分支:: FileTree -> 分支机构

    Parameter / ReturnedTypeDescription
    fileTreeFileTreeA file tree
    returnedBranchesThe branches of the tree

  • entryName:: 条目 -> 字符串

    Parameter / ReturnedTypeDescription
    entryEntryAn entry, file or directory
    returnedStringThe name of the entry, which is the last element of the absolute path

  • isDirectoryEntry:: 条目 -> 布尔值

    ParameterTypeDescription
    entryEntryThe entry checked

  • selectedBranch:: 选择 -> 分支机构

    Parameter / ReturnedTypeDescription
    selectionInFileTreeSelectionA selection
    returnedBranchesThe branch to which the selection belongs. Use if implementing new behavior

  • selectedEntry:: 选择 -> 选定项

    ParameterTypeDescription
    selectionInFileTreeSelectionA selection

  • <代码>selectedEntryBranchName::SelectedEntry -> 字符串

    Parameter / ReturnedTypeDescription
    selectedEntrySelectedEntryA selected entry
    returnedStringThe path to the entry without its name and without the selected file tree's root path, written /pathFromRoot/justBeforeTheEntry

  • selectedEntryHandle:: SelectedEntry -> 任何

    Parameter / ReturnedTypeDescription
    selectedEntrySelectedEntryA selected file
    returnedAnyThe associated handle

  • selectedEntryLeafName:: SelectedEntry -> 字符串

    Parameter / ReturnedTypeDescription
    selectedEntrySelectedEntryA selected entry
    returnedStringThe name of the entry, which is the last element of the absolute path

  • selectedEntryName:: SelectedEntry -> 字符串

    Parameter / ReturnedTypeDescription
    selectedEntrySelectedEntryA selected entry
    returnedStringThe path to the entry with its name but without the selected file tree's root path, written /pathFromRoot/to/entry

  • <代码>isFileSelected::SelectedEntry -> Boolean

    ParameterTypeDescription
    selectedEntrySelectedEntryA 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
    

    具有多个条目的文件树可以通过选择进行导航。 后者可以访问目录的内容或检查父目录或嵌套目录。 在任何时候,选择都是指所选条目所属的所选分支。 导航是通过 selectNextselectPreviousvisitChildBranchvisitParentBranch 实现的:

    • selectNext:: 选择 -> 选择

      Parameter / ReturnedTypeDescription
      selectionInFileTreeSelectionA selection
      returnedSelectionA 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 / ReturnedTypeDescription
      selectionInFileTreeSelectionA selection
      returnedSelectionA 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 / ReturnedTypeDescription
      selectionInFileTreeSelectionA selection
      returnedSelectionIf 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 / ReturnedTypeDescription
      selectionInFileTreeSelectionA selection
      returnedSelectionIf 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

    ParameterTypeDescription
    rootMaybe\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 behaviour
    branchesMaybe\The initial branches of the tree. Default: the empty branch. Specify if implementing new behaviour

  • makeSelectionInFileTree:: (FileTree, Maybe<Branches>, Maybe<SelectedEntry>) -> Selection

    ParameterTypeDescription
    fileTreeFileTreeThe file tree on which the selection is made. It has to be the empty file tree unless implementing new behaviour
    selectedBranchMaybe\Not used when the file tree is empty. When defining new behaviour, it is the branch to which the selected entry belongs
    selectedEntryMaybe\Not used when the file tree is empty. When defining new behaviour, it is the selected entry

  • insertInFileTree:: (FileTree, String, FileEntry) -> FileTree

    ParameterTypeDescription
    fileTreeFileTreeThe file tree to insert to
    pathStringThe path to the file without the file name, written /some/path
    fileFileEntryThe file entry to insert

  • makeFileEntry:: (String, Any) -> FileEntry

    ParameterTypeDescription
    nameStringThe file name read in /path/name
    handleAnyThe handle can be anything relevant in the application's context such as a file id, a callback, etc

  • refreshSelectedFileTree:: (Selection, FileTree) -> Selection

    ParameterTypeDescription
    selectionInFileTreeSelectionThe selection with an outdated referenced tree
    newFileTreeFileTreeThe referenced tree after insertion

  • parseFilePath:: String -> (String, String)

    Parameter / ReturnedTypeDescription
    fullPathStringThe 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 name

  • root:: FileTree -> String

    Parameter / ReturnedTypeDescription
    fileTreeFileTreeA file tree
    returnedStringThe path to the root, written /path/to/root

  • branches:: FileTree -> Branches

    Parameter / ReturnedTypeDescription
    fileTreeFileTreeA file tree
    returnedBranchesThe branches of the tree

  • entryName:: Entry -> String

    Parameter / ReturnedTypeDescription
    entryEntryAn entry, file or directory
    returnedStringThe name of the entry, which is the last element of the absolute path

  • isDirectoryEntry:: Entry -> Boolean

    ParameterTypeDescription
    entryEntryThe entry checked

  • selectedBranch:: Selection -> Branches

    Parameter / ReturnedTypeDescription
    selectionInFileTreeSelectionA selection
    returnedBranchesThe branch to which the selection belongs. Use if implementing new behavior

  • selectedEntry:: Selection -> SelectedEntry

    ParameterTypeDescription
    selectionInFileTreeSelectionA selection

  • selectedEntryBranchName:: SelectedEntry -> String

    Parameter / ReturnedTypeDescription
    selectedEntrySelectedEntryA selected entry
    returnedStringThe path to the entry without its name and without the selected file tree's root path, written /pathFromRoot/justBeforeTheEntry

  • selectedEntryHandle:: SelectedEntry -> Any

    Parameter / ReturnedTypeDescription
    selectedEntrySelectedEntryA selected file
    returnedAnyThe associated handle

  • selectedEntryLeafName:: SelectedEntry -> String

    Parameter / ReturnedTypeDescription
    selectedEntrySelectedEntryA selected entry
    returnedStringThe name of the entry, which is the last element of the absolute path

  • selectedEntryName:: SelectedEntry -> String

    Parameter / ReturnedTypeDescription
    selectedEntrySelectedEntryA selected entry
    returnedStringThe path to the entry with its name but without the selected file tree's root path, written /pathFromRoot/to/entry

  • isFileSelected:: SelectedEntry -> Boolean

    ParameterTypeDescription
    selectedEntrySelectedEntryA 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

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 / ReturnedTypeDescription
    selectionInFileTreeSelectionA selection
    returnedSelectionA 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 / ReturnedTypeDescription
    selectionInFileTreeSelectionA selection
    returnedSelectionA 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 / ReturnedTypeDescription
    selectionInFileTreeSelectionA selection
    returnedSelectionIf 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 / ReturnedTypeDescription
    selectionInFileTreeSelectionA selection
    returnedSelectionIf 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
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文