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

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

export const plugin = {
    install: function (Vue) {
B
baiy 已提交
49 50
        Vue.prototype.$getToolData = function (clipboardField = '') {
            let data = history(model.getCurrentTool()).current()
51 52 53 54 55
            if (clipboardField) {
                if (fixeInputData) { // 使用固定输入数据
                    data[clipboardField] = fixeInputData
                    fixeInputData = ""
                } else if (setting.autoReadCopy()) {
B
baiy 已提交
56
                    let paste = clipboard.paste()
57
                    if (!data[clipboardField] && paste) {
B
baiy 已提交
58
                        if (setting.autoReadCopyFilter()) {
59 60
                            paste = paste.trim()
                        }
61 62
                        data[clipboardField] = paste
                    }
B
baiy 已提交
63
                }
B
baiy 已提交
64
            }
B
baiy 已提交
65 66
            return data
        }
B
baiy 已提交
67
        Vue.prototype.$saveToolData = function (data) {
B
baiy 已提交
68 69
            return history(model.getCurrentTool()).push(data)
        }
B
baiy 已提交
70 71 72 73 74 75
        Vue.prototype.$clipboardCopy = function (data, force = false) {
            if ((setting.autoSaveCopy() || force) && data){
                clipboard.copy(data, () => {
                    this.$Message.success('结果已复制 ^o^')
                })
            }
B
baiy 已提交
76
        }
B
baiy 已提交
77 78 79 80 81 82
        Vue.prototype.$clipboardCopyImages = function (data, force = false) {
            if ((setting.autoSaveCopy() || force) && data){
                clipboard.copyImage(data, () => {
                    this.$Message.success('图片已复制 ^o^')
                })
            }
B
baiy 已提交
83 84 85
        }
    },
}
B
baiy 已提交
86

B
baiy 已提交
87
export default model