feat(Folder Structure): Add backwards converter for Folder Structure Page (#105)

* Folder Structure Diagram
- Added an extra column to convert folder trees back to their indented structures
- Created the `convertTreeToStructure` function which handles converting folder trees back to an indented structure

TextareaCopyable component
- Added `scrollable` boolean and `maxHeight` property to allow the content in the field to become scrollable, and define the maximum height before becoming scrollable

---------

Co-authored-by: ShareVB <sharevb@gmail.com>
This commit is contained in:
Dark_Phoenix_andShareVB authored and ShareVB committed 2025-06-22 15:39:42 +02:00
1 parent de76ee5b4d
commit 9186038c6f
4 files changed
+134 -34

No files matched your search

+11
View File
@@ -302,6 +302,11 @@ declare module '@vue/runtime-core' {
NanoMemo: typeof import('./src/tools/nano-memo/nano-memo.vue')['default']
'NanoMemo.content': typeof import('./src/tools/nano-memo/nano-memo.content.md')['default']
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
NCode: typeof import('naive-ui')['NCode']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDivider: typeof import('naive-ui')['NDivider']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NFormItem: typeof import('naive-ui')['NFormItem']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDivider: typeof import('naive-ui')['NDivider']
NEllipsis: typeof import('naive-ui')['NEllipsis']
@@ -311,6 +316,12 @@ declare module '@vue/runtime-core' {
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NTable: typeof import('naive-ui')['NTable']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
+23 -3
View File
@@ -31,6 +31,8 @@ const props = withDefaults(
wordWrap?: boolean
downloadFileName?: string
downloadButtonText?: string
scrollable?: boolean
maxHeight?: string
}>(),
{
followHeightOf: null,
@@ -39,6 +41,8 @@ const props = withDefaults(
copyMessage: 'Copy to clipboard',
downloadFileName: '',
downloadButtonText: 'Download',
scrollable: false,
maxHeight: '400px',
},
);
hljs.registerLanguage('sql', sqlHljs);
@@ -62,6 +66,17 @@ hljs.registerLanguage('protobuf', protobufHljs);
const { value, language, followHeightOf, copyPlacement, copyMessage, downloadFileName, downloadButtonText } = toRefs(props);
const { height } = followHeightOf.value ? useElementSize(followHeightOf) : { height: ref(null) };
const scrollbarRef = ref();
// Watch for content changes and scroll to bottom
watch(value, () => {
nextTick(() => {
if (scrollbarRef.value) {
scrollbarRef.value.scrollTo({ top: scrollbarRef.value.scrollbarInstRef.containerRef.scrollHeight });
}
});
}, { flush: 'post' });
const { copy, isJustCopied } = useCopy({ source: value, createToast: false });
const tooltipText = computed(() => isJustCopied.value ? 'Copied!' : copyMessage.value);
@@ -80,9 +95,14 @@ const { download } = useDownloadFileFromBase64(
:style="(copyPlacement === 'top-right' || copyPlacement === 'bottom-right') ? `padding-bottom: 50px` : ''"
>
<n-scrollbar
x-scrollable
ref="scrollbarRef"
:x-scrollable="true"
:y-scrollable="scrollable"
trigger="none"
:style="height ? `min-height: ${height - 40 /* card padding */ + 10 /* negative margin compensation */}px` : ''"
:style="{
minHeight: height ? `${height - 40 /* card padding */ + 10 /* negative margin compensation */}px` : '',
maxHeight: scrollable ? maxHeight : '',
}"
>
<n-config-provider :hljs="hljs">
<n-code :code="value" :language="language" :word-wrap="wordWrap" :trim="false" data-test-id="area-content" />
@@ -90,10 +110,10 @@ const { download } = useDownloadFileFromBase64(
</n-scrollbar>
<div
v-if="value && copyPlacement !== 'none'"
:top-10px="copyPlacement === 'top-right' ? '' : 'no'"
:bottom-10px="copyPlacement === 'bottom-right' ? '' : 'no'"
absolute right-10px
:style="scrollable ? 'z-index: 10; background: var(--bg-color); border-radius: 50%; padding: 2px;' : ''"
>
<c-tooltip v-if="value && copyPlacement !== 'outside'" :tooltip="tooltipText" position="left">
<c-button circle important:h-10 important:w-10 @click="copy()">
@@ -1,27 +1,35 @@
<script setup lang="ts">
import convertTreeToStructure from './lib/convert-tree-to-structure';
import { generateTree } from './lib/generate-tree';
import { parseInput } from './lib/parse-input';
import { withDefaultOnError } from '@/utils/defaults';
const inputStructure = ref([
'my-app',
' src',
' index.html',
' main.ts',
' main.scss',
' - build',
' - index.html',
' main.js',
' main.css',
'',
' ',
' .prettierrc.json',
' .gitlab-ci.yml',
' README.md',
'empty dir',
].join('\n'));
const inputStructureTemplate = `my-app
src
index.html
main.ts
main.scss
build
main.js
main.css
- test
- index.html
-main.js
-
...
...
README.md
empty dir`;
const inputStructure = ref(inputStructureTemplate);
const outputTree = computed(() => withDefaultOnError(() => generateTree(parseInput(inputStructure.value)), ''));
const inputTree = ref(generateTree(parseInput(inputStructureTemplate)));
const outputStructure = computed(() => withDefaultOnError(() => convertTreeToStructure((inputTree.value)), ''));
const MONACO_EDITOR_OPTIONS = {
automaticLayout: true,
formatOnType: true,
@@ -30,21 +38,36 @@ const MONACO_EDITOR_OPTIONS = {
</script>
<template>
<div>
<c-card title="Your indented structure" w-full>
<c-monaco-editor
v-model:value="inputStructure"
theme="vs-dark"
height="250px"
placeholder="Paste your indented structure here..."
:options="MONACO_EDITOR_OPTIONS"
/>
</c-card>
<div class="min-h-0 flex flex-row gap-8" style="margin: 0 auto; max-width: 2200px; flex: 1 !important;">
<div class="min-w-0 flex-1">
<c-card title="Indented Structure → Folder Tree">
<c-monaco-editor
v-model:value="inputStructure"
theme="vs-dark"
height="400px"
placeholder="Paste your indented structure here..."
:options="MONACO_EDITOR_OPTIONS"
/>
</c-card>
<n-divider />
<n-divider />
<n-form-item label="Your tree-like structure:">
<TextareaCopyable :value="outputTree" />
</n-form-item>
<TextareaCopyable :value="outputTree" :scrollable="true" />
</div>
<div class="min-w-0 flex-1">
<c-card title="Folder Tree → Indented Structure">
<c-monaco-editor
v-model:value="inputTree"
theme="vs-dark"
height="400px"
placeholder="Paste your folder tree here..."
:options="MONACO_EDITOR_OPTIONS"
/>
</c-card>
<n-divider />
<TextareaCopyable :value="outputStructure" :scrollable="true" />
</div>
</div>
</template>
@@ -0,0 +1,46 @@
import { LINE_STRINGS } from './line-strings';
export default function convertTreeToStructure(tree: string): string {
const treeLines = tree.split('\n').filter(line => line.trim());
if (treeLines.length === 0) {
return '';
}
// Detect charset by checking for unicode box drawing characters (U+2500 to U+257F)
const charset = treeLines.some(line => /[\u2500-\u257F]/.test(line)) ? 'utf-8' : 'ascii';
const lines = LINE_STRINGS[charset];
// Create a single regex to match all tree connectors
const treeConnectorRegex = new RegExp(`(${lines.CHILD}|${lines.LAST_CHILD})`, 'g');
return treeLines
.flatMap((line) => {
// Find the last tree connector to determine depth and extract name
const matches = [...line.matchAll(treeConnectorRegex)];
if (matches.length === 0) {
// No tree connectors found - likely root or invalid line
return []; // Return empty array instead of null or empty string so the flatMap filters it out and doesn't leave unnecessary empty lines
}
const lastMatch = matches[matches.length - 1];
const connectorEnd = lastMatch.index! + lastMatch[0].length;
// Calculate depth (assuming 4 characters per level)
const depth = Math.floor(lastMatch.index! / 4);
// Extract folder name after the last connector
let folderName = line.substring(connectorEnd);
// When the folder name is an empty string (because it was created with "- ") or starts with a space, add a dash and space
// to preserve the empty tree connector or the spaces before actual text characters in the folder name
if (folderName === '' || folderName.startsWith(' ')) {
folderName = `- ${folderName}`;
}
// Create indented structure (2 spaces per level)
return [`${' '.repeat(depth * 2)}${folderName}`];
})
.join('\n');
}