UNPKG

969 BJavaScriptView Raw
1import { formatMode, binsearch } from '../utils.js';
2
3var properties = ['mode', 'time', 'text', 'render', 'style'];
4
5/* eslint-disable no-invalid-this */
6export default function(obj) {
7 if (!obj || Object.prototype.toString.call(obj) !== '[object Object]') {
8 return this;
9 }
10 var cmt = {};
11 for (var i = 0; i < properties.length; i++) {
12 if (obj[properties[i]] !== undefined) {
13 cmt[properties[i]] = obj[properties[i]];
14 }
15 }
16 cmt.text = (cmt.text || '').toString();
17 cmt.mode = formatMode(cmt.mode);
18 cmt._utc = Date.now() / 1000;
19 if (this.media) {
20 var position = 0;
21 if (cmt.time === undefined) {
22 cmt.time = this.media.currentTime;
23 position = this._.position;
24 } else {
25 position = binsearch(this.comments, 'time', cmt.time);
26 if (position < this._.position) {
27 this._.position += 1;
28 }
29 }
30 this.comments.splice(position, 0, cmt);
31 } else {
32 this.comments.push(cmt);
33 }
34 return this;
35}