feat: add copy for view code

This commit is contained in:
Justineo 2022-02-24 14:38:23 +08:00
parent 74da37a7a6
commit cfe14b966c
No known key found for this signature in database
GPG Key ID: B73F0979CF18A0EA

View File

@ -66,6 +66,15 @@
class="source" class="source"
:style="{ height: codeExpanded ? `${sourceHeight || 0}px` : '0' }" :style="{ height: codeExpanded ? `${sourceHeight || 0}px` : '0' }"
> >
<div class="source-toolbar">
<veui-button
v-tooltip="t('@onelive.copyCode')"
ui="icon reverse"
@click="copy"
>
<veui-icon name="copy"/>
</veui-button>
</div>
<slot name="source"/> <slot name="source"/>
</section> </section>
<transition name="editor"> <transition name="editor">
@ -85,15 +94,20 @@
</template> </template>
<script> <script>
import Vue from 'vue'
import { Button, Icon } from 'veui' import { Button, Icon } from 'veui'
import tooltip from 'veui/directives/tooltip' import tooltip from 'veui/directives/tooltip'
import modal from 'veui/managers/modal' import modal from 'veui/managers/modal'
import i18n from 'veui/mixins/i18n' import i18n from 'veui/mixins/i18n'
import toast from 'veui/plugins/toast'
import { BrowserWindow } from 'vue-windows' import { BrowserWindow } from 'vue-windows'
import { getLocale } from '../common/i18n' import { getLocale } from '../common/i18n'
import { play } from '../common/play' import { play } from '../common/play'
import OneEditLink from './OneEditLink' import OneEditLink from './OneEditLink'
import OneRepl from './OneRepl' import OneRepl from './OneRepl'
import 'veui-theme-dls-icons/copy'
Vue.use(toast)
export default { export default {
name: 'one-demo', name: 'one-demo',
@ -143,13 +157,21 @@ export default {
this.sourceHeight = source.offsetHeight this.sourceHeight = source.offsetHeight
style.height = '0' style.height = '0'
this.code = this.$refs.source?.textContent this.code = this.$refs.source?.querySelector('pre')?.textContent || ''
}, },
methods: { methods: {
play (vendor) { play (vendor) {
let locale = getLocale(this.$route.path) let locale = getLocale(this.$route.path)
play(this.$refs.source.textContent, { locale, vendor }) play(this.$refs.source.textContent, { locale, vendor })
}, },
async copy () {
try {
await navigator.clipboard.writeText(this.code)
this.$toast.success(this.t('@onelive.copySuccess'))
} catch (e) {
this.$toast.error(this.t('@onelive.copyFailed'))
}
},
handleEditorClose () { handleEditorClose () {
this.editing = false this.editing = false
}, },
@ -205,6 +227,7 @@ Icon.register({
background-color #fcfcfc background-color #fcfcfc
.source .source
position relative
overflow hidden overflow hidden
transition height 0.3s transition height 0.3s
@ -292,6 +315,23 @@ Icon.register({
border-color #0052cc !important border-color #0052cc !important
box-shadow 0 0 0 2px rgba(0, 102, 255, 0.2) !important box-shadow 0 0 0 2px rgba(0, 102, 255, 0.2) !important
.source-toolbar
position absolute
top 12px
right 28px
display flex
align-items center
.veui-button[ui~="icon"][ui~="reverse"]
color #ebedf5
&:hover
&[data-focus-visible-added]
color #f6f7fa
&:active
color #fff
@media (max-width 480px) @media (max-width 480px)
.toggle-editor .toggle-editor
display none display none