From 1f06e0a552a0bca528b081f6f75be929e1145e00 Mon Sep 17 00:00:00 2001 From: Alexandre Rousseau Date: Tue, 7 Jan 2025 13:50:07 +0100 Subject: [PATCH] show open file + set monaco lang --- .../src/builder/panels/BuilderCodePanel.vue | 20 ++---- .../ShareSourceFilesTree.vue | 2 + .../ShareSourceFilesTreeNode.vue | 31 +++++++++- src/ui/src/core/useSourceFiles.ts | 61 ++++++++++++++++++- 4 files changed, 96 insertions(+), 18 deletions(-) diff --git a/src/ui/src/builder/panels/BuilderCodePanel.vue b/src/ui/src/builder/panels/BuilderCodePanel.vue index e69410a5..67d798c4 100644 --- a/src/ui/src/builder/panels/BuilderCodePanel.vue +++ b/src/ui/src/builder/panels/BuilderCodePanel.vue @@ -11,14 +11,15 @@
@@ -37,6 +38,7 @@ import BuilderPanel, { type BuilderPanelAction } from "./BuilderPanel.vue"; import BuilderAsyncLoader from "../BuilderAsyncLoader.vue"; import injectionKeys from "@/injectionKeys"; import ShareSourceFilesTree from "@/components/shared/SharedSourceFilesTree/ShareSourceFilesTree.vue"; +import { useSourceFiles } from "@/core/useSourceFiles"; defineProps<{ contentsTeleportEl: HTMLElement; @@ -49,19 +51,9 @@ const BuilderEmbeddedCodeEditor = defineAsyncComponent({ const wf = inject(injectionKeys.core); -const sourceFiles = computed(() => wf.sourceFiles.value ?? {}); +const { sourceFiles, openFile, code, openedFileLanguage, openedFilePath } = + useSourceFiles(wf); -function updateEditedFile(path: string[]) { - console.log("##updateEditedFile", path); - - const file = path.reduce((acc, value) => { - return acc[value]; - }, sourceFiles.value); - - code.value = file; -} - -const code = ref(wf.runCode.value); const status = ref, required: true }, path: { type: Array as PropType, default: () => [] }, + pathActive: { type: Array as PropType, default: () => [] }, }); defineEmits({ @@ -25,6 +26,7 @@ const sourceFilesEntries = computed(() => :path="path" :content="content" :root="filePath" + :path-active="pathActive" @select="$emit('select', $event)" />
diff --git a/src/ui/src/components/shared/SharedSourceFilesTree/ShareSourceFilesTreeNode.vue b/src/ui/src/components/shared/SharedSourceFilesTree/ShareSourceFilesTreeNode.vue index fb2880ff..e52b9313 100644 --- a/src/ui/src/components/shared/SharedSourceFilesTree/ShareSourceFilesTreeNode.vue +++ b/src/ui/src/components/shared/SharedSourceFilesTree/ShareSourceFilesTreeNode.vue @@ -10,6 +10,7 @@ const props = defineProps({ required: true, }, path: { type: Array as PropType, default: () => [] }, + pathActive: { type: Array as PropType, default: () => [] }, root: { type: String, required: true }, }); @@ -17,18 +18,33 @@ defineEmits({ select: (path: string[]) => Array.isArray(path), }); +const isPathActive = computed(() => + props.pathActive + .join("/") + .startsWith([...props.path, props.root].join("/")), +); + const isDirectory = computed(() => typeof props.content !== "string"); @@ -56,5 +76,14 @@ const isDirectory = computed(() => typeof props.content !== "string"); cursor: pointer; padding: none; text-align: left; + + display: grid; + grid-template-columns: auto 1fr; + gap: 4px; + align-items: center; +} +.SharedSourceFilesTreeNode__folder--active, +.SharedSourceFilesTreeNode__file--active { + color: var(--accentColor); } diff --git a/src/ui/src/core/useSourceFiles.ts b/src/ui/src/core/useSourceFiles.ts index e5ff2932..088bb865 100644 --- a/src/ui/src/core/useSourceFiles.ts +++ b/src/ui/src/core/useSourceFiles.ts @@ -1,4 +1,59 @@ -import { SourceFiles } from "@/writerTypes"; -import { ComputedRef } from "vue"; +import { Core, SourceFiles } from "@/writerTypes"; +import { computed, onMounted, ref, shallowRef } from "vue"; -export function useSourceFiles(sourceFiles: ComputedRef) {} +export function useSourceFiles(wf: Core) { + const sourceFiles = computed(() => wf.sourceFiles.value ?? {}); + + const code = ref(""); + const openedFilePath = shallowRef(); + + const openedFileExtension = computed(() => { + if (!openedFilePath.value?.length) return undefined; + const filename = openedFilePath.value.at(-1); + return filename.split(".").at(-1); + }); + + const openedFileLanguage = computed(() => { + switch (openedFileExtension.value) { + case "py": + return "python"; + case "md": + case "markdown": + return "markdown"; + default: + return openedFileExtension.value; + } + }); + + function openFile(path: string[]) { + try { + code.value = getFileContent(path); + openedFilePath.value = path; + } catch (e) { + console.error(e); + } + } + + onMounted(() => { + openFile(["main.py"]); + }); + + function getFileContent(path: string[]) { + const file = path.reduce((acc, value) => { + return acc[value]; + }, sourceFiles.value); + + if (typeof file !== "string") { + throw Error(`Could not get file ${path.join("/")}`); + } + return file; + } + + return { + code, + sourceFiles, + openedFilePath, + openedFileLanguage, + openFile, + }; +}