-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |