Skip to content

Commit 7e63511

Browse files
authored
feat: allow starting on a specific view (#15)
1 parent 8bcf7f0 commit 7e63511

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ import '@vue/repl/style.css'
2222
import { watchEffect } from 'vue'
2323
import { Repl, ReplStore } from '@vue/repl'
2424
25+
// retrieve some configuration options from the URL
26+
const query = new URLSearchParams(location.search)
27+
2528
const store = new ReplStore({
2629
// initialize repl with previously serialized state
2730
serializedState: location.hash.slice(1),
2831
32+
// starts on the output pane (mobile only) if the URL has a showOutput query
33+
showOutput: query.has('showOutput'),
34+
// starts on a different tab on the output pane if the URL has a outputMode query
35+
// and default to the "preview" tab
36+
outputMode: (query.get('outputMode') || 'preview')
37+
2938
// specify the default URL to import Vue runtime from in the sandbox
3039
// default is the CDN link from unpkg.com with version matching Vue's version
3140
// from peerDependency

src/SplitPane.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<script setup lang="ts">
2-
import { ref, reactive, computed } from 'vue'
2+
import { ref, reactive, computed, inject } from 'vue'
3+
import { Store } from './store'
34
45
const props = defineProps<{ layout?: string }>()
56
const isVertical = computed(() => props.layout === 'vertical')
67
78
const container = ref()
89
910
// mobile only
10-
const showOutput = ref(false)
11+
const store = inject('store') as Store
12+
const showOutput = ref(store.initialShowOutput)
1113
1214
const state = reactive({
1315
dragging: false,

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { default as Repl } from './Repl.vue'
22
export { ReplStore, File } from './store'
33
export { compileFile } from './transform'
44
export type { Store, SFCOptions, StoreState } from './store'
5+
export type { OutputModes } from './output/types'

src/output/Output.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Preview from './Preview.vue'
33
import CodeMirror from '../codemirror/CodeMirror.vue'
44
import { Store } from '../store'
55
import { inject, ref, computed } from 'vue'
6+
import type { OutputModes } from './types'
67
78
const props = defineProps<{
89
showCompileOutput?: boolean
@@ -15,8 +16,11 @@ const modes = computed(() =>
1516
: (['preview'] as const)
1617
)
1718
18-
type Modes = typeof modes.value[number]
19-
const mode = ref<Modes>('preview')
19+
const mode = ref<OutputModes>(
20+
(modes.value as readonly string[]).includes(store.initialOutputMode)
21+
? store.initialOutputMode as OutputModes
22+
: 'preview'
23+
)
2024
</script>
2125

2226
<template>

src/output/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type OutputModes = 'preview' | 'js' | 'css' | 'ssr'

src/store.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SFCAsyncStyleCompileOptions,
88
SFCTemplateCompileOptions
99
} from 'vue/compiler-sfc'
10+
import { OutputModes } from './output/types'
1011

1112
const defaultMainFile = 'App.vue'
1213

@@ -66,15 +67,22 @@ export class ReplStore implements Store {
6667
state: StoreState
6768
compiler = defaultCompiler
6869
options?: SFCOptions
70+
initialShowOutput: boolean
71+
initialOutputMode: OutputModes | string
6972

7073
private defaultVueRuntimeURL: string
7174
private pendingCompiler: Promise<any> | null = null
7275

7376
constructor({
7477
serializedState = '',
75-
defaultVueRuntimeURL = `https://unpkg.com/@vue/runtime-dom@${version}/dist/runtime-dom.esm-browser.js`
78+
defaultVueRuntimeURL = `https://unpkg.com/@vue/runtime-dom@${version}/dist/runtime-dom.esm-browser.js`,
79+
showOutput = false,
80+
outputMode = 'preview'
7681
}: {
7782
serializedState?: string
83+
showOutput?: boolean
84+
// loose type to allow getting from the URL without inducing a typing error
85+
outputMode?: OutputModes | string
7886
defaultVueRuntimeURL?: string
7987
} = {}) {
8088
let files: StoreState['files'] = {}
@@ -91,6 +99,8 @@ export class ReplStore implements Store {
9199
}
92100

93101
this.defaultVueRuntimeURL = defaultVueRuntimeURL
102+
this.initialShowOutput = showOutput
103+
this.initialOutputMode = outputMode
94104

95105
let mainFile = defaultMainFile
96106
if (!files[mainFile]) {

test/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import { Repl, ReplStore } from '../src'
55

66
const App = {
77
setup() {
8+
const query = new URLSearchParams(location.search)
89
const store = new ReplStore({
910
serializedState: location.hash.slice(1),
11+
showOutput: query.has('so'),
12+
outputMode: query.get('om') || 'preview',
1013
defaultVueRuntimeURL: import.meta.env.PROD
1114
? undefined
1215
: `${location.origin}/src/vue-dev-proxy`

0 commit comments

Comments
 (0)