0.1.4 • Published 8 months ago

markhu-file-tree v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

markhu-file-tree

markhu-file-tree is a customizable draggable file tree component for Vue 3 that draws inspiration from the VSCode resource explorer.

Example

Feature

  • All nodes in the tree have a type of either 'folder' or 'file'.
  • Folders can be expanded regardless of whether they are empty or not.
  • Items can be dragged and dropped into folders, but not into files.
  • 'Folder' or 'file' names can be renamed to new names.
  • Multiple files can be selected using the Ctrl/Shift keys.
  • The selected item can be navigated using the up and down arrow keys.

Install

npm i markhu-file-tree or yarn add markhu-file-tree

Quick start

<script setup>
    import {reactive} from "vue";
    import "markhu-file-tree/dist/style.css";

    const treeData = reactive([
                {
                    title: "Documents",
                    path: "/users/Jim/Documents",
                    type: "folder",
                    children: [
                        {
                            title: "File1.txt",
                            path: "/users/Jim/Documents/File1.txt",
                            type: "file",
                        },
                        {
                            title: "File2.txt",
                            path: "/users/Jim/Documents/File2.txt",
                            type: "file"
                        }
                    ]
                },
                {
                    title: "Pictures",
                    path: "/users/Jim/Pictures",
                    type: "folder",
                    children: [
                        {
                            title: "Image1.jpg",
                            path: "/users/Jim/Pictures/Image1.jpg",
                            type: "file"
                        },
                        {
                            title: "Image2.jpg",
                            path: "/users/Jim/Pictures/Image2.jpg",
                            type: "file"
                        }
                    ]
                },
            ]
    );
</script>

<template>
    <file-tree :data="treeData"></file-tree>
</template>

The data property is an object of TreeNode nodes:

export interface TreeNode {
    title: string,
    path: string,
    type: string,
    expanded?: boolean,
    selected?: boolean,
    focused?: boolean,
    children?: TreeNode[],
    addingFile?: boolean,
    addingFolder?: boolean,
    editing?: boolean
}

Props

proptypedefaultdescription
dataTreeNodeA tree to show. The root node will not show in ui

Events

Event NameCallback ParametersPurpose
@nodeSelectitems: TreeNode[]Triggered when nodes are selected.
@nodeCreatenode: TreeNode, title: stringTriggered when a new node is created.
@nodeRenamenode: TreeNode, newTitle: string, oldTitle: stringTriggered when a node is renamed.
@nodeExpandnode: TreeNodeTriggered when a node is expanded.
@nodeCollapsenode: TreeNodeTriggered when a node is collapsed.
@nodeDropnewPath: string, oldPath: stringTriggered when a node is dragged and dropped to a new location.
@nodeMovenewPath: string, oldPath: stringTriggered when a node is moved to a new location.
@nodeContextmenue: MouseEvent, d: TreeNodeTriggered when a node's context menu is opened.

Methods

methoddescription

Slots

Slot NameParametersPurpose
title{nodeData}Used to customize the title content of a node.
toggler{nodeData}Used to customize the expand/collapse icon of a node.
icon{nodeData}Used to customize the icon of a node.

Examples

Customize title, toggle icon and file icon with slots.

Note: In this example, we use Font Awesome as icon lib.

  <file-tree :data="treeData" @nodeSelect="onNodeSelect" @fileCreate="onFileCreate"
             @folderCreate="onFolderCreate" @nodeRename="onNodeRename"
             @nodeExpand="onNodeExpand" @nodeCollapse="onNodeCollapse"
             @nodeDrop="onNodeDrop" @nodeMove="onNodeMove" @nodeContextmenu="onNodeContextmenu"
>
    <template v-slot:title="{nodeData}">{{ nodeData.title }}</template>
    <template v-slot:toggler="{nodeData}">
          <span style="margin-right: 5px">
          <i class="fa-solid fa-chevron-down" v-if="nodeData.expanded"></i>
          <i class="fa-solid fa-chevron-right" v-else></i>
            </span>
    </template>
    <template v-slot:icon="{nodeData}">
          <span style="margin-right: 5px">
            <i class="fa-regular fa-folder" v-if="nodeData.type==='folder'"></i>
            <i class="fa-regular fa-file-audio" v-else-if="nodeData.title.endsWith('.mp3')"></i>
            <i class="fa-regular fa-file-image" v-else-if="nodeData.title.endsWith('.jpg')"></i>
            <i class="fa-solid fa-file-word" v-else-if="nodeData.title.endsWith('.doc')"></i>
            <i class="fa-solid fa-file-pdf" v-else-if="nodeData.title.endsWith('.pdf')"></i>
            <i class="fa-solid fa-file-csv" v-else-if="nodeData.title.endsWith('.csv')"></i>
            <i class="fa-regular fa-file-lines" v-else-if="nodeData.title.endsWith('.txt')"></i>
            <i class="fa-regular fa-file" v-else></i>
          </span>
    </template>
</file-tree>

Changelog

0.1.4

8 months ago

0.1.3

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago