wzp
2021-09-01 2891fe0769189be39c9634b2cbc1841dbd52d022
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
    上传进度条
*/
 
import $ from '../../util/dom-core.js'
 
function Progress(editor) {
    this.editor = editor
    this._time = 0
    this._isShow = false
    this._isRender = false
    this._timeoutId = 0
    this.$textContainer = editor.$textContainerElem
    this.$bar = $('<div class="w-e-progress"></div>')
}
 
Progress.prototype = {
    constructor: Progress,
 
    show: function (progress) {
        // 状态处理
        if (this._isShow) {
            return
        }
        this._isShow = true
 
        // 渲染
        const $bar = this.$bar
        if (!this._isRender) {
            const $textContainer = this.$textContainer
            $textContainer.append($bar)
        } else {
            this._isRender = true
        }
 
        // 改变进度(节流,100ms 渲染一次)
        if (Date.now() - this._time > 100) {
            if (progress <= 1) {
                $bar.css('width', progress * 100 + '%')
                this._time = Date.now()
            }
        }
 
        // 隐藏
        let timeoutId = this._timeoutId
        if (timeoutId) {
            clearTimeout(timeoutId)
        }
        timeoutId = setTimeout(() => {
            this._hide()
        }, 500)
    },
 
    _hide: function () {
        const $bar = this.$bar
        $bar.remove()
 
        // 修改状态
        this._time = 0
        this._isShow = false
        this._isRender = false
    }
}
 
export default Progress