Skip to content

Commit

Permalink
show open file + set monaco lang
Browse files Browse the repository at this point in the history
  • Loading branch information
madeindjs committed Jan 7, 2025
1 parent 3b8ebeb commit 1f06e0a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 18 deletions.
20 changes: 6 additions & 14 deletions src/ui/src/builder/panels/BuilderCodePanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
<div class="BuilderCodePanel__content">
<ShareSourceFilesTree
:source-files="sourceFiles"
:path-active="openedFilePath"
class="BuilderCodePanel__content__tree"
@select="updateEditedFile"
@select="openFile"
/>
<BuilderEmbeddedCodeEditor
v-model="code"
class="BuilderCodePanel__content__editor"
variant="full"
language="python"
:language="openedFileLanguage"
:disabled="isDisabled"
@update:model-value="handleUpdate"
></BuilderEmbeddedCodeEditor>
Expand All @@ -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;
Expand All @@ -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<string>(wf.runCode.value);
const status = ref<null | {
type: "error" | "success" | "neutral";
message: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ShareSourceFilesTreeNode from "./ShareSourceFilesTreeNode.vue";
const props = defineProps({
sourceFiles: { type: Object as PropType<SourceFiles>, required: true },
path: { type: Array as PropType<string[]>, default: () => [] },
pathActive: { type: Array as PropType<string[]>, default: () => [] },
});
defineEmits({
Expand All @@ -25,6 +26,7 @@ const sourceFilesEntries = computed(() =>
:path="path"
:content="content"
:root="filePath"
:path-active="pathActive"
@select="$emit('select', $event)"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,41 @@ const props = defineProps({
required: true,
},
path: { type: Array as PropType<string[]>, default: () => [] },
pathActive: { type: Array as PropType<string[]>, default: () => [] },
root: { type: String, required: true },
});
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");
</script>

<template>
<SharedCollapsible v-if="isDirectory">
<template #title
><span>{{ root }}</span></template
><span
class="SharedSourceFilesTreeNode__folder"
:class="{
'SharedSourceFilesTreeNode__folder--active': isPathActive,
}"
>
<span class="material-symbols-outlined">folder</span>
{{ root }}</span
></template
>
<template #content>
<div class="SharedSourceFilesTreeNode__content">
<ShareSourceFilesTree
:path="[...path, root]"
:path-active="pathActive"
:source-files="content as any"
@select="$emit('select', $event)"
/>
Expand All @@ -38,8 +54,12 @@ const isDirectory = computed(() => typeof props.content !== "string");
<button
v-else
class="SharedSourceFilesTreeNode__file"
:class="{
'SharedSourceFilesTreeNode__file--active': isPathActive,
}"
@click="$emit('select', [...path, root])"
>
<span class="material-symbols-outlined"> description </span>
{{ root }}
</button>
</template>
Expand All @@ -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);
}
</style>
61 changes: 58 additions & 3 deletions src/ui/src/core/useSourceFiles.ts
Original file line number Diff line number Diff line change
@@ -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<SourceFiles>) {}
export function useSourceFiles(wf: Core) {
const sourceFiles = computed<SourceFiles>(() => wf.sourceFiles.value ?? {});

const code = ref("");
const openedFilePath = shallowRef<string[] | undefined>();

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,
};
}

0 comments on commit 1f06e0a

Please sign in to comment.