Skip to content

Commit c8ea488

Browse files
committed
add support for playground in markup
1 parent 5bbc595 commit c8ea488

File tree

5 files changed

+73
-23
lines changed

5 files changed

+73
-23
lines changed

packages/docs/auto-imports.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ declare global {
172172
export type { Component, Slot, Slots, ComponentPublicInstance, ComputedRef, DirectiveBinding, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, MaybeRef, MaybeRefOrGetter, VNode, WritableComputedRef } from 'vue'
173173
import('vue')
174174
// @ts-ignore
175+
export type { CodeSection } from './src/composables/playground'
176+
import('./src/composables/playground')
177+
// @ts-ignore
175178
export type { Category } from './src/stores/app'
176179
import('./src/stores/app')
177180
// @ts-ignore

packages/docs/src/components/app/Markup.vue

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@
5353
<v-btn
5454
v-if="isHovering"
5555
:key="icon"
56+
:icon="needsPlaygroundLink ? '$vuetify-play' : '$vuetify-bin'"
5657
class="text-disabled me-12 mt-2 app-markup-btn position-absolute right-0 top-0"
5758
density="comfortable"
58-
icon="$vuetify-bin"
5959
size="small"
6060
v-bind="activatorProps"
6161
variant="text"
62-
@click="bin"
62+
@click="openCode"
6363
/>
6464
</v-fade-transition>
6565
</template>
6666

67-
<span>{{ t('open-in-vuetify-bin') }}</span>
67+
<span>{{ needsPlaygroundLink ? t('open-in-playground') : t('open-in-vuetify-bin') }}</span>
6868
</v-tooltip>
6969

7070
<div class="pa-4 pe-12">
@@ -97,7 +97,10 @@
9797
9898
const props = defineProps({
9999
resource: String,
100-
code: null,
100+
code: {
101+
type: [String, Array] as PropType<string | CodeSection[]>,
102+
default: '',
103+
},
101104
inline: Boolean,
102105
language: {
103106
type: String,
@@ -130,16 +133,35 @@
130133
const root = ref<ComponentPublicInstance>()
131134
132135
const highlighted = shallowRef('')
133-
watchEffect(async () => {
134-
highlighted.value = props.code && props.language && Prism.highlight(await props.code, Prism.languages[props.language], props.language)
135-
})
136136
137137
const className = computed(() => `language-${props.language}`)
138138
const icon = computed(() => clicked.value ? 'mdi-check' : 'mdi-clipboard-text-outline')
139139
140-
async function bin () {
140+
const needsPlaygroundLink = computed(() => Array.isArray(props.code))
141+
142+
const displayedCode = computed(() => {
143+
if (typeof props.code === 'string') {
144+
return props.code
145+
}
146+
147+
return props.code.map((section: CodeSection) => section.content).join('\n\n')
148+
})
149+
150+
watchEffect(async () => {
151+
highlighted.value = displayedCode.value && props.language && Prism.highlight(await displayedCode.value, Prism.languages[props.language], props.language)
152+
})
153+
154+
function openCode () {
155+
if (needsPlaygroundLink.value) {
156+
openPlayground()
157+
} else {
158+
openBin()
159+
}
160+
}
161+
162+
async function openBin () {
141163
const el = root.value?.$el.querySelector('code')
142-
const code = props.code || el?.innerText || ''
164+
const code = displayedCode.value || el?.innerText || ''
143165
const language = props.language || 'markdown'
144166
const title = props.resource
145167
@@ -148,10 +170,18 @@
148170
window.open(compressed, '_blank')
149171
}
150172
173+
async function openPlayground () {
174+
if (typeof props.code === 'string') return
175+
176+
const url = usePlayground(props.code)
177+
178+
window.open(url, '_blank')
179+
}
180+
151181
async function copy () {
152182
const el = root.value?.$el.querySelector('code')
153183
154-
navigator.clipboard.writeText(props.code || el?.innerText || '')
184+
navigator.clipboard.writeText(displayedCode.value || el?.innerText || '')
155185
156186
clicked.value = true
157187

packages/docs/src/components/home/Entry.vue

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@
9393

9494
<v-col class="d-md-block d-none" cols="12" md="6">
9595
<v-sheet class="rounded-lg bg-surface elevation-10">
96-
<AppMarkup :code="code" class="bg-black rounded-lg" />
96+
<AppMarkup
97+
:code="code"
98+
class="bg-black rounded-lg pa-5"
99+
open-in-playground
100+
/>
97101
</v-sheet>
98102
</v-col>
99103
</v-row>
@@ -106,17 +110,23 @@
106110
</template>
107111

108112
<script setup>
109-
const code = `
110-
<template>
111-
<v-app>
112-
<v-container>
113-
<v-btn color="primary">
114-
Hello Vuetify!
115-
</v-btn>
116-
</v-container>
117-
</v-app>
118-
</template>
119-
`
113+
const codeContent = `<template>
114+
<v-app>
115+
<v-container>
116+
<v-btn color="primary">
117+
Hello Vuetify!
118+
</v-btn>
119+
</v-container>
120+
</v-app>
121+
</template>`
122+
123+
const code = [
124+
{
125+
name: 'template',
126+
content: codeContent,
127+
language: 'html',
128+
},
129+
]
120130
121131
const isCopying = shallowRef(false)
122132

packages/docs/src/composables/playground.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { strFromU8, strToU8, zlibSync } from 'fflate'
33
import { version as vuetifyVersion } from 'vuetify'
44
import { version as vueVersion } from 'vue'
55

6+
export type CodeSection = {
7+
name: string
8+
content: string
9+
language: string
10+
}
11+
612
// This is copied directly from playground
713
function utoa (data: string): string {
814
const buffer = strToU8(data)
@@ -12,7 +18,7 @@ function utoa (data: string): string {
1218
}
1319

1420
export function usePlayground (
15-
sections: ({ name: string, content: string, language: string})[] = [],
21+
sections: CodeSection[] = [],
1622
css: string[] = [],
1723
imports: Record<string, string> = {},
1824
setup?: string,

packages/docs/src/i18n/messages/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
"discord": "Discord",
139139
"done": "All done",
140140
"edit-in-playground": "Edit in Vuetify Playground",
141+
"open-in-playground": "Open in Vuetify Playground",
141142
"edit-this-page": "Edit this page on",
142143
"edit": "Edit",
143144
"enable-banners": "Enable banners",

0 commit comments

Comments
 (0)