model.js 2.3 KB
Newer Older
B
baiy 已提交
1 2 3
import config from './config'
import cache from './cache'
import history from './history.js'
B
baiy 已提交
4 5

const model = {
B
baiy 已提交
6
    getCategoryHistory () {
B
baiy 已提交
7
        return cache.get('page_category_history', 'common')
B
baiy 已提交
8
    },
B
baiy 已提交
9
    setCategoryHistory (cat) {
B
baiy 已提交
10
        return cache.set('page_category_history', cat)
B
baiy 已提交
11
    },
B
baiy 已提交
12
    getToolHistory (cat) {
B
baiy 已提交
13
        let all = cache.get('category_tool_history', {})
B
baiy 已提交
14
        if (all[cat]) {
B
baiy 已提交
15
            return all[cat]
B
baiy 已提交
16
        }
B
baiy 已提交
17
        return config.getToolByCategory(cat)[0]['name']
B
baiy 已提交
18
    },
B
baiy 已提交
19
    setToolHistory (cat, name) {
B
baiy 已提交
20 21 22
        let all = cache.get('category_tool_history', {})
        all[cat] = name
        return cache.set('category_tool_history', all)
B
baiy 已提交
23
    },
B
baiy 已提交
24
    getCurrentTool () {
B
baiy 已提交
25
        return cache.get('current_tool', '')
B
baiy 已提交
26
    },
B
baiy 已提交
27
    setCurrentTool (name) {
B
baiy 已提交
28
        return cache.set('current_tool', name)
B
baiy 已提交
29
    }
B
baiy 已提交
30
}
B
baiy 已提交
31

B
baiy 已提交
32 33 34 35 36 37 38 39 40
const clipboardPaste = function () {
    document.querySelector(
        '#clipboard').innerHTML = '<textarea id="clipboard-text"></textarea>'
    document.querySelector('#clipboard-text').select()
    document.execCommand('paste')
    let r = document.querySelector('#clipboard-text').value ||
        document.querySelector('#clipboard-text').innerHTML
    document.querySelector('#clipboard').innerHTML = ''
    return r ? r : ''
B
baiy 已提交
41 42
}

B
baiy 已提交
43 44
export const plugin = {
    install: function (Vue) {
B
baiy 已提交
45 46 47 48 49
        Vue.prototype.$getToolData = function (clipboardField = '') {
            let data = history(model.getCurrentTool()).current()
            let paste = clipboardPaste()
            if (clipboardField && !data[clipboardField] && paste) {
                data[clipboardField] = paste
B
baiy 已提交
50
            }
B
baiy 已提交
51
            return data
B
baiy 已提交
52
        }
B
baiy 已提交
53 54
        Vue.prototype.$saveToolData = function (data) {
            return history(model.getCurrentTool()).push(data)
B
baiy 已提交
55
        }
B
baiy 已提交
56 57 58 59 60 61 62 63
        Vue.prototype.$clipboardCopy = function (data) {
            if (!data) return
            document.querySelector(
                '#clipboard').innerHTML = '<textarea id="clipboard-text"></textarea>'
            document.querySelector('#clipboard-text').value = data
            document.querySelector('#clipboard-text').select()
            if (document.execCommand('copy')) {
                this.$Message.success('结果已复制 ^o^')
B
baiy 已提交
64
            }
B
baiy 已提交
65
            document.querySelector('#clipboard').innerHTML = ''
B
baiy 已提交
66 67 68
        }
    },
}
B
baiy 已提交
69

B
baiy 已提交
70
export default model