1
13693261870
2022-09-16 762f2fb45db004618ba099aa3c0bd89dba1eb843
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
 
const {waitTime} = require('./util');
 
module.exports = class Timeline {
 
    constructor(page) {
        this._page = page;
 
        this._timer = 0;
        this._current = 0;
 
        this._ops = [];
        this._currentOpIndex = 0;
 
        this._client;
 
        this._isLastOpMousewheel = false;
    }
 
    _reset() {
        this._currentOpIndex = 0;
        this._current = Date.now();
        this._elapsedTime = 0;
        this._isLastOpMousewheel = false;
    }
 
 
    async runAction(action, takeScreenshot, playbackSpeed) {
        if (!this._client) {
            this._client = await this._page.target().createCDPSession();
        }
 
        this.stop();
 
        playbackSpeed = playbackSpeed || 1;
 
        if (!action.ops.length) {
            return;
        }
 
        this._ops = action.ops.slice().sort((a, b) => {
            return a.time - b.time;
        });
        let firstOp = this._ops[0];
        this._ops.forEach(op => {
            op.time -= firstOp.time;
        });
 
        this._reset();
 
        let self = this;
 
        return new Promise(resolve => {
            async function tick() {
                let current = Date.now();
                let dTime = current - self._current;
                self._elapsedTime += dTime * playbackSpeed;
                self._current = current;
 
                await self._update(takeScreenshot, playbackSpeed);
                if (self._currentOpIndex >= self._ops.length) {
                    // Finished
                    resolve();
                }
                else {
                    self._timer = setTimeout(tick, 16);
                }
            }
            tick();
        });
    }
 
 
    stop() {
        if (this._timer) {
            clearTimeout(this._timer);
            this._timer = 0;
        }
    }
 
    async _update(takeScreenshot, playbackSpeed) {
        let op = this._ops[this._currentOpIndex];
 
        if (op.time > this._elapsedTime) {
            // Not yet.
            return;
        }
 
        let page = this._page;
        let takenScreenshot = false;
        switch (op.type) {
            case 'mousedown':
                await page.mouse.move(op.x, op.y);
                await page.mouse.down();
                break;
            case 'mouseup':
                await page.mouse.move(op.x, op.y);
                await page.mouse.up();
                break;
            case 'mousemove':
                await page.mouse.move(op.x, op.y);
                break;
            case 'mousewheel':
                await page.evaluate((x, y, deltaX, deltaY) => {
                    let element = document.elementFromPoint(x, y);
                    // Here dispatch mousewheel event because echarts used it.
                    // TODO Consider upgrade?
                    let event = new WheelEvent('mousewheel', {
                        // PENDING
                        // Needs inverse delta?
                        deltaY,
                        clientX: x, clientY: y,
                        // Needs bubble to parent container
                        bubbles: true
                    });
 
                    element.dispatchEvent(event);
                }, op.x, op.y, op.deltaX || 0, op.deltaY);
                this._isLastOpMousewheel = true;
                // console.log('mousewheel', op.x, op.y, op.deltaX, op.deltaY);
                // await this._client.send('Input.dispatchMouseEvent', {
                //     type: 'mouseWheel',
                //     x: op.x,
                //     y: op.y,
                //     deltaX: op.deltaX,
                //     deltaY: op.deltaY
                // });
                break;
            case 'screenshot':
                await takeScreenshot();
                takenScreenshot = true;
                break;
            case 'valuechange':
                if (op.target === 'select') {
                    await page.select(op.selector, op.value);
                }
                break;
        }
 
        this._currentOpIndex++;
 
        // If next op is an auto screenshot
        let nextOp = this._ops[this._currentOpIndex];
        if (nextOp && nextOp.type === 'screenshot-auto') {
            let delay = nextOp.delay == null ? 400 : nextOp.delay;
            // TODO Configuration time
            await waitTime(delay / playbackSpeed);
            await takeScreenshot();
            takenScreenshot = true;
            this._currentOpIndex++;
        }
 
        if (this._isLastOpMousewheel && op.type !== 'mousewheel') {
            // Only take screenshot after mousewheel finished
            if (!takenScreenshot) {
                takeScreenshot();
            }
            this._isLastOpMousewheel = false;
        }
    }
};