Skip to content

Commit 58c36b6

Browse files
committed
[repo] Add a new FileTree structure component
Support creation of clear directory structures in a tree-like format using JSON notation: ``` <FileTree structure={{ "folder": { "subfolder": { "file": null, "fileWithComment.txt": "This is the comment" }, "anotherSubfolder": { "_comment": "A comment on the subfolder", "_children": { "_comment": "A file named _comment" } } } }} /> ```
1 parent 8e855d0 commit 58c36b6

2 files changed

Lines changed: 131 additions & 25 deletions

File tree

‎docs/guides/javascript/react/testing.md‎

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,53 @@ npm test -- --coverage
4343

4444
Test files must match the glob `**/esm/tests/**/*.test.{ts,tsx}`. Place them alongside the source they test:
4545

46-
```
47-
public
48-
└── lib
49-
└── js
50-
└── esm
51-
├── src
52-
│ └── output
53-
│ └── ExampleComponent.tsx
54-
└── tests
55-
└── output
56-
└── ExampleComponent.test.ts
57-
```
46+
import FileTree from '@site/src/components/FileTree';
47+
48+
<FileTree structure={{
49+
"public": {
50+
"lib": {
51+
"js": {
52+
"esm": {
53+
"src": {
54+
"output": {
55+
"ExampleComponent.tsx": "The source file being tested"
56+
}
57+
},
58+
"tests": {
59+
"output": {
60+
"ExampleComponent.test.ts": "The test for ExampleComponent.tsx"
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}} />
5868

5969
The same convention applies to plugin components:
6070

61-
```
62-
public
63-
└── mod
64-
└── forum
65-
└── js
66-
└── esm
67-
├── src
68-
│ └── output
69-
│ └── ExampleComponent.tsx
70-
└── tests
71-
└── output
72-
└── ExampleComponent.test.ts
73-
```
71+
<FileTree structure={{
72+
"public": {
73+
"mod": {
74+
"forum": {
75+
"js": {
76+
"esm": {
77+
"src": {
78+
"output": {
79+
"ExampleComponent.tsx": "The source file being tested"
80+
}
81+
},
82+
"tests": {
83+
"output": {
84+
"ExampleComponent.test.ts": "The test for ExampleComponent.tsx"
85+
}
86+
}
87+
}
88+
}
89+
}
90+
}
91+
}
92+
}} />
7493

7594
## Writing a test
7695

‎src/components/FileTree/index.tsx‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright (c) Moodle Pty Ltd.
3+
*
4+
* Moodle is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Moodle is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
import React from 'react';
19+
import CodeBlock from '@theme/CodeBlock';
20+
21+
function parseObjectTree(tree: DirectoryStructure, prefix = ''): string {
22+
const keys = Object.keys(tree);
23+
let result = '';
24+
25+
keys.forEach((key, index) => {
26+
const isLast = index === keys.length - 1;
27+
const pointer = isLast ? '└── ' : '├── ';
28+
const nextPrefix = prefix + (isLast ? ' ' : '│ ');
29+
30+
const value = tree[key];
31+
32+
let comment = '';
33+
let children: DirectoryStructure | undefined;
34+
35+
// Determine the format at runtime
36+
if (value !== null && typeof value === 'object') {
37+
if ('_comment' in value || '_children' in value) {
38+
// It's using the expanded object format
39+
const expanded = value as ExpandedItem;
40+
comment = expanded._comment ? ` # ${expanded._comment}` : ''; // eslint-disable-line no-underscore-dangle
41+
children = expanded._children; // eslint-disable-line no-underscore-dangle
42+
} else {
43+
// It's using the simple nested directory format
44+
children = value as DirectoryStructure;
45+
}
46+
} else if (typeof value === 'string') {
47+
// Inline shorthand string comment support: "filename": "comment string"
48+
comment = ` # ${value}`;
49+
}
50+
51+
// 1. Append the node line
52+
result += `${prefix}${pointer}${key}${comment}\n`;
53+
54+
// 2. Recurse into children if they exist
55+
if (children) {
56+
result += parseObjectTree(children, nextPrefix);
57+
}
58+
});
59+
60+
return result;
61+
}
62+
63+
type FileValue = string | null;
64+
65+
export interface ExpandedItem {
66+
_comment?: string;
67+
_children?: DirectoryStructure;
68+
}
69+
70+
// A node can now be a primitive file, a simple nested directory, or an expanded item
71+
export type TreeItem = FileValue | DirectoryStructure | ExpandedItem;
72+
73+
export interface DirectoryStructure {
74+
[nodeName: string]: TreeItem;
75+
}
76+
77+
export type FileTreeProps = {
78+
structure: DirectoryStructure;
79+
};
80+
81+
export default function FileTree({ structure }: FileTreeProps): JSX.Element {
82+
return (
83+
<CodeBlock>
84+
{parseObjectTree(structure)}
85+
</CodeBlock>
86+
);
87+
}

0 commit comments

Comments
 (0)