model.js 4.5 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

const appendData = async function (field = "", check = "") {
    const result = (data = "") => {
        if (data){
            if (
                !check
                || (_.isFunction(check) && check(data)) // 函数校验
            ){
                return field ? {[field]: data} : data
            }
        }
        return field ? {} : ""
    }
    return new Promise(async (resolve) => {
        try {
            // 使用固定输入数据
            if (fixeInputData) {
                return resolve(result(fixeInputData))
            }
            if (setting.autoReadCopy()) {
                let paste = (await clipboard.paste()).trim()
                if (paste) {
                    resolve(result(paste))
                }
            }
            resolve(result())
        } catch {
            resolve(result())
        }
    });
}

B
baiy 已提交
86 87
export const plugin = {
    install: function (Vue) {
B
baiy 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        Vue.prototype.$initToolData = function (input = "", inputCheck = "", field = "current", isLoadHistory = true) {
            let current = _.cloneDeep(this[field])
            if (isLoadHistory) {
                Object.assign(current, this.$getToolData())
            }
            if (!input) {
                this[field] = current
                return;
            }

            // 初始化默认值
            if (!(input in current)){
                current[input] = "";
            }

            // 保存默认值
            let inputDefault = current[input]
            current[input] = "";

            appendData(input, inputCheck).then((append) => {
                for (let key of Object.keys(append)) {
                    if ((key in current) && !current[key]) {
                        current[key] = append[key]
B
baiy 已提交
111 112
                    }
                }
B
baiy 已提交
113 114 115
                if (!current[input]){
                    // 使用默认值
                    current[input] = inputDefault
B
baiy 已提交
116
                }
B
baiy 已提交
117 118 119 120 121
                this[field] = current
            })
        }
        Vue.prototype.$getToolData = function () {
            return _.cloneDeep(history(model.getCurrentTool()).current())
B
baiy 已提交
122
        }
B
baiy 已提交
123 124 125 126 127
        Vue.prototype.$saveToolData = function (data, ignoreDebounce = false) {
            if (ignoreDebounce) {
                return history(model.getCurrentTool()).push(_.cloneDeep(data))
            }
            debounceSaveToolData = {tool: model.getCurrentTool(), data: _.cloneDeep(data)}
128
            debounceSaveToolDataMethod()
B
baiy 已提交
129
        }
B
baiy 已提交
130
        Vue.prototype.$clipboardCopy = function (data, force = false) {
131
            if ((setting.autoSaveCopy() || force) && data) {
B
baiy 已提交
132
                clipboard.copy(data, () => {
B
baiy 已提交
133
                    this.$Message.success(this.$t('main_ui_copy_text_ok').toString())
B
baiy 已提交
134 135
                })
            }
B
baiy 已提交
136
        }
B
baiy 已提交
137
        Vue.prototype.$clipboardCopyImages = function (data, force = false) {
138
            if ((setting.autoSaveCopy() || force) && data) {
B
baiy 已提交
139
                clipboard.copyImage(data, () => {
B
baiy 已提交
140
                    this.$Message.success(this.$t('main_ui_copy_image_ok').toString())
B
baiy 已提交
141 142
                })
            }
B
baiy 已提交
143 144 145
        }
    },
}
B
baiy 已提交
146

B
baiy 已提交
147
export default model