-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: useStore & useVueImportMap composable #207
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f54863
refactor
sxzz 06f67b4
fix: don't show tsconfig
sxzz abbd541
feat: setFiles & getFiles
sxzz 85a0a13
feat: export ReplStore
sxzz 0bad786
fix: import map
sxzz 5f61884
refactor!: drop sfcOptions on component
sxzz 324ce89
feat: export mergeImportMap
sxzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<script setup lang="ts"> | ||
import SplitPane from './SplitPane.vue' | ||
import Output from './output/Output.vue' | ||
import { ReplStore, type SFCOptions, type Store } from './store' | ||
import { computed, provide, ref, toRef, watchEffect } from 'vue' | ||
import type { EditorComponentType } from './types' | ||
import { type Store, useStore } from './store' | ||
import { computed, provide, ref, toRef } from 'vue' | ||
import { type EditorComponentType, injectKeyStore } from './types' | ||
import EditorContainer from './editor/EditorContainer.vue' | ||
|
||
export interface Props { | ||
|
@@ -15,7 +15,6 @@ export interface Props { | |
showImportMap?: boolean | ||
showTsConfig?: boolean | ||
clearConsole?: boolean | ||
sfcOptions?: SFCOptions | ||
layout?: 'horizontal' | 'vertical' | ||
layoutReverse?: boolean | ||
ssr?: boolean | ||
|
@@ -32,7 +31,7 @@ export interface Props { | |
|
||
const props = withDefaults(defineProps<Props>(), { | ||
theme: 'light', | ||
store: () => new ReplStore(), | ||
store: () => useStore(), | ||
autoResize: true, | ||
showCompileOutput: true, | ||
showImportMap: true, | ||
|
@@ -49,7 +48,6 @@ const props = withDefaults(defineProps<Props>(), { | |
useCode: '', | ||
}, | ||
}), | ||
sfcOptions: () => ({}), | ||
layout: 'horizontal', | ||
}) | ||
|
||
|
@@ -59,28 +57,12 @@ if (!props.editor) { | |
|
||
const outputRef = ref<InstanceType<typeof Output>>() | ||
|
||
watchEffect(() => { | ||
const { store } = props | ||
const sfcOptions = (store.options = props.sfcOptions || {}) | ||
sfcOptions.script ||= {} | ||
sfcOptions.script.fs = { | ||
fileExists(file: string) { | ||
if (file.startsWith('/')) file = file.slice(1) | ||
return !!store.state.files[file] | ||
}, | ||
readFile(file: string) { | ||
if (file.startsWith('/')) file = file.slice(1) | ||
return store.state.files[file].code | ||
}, | ||
} | ||
}) | ||
Comment on lines
-62
to
-76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to store |
||
|
||
props.store.init() | ||
|
||
const editorSlotName = computed(() => (props.layoutReverse ? 'right' : 'left')) | ||
const outputSlotName = computed(() => (props.layoutReverse ? 'left' : 'right')) | ||
|
||
provide('store', props.store) | ||
provide(injectKeyStore, props.store) | ||
provide('autoresize', props.autoResize) | ||
provide('import-map', toRef(props, 'showImportMap')) | ||
provide('tsconfig', toRef(props, 'showTsConfig')) | ||
|
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { computed, version as currentVersion, ref } from 'vue' | ||
|
||
export function useVueImportMap( | ||
defaults: { | ||
runtimeDev?: string | (() => string) | ||
runtimeProd?: string | (() => string) | ||
serverRenderer?: string | (() => string) | ||
} = {}, | ||
) { | ||
function normalizeDefaults(defaults?: string | (() => string)) { | ||
if (!defaults) return | ||
return typeof defaults === 'string' ? defaults : defaults() | ||
} | ||
|
||
const productionMode = ref(false) | ||
const vueVersion = ref<string | undefined>() | ||
const importMap = computed<ImportMap>(() => { | ||
const vue = | ||
(!vueVersion.value && | ||
normalizeDefaults( | ||
productionMode.value ? defaults.runtimeProd : defaults.runtimeDev, | ||
)) || | ||
`https://cdn.jsdelivr.net/npm/@vue/runtime-dom@${ | ||
vueVersion.value || currentVersion | ||
}/dist/runtime-dom.esm-browser${productionMode.value ? `.prod` : ``}.js` | ||
|
||
const serverRenderer = | ||
(!vueVersion.value && normalizeDefaults(defaults.serverRenderer)) || | ||
`https://cdn.jsdelivr.net/npm/@vue/server-renderer@${ | ||
vueVersion.value || currentVersion | ||
}/dist/server-renderer.esm-browser.js` | ||
return { | ||
imports: { | ||
vue, | ||
'vue/server-renderer': serverRenderer, | ||
}, | ||
} | ||
}) | ||
|
||
return { | ||
productionMode, | ||
importMap, | ||
vueVersion, | ||
defaultVersion: currentVersion, | ||
} | ||
} | ||
|
||
export interface ImportMap { | ||
imports?: Record<string, string | undefined> | ||
scopes?: Record<string, Record<string, string>> | ||
} | ||
|
||
export function mergeImportMap(a: ImportMap, b: ImportMap): ImportMap { | ||
return { | ||
imports: { ...a.imports, ...b.imports }, | ||
scopes: { ...a.scopes, ...b.scopes }, | ||
} | ||
} |
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,7 +1,14 @@ | ||
export { default as Repl } from './Repl.vue' | ||
export { default as Preview } from './output/Preview.vue' | ||
export { ReplStore, File } from './store' | ||
export { | ||
useStore, | ||
File, | ||
type SFCOptions, | ||
type StoreState, | ||
type Store, | ||
type ReplStore, | ||
} from './store' | ||
export { useVueImportMap, mergeImportMap, type ImportMap } from './import-map' | ||
export { compileFile } from './transform' | ||
export type { Props as ReplProps } from './Repl.vue' | ||
export type { Store, StoreOptions, SFCOptions, StoreState } from './store' | ||
export type { OutputModes } from './types' |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking change: remove
sfcOptions
on component but moved toStore