Skip to content

Commit

Permalink
feat: add and expose resetFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Dsaquel committed Jan 6, 2025
1 parent 47da477 commit 3f9bf09
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Repl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ function reload() {
outputRef.value?.reload()
}
defineExpose({ reload })
/**
* Reset playground files
*/
function resetFiles() {
props.store.resetFiles()
}
defineExpose({ reload, resetFiles })
</script>

<template>
Expand Down
11 changes: 11 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ export function useStore(
)
}

const resetFiles = () => {
setActive(mainFile.value)
setDefaultFile()
for (const filename of Object.keys(files.value))
if (![mainFile.value, tsconfigFile, importMapFile].includes(filename))
delete files.value[filename]
}

if (serializedState) {
deserialize(serializedState)
} else {
Expand Down Expand Up @@ -373,6 +381,7 @@ export function useStore(
deserialize,
getFiles,
setFiles,
resetFiles,
})
return store
}
Expand Down Expand Up @@ -441,6 +450,7 @@ export interface ReplStore extends UnwrapRef<StoreState> {
deserialize(serializedState: string): void
getFiles(): Record<string, string>
setFiles(newFiles: Record<string, string>, mainFile?: string): Promise<void>
resetFiles(): void
}

export type Store = Pick<
Expand All @@ -465,6 +475,7 @@ export type Store = Pick<
| 'renameFile'
| 'getImportMap'
| 'getTsConfig'
| 'resetFiles'
>

export class File {
Expand Down

0 comments on commit 3f9bf09

Please sign in to comment.