@xpadev-net/niconicomments 0.2.42 → 0.2.43

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/dist/bundle.d.ts CHANGED
@@ -72,6 +72,11 @@ declare let nicoScripts: nicoScript;
72
72
  declare const resetNicoScripts: () => void;
73
73
 
74
74
 
75
+ import { IPlugin } from "@/@types/IPlugins";
76
+ declare let plugins: IPlugin[];
77
+ declare const setPlugins: (input: IPlugin[]) => void;
78
+
79
+
75
80
  declare const colors: {
76
81
  white: string;
77
82
  red: string;
@@ -240,6 +245,20 @@ export interface IComment {
240
245
  draw: (vpos: number, showCollision: boolean, isDebug: boolean) => void;
241
246
  }
242
247
 
248
+ import { formattedComment } from "@/@types/format.formatted";
249
+
250
+ export interface IPluginConstructor {
251
+ id: string;
252
+ new (Canvas: HTMLCanvasElement, comments: formattedComment[]): IPlugin;
253
+ }
254
+
255
+ export interface IPlugin {
256
+ draw(vpos: number): void;
257
+ addComments(comments: formattedComment[]): void;
258
+ }
259
+
260
+ import { IPluginConstructor } from "@/@types/IPlugins";
261
+
243
262
  type configItem<T> = T | { html5: T; flash: T };
244
263
  type configSizeItem<T> = { big: T; medium: T; small: T };
245
264
  type configResizedItem<T> = { default: T; resized: T };
@@ -299,6 +318,7 @@ type BaseConfig = {
299
318
  sameCARange: number;
300
319
  letterSpacing: number;
301
320
  scriptCharOffset: number;
321
+ plugins: IPluginConstructor[];
302
322
  };
303
323
 
304
324
  export type Config = Partial<BaseConfig>;
@@ -679,7 +699,7 @@ declare class NiconiComments {
679
699
  private getCommentPos;
680
700
  private sortComment;
681
701
  addComments(...rawComments: formattedComment[]): void;
682
- drawCanvas(vpos: number, forceRendering?: boolean): void;
702
+ drawCanvas(vpos: number, forceRendering?: boolean): boolean;
683
703
  clear(): void;
684
704
  }
685
705
 
@@ -774,12 +794,12 @@ declare const arrayPush: (array: {
774
794
  }, key: string | number, push: IComment) => void;
775
795
  declare const hex2rgb: (hex: string) => number[];
776
796
  declare const hex2rgba: (hex: string) => number[];
777
- declare const replaceAll: (string: string, target: string, replace: string) => string;
778
797
  declare const changeCALayer: (rawData: formattedComment[]) => formattedComment[];
779
798
  declare const getConfig: <T>(input: configItem<T>, isFlash?: boolean) => T;
780
799
  declare const isFlashComment: (comment: formattedComment) => boolean;
781
800
  declare const parseCommandAndNicoScript: (comment: formattedComment) => formattedCommentWithFont;
782
801
  declare const getStrokeColor: (comment: formattedCommentWithSize) => string;
802
+ declare const ArrayEqual: (a: unknown[], b: unknown[]) => boolean;
783
803
 
784
804
 
785
805
  export default NiconiComments;
package/dist/bundle.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- niconicomments.js v0.2.42
2
+ niconicomments.js v0.2.43
3
3
  (c) 2021 xpadev-net https://xpadev.net
4
4
  Released under the MIT License.
5
5
  */
@@ -142,14 +142,7 @@
142
142
  if (!value)
143
143
  continue;
144
144
  if (value.nodeName === "chat" &&
145
- !typeAttributeVerify(value, [
146
- "no",
147
- "vpos",
148
- "date",
149
- "date_usec",
150
- "mail",
151
- "premium",
152
- ]))
145
+ !typeAttributeVerify(value, ["no", "vpos", "date", "date_usec", "mail"]))
153
146
  return false;
154
147
  }
155
148
  return true;
@@ -283,7 +276,7 @@
283
276
  config: isObject,
