tiptap/demos/preview/Demo.vue

239 lines
5.9 KiB
Vue
Raw Normal View History

2021-08-25 17:52:20 +08:00
<template>
<div class="overflow-hidden antialiased rounded-lg">
<div class="px-3 py-1 bg-black flex items-center gap-0.5">
2021-08-25 17:52:20 +08:00
<button
v-for="(language, index) in sortedTabs"
:key="index"
@click="setTab(language.name)"
2024-05-30 23:28:46 +08:00
class="px-3 py-2 text-sm text-white leading-[125%] font-semibold rounded-[0.625rem] transition-all"
2021-08-25 17:52:20 +08:00
:class="[currentTab === language.name
2024-05-30 23:28:46 +08:00
? 'opacity-100 bg-[#1C1917]'
: 'opacity-50 bg-transparent hover:opacity-100 hover:bg-[#1C1917]'
2021-08-25 17:52:20 +08:00
]"
>
{{ language.name }}
</button>
</div>
2024-05-30 23:28:46 +08:00
<div class="overflow-hidden">
2021-08-25 17:52:20 +08:00
<div
2024-05-30 23:28:46 +08:00
class="bg-white"
:class="[hidePreview ? 'hidden' : '']"
2021-08-25 17:52:20 +08:00
>
<demo-frame
:src="currentIframeUrl"
:key="currentIframeUrl"
/>
</div>
<div class="text-white bg-black" v-if="!hideSource && currentFile">
2021-08-25 17:52:20 +08:00
<div class="flex overflow-x-auto">
<div class="flex flex-auto px-4 border-b-2 border-gray-800">
<button
2021-08-27 05:03:31 +08:00
class="inline-flex relative mr-4 py-2 pb-[calc(0.3rem + 2px)] mb-[-2px] border-b-2 border-transparent font-mono text-sm whitespace-nowrap"
2021-08-25 17:52:20 +08:00
:class="[!showDebug && currentFile.content === file.content
? 'text-white border-white font-bold'
: 'text-gray-400'
]"
v-for="(file, index) in source"
:key="index"
@click="setFile(file.name)"
>
{{ file.name }}
</button>
<button
v-if="debugJSON"
class="inline-flex relative py-2 pb-[calc(0.3rem + 2px)] mb-[-2px] border-b-2 border-transparent font-mono text-sm ml-auto"
:class="[showDebug
? 'text-white border-white font-bold'
: 'text-gray-400'
]"
@click="showDebug = !showDebug"
>
2021-09-19 04:38:27 +08:00
Inspect
2021-08-25 17:52:20 +08:00
</button>
</div>
</div>
<div class="overflow-dark overflow-auto max-h-[500px] relative text-white">
<shiki
class="p-4 overflow-visible"
2021-08-25 17:52:20 +08:00
:language="debugJSON && showDebug ? 'js' : getFileExtension(currentFile.name)"
:code="debugJSON && showDebug ? debugJSON : currentFile.content"
/>
</div>
<div class="flex justify-between px-4 py-2 text-gray-400 border-t border-gray-800 text-md">
<a class="flex-shrink min-w-0 overflow-hidden overflow-ellipsis whitespace-nowrap" :href="currentIframeUrl">
2021-08-25 17:52:20 +08:00
{{ name }}/{{ currentTab }}
</a>
<a class="pl-4 whitespace-nowrap" :href="githubUrl" target="_blank">
2021-08-25 17:52:20 +08:00
Edit on GitHub
</a>
</div>
</div>
</div>
</div>
</template>
<script>
import { getDebugJSON } from '@tiptap/core'
2021-08-25 17:52:20 +08:00
import DemoFrame from './DemoFrame.vue'
import Shiki from './Shiki.vue'
export default {
components: {
DemoFrame,
Shiki,
},
props: {
name: {
type: String,
required: true,
},
tabs: {
type: Object,
required: true,
},
},
data() {
return {
data: [],
sources: {},
currentTab: null,
currentFile: null,
tabOrder: ['React', 'Vue', 'Svelte', 'JS'],
2021-08-25 17:52:20 +08:00
debugJSON: null,
showDebug: false,
}
},
computed: {
currentIframeUrl() {
return `/src/${this.name}/${this.currentTab}/`
},
firstTabSelected() {
return this.sortedTabs[0].name === this.currentTab
},
sortedTabs() {
return [...this.tabs].sort((a, b) => {
return this.tabOrder.indexOf(a.name) - this.tabOrder.indexOf(b.name)
})
},
query() {
return Object.fromEntries(Object
.entries(this.$route.query)
.map(([key, value]) => [key, this.fromString(value)]))
},
inline() {
return this.query.inline || false
},
hideSource() {
return this.query.hideSource || false
},
hidePreview() {
return this.query.hidePreview || false
},
2021-08-25 17:52:20 +08:00
githubUrl() {
2021-09-22 21:14:27 +08:00
return `https://github.com/ueberdosis/tiptap/tree/main/demos/src/${this.name}`
2021-08-25 17:52:20 +08:00
},
source() {
return this.sources[this.currentTab]
},
},
methods: {
getFileExtension(name) {
return name.split('.').pop()
},
setTab(name, persist = true) {
2021-08-25 17:52:20 +08:00
this.currentTab = name
if (persist) {
localStorage.tab = name
}
2021-08-25 17:52:20 +08:00
},
setFile(name) {
this.showDebug = false
this.currentFile = this.source.find(item => item.name === name)
},
onSource(event) {
this.sources[this.currentTab] = event.detail
this.setFile(this.source[0].name)
},
onEditor(event) {
const editor = event.detail
if (!editor) {
this.debugJSON = null
return
}
this.debugJSON = JSON.stringify(getDebugJSON(editor.state.doc), null, ' ')
editor.on('update', () => {
this.debugJSON = JSON.stringify(getDebugJSON(editor.state.doc), null, ' ')
})
},
fromString(value) {
2021-09-20 16:05:13 +08:00
if (value === null) {
return true
2021-08-25 17:52:20 +08:00
}
if (value.match(/^\d*(\.\d+)?$/)) {
return Number(value)
}
if (value === 'true') {
return true
}
if (value === 'false') {
return false
}
if (value === 'null') {
return null
}
return value
},
},
mounted() {
// TODO: load language from url params
const intitialTab = localStorage.tab && this.tabs.some(tab => tab.name === localStorage.tab)
? localStorage.tab
: this.sortedTabs[0]?.name
this.setTab(intitialTab, false)
2021-08-25 17:52:20 +08:00
window.document.addEventListener('editor', this.onEditor, false)
window.document.addEventListener('source', this.onSource, false)
},
beforeUnmount() {
window.document.removeEventListener('editor', this.onEditor)
window.document.removeEventListener('source', this.onSource)
},
}
</script>