This commit is contained in:
花裤衩 2019-09-29 17:35:27 +08:00
parent 4d2d52ef03
commit e76088d4d3
3 changed files with 46 additions and 23 deletions

View File

@ -1,32 +1,47 @@
import Vue from 'vue' import Vue from 'vue'
import Clipboard from 'clipboard' import Clipboard from 'clipboard'
function clipboardSuccess() { const VueClipboardConfig = {
autoSetContainer: false,
appendToBody: true // This fixes IE, see #50
}
function clipboardSuccess(successText) {
Vue.prototype.$message({ Vue.prototype.$message({
message: 'Copy successfully', message: successText || 'Copy successfully',
type: 'success', type: 'success',
duration: 1500 duration: 1500
}) })
} }
function clipboardError() { function clipboardError(errorText) {
Vue.prototype.$message({ Vue.prototype.$message({
message: 'Copy failed', message: errorText || 'Copy failed',
type: 'error' type: 'error'
}) })
} }
export default function handleClipboard(text, event) { export default function handleClipboard({ text, container, successText, errorText } = {}) {
const clipboard = new Clipboard(event.target, { return new Promise(function(resolve, reject) {
text: () => text var fakeElement = document.createElement('button')
var clipboard = new Clipboard(fakeElement, {
text: function() { return text },
action: function() { return 'copy' },
container: typeof container === 'object' ? container : document.body
})
clipboard.on('success', function(e) {
clipboard.destroy()
clipboardSuccess(successText)
resolve(e)
})
clipboard.on('error', function(e) {
clipboard.destroy()
clipboardError(errorText)
reject(e)
})
if (VueClipboardConfig.appendToBody) document.body.appendChild(fakeElement)
fakeElement.click()
if (VueClipboardConfig.appendToBody) document.body.removeChild(fakeElement)
}) })
clipboard.on('success', () => {
clipboardSuccess()
clipboard.destroy()
})
clipboard.on('error', () => {
clipboardError()
clipboard.destroy()
})
clipboard.onClick(event)
} }

View File

@ -1,9 +1,14 @@
<template> <template>
<div class="app-container"> <div class="app-container">
<aside>
<a target="_blank" href="https://panjiachen.github.io/vue-element-admin-site/feature/component/clipboard.html">Documentation</a>
</aside>
<el-tabs v-model="activeName"> <el-tabs v-model="activeName">
<el-tab-pane label="use clipboard directly" name="directly"> <el-tab-pane label="use clipboard directly" name="directly">
<el-input v-model="inputData" placeholder="Please input" style="width:400px;max-width:100%;" /> <el-input v-model="inputData" placeholder="Please input" style="width:400px;max-width:100%;" />
<el-button type="primary" icon="el-icon-document" @click="handleCopy(inputData,$event)"> <el-button type="primary" icon="el-icon-document" @click="handleCopy(inputData)">
copy copy
</el-button> </el-button>
</el-tab-pane> </el-tab-pane>
@ -33,8 +38,11 @@ export default {
} }
}, },
methods: { methods: {
handleCopy(text, event) { handleCopy(text) {
clip(text, event) // return a promise
clip({ text: text }).then(() => {
console.log('success')
})
}, },
clipboardSuccess() { clipboardSuccess() {
this.$message({ this.$message({

View File

@ -6,7 +6,7 @@
</aside> </aside>
<el-tabs type="border-card"> <el-tabs type="border-card">
<el-tab-pane label="Icons"> <el-tab-pane label="Icons">
<div v-for="item of svgIcons" :key="item" @click="handleClipboard(generateIconCode(item),$event)"> <div v-for="item of svgIcons" :key="item" @click="handleClipboard(generateIconCode(item))">
<el-tooltip placement="top"> <el-tooltip placement="top">
<div slot="content"> <div slot="content">
{{ generateIconCode(item) }} {{ generateIconCode(item) }}
@ -19,7 +19,7 @@
</div> </div>
</el-tab-pane> </el-tab-pane>
<el-tab-pane label="Element-UI Icons"> <el-tab-pane label="Element-UI Icons">
<div v-for="item of elementIcons" :key="item" @click="handleClipboard(generateElementIconCode(item),$event)"> <div v-for="item of elementIcons" :key="item" @click="handleClipboard(generateElementIconCode(item))">
<el-tooltip placement="top"> <el-tooltip placement="top">
<div slot="content"> <div slot="content">
{{ generateElementIconCode(item) }} {{ generateElementIconCode(item) }}
@ -55,8 +55,8 @@ export default {
generateElementIconCode(symbol) { generateElementIconCode(symbol) {
return `<i class="el-icon-${symbol}" />` return `<i class="el-icon-${symbol}" />`
}, },
handleClipboard(text, event) { handleClipboard(text) {
clipboard(text, event) clipboard({ text: text })
} }
} }
} }