284
277
  format: function (i) {
285
278
  return typeof i === "string" &&
286
- !!i.match(/^(niconicome|formatted|legacy|legacyOwner|owner|v1|default)$/);
279
+ !!i.match(/^(niconicome|formatted|legacy|legacyOwner|owner|v1|default|empty)$/);
287
280
  },
288
281
  video: function (i) {
289
282
  return typeof i === "object" && i.nodeName === "VIDEO";
@@ -847,6 +840,7 @@
847
840
  sameCARange: 3600,
848
841
  sameCAGap: 100,
849
842
  sameCAMinScore: 10,
843
+ plugins: [],
850
844
  flashThreshold: 1499871600,
851
845
  flashChar: {
852
846
  gulim: "[\u0126\u0127\u0132\u0133\u0138\u013f\u0140\u0149-\u014b\u0166\u0167\u02d0\u02da\u2074\u207f\u2081-\u2084\u2113\u2153\u2154\u215c-\u215e\u2194\u2195\u223c\u249c-\u24b5\u24d0-\u24e9\u25a3-\u25a9\u25b6\u25b7\u25c0\u25c1\u25c8\u25d0\u25d1\u260e\u260f\u261c\u261e\u2660\u2661\u2663-\u2665\u2667-\u2669\u266c\u3131-\u316e\u3200-\u321c\u3260-\u327b\u3380-\u3384\u3388-\u338d\u3390-\u339b\u339f\u33a0\u33a2-\u33ca\u33cf\u33d0\u33d3\u33d6\u33d8\u33db-\u33dd\uf900-\uf928\uf92a-\uf994\uf996\ufa0b\uffe6]",
@@ -1035,12 +1029,6 @@
1035
1029
  return parseInt(str, 16);
1036
1030
  });
1037
1031
  };
