model.js 3.1 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'
6
import _ from "lodash";
B
baiy 已提交
7

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

48 49 50
// 保存历史记录防抖
let debounceSaveToolData = {};
const debounceSaveToolDataMethod = _.debounce(function () {
B
baiy 已提交
51 52
    return history(debounceSaveToolData['tool']).push(debounceSaveToolData['data'])
}, 1000)
53

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

B
baiy 已提交
95
export default model