model.js 3.7 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
        Vue.prototype.$getToolData = function (clipboardField = '',clipboardFieldRegex = "") {
B
baiy 已提交
57
            let data = history(model.getCurrentTool()).current()
58
            if (clipboardField) {
B
baiy 已提交
59
                let inputData = "";
60
                if (fixeInputData) { // 使用固定输入数据
B
baiy 已提交
61
                    inputData = fixeInputData
62 63
                    fixeInputData = ""
                } else if (setting.autoReadCopy()) {
B
baiy 已提交
64
                    let paste = clipboard.paste()
65
                    if (!data[clipboardField] && paste) {
B
baiy 已提交
66
                        if (setting.autoReadCopyFilter()) {
67 68
                            paste = paste.trim()
                        }
B
baiy 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
                        inputData = paste
                    }
                }
                if (inputData){
                    if (
                        !(clipboardFieldRegex instanceof RegExp)
                        ||
                        (
                            clipboardFieldRegex instanceof RegExp
                            && clipboardFieldRegex.test(inputData)
                        )
                    ){
                        data[clipboardField] = inputData
82
                    }
B
baiy 已提交
83
                }
B
baiy 已提交
84
            }
B
baiy 已提交
85 86
            return data
        }
B
baiy 已提交
87 88 89 90 91
        Vue.prototype.$saveToolData = function (data, ignoreDebounce = false) {
            if (ignoreDebounce) {
                return history(model.getCurrentTool()).push(_.cloneDeep(data))
            }
            debounceSaveToolData = {tool: model.getCurrentTool(), data: _.cloneDeep(data)}
92
            debounceSaveToolDataMethod()
B
baiy 已提交
93
        }
B
baiy 已提交
94
        Vue.prototype.$clipboardCopy = function (data, force = false) {
95
            if ((setting.autoSaveCopy() || force) && data) {
B
baiy 已提交
96
                clipboard.copy(data, () => {
B
baiy 已提交
97
                    this.$Message.success(this.$t('main_ui_copy_text_ok').toString())
B
baiy 已提交
98 99
                })
            }
B
baiy 已提交
100
        }
B
baiy 已提交
101
        Vue.prototype.$clipboardCopyImages = function (data, force = false) {
102
            if ((setting.autoSaveCopy() || force) && data) {
B
baiy 已提交
103
                clipboard.copyImage(data, () => {
B
baiy 已提交
104
                    this.$Message.success(this.$t('main_ui_copy_image_ok').toString())
B
baiy 已提交
105 106
                })
            }
B
baiy 已提交
107 108 109
        }
    },
}
B
baiy 已提交
110

B
baiy 已提交
111
export default model