|
53 | 53 | <v-btn |
54 | 54 | v-if="isHovering" |
55 | 55 | :key="icon" |
| 56 | + :icon="needsPlaygroundLink ? '$vuetify-play' : '$vuetify-bin'" |
56 | 57 | class="text-disabled me-12 mt-2 app-markup-btn position-absolute right-0 top-0" |
57 | 58 | density="comfortable" |
58 | | - icon="$vuetify-bin" |
59 | 59 | size="small" |
60 | 60 | v-bind="activatorProps" |
61 | 61 | variant="text" |
62 | | - @click="bin" |
| 62 | + @click="openCode" |
63 | 63 | /> |
64 | 64 | </v-fade-transition> |
65 | 65 | </template> |
66 | 66 |
|
67 | | - <span>{{ t('open-in-vuetify-bin') }}</span> |
| 67 | + <span>{{ needsPlaygroundLink ? t('open-in-playground') : t('open-in-vuetify-bin') }}</span> |
68 | 68 | </v-tooltip> |
69 | 69 |
|
70 | 70 | <div class="pa-4 pe-12"> |
|
97 | 97 |
|
98 | 98 | const props = defineProps({ |
99 | 99 | resource: String, |
100 | | - code: null, |
| 100 | + code: { |
| 101 | + type: [String, Array] as PropType<string | CodeSection[]>, |
| 102 | + default: '', |
| 103 | + }, |
101 | 104 | inline: Boolean, |
102 | 105 | language: { |
103 | 106 | type: String, |
|
130 | 133 | const root = ref<ComponentPublicInstance>() |
131 | 134 |
|
132 | 135 | 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 | | - }) |
136 | 136 |
|
137 | 137 | const className = computed(() => `language-${props.language}`) |
138 | 138 | const icon = computed(() => clicked.value ? 'mdi-check' : 'mdi-clipboard-text-outline') |
139 | 139 |
|
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 () { |
141 | 163 | const el = root.value?.$el.querySelector('code') |
142 | | - const code = props.code || el?.innerText || '' |
| 164 | + const code = displayedCode.value || el?.innerText || '' |
143 | 165 | const language = props.language || 'markdown' |
144 | 166 | const title = props.resource |
145 | 167 |
|
|
148 | 170 | window.open(compressed, '_blank') |
149 | 171 | } |
150 | 172 |
|
| 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 | +
|
151 | 181 | async function copy () { |
152 | 182 | const el = root.value?.$el.querySelector('code') |
153 | 183 |
|
154 | | - navigator.clipboard.writeText(props.code || el?.innerText || '') |
| 184 | + navigator.clipboard.writeText(displayedCode.value || el?.innerText || '') |
155 | 185 |
|
156 | 186 | clicked.value = true |
157 | 187 |
|
|
0 commit comments