1038
- var replaceAll = function (string, target, replace) {
1039
- while (string.indexOf(target) !== -1) {
1040
- string = string.replace(target, replace);
1041
- }
1042
- return string;
1043
- };
1044
1032
  var changeCALayer = function (rawData) {
1045
1033
  var userList = {};
1046
1034
  var data = [], index = {};
@@ -1105,79 +1093,76 @@
1105
1093
  var isFlash = isFlashComment(comment);
1106
1094
  var data = parseCommand(comment), string = comment.content, nicoscript = string.match(/^(?:@|\uff20)(\u30c7\u30d5\u30a9\u30eb\u30c8|\u7f6e\u63db|\u9006|\u30b3\u30e1\u30f3\u30c8\u7981\u6b62|\u30b7\u30fc\u30af\u7981\u6b62|\u30b8\u30e3\u30f3\u30d7)/);
1107
1095
  if (nicoscript && comment.owner) {
1108
- var reverse = comment.content.match(/^(?:@|\uff20)\u9006 ?(\u5168|\u30b3\u30e1|\u6295\u30b3\u30e1)?/);
1096
+ var reverse = comment.content.match(/^(?:@|\uff20)\u9006(?:\s+)?(\u5168|\u30b3\u30e1|\u6295\u30b3\u30e1)?/);
1109
1097
  var content = comment.content.split(""), result = [];
1110
1098
  var quote = "", last_i = "", string_1 = "";
1111
- switch (nicoscript[1]) {
1112
- case "\u30c7\u30d5\u30a9\u30eb\u30c8":
1113
- nicoScripts.default.unshift({
1114
- start: comment.vpos,
1115
- long: data.long === undefined ? undefined : Math.floor(data.long * 100),
1116
- color: data.color,
1117
- size: data.size,
1118
- font: data.font,
1119
- loc: data.loc,
1120
- });
1121
- break;
1122
- case "\u9006":
1123
- if (!reverse ||
1124
- !reverse[1] ||
1125
- !typeGuard.nicoScript.range.target(reverse[1]))
1126
- break;
1127
- if (data.long === undefined) {
1128
- data.long = 30;
1099
+ if (nicoscript[1] === "\u30c7\u30d5\u30a9\u30eb\u30c8") {
1100
+ nicoScripts.default.unshift({
1101
+ start: comment.vpos,
1102
+ long: data.long === undefined ? undefined : Math.floor(data.long * 100),
1103
+ color: data.color,
1104
+ size: data.size,
1105
+ font: data.font,
1106
+ loc: data.loc,
1107
+ });
1108
+ }
1109
+ else if (nicoscript[1] === "\u9006" &&
1110
+ reverse &&
1111
+ reverse[1] &&
1112
+ typeGuard.nicoScript.range.target(reverse[1])) {
1113
+ if (data.long === undefined) {
1114
+ data.long = 30;
1115
+ }
1116
+ nicoScripts.reverse.unshift({
1117
+ start: comment.vpos,
1118
+ end: comment.vpos + data.long * 100,
1119
+ target: reverse[1],
1120
+ });
1121
+ }
1122
+ else if (nicoscript[1] === "\u30b3\u30e1\u30f3\u30c8\u7981\u6b62") {
1123
+ if (data.long === undefined) {
1124
+ data.long = 30;
1125
+ }
1126
+ nicoScripts.ban.unshift({
1127
+ start: comment.vpos,
1128
+ end: comment.vpos + data.long * 100,
1129
+ });
1130
+ }
1131
+ else if (nicoscript[1] === "\u7f6e\u63db") {
1132
+ for (var _i = 0, _a = content.slice(4); _i < _a.length; _i++) {
1133
+ var i = _a[_i];
1134
+ if (i.match(/["'\u300c]/) && quote === "") {
1135
+ quote = i;
1129
1136
  }
1130
- nicoScripts.reverse.unshift({
1131
- start: comment.vpos,
1132
- end: comment.vpos + data.long * 100,
1133
- target: reverse[1],
1134
- });
1135
- break;
1136
- case "\u30b3\u30e1\u30f3\u30c8\u7981\u6b62":
1137
- if (data.long === undefined) {
1138
- data.long = 30;
1137
+ else if (i.match(/["']/) && quote === i && last_i !== "\\") {
1138
+ result.push(string_1.replaceAll("\\n", "\n"));
1139
+ quote = "";
1140
+ string_1 = "";
1139
1141
  }
1140
- nicoScripts.ban.unshift({
1141
- start: comment.vpos,
1142
- end: comment.vpos + data.long * 100,
1143
- });
1144
- break;
1145
- case "\u7f6e\u63db":
1146
- for (var _i = 0, _a = content.slice(4); _i < _a.length; _i++) {
1147
- var i = _a[_i];
1148
- if (i.match(/["'\u300c]/) && quote === "") {
1149
- quote = i;
1150
- }
1151
- else if (i.match(/["']/) && quote === i && last_i !== "\\") {
1152
- result.push(replaceAll(string_1, "\\n", "\n"));
1153
- quote = "";
1154
- string_1 = "";
1155
- }
1156
- else if (i.match(/\u300d/) && quote === "\u300c") {
1142
+ else if (i.match(/\u300d/) && quote === "\u300c") {
1143
+ result.push(string_1);
1144
+ quote = "";
1145
+ string_1 = "";
1146
+ }
1147
+ else if (quote === "" && i.match(/\s+/)) {
1148
+ if (string_1) {
1157
1149
  result.push(string_1);
1158
- quote = "";
1159
1150
  string_1 = "";
1160
1151
  }
1161
- else if (quote === "" && i.match(/\s+/)) {
1162
- if (string_1) {
1163
- result.push(string_1);
1164
- string_1 = "";
1165
- }
1166
- }
1167
- else {
1168
- string_1 += i;
1169
- }
1170
- last_i = i;
1171
1152
  }
1172
- result.push(string_1);
1173
- if (result[0] === undefined ||
1174
- (result[2] !== undefined &&
1175
- !typeGuard.nicoScript.replace.range(result[2])) ||
1176
- (result[3] !== undefined &&
1177
- !typeGuard.nicoScript.replace.target(result[3])) ||
1178
- (result[4] !== undefined &&
1179
- !typeGuard.nicoScript.replace.condition(result[4])))
1180
- break;
1153
+ else {
1154
+ string_1 += i;
1155
+ }
1156
+ last_i = i;
1157
+ }
1158
+ result.push(string_1);
1159
+ if (!(result[0] === undefined ||
1160
+ (result[2] !== undefined &&
1161
+ !typeGuard.nicoScript.replace.range(result[2])) ||
1162
+ (result[3] !== undefined &&
1163
+ !typeGuard.nicoScript.replace.target(result[3])) ||
1164
+ (result[4] !== undefined &&
1165
+ !typeGuard.nicoScript.replace.condition(result[4])))) {
1181
1166
  nicoScripts.replace.unshift({
1182
1167
  start: comment.vpos,
1183
1168
  long: data.long === undefined ? undefined : Math.floor(data.long * 100),
@@ -1203,7 +1188,7 @@
1203
1188
  return 1;
1204
1189
  return 0;
1205
1190
  });
1206
- break;
1191
+ }
1207
1192
  }
1208
1193
  data.invisible = true;
1209
1194
  }
@@ -1248,7 +1233,7 @@
1248
1233
  (item.condition === "\u90e8\u5206\u4e00\u81f4" &&
1249
1234
  comment.content.indexOf(item.keyword) !== -1)) {
1250
1235
  if (item.range === "\u5358") {
1251
- comment.content = replaceAll(comment.content, item.keyword, item.replace);
1236
+ comment.content = comment.content.replaceAll(item.keyword, item.replace);
1252
1237
  }
1253
1238
  else {
1254
1239
  comment.content = item.replace;
@@ -1350,6 +1335,9 @@
1350
1335
  }
1351
1336
  }
1352
1337
  }
1338
+ if (comment.content.startsWith("/")) {
1339
+ result.invisible = true;
1340
+ }
1353
1341
  return result;
1354
1342
  };
1355
1343
  var getStrokeColor = function (comment) {
@@ -1366,6 +1354,15 @@
1366
1354
  ? config.contextStrokeInversionColor
1367
1355
  : config.contextStrokeColor).join(","), ",").concat(config.contextStrokeOpacity, ")");
1368
1356
  };
1357
+ var ArrayEqual = function (a, b) {
1358
+ if (a.length !== b.length)
1359
+ return false;
1360
+ for (var i = 0, n = a.length; i < n; ++i) {
1361
+ if (a[i] !== b[i])
1362
+ return false;
1363
+ }
1364
+ return true;
1365
+ };
1369
1366
 
1370
1367
  var getLineHeight = function (fontSize, isFlash, resized) {
1371
1368
  if (resized === void 0) { resized = false; }
@@ -2370,6 +2367,11 @@
2370
2367
  return FlashComment;
2371
2368
  }());
2372
2369
 
2370
+ var plugins = [];
2371
+ var setPlugins = function (input) {
2372
+ plugins = input;
2373
+ };
2374
+
2373
2375
  var isDebug = false;
2374
2376
  var NiconiComments = (function () {
2375
2377
  function NiconiComments(canvas, data, initOptions) {
@@ -2406,6 +2408,7 @@
2406
2408
  options.mode = "html5";
2407
2409
  }
2408
2410
  var parsedData = convert2formattedComment(data, formatType);
2411
+ setPlugins(config.plugins.map(function (val) { return new val(canvas, parsedData); }));
2409
2412
  this.video = options.video || undefined;
2410
2413
  this.showCollision = options.showCollision;
2411
2414
  this.showFPS = options.showFPS;
@@ -2563,6 +2566,7 @@
2563
2566
  for (var _i = 0; _i < arguments.length; _i++) {
2564
2567
  rawComments[_i] = arguments[_i];
2565
2568
  }
2569
+ plugins.forEach(function (val) { return val.addComments(rawComments); });
2566
2570
  var comments = rawComments.reduce(function (pv, val) {
2567
2571
  if (isFlashComment(val)) {
2568
2572
  pv.push(new FlashComment(val, _this.context));
@@ -2672,10 +2676,20 @@
2672
2676
  }
2673
2677
  };
2674
2678
  NiconiComments.prototype.drawCanvas = function (vpos, forceRendering) {
2679
+ var _a, _b, _c;
2675
2680
  if (forceRendering === void 0) { forceRendering = false; }
2676
2681
  var drawCanvasStart = performance.now();
2677
2682
  if (this.lastVpos === vpos && !forceRendering)
2678
- return;
2683
+ return false;
2684
+ var timelineRange = this.timeline[vpos];
2685
+ if (!forceRendering &&
2686
+ (timelineRange === null || timelineRange === void 0 ? void 0 : timelineRange.filter(function (item) { return item.loc === "naka"; }).length) === 0 &&
2687
+ ((_b = (_a = this.timeline[this.lastVpos]) === null || _a === void 0 ? void 0 : _a.filter(function (item) { return item.loc === "naka"; })) === null || _b === void 0 ? void 0 : _b.length) === 0) {
2688
+ var current = timelineRange.filter(function (item) { return item.loc !== "naka"; }), last = ((_c = this.timeline[this.lastVpos]) === null || _c === void 0 ? void 0 : _c.filter(function (item) { return item.loc !== "naka"; })) ||
2689
+ [];
2690
+ if (ArrayEqual(current, last))
2691
+ return false;
2692
+ }
2679
2693
  this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
2680
2694
  this.lastVpos = vpos;
2681
2695
  if (this.video) {
@@ -2690,7 +2704,6 @@
2690
2704
  var offsetX = (this.canvas.width - this.video.videoWidth * scale) * 0.5, offsetY = (this.canvas.height - this.video.videoHeight * scale) * 0.5;
2691
2705
  this.context.drawImage(this.video, offsetX, offsetY, this.video.videoWidth * scale, this.video.videoHeight * scale);
2692
2706
  }
2693
- var timelineRange = this.timeline[vpos];
2694
2707
  if (this.showCollision) {
2695
2708
  var leftCollision = this.collision.left[vpos], rightCollision = this.collision.right[vpos];
2696
2709
  this.context.fillStyle = "red";
@@ -2701,21 +2714,22 @@
2701
2714
  }
2702
2715
  }
2703
2716
  if (rightCollision) {
2704
- for (var _a = 0, rightCollision_1 = rightCollision; _a < rightCollision_1.length; _a++) {
2705
- var comment = rightCollision_1[_a];
2717
+ for (var _d = 0, rightCollision_1 = rightCollision; _d < rightCollision_1.length; _d++) {
2718
+ var comment = rightCollision_1[_d];
2706
2719
  this.context.fillRect(config.collisionRange.right, comment.posY, config.contextLineWidth * -1, comment.height);
2707
2720
  }
2708
2721
  }
2709
2722
  }
2710
2723
  if (timelineRange) {
2711
- for (var _b = 0, timelineRange_1 = timelineRange; _b < timelineRange_1.length; _b++) {
2712
- var comment = timelineRange_1[_b];
2724
+ for (var _e = 0, timelineRange_1 = timelineRange; _e < timelineRange_1.length; _e++) {
2725
+ var comment = timelineRange_1[_e];
2713
2726
  if (comment.invisible) {
2714
2727
  continue;
2715
2728
  }
2716
2729
  comment.draw(vpos, this.showCollision, isDebug);
2717
2730
  }
2718
2731
  }
2732
+ plugins.forEach(function (val) { return val.draw(vpos); });
2719
2733
  if (this.showFPS) {
2720
2734
  this.context.font = parseFont("defont", 60);
2721
2735
  this.context.fillStyle = "#00FF00";
@@ -2739,6 +2753,7 @@
2739
2753
  }
2740
2754
  }
2741
2755
  logger("drawCanvas complete: ".concat(performance.now() - drawCanvasStart, "ms"));
2756
+ return true;
2742
2757
  };
2743
2758
  NiconiComments.prototype.clear = function () {
2744
2759
  this.context.clearRect(0, 0, config.canvasWidth, config.canvasHeight);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xpadev-net/niconicomments",
3
- "version": "0.2.42",
3
+ "version": "0.2.43",
4
4
  "description": "NiconiComments is a comment drawing library that is somewhat compatible with the official Nico Nico Douga player.",
5
5
  "main": "dist/bundle.js",
6
6
  "types": "dist/bundle.d.ts",