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

6 7
let fixeInputData;
let toolCurrentFeature = "";
B
baiy 已提交
8
const model = {
9
    getCategoryHistory() {
B
baiy 已提交
10
        return cache.get('page_category_history', 'common')
B
baiy 已提交
11
    },
12
    setCategoryHistory(cat) {
B
baiy 已提交
13
        return cache.set('page_category_history', cat)
B
baiy 已提交
14
    },
15
    getToolHistory(cat) {
B
baiy 已提交
16
        let all = cache.get('category_tool_history', {})
B
baiy 已提交
17
        if (all[cat]) {
B
baiy 已提交
18
            return all[cat]
B
baiy 已提交
19
        }
B
baiy 已提交
20
        return config.getToolByCategory(cat)[0]['name']
B
baiy 已提交
21
    },
22
    setToolHistory(cat, name) {
B
baiy 已提交
23 24 25
        let all = cache.get('category_tool_history', {})
        all[cat] = name
        return cache.set('category_tool_history', all)
B
baiy 已提交
26
    },
27
    getCurrentTool() {
B
baiy 已提交
28
        return cache.get('current_tool', '')
B
baiy 已提交
29
    },
30
    setCurrentTool(name) {
B
baiy 已提交
31
        return cache.set('current_tool', name)
32 33 34 35 36 37 38 39 40 41 42
    },
    setFixeInputData: (value) => {
        fixeInputData = value;
    },
    setToolCurrentFeature: (value) => {
        toolCurrentFeature = value;
    },
    getToolCurrentFeature: (def = "") => {
        let temp = toolCurrentFeature
        toolCurrentFeature = "";
        return temp ? temp : def
B
baiy 已提交
43
    }
B
baiy 已提交
44
}
B
baiy 已提交
45

B
baiy 已提交
46 47 48 49 50 51 52 53 54
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 已提交
55 56
}

B
baiy 已提交
57 58
export const plugin = {
    install: function (Vue) {
B
baiy 已提交
59 60
        Vue.prototype.$getToolData = function (clipboardField = '') {
            let data = history(model.getCurrentTool()).current()
61 62 63 64 65 66 67 68 69
            if (clipboardField) {
                if (fixeInputData) { // 使用固定输入数据
                    data[clipboardField] = fixeInputData
                    fixeInputData = ""
                } else if (setting.autoReadCopy()) {
                    let paste = clipboardPaste()
                    if (!data[clipboardField] && paste) {
                        data[clipboardField] = paste
                    }
B
baiy 已提交
70
                }
B
baiy 已提交
71
            }
B
baiy 已提交
72
            return data
B
baiy 已提交
73
        }
B
baiy 已提交
74 75
        Vue.prototype.$saveToolData = function (data) {
            return history(model.getCurrentTool()).push(data)
B
baiy 已提交
76
        }
B
baiy 已提交
77
        Vue.prototype.$clipboardCopy = function (data) {
B
baiy 已提交
78
            if (!setting.autoSaveCopy() || !data) return
B
baiy 已提交
79 80 81 82 83 84
            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 已提交
85
            }
B
baiy 已提交
86
            document.querySelector('#clipboard').innerHTML = ''
B
baiy 已提交
87 88 89
        }
    },
}
B
baiy 已提交
90

B
baiy 已提交
91
export default model