primevue-mirror/components/doc/DocSectionCode.vue

141 lines
5.0 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
2023-10-15 04:50:22 +00:00
<div v-if="!embedded" class="doc-section-code">
2024-05-20 12:08:32 +00:00
<div class="doc-section-code-buttons animate-scalein animate-duration-300">
2023-02-28 08:29:30 +00:00
<template v-if="codeMode !== 'basic' && !hideToggleCode">
2024-05-20 12:08:32 +00:00
<button :class="['py-0 px-2 rounded h-2rem', { 'code-active': codeLang === 'composition' }]" @click="codeLang = 'composition'">Composition API</button>
<button :class="['py-0 px-2 rounded h-2rem', { 'code-active': codeLang === 'options' }]" @click="codeLang = 'options'">Options API</button>
2023-02-28 08:29:30 +00:00
</template>
<template v-if="!hideToggleCode">
2024-05-20 12:08:32 +00:00
<button v-tooltip.bottom="{ value: 'Toggle Full Code', class: 'doc-section-code-tooltip' }" type="button" @click="toggleCodeMode('composition')" class="h-8 w-8 p-0 inline-flex items-center justify-center">
2023-10-10 15:46:53 +00:00
<i class="pi pi-code"></i>
</button>
2023-02-28 08:29:30 +00:00
</template>
<template v-if="!hideToggleCode && code.data">
2024-05-20 12:08:32 +00:00
<button v-tooltip.bottom="{ value: 'View Data', class: 'doc-section-code-tooltip' }" type="button" @click="onToggleData" class="h-8 w-8 p-0 inline-flex items-center justify-center">
2023-10-10 15:46:53 +00:00
<i class="pi pi-database"></i>
</button>
2023-02-28 08:29:30 +00:00
</template>
<template v-if="!hideStackBlitz">
2024-05-20 12:08:32 +00:00
<button v-tooltip.bottom="{ value: 'Edit in StackBlitz', class: 'doc-section-code-tooltip' }" type="button" class="h-8 w-8 p-0 inline-flex items-center justify-center" @click="showStackblitz">
2023-10-18 10:15:34 +00:00
<svg role="img" width="13" height="18" viewBox="0 0 13 19" fill="currentColor" xmlns="http://www.w3.org/2000/svg" style="display: 'block'">
<path d="M0 10.6533H5.43896L2.26866 18.1733L12.6667 7.463H7.1986L10.3399 0L0 10.6533Z" />
2023-10-10 15:46:53 +00:00
</svg>
</button>
2023-02-28 08:29:30 +00:00
</template>
2024-05-20 12:08:32 +00:00
<button v-tooltip.bottom="{ value: 'Copy Code', class: 'doc-section-code-tooltip' }" type="button" @click="copyCode" class="h-8 w-8 p-0 inline-flex items-center justify-center">
2023-10-10 15:46:53 +00:00
<i class="pi pi-copy"></i>
</button>
2023-02-28 08:29:30 +00:00
</div>
2023-10-15 04:50:22 +00:00
<div>
2023-08-16 14:34:47 +00:00
<template v-if="codeMode === 'basic' && importCode">
<pre v-code.script><code>{{ code.basic }}
2023-02-28 17:40:48 +00:00
</code></pre>
2023-08-16 14:34:47 +00:00
</template>
2023-02-28 08:29:30 +00:00
2023-08-16 14:34:47 +00:00
<template v-if="codeMode === 'basic' && !importCode">
2023-10-15 06:00:52 +00:00
<pre v-code><code>{{ code.basic }}
2023-02-28 08:29:30 +00:00
</code></pre>
2023-08-16 14:34:47 +00:00
</template>
2023-02-28 08:29:30 +00:00
2023-08-16 14:34:47 +00:00
<template v-if="codeMode !== 'basic' && codeLang === 'options'">
2023-10-15 06:00:52 +00:00
<pre v-code><code>{{ code.options }}
2023-02-28 08:29:30 +00:00
</code></pre>
2023-08-16 14:34:47 +00:00
</template>
2023-02-28 08:29:30 +00:00
2023-08-16 14:34:47 +00:00
<template v-if="codeMode !== 'basic' && codeLang === 'composition'">
2023-10-15 06:00:52 +00:00
<pre v-code><code>{{ code.composition }}
2023-02-28 08:29:30 +00:00
</code></pre>
2023-08-16 14:34:47 +00:00
</template>
2023-02-28 08:29:30 +00:00
2023-08-16 14:34:47 +00:00
<template v-if="codeMode !== 'basic' && codeLang === 'data'">
2023-10-15 06:00:52 +00:00
<pre v-code.json><code>{{ code.data }}
2023-02-28 08:29:30 +00:00
</code></pre>
2023-08-16 14:34:47 +00:00
</template>
</div>
2023-02-28 08:29:30 +00:00
</div>
2023-07-25 19:30:22 +00:00
<div v-else id="embed"></div>
2023-02-28 08:29:30 +00:00
</template>
<script>
2024-01-30 08:16:35 +00:00
import { useStackBlitz } from './codeeditor';
2023-02-28 08:29:30 +00:00
export default {
inheritAttrs: false,
props: {
code: {
type: null,
default: null
},
service: {
type: Array,
default: null
},
hideToggleCode: {
type: Boolean,
default: false
},
hideCodeSandbox: {
type: Boolean,
2024-01-30 08:16:35 +00:00
default: true
2023-02-28 08:29:30 +00:00
},
hideStackBlitz: {
type: Boolean,
2024-04-02 12:10:37 +00:00
default: false
2023-02-28 08:29:30 +00:00
},
dependencies: {
type: null,
default: null
},
component: {
type: String,
default: null
},
extFiles: {
type: null,
default: null
2023-02-28 17:40:48 +00:00
},
importCode: {
type: Boolean,
default: false
2023-07-25 19:30:22 +00:00
},
embedded: {
type: Boolean,
default: false
2023-09-22 12:54:14 +00:00
},
scrollable: {
type: Boolean,
default: false
2023-02-28 08:29:30 +00:00
}
},
data() {
return {
codeMode: 'basic',
2023-03-20 09:59:03 +00:00
codeLang: this.code['options'] ? 'composition' : 'basic'
2023-02-28 08:29:30 +00:00
};
},
2023-07-25 19:30:22 +00:00
mounted() {
this.embedded && useStackBlitz(this.codeLang, this.code['composition'], this.service, this.code.pages, this.dependencies, this.component, this.extFiles, this.embedded);
},
2023-02-28 08:29:30 +00:00
methods: {
toggleCodeMode(content) {
this.codeMode = this.codeMode === 'basic' ? content : 'basic';
},
onToggleData() {
this.toggleCodeMode('data');
this.codeLang = 'data';
},
async copyCode() {
await navigator.clipboard.writeText(this.code[this.codeLang]);
},
showStackblitz() {
useStackBlitz(this.codeLang, this.code[this.codeLang === 'data' ? 'options' : this.codeLang], this.service, this.code.pages, this.dependencies, this.component, this.extFiles);
}
}
};
</script>