@soffits/dsh-media-hub 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +2 -1
- package/dist/danmaku.mjs +561 -0
- package/dist/index.js +304 -78
- package/locale/en.json +28 -3
- package/locale/fr.json +28 -3
- package/locale/zh.json +28 -3
- package/package.json +2 -1
- package/web/comments-view.js +55 -0
- package/web/danmaku-layer.js +83 -0
- package/web/feed-view.js +32 -24
- package/web/page.js +3 -1
- package/web/panel.css +32 -15
- package/web/player-bar.js +57 -0
- package/web/player.js +2 -0
- package/web/presence-view.js +6 -33
- package/web/result-card.js +1 -3
- package/web/room-view.js +105 -38
- package/web/ui.js +31 -9
- package/web/watching-view.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.14.0
|
|
4
|
+
|
|
5
|
+
- Danmaku in rooms: the episode's danmaku fly over the picture in time with it, for sources that have them (Fanqie's short dramas). A *Danmaku* switch sits under the player, and its settings button sets their opacity, how much of the picture they cover and their size, kept per browser. The new full-screen button keeps them on the picture (on iPhone the player covers the page).
|
|
6
|
+
- A room's *Comments* tab shows what people wrote about the work on the site, with more pages on request.
|
|
7
|
+
- Quality in rooms: when the source has a show in several sizes, the player's settings pick the one the hub fetches, for everyone in the room, from the next episode. Fanqie dramas list theirs (only the ones that play: many have just 1080p).
|
|
8
|
+
- Source protocol: `info` may list `qualities` and `download` then takes `--quality`; two optional verbs, `danmaku` and `comments`, declared per kind with `"danmaku": true` and `"comments": true`.
|
|
9
|
+
|
|
10
|
+
## 0.13.0
|
|
11
|
+
|
|
12
|
+
- The recommender's picks moved from Discover to the Watching tab, under what you are watching, as *For you* (still switched by `presence.picks`). Each is the card of its kind (watch it, or download a book) under the reason it was picked. Discover keeps each source's trending lists and search.
|
|
13
|
+
- Fanqie's audiobooks no longer declare trending: the app's listening tab holds no audiobook works to list.
|
|
14
|
+
- The recommender protocol changed: `recommend` no longer takes `--source` and `--kind`; the hub passes the trending lists it already holds as candidates on stdin (`{ "candidates": [...] }`), so one run a day serves every category and the sources are asked nothing twice. Picks are kept as one dated copy per person and language and made in the background.
|
|
15
|
+
|
|
3
16
|
## 0.12.0
|
|
4
17
|
|
|
5
18
|
- A show and a book look the same in the Library. Every item is one card: the same cover slot, a title with its type, one line of facts (a show's rating, status and progress; a book's author, format and size), tags, and the actions in the same place, with Delete under the … menu for books as it is for shows.
|
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ This plugin was called Video Sync (`@soffits/dsh-video-sync`) until 0.2.0; the r
|
|
|
14
14
|
- **Sources**: one search box over *All sources* or one of them; each source is a configured command line tool that speaks a small JSON protocol ([docs/source-protocol.md](docs/source-protocol.md), the hub's public interface for extensions, see [docs/extensions.md](docs/extensions.md)) and brings its kinds. The defaults are `fanqie` (short dramas, audiobooks, novels) and `bookfetch source` (ebooks and papers); every request runs through the source's command, so any proxy its launcher enforces holds, and a wrapper for another site is one more entry. *Watch* opens it at once, for you alone (*Watch together* in the player opens that room to others where it stands and copies its link); *Save to library* adds the show; *Download all (offline)* keeps every episode on disk. Sources implement one small interface (`src/sources/index.js`).
|
|
15
15
|
- **Search results as cards**: cover, title, score, how many watched it ("36.7万次播放" or "36.7K plays", by the page language), episodes, whether it is finished, tags; *Details* shows everything the source gives (cast, heat, last update…) and the other seasons and linked works.
|
|
16
16
|
- **Pictures for everything**: a work's cover, a show's poster and a room's preview all come from the host and are cached under `<stateDir>/covers`: a source's cover through the source's own command (so its proxy keeps applying), a link's thumbnail through the `proxies` setting, and a still of the video for local files and shows without a cover. A browser never contacts a site's CDN, and a placeholder shows only when there is no picture at all.
|
|
17
|
+
- **Danmaku, comments and quality in the room**: for a source that has them (Fanqie's short dramas), the episode's danmaku fly over the picture in time with it, switched on or off under the player, with their opacity, how much of the picture they cover and their size in the settings next to it (each viewer's own choice). The *Comments* tab reads what people wrote about the work on the site. When the source has the show in several sizes, the same settings pick the one the hub fetches, for everyone in the room, from the next episode. The full-screen button keeps the danmaku on the picture.
|
|
17
18
|
- **Details kept with a show**: a show from a source keeps what the source said about its work (cover, score, popularity, tags, blurb, other fields, related works, episode titles), so the library, a room's *About* tab and the episode list show it at once; a missing or day-old copy is refreshed in the background, and a copy over a week old says when it was saved.
|
|
18
19
|
- **Follow**: a followed work is checked about once a day through its source (and when someone opens it). A new season is added to the library as an empty show, grouped with the earlier ones and followed too, and shows under *New* in Watching and in the watch log; more episodes extend the show. With `notifyCommand` set, that command gets one line of JSON on its standard input for each piece of news.
|
|
19
20
|
- **Rolling cache**: a show from a source lists every episode, but only a window is on disk: the episode people are on is fetched first, then the next few (`cacheAhead`, 5), and `cacheBehind` (1) watched ones stay. Watched episodes that fall out of the window are deleted (the watch log still marks them watched). Jumping to an episode that is not cached fetches it first (the room shows it as preparing), then refills the window around it. A show kept offline (*Download all (offline)*, or the library menu) is never trimmed. All cached episodes of other shows together stay under `cacheLimitGb` (5 GB): past it the least recently watched go first, never one near an open room. The library says *cached N of M* (and *offline*), and the episode list marks the ones not on disk.
|
|
@@ -30,7 +31,7 @@ The bundle adds two rows: `media-hub` (the page, shelves, rooms, media) and `med
|
|
|
30
31
|
|
|
31
32
|
## Use
|
|
32
33
|
|
|
33
|
-
- **From the page**: open **Media** in the sidebar (the `label` setting renames it). Three tabs: **Watching** (the default) lists the rooms opened to others, with who is in them and the episode, with copying a room's link or closing it, and the shows someone started, to continue; **Library** manages the shows and starts or continues one; **Discover** searches: pick a source (*All sources*, every configured one, or *Link* to paste the address of a video or playlist), then one of that source's categories, then search. Before any search it shows what the sources put forward for that category (their rank lists and new arrivals, from a source's `trending` verb)
|
|
34
|
+
- **From the page**: open **Media** in the sidebar (the `label` setting renames it). Three tabs: **Watching** (the default) lists the rooms opened to others, with who is in them and the episode, with copying a room's link or closing it, and the shows someone started, to continue; **Library** manages the shows and starts or continues one; **Discover** searches: pick a source (*All sources*, every configured one, or *Link* to paste the address of a video or playlist), then one of that source's categories, then search. Before any search it shows what the sources put forward for that category (their rank lists and new arrivals, from a source's `trending` verb) (Link has none). They are kept as dated copies and refreshed in the background about daily, so the page never waits on a site; *All sources* lists each source's boards under its name. Picks from a `recommender` show on the Watching tab. The tab, source and category are in the page's URL (`?vs-tab=library`, `?vs-tab=discover&vs-source=<id>&vs-kind=<id>`), so a refresh and the back button keep them and a link can open a tab directly; the last source and category are remembered per browser.
|
|
34
35
|
- **Watching**: a card at the top for the show someone is in now, or the one you watched last, over its blurred cover: who is in the room, where it stands, a strip of its episodes (watched, now, on disk), and joining them, or continuing alone, or *Watch together* to open it to others. Optional parts, each off until switched on in `presence`: *So far*, a recap without spoilers kept with the show; *How was it?*, one tap that writes a rating into the watch log for a show finished or removed lately; and *Picks*, works a recommender extension suggests, each with its reason. Steps suggested for a show (written through the tool's `annotate`) show on the card when [@soffits/dsh-ui-actions](https://github.com/soffits/dsh-ui-actions) is installed.
|
|
35
36
|
- **From the assistant**: `videosync` with `action: start` and `source: local` plus `path`, `source: link` plus `url` (and `quality`), or `source: <source id>` plus `id` (and `offline: true` to keep every episode), returns a link to send. `list` and `close` manage rooms, `library` lists the shows, `history` summarises the watch log (shows, episodes finished, where each viewer stopped, with whom, when).
|
|
36
37
|
|
package/dist/danmaku.mjs
ADDED
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
var transform = (function() {
|
|
2
|
+
/* istanbul ignore next */
|
|
3
|
+
if (typeof document === 'undefined') return 'transform';
|
|
4
|
+
var properties = [
|
|
5
|
+
'oTransform', // Opera 11.5
|
|
6
|
+
'msTransform', // IE 9
|
|
7
|
+
'mozTransform',
|
|
8
|
+
'webkitTransform',
|
|
9
|
+
'transform'
|
|
10
|
+
];
|
|
11
|
+
var style = document.createElement('div').style;
|
|
12
|
+
for (var i = 0; i < properties.length; i++) {
|
|
13
|
+
/* istanbul ignore else */
|
|
14
|
+
if (properties[i] in style) {
|
|
15
|
+
return properties[i];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/* istanbul ignore next */
|
|
19
|
+
return 'transform';
|
|
20
|
+
}());
|
|
21
|
+
|
|
22
|
+
function createCommentNode(cmt) {
|
|
23
|
+
var node = document.createElement('div');
|
|
24
|
+
node.style.cssText = 'position:absolute;';
|
|
25
|
+
if (typeof cmt.render === 'function') {
|
|
26
|
+
var $el = cmt.render();
|
|
27
|
+
if ($el instanceof HTMLElement) {
|
|
28
|
+
node.appendChild($el);
|
|
29
|
+
return node;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
node.textContent = cmt.text;
|
|
33
|
+
if (cmt.style) {
|
|
34
|
+
for (var key in cmt.style) {
|
|
35
|
+
node.style[key] = cmt.style[key];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return node;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function init$1() {
|
|
42
|
+
var stage = document.createElement('div');
|
|
43
|
+
stage.style.cssText = 'overflow:hidden;white-space:nowrap;transform:translateZ(0);';
|
|
44
|
+
return stage;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function clear$1(stage) {
|
|
48
|
+
var lc = stage.lastChild;
|
|
49
|
+
while (lc) {
|
|
50
|
+
stage.removeChild(lc);
|
|
51
|
+
lc = stage.lastChild;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function resize$1(stage, width, height) {
|
|
56
|
+
stage.style.width = width + 'px';
|
|
57
|
+
stage.style.height = height + 'px';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function framing() {
|
|
61
|
+
//
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function setup(stage, comments) {
|
|
65
|
+
var df = document.createDocumentFragment();
|
|
66
|
+
var i = 0;
|
|
67
|
+
var cmt = null;
|
|
68
|
+
for (i = 0; i < comments.length; i++) {
|
|
69
|
+
cmt = comments[i];
|
|
70
|
+
cmt.node = cmt.node || createCommentNode(cmt);
|
|
71
|
+
df.appendChild(cmt.node);
|
|
72
|
+
}
|
|
73
|
+
if (comments.length) {
|
|
74
|
+
stage.appendChild(df);
|
|
75
|
+
}
|
|
76
|
+
for (i = 0; i < comments.length; i++) {
|
|
77
|
+
cmt = comments[i];
|
|
78
|
+
cmt.width = cmt.width || cmt.node.offsetWidth;
|
|
79
|
+
cmt.height = cmt.height || cmt.node.offsetHeight;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function render(stage, cmt) {
|
|
84
|
+
cmt.node.style[transform] = 'translate(' + cmt.x + 'px,' + cmt.y + 'px)';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* eslint no-invalid-this: 0 */
|
|
88
|
+
function remove(stage, cmt) {
|
|
89
|
+
stage.removeChild(cmt.node);
|
|
90
|
+
/* istanbul ignore else */
|
|
91
|
+
if (!this.media) {
|
|
92
|
+
cmt.node = null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
var domEngine = {
|
|
97
|
+
name: 'dom',
|
|
98
|
+
init: init$1,
|
|
99
|
+
clear: clear$1,
|
|
100
|
+
resize: resize$1,
|
|
101
|
+
framing: framing,
|
|
102
|
+
setup: setup,
|
|
103
|
+
render: render,
|
|
104
|
+
remove: remove,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
var raf = (function() {
|
|
108
|
+
if (typeof window !== 'undefined') {
|
|
109
|
+
var rAF = (
|
|
110
|
+
window.requestAnimationFrame ||
|
|
111
|
+
window.mozRequestAnimationFrame ||
|
|
112
|
+
window.webkitRequestAnimationFrame
|
|
113
|
+
);
|
|
114
|
+
if (rAF) return rAF.bind(window);
|
|
115
|
+
}
|
|
116
|
+
return function(cb) {
|
|
117
|
+
return setTimeout(cb, 50 / 3);
|
|
118
|
+
};
|
|
119
|
+
})();
|
|
120
|
+
|
|
121
|
+
var caf = (function() {
|
|
122
|
+
if (typeof window !== 'undefined') {
|
|
123
|
+
var cAF = (
|
|
124
|
+
window.cancelAnimationFrame ||
|
|
125
|
+
window.mozCancelAnimationFrame ||
|
|
126
|
+
window.webkitCancelAnimationFrame
|
|
127
|
+
);
|
|
128
|
+
if (cAF) return cAF.bind(window);
|
|
129
|
+
}
|
|
130
|
+
return clearTimeout;
|
|
131
|
+
})();
|
|
132
|
+
|
|
133
|
+
function binsearch(arr, prop, key) {
|
|
134
|
+
var mid = 0;
|
|
135
|
+
var left = 0;
|
|
136
|
+
var right = arr.length;
|
|
137
|
+
while (left < right - 1) {
|
|
138
|
+
mid = (left + right) >> 1;
|
|
139
|
+
if (key >= arr[mid][prop]) {
|
|
140
|
+
left = mid;
|
|
141
|
+
} else {
|
|
142
|
+
right = mid;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (arr[left] && key < arr[left][prop]) {
|
|
146
|
+
return left;
|
|
147
|
+
}
|
|
148
|
+
return right;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
function formatMode(mode) {
|
|
153
|
+
if (!/^(ltr|top|bottom)$/i.test(mode)) {
|
|
154
|
+
return 'rtl';
|
|
155
|
+
}
|
|
156
|
+
return mode.toLowerCase();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function collidableRange() {
|
|
160
|
+
var max = 9007199254740991;
|
|
161
|
+
return [
|
|
162
|
+
{ range: 0, time: -max, width: max, height: 0 },
|
|
163
|
+
{ range: max, time: max, width: 0, height: 0 }
|
|
164
|
+
];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function resetSpace(space) {
|
|
168
|
+
space.ltr = collidableRange();
|
|
169
|
+
space.rtl = collidableRange();
|
|
170
|
+
space.top = collidableRange();
|
|
171
|
+
space.bottom = collidableRange();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function now() {
|
|
175
|
+
return typeof window.performance !== 'undefined' && window.performance.now
|
|
176
|
+
? window.performance.now()
|
|
177
|
+
: Date.now();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* eslint no-invalid-this: 0 */
|
|
181
|
+
function allocate(cmt) {
|
|
182
|
+
var that = this;
|
|
183
|
+
var ct = this.media ? this.media.currentTime : now() / 1000;
|
|
184
|
+
function willCollide(cr, cmt) {
|
|
185
|
+
if (cmt.mode === 'top' || cmt.mode === 'bottom') {
|
|
186
|
+
return ct - cr.time < that._.duration;
|
|
187
|
+
}
|
|
188
|
+
// `ct`, `cr.time` and `that._.duration` all live on the same timeline
|
|
189
|
+
// (media time when bound to a media element, wall-clock time otherwise),
|
|
190
|
+
// so `ct - cr.time` is already the elapsed travelling time on it.
|
|
191
|
+
// Scaling it by `playbackRate` would count the rate twice.
|
|
192
|
+
var crTotalWidth = that._.width + cr.width;
|
|
193
|
+
var crElapsed = crTotalWidth * (ct - cr.time) / that._.duration;
|
|
194
|
+
if (cr.width > crElapsed) {
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
// (rtl mode) the right end of `cr` move out of left side of stage
|
|
198
|
+
var crLeftTime = that._.duration + cr.time - ct;
|
|
199
|
+
var cmtTotalWidth = that._.width + cmt.width;
|
|
200
|
+
var cmtTime = that.media ? cmt.time : cmt._utc;
|
|
201
|
+
var cmtElapsed = cmtTotalWidth * (ct - cmtTime) / that._.duration;
|
|
202
|
+
var cmtArrival = that._.width - cmtElapsed;
|
|
203
|
+
// (rtl mode) the left end of `cmt` reach the left side of stage
|
|
204
|
+
var cmtArrivalTime = that._.duration * cmtArrival / (that._.width + cmt.width);
|
|
205
|
+
return crLeftTime > cmtArrivalTime;
|
|
206
|
+
}
|
|
207
|
+
var crs = this._.space[cmt.mode];
|
|
208
|
+
var last = 0;
|
|
209
|
+
var curr = 0;
|
|
210
|
+
for (var i = 1; i < crs.length; i++) {
|
|
211
|
+
var cr = crs[i];
|
|
212
|
+
var requiredRange = cmt.height;
|
|
213
|
+
if (cmt.mode === 'top' || cmt.mode === 'bottom') {
|
|
214
|
+
requiredRange += cr.height;
|
|
215
|
+
}
|
|
216
|
+
if (cr.range - cr.height - crs[last].range >= requiredRange) {
|
|
217
|
+
curr = i;
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
if (willCollide(cr, cmt)) {
|
|
221
|
+
last = i;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
var channel = crs[last].range;
|
|
225
|
+
var crObj = {
|
|
226
|
+
range: channel + cmt.height,
|
|
227
|
+
time: this.media ? cmt.time : cmt._utc,
|
|
228
|
+
width: cmt.width,
|
|
229
|
+
height: cmt.height
|
|
230
|
+
};
|
|
231
|
+
crs.splice(last + 1, curr - last - 1, crObj);
|
|
232
|
+
|
|
233
|
+
if (cmt.mode === 'bottom') {
|
|
234
|
+
return this._.height - cmt.height - channel % this._.height;
|
|
235
|
+
}
|
|
236
|
+
return channel % (this._.height - cmt.height);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/* eslint no-invalid-this: 0 */
|
|
240
|
+
function createEngine(framing, setup, render, remove) {
|
|
241
|
+
return function(_timestamp) {
|
|
242
|
+
framing(this._.stage);
|
|
243
|
+
var timestamp = _timestamp || now();
|
|
244
|
+
var dn = timestamp / 1000;
|
|
245
|
+
var ct = this.media ? this.media.currentTime : dn;
|
|
246
|
+
var pbr = this.media ? this.media.playbackRate : 1;
|
|
247
|
+
var cmt = null;
|
|
248
|
+
var cmtt = 0;
|
|
249
|
+
var i = 0;
|
|
250
|
+
for (i = this._.runningList.length - 1; i >= 0; i--) {
|
|
251
|
+
cmt = this._.runningList[i];
|
|
252
|
+
cmtt = this.media ? cmt.time : cmt._utc;
|
|
253
|
+
if (ct - cmtt > this._.duration) {
|
|
254
|
+
remove(this._.stage, cmt);
|
|
255
|
+
this._.runningList.splice(i, 1);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
var pendingList = [];
|
|
259
|
+
while (this._.position < this.comments.length) {
|
|
260
|
+
cmt = this.comments[this._.position];
|
|
261
|
+
cmtt = this.media ? cmt.time : cmt._utc;
|
|
262
|
+
if (cmtt >= ct) {
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
// when clicking controls to seek, media.currentTime may changed before
|
|
266
|
+
// `pause` event is fired, so here skips comments out of duration,
|
|
267
|
+
// see https://github.com/weizhenye/Danmaku/pull/30 for details.
|
|
268
|
+
if (ct - cmtt > this._.duration) {
|
|
269
|
+
++this._.position;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (this.media) {
|
|
273
|
+
cmt._utc = dn - (this.media.currentTime - cmt.time);
|
|
274
|
+
}
|
|
275
|
+
pendingList.push(cmt);
|
|
276
|
+
++this._.position;
|
|
277
|
+
}
|
|
278
|
+
setup(this._.stage, pendingList);
|
|
279
|
+
for (i = 0; i < pendingList.length; i++) {
|
|
280
|
+
cmt = pendingList[i];
|
|
281
|
+
cmt.y = allocate.call(this, cmt);
|
|
282
|
+
this._.runningList.push(cmt);
|
|
283
|
+
}
|
|
284
|
+
for (i = 0; i < this._.runningList.length; i++) {
|
|
285
|
+
cmt = this._.runningList[i];
|
|
286
|
+
var totalWidth = this._.width + cmt.width;
|
|
287
|
+
var elapsed = totalWidth * (dn - cmt._utc) * pbr / this._.duration;
|
|
288
|
+
if (cmt.mode === 'ltr') cmt.x = elapsed - cmt.width;
|
|
289
|
+
if (cmt.mode === 'rtl') cmt.x = this._.width - elapsed;
|
|
290
|
+
if (cmt.mode === 'top' || cmt.mode === 'bottom') {
|
|
291
|
+
cmt.x = (this._.width - cmt.width) >> 1;
|
|
292
|
+
}
|
|
293
|
+
render(this._.stage, cmt);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/* eslint no-invalid-this: 0 */
|
|
299
|
+
function play() {
|
|
300
|
+
if (!this._.visible || !this._.paused) {
|
|
301
|
+
return this;
|
|
302
|
+
}
|
|
303
|
+
this._.paused = false;
|
|
304
|
+
if (this.media) {
|
|
305
|
+
for (var i = 0; i < this._.runningList.length; i++) {
|
|
306
|
+
var cmt = this._.runningList[i];
|
|
307
|
+
cmt._utc = now() / 1000 - (this.media.currentTime - cmt.time);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
var that = this;
|
|
311
|
+
var engine = createEngine(
|
|
312
|
+
this._.engine.framing.bind(this),
|
|
313
|
+
this._.engine.setup.bind(this),
|
|
314
|
+
this._.engine.render.bind(this),
|
|
315
|
+
this._.engine.remove.bind(this)
|
|
316
|
+
);
|
|
317
|
+
function frame(timestamp) {
|
|
318
|
+
engine.call(that, timestamp);
|
|
319
|
+
that._.requestID = raf(frame);
|
|
320
|
+
}
|
|
321
|
+
this._.requestID = raf(frame);
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/* eslint no-invalid-this: 0 */
|
|
326
|
+
function pause() {
|
|
327
|
+
if (!this._.visible || this._.paused) {
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
this._.paused = true;
|
|
331
|
+
caf(this._.requestID);
|
|
332
|
+
this._.requestID = 0;
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/* eslint no-invalid-this: 0 */
|
|
337
|
+
function seek() {
|
|
338
|
+
if (!this.media) {
|
|
339
|
+
return this;
|
|
340
|
+
}
|
|
341
|
+
this.clear();
|
|
342
|
+
resetSpace(this._.space);
|
|
343
|
+
var position = binsearch(this.comments, 'time', this.media.currentTime);
|
|
344
|
+
this._.position = Math.max(0, position - 1);
|
|
345
|
+
return this;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/* eslint no-invalid-this: 0 */
|
|
349
|
+
function bindEvents(_) {
|
|
350
|
+
_.play = play.bind(this);
|
|
351
|
+
_.pause = pause.bind(this);
|
|
352
|
+
_.seeking = seek.bind(this);
|
|
353
|
+
this.media.addEventListener('play', _.play);
|
|
354
|
+
this.media.addEventListener('pause', _.pause);
|
|
355
|
+
this.media.addEventListener('playing', _.play);
|
|
356
|
+
this.media.addEventListener('waiting', _.pause);
|
|
357
|
+
this.media.addEventListener('seeking', _.seeking);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/* eslint no-invalid-this: 0 */
|
|
361
|
+
function unbindEvents(_) {
|
|
362
|
+
this.media.removeEventListener('play', _.play);
|
|
363
|
+
this.media.removeEventListener('pause', _.pause);
|
|
364
|
+
this.media.removeEventListener('playing', _.play);
|
|
365
|
+
this.media.removeEventListener('waiting', _.pause);
|
|
366
|
+
this.media.removeEventListener('seeking', _.seeking);
|
|
367
|
+
_.play = null;
|
|
368
|
+
_.pause = null;
|
|
369
|
+
_.seeking = null;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/* eslint-disable no-invalid-this */
|
|
373
|
+
function init(opt) {
|
|
374
|
+
this._ = {};
|
|
375
|
+
this.container = opt.container || document.createElement('div');
|
|
376
|
+
this.media = opt.media;
|
|
377
|
+
this._.visible = true;
|
|
378
|
+
|
|
379
|
+
/* eslint-disable no-undef */
|
|
380
|
+
/* istanbul ignore next */
|
|
381
|
+
{
|
|
382
|
+
this.engine = 'dom';
|
|
383
|
+
this._.engine = domEngine;
|
|
384
|
+
}
|
|
385
|
+
/* eslint-enable no-undef */
|
|
386
|
+
this._.requestID = 0;
|
|
387
|
+
|
|
388
|
+
this._.speed = Math.max(0, opt.speed) || 144;
|
|
389
|
+
this._.duration = 4;
|
|
390
|
+
|
|
391
|
+
this.comments = opt.comments || [];
|
|
392
|
+
this.comments.sort(function(a, b) {
|
|
393
|
+
return a.time - b.time;
|
|
394
|
+
});
|
|
395
|
+
for (var i = 0; i < this.comments.length; i++) {
|
|
396
|
+
this.comments[i].mode = formatMode(this.comments[i].mode);
|
|
397
|
+
}
|
|
398
|
+
this._.runningList = [];
|
|
399
|
+
this._.position = 0;
|
|
400
|
+
|
|
401
|
+
this._.paused = true;
|
|
402
|
+
if (this.media) {
|
|
403
|
+
this._.listener = {};
|
|
404
|
+
bindEvents.call(this, this._.listener);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
this._.stage = this._.engine.init(this.container);
|
|
408
|
+
this._.stage.style.cssText += 'position:relative;pointer-events:none;';
|
|
409
|
+
|
|
410
|
+
this.resize();
|
|
411
|
+
this.container.appendChild(this._.stage);
|
|
412
|
+
|
|
413
|
+
this._.space = {};
|
|
414
|
+
resetSpace(this._.space);
|
|
415
|
+
|
|
416
|
+
if (!this.media || !this.media.paused) {
|
|
417
|
+
seek.call(this);
|
|
418
|
+
play.call(this);
|
|
419
|
+
}
|
|
420
|
+
return this;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/* eslint-disable no-invalid-this */
|
|
424
|
+
function destroy() {
|
|
425
|
+
if (!this.container) {
|
|
426
|
+
return this;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
pause.call(this);
|
|
430
|
+
this.clear();
|
|
431
|
+
this.container.removeChild(this._.stage);
|
|
432
|
+
if (this.media) {
|
|
433
|
+
unbindEvents.call(this, this._.listener);
|
|
434
|
+
}
|
|
435
|
+
for (var key in this) {
|
|
436
|
+
/* istanbul ignore else */
|
|
437
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
438
|
+
this[key] = null;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return this;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
var properties = ['mode', 'time', 'text', 'render', 'style'];
|
|
445
|
+
|
|
446
|
+
/* eslint-disable no-invalid-this */
|
|
447
|
+
function emit(obj) {
|
|
448
|
+
if (!obj || Object.prototype.toString.call(obj) !== '[object Object]') {
|
|
449
|
+
return this;
|
|
450
|
+
}
|
|
451
|
+
var cmt = {};
|
|
452
|
+
for (var i = 0; i < properties.length; i++) {
|
|
453
|
+
if (obj[properties[i]] !== undefined) {
|
|
454
|
+
cmt[properties[i]] = obj[properties[i]];
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
cmt.text = (cmt.text || '').toString();
|
|
458
|
+
cmt.mode = formatMode(cmt.mode);
|
|
459
|
+
cmt._utc = now() / 1000;
|
|
460
|
+
if (this.media) {
|
|
461
|
+
var position = 0;
|
|
462
|
+
if (cmt.time === undefined) {
|
|
463
|
+
cmt.time = this.media.currentTime;
|
|
464
|
+
position = this._.position;
|
|
465
|
+
} else {
|
|
466
|
+
position = binsearch(this.comments, 'time', cmt.time);
|
|
467
|
+
if (position < this._.position) {
|
|
468
|
+
this._.position += 1;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
this.comments.splice(position, 0, cmt);
|
|
472
|
+
} else {
|
|
473
|
+
this.comments.push(cmt);
|
|
474
|
+
}
|
|
475
|
+
return this;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/* eslint-disable no-invalid-this */
|
|
479
|
+
function show() {
|
|
480
|
+
if (this._.visible) {
|
|
481
|
+
return this;
|
|
482
|
+
}
|
|
483
|
+
this._.visible = true;
|
|
484
|
+
if (this.media && this.media.paused) {
|
|
485
|
+
return this;
|
|
486
|
+
}
|
|
487
|
+
seek.call(this);
|
|
488
|
+
play.call(this);
|
|
489
|
+
return this;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/* eslint-disable no-invalid-this */
|
|
493
|
+
function hide() {
|
|
494
|
+
if (!this._.visible) {
|
|
495
|
+
return this;
|
|
496
|
+
}
|
|
497
|
+
pause.call(this);
|
|
498
|
+
this.clear();
|
|
499
|
+
this._.visible = false;
|
|
500
|
+
return this;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/* eslint-disable no-invalid-this */
|
|
504
|
+
function clear() {
|
|
505
|
+
this._.engine.clear(this._.stage, this._.runningList);
|
|
506
|
+
this._.runningList = [];
|
|
507
|
+
return this;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/* eslint-disable no-invalid-this */
|
|
511
|
+
function resize() {
|
|
512
|
+
this._.width = this.container.offsetWidth;
|
|
513
|
+
this._.height = this.container.offsetHeight;
|
|
514
|
+
this._.engine.resize(this._.stage, this._.width, this._.height);
|
|
515
|
+
this._.duration = this._.width / this._.speed;
|
|
516
|
+
return this;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
var speed = {
|
|
520
|
+
get: function() {
|
|
521
|
+
return this._.speed;
|
|
522
|
+
},
|
|
523
|
+
set: function(s) {
|
|
524
|
+
if (typeof s !== 'number' ||
|
|
525
|
+
isNaN(s) ||
|
|
526
|
+
!isFinite(s) ||
|
|
527
|
+
s <= 0) {
|
|
528
|
+
return this._.speed;
|
|
529
|
+
}
|
|
530
|
+
this._.speed = s;
|
|
531
|
+
if (this._.width) {
|
|
532
|
+
this._.duration = this._.width / s;
|
|
533
|
+
}
|
|
534
|
+
return s;
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
function Danmaku(opt) {
|
|
539
|
+
opt && init.call(this, opt);
|
|
540
|
+
}
|
|
541
|
+
Danmaku.prototype.destroy = function() {
|
|
542
|
+
return destroy.call(this);
|
|
543
|
+
};
|
|
544
|
+
Danmaku.prototype.emit = function(cmt) {
|
|
545
|
+
return emit.call(this, cmt);
|
|
546
|
+
};
|
|
547
|
+
Danmaku.prototype.show = function() {
|
|
548
|
+
return show.call(this);
|
|
549
|
+
};
|
|
550
|
+
Danmaku.prototype.hide = function() {
|
|
551
|
+
return hide.call(this);
|
|
552
|
+
};
|
|
553
|
+
Danmaku.prototype.clear = function() {
|
|
554
|
+
return clear.call(this);
|
|
555
|
+
};
|
|
556
|
+
Danmaku.prototype.resize = function() {
|
|
557
|
+
return resize.call(this);
|
|
558
|
+
};
|
|
559
|
+
Object.defineProperty(Danmaku.prototype, 'speed', speed);
|
|
560
|
+
|
|
561
|
+
export { Danmaku as default };
|