model.js 3.1 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 46

const clipboardPaste = function () {
B
baiy 已提交
47 48 49 50 51 52 53 54 55
    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 已提交
56 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
            if (clipboardField) {
                if (fixeInputData) { // 使用固定输入数据
                    data[clipboardField] = fixeInputData
                    fixeInputData = ""
                } else if (setting.autoReadCopy()) {
                    let paste = clipboardPaste()
                    if (!data[clipboardField] && paste) {
68 69 70
                        if (setting.autoReadCopyFilter()){
                            paste = paste.trim()
                        }
71 72
                        data[clipboardField] = paste
                    }
B
baiy 已提交
73
                }
B
baiy 已提交
74
            }
B
baiy 已提交
75 76
            return data
        }
B
baiy 已提交
77
        Vue.prototype.$saveToolData = function (data) {
B
baiy 已提交
78 79
            return history(model.getCurrentTool()).push(data)
        }
B
baiy 已提交
80
        Vue.prototype.$clipboardCopy = function (data) {
B
baiy 已提交
81
            if (!setting.autoSaveCopy() || !data) return
B
baiy 已提交
82 83 84 85
            document.querySelector(
                '#clipboard').innerHTML = '<textarea id="clipboard-text"></textarea>'
            document.querySelector('#clipboard-text').value = data
            document.querySelector('#clipboard-text').select()
B
baiy 已提交
86
            if (document.execCommand('copy')) {
B
baiy 已提交
87
                this.$Message.success('结果已复制 ^o^')
B
baiy 已提交
88
            }
B
baiy 已提交
89 90 91 92
            document.querySelector('#clipboard').innerHTML = ''
        }
    },
}
B
baiy 已提交
93 94

export default model