Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tree view #1304

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"d3-selection": "^3.0.0",
"d3-shape": "^3.0.0",
"d3-time-format": "^4.0.0",
"path-browserify": "^1.0.1",
"svelte": "^3.38.3"
}
}
88 changes: 77 additions & 11 deletions frontend/src/editor/EditorMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { toggleComment } from "@codemirror/comment";
import { foldAll, unfoldAll } from "@codemirror/fold";
import type { EditorView } from "@codemirror/view";
import path from "path-browserify";

import { beancountFormat } from "../codemirror/beancount-format";
import { scrollToLine } from "../codemirror/scroll-to-line";
Expand All @@ -11,6 +12,7 @@
import router from "../router";
import { favaOptions, options } from "../stores";

import Folder from "./Folder.svelte";
import Key from "./Key.svelte";

export let file_path: string;
Expand All @@ -31,20 +33,88 @@
editor.focus();
}
}

function _dummy_folder(name: string, sub_node) {
if ("subfolders" in sub_node) {
return { name, subfolders: [sub_node], subfiles: [] };
}
return { name, subfolders: [], subfiles: [sub_node] };
}
function dummy_folder(path_str: string) {
let p = path.parse(path_str);
let folder_path = p.dir;
const { root } = p;

const file_node = { name: p.base, path: path_str };
let last_node = file_node;
while (folder_path !== root) {
p = path.parse(folder_path);
last_node = _dummy_folder(p.base, last_node);
folder_path = p.dir;
}
return _dummy_folder(root, last_node);
}
function shorten_folder(folder) {
// Flatten the nested folder when possible.
if (folder.subfiles.length === 0 && folder.subfolders.length === 1) {
const subfolder = folder.subfolders[0];
const new_name = path.join(folder.name, subfolder.name);
return shorten_folder({
name: new_name,
subfolders: subfolder.subfolders,
subfiles: subfolder.subfiles,
});
}
return {
name: folder.name,
subfolders: folder.subfolders.map(shorten_folder),
subfiles: folder.subfiles,
};
}
let merge_folder;
function merge_same_name_folder(name: string, i_folders: []) {
if (i_folders.length === 1) {
return i_folders[0];
}
return {
name,
subfolders: merge_folder(i_folders.map((o) => o.subfolders).flat()),
subfiles: i_folders.map((o) => o.subfiles).flat(),
};
}
function groupBy(xs: [], key: string) {
return xs.reduce((rv, x) => {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
merge_folder = (i_folders: []): [] => {
const dict = groupBy(
i_folders.sort((a, b) => {
if (a.name === b.name) {
return 0;
}
return a.name > b.name ? 1 : -1;
}),
"name"
);
return Object.entries(dict).map(([name, group]) =>
merge_same_name_folder(name, group)
);
};
function source_tree(files: string[]): [] {
return merge_folder(files.map(dummy_folder)).map(shorten_folder);
}
$: folders = source_tree(sources);
</script>

<div class="fieldset">
<div class="dropdown">
<span>
{_("File")}
<ul>
{#each sources as source}
<li
class:selected={source === file_path}
on:click={() => goToFileAndLine(source)}
>
{source}
</li>
{#each folders as folder}
<Folder {folder} {file_path} expanded />
{/each}
</ul>
</span>
Expand Down Expand Up @@ -104,10 +174,6 @@
margin-right: 0.5rem;
}

.selected::before {
content: "›";
}

li {
padding: 2px 10px;
cursor: pointer;
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/editor/File.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
import { urlFor } from "../helpers";

export let file;
/** @type {string} */
export let file_path;
</script>

<span class:selected={file.path === file_path}>
<a href={urlFor("editor", { file_path: file.path })}>{file.name}</a>
</span>

<style>
.selected::before {
content: "›";
}

span {
padding: 2px 10px;
cursor: pointer;
}
</style>
52 changes: 52 additions & 0 deletions frontend/src/editor/Folder.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script>
import File from "./File.svelte";

export let expanded = false;
export let folder;
/** @type {string} */
export let file_path;

function toggle() {
expanded = !expanded;
}
</script>

<span class:collapse={!expanded} on:click={toggle}>{folder.name}</span>

{#if expanded}
<ul>
{#each folder.subfiles as file}
<li>
<File {file} {file_path} />
</li>
{/each}
{#each folder.subfolders as subfolder}
<li>
<svelte:self folder={subfolder} {file_path} expanded />
</li>
{/each}
</ul>
{/if}

<style>
span {
padding: 2px 10px;
font-weight: bold;
cursor: pointer;
}

.collapse::before {
content: "+";
}

ul {
padding: 0.2em 0 0 0.5em;
margin: 0 0 0 0.5em;
list-style: none;
border-left: 1px solid #eee;
}

li {
padding: 0.2em 0;
}
</style>