@xpadev-net/niconicomments 0.2.44 → 0.2.46

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
@@ -21,7 +21,6 @@ declare class FlashComment implements IComment {
21
21
  get owner(): boolean;
22
22
  get mail(): string[];
23
23
  get lineCount(): number;
24
- parseCommand(comment: formattedComment): parsedCommand;
25
24
  parseCommandAndNicoscript(comment: formattedComment): formattedCommentWithFont;
26
25
  measureText(comment: measureTextInput): measureTextResult;
27
26
  getCommentSize(parsedData: formattedCommentWithFont): formattedCommentWithSize;
@@ -410,7 +409,7 @@ export type v1Thread = {
410
409
  id: string;
411
410
  fork: string;
412
411
  commentCount: number;
413
- comments: { [key: string]: v1Comment };
412
+ comments: v1Comment[];
414
413
  };
415
414
  type v1Comment = {
416
415
  id: string;
@@ -489,6 +488,7 @@ type formattedCommentWithFont = {
489
488
  font: commentFont;
490
489
  color: string;
491
490
  strokeColor?: string;
491
+ wakuColor?: string;
492
492
  full: boolean;
493
493
  ender: boolean;
494
494
  _live: boolean;
@@ -590,6 +590,7 @@ type parsedCommand = {
590
590
  fontSize: number | undefined;
591
591
  color: string | undefined;
592
592
  strokeColor?: string;
593
+ wakuColor?: string;
593
594
  font: commentFont | undefined;
594
595
  full: boolean;
595
596
  ender: boolean;
@@ -782,6 +783,7 @@ declare const typeGuard: {
782
783
 
783
784
 
784
785
 
786
+
785
787
  declare const getPosY: (currentPos: number, targetComment: IComment, collision: IComment[] | undefined) => {
786
788
  currentPos: number;
787
789
  isChanged: boolean;
@@ -800,6 +802,10 @@ declare const isFlashComment: (comment: formattedComment) => boolean;
800
802
  declare const parseCommandAndNicoScript: (comment: formattedComment) => formattedCommentWithFont;
801
803
  declare const getStrokeColor: (comment: formattedCommentWithSize) => string;
802
804
  declare const ArrayEqual: (a: unknown[], b: unknown[]) => boolean;
805
+ declare const getFlashFontIndex: (part: string) => commentContentIndex[];
806
+ declare const getFlashFontName: (font: string) => commentFlashFont;
807
+ declare const getValue: <T>(value: T | null | undefined, alternative: T) => T;
808
+ declare const nativeSort: <T>(getter: (input: T) => number) => (a: T, b: T) => 0 | 1 | -1;
803
809
 
804
810
 
805
811
  export default NiconiComments;
package/dist/bundle.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- niconicomments.js v0.2.44
2
+ niconicomments.js v0.2.46
3
3
  (c) 2021 xpadev-net https://xpadev.net
4
4
  Released under the MIT License.
5
5
  */
@@ -137,12 +137,11 @@
137
137
  return false;
138
138
  if (!i.documentElement.children)
139
139
  return false;
140
- for (var index = 0; index < i.documentElement.children.length; index++) {
141
- var value = i.documentElement.children[index];
142
- if (!value)
140
+ for (var _i = 0, _a = Array.from(i.documentElement.children); _i < _a.length; _i++) {
141
+ var element = _a[_i];
142
+ if (!element || element.nodeName !== "chat")
143
143
  continue;
144
- if (value.nodeName === "chat" &&
145
- !typeAttributeVerify(value, ["no", "vpos", "date", "date_usec", "mail"]))
144
+ if (!typeAttributeVerify(element, ["vpos", "date"]))
146
145
  return false;
147
146
  }
148
147
  return true;
@@ -197,9 +196,9 @@
197
196
  thread: function (i) {
198
197
  if (!objectVerify(i, ["id", "fork", "commentCount", "comments"]))
199
198
  return false;
200
- for (var _i = 0, _a = Object.keys(i.comments); _i < _a.length; _i++) {
201
- var item = _a[_i];
202
- if (!typeGuard.v1.comment(i.comments[item]))
199
+ for (var _i = 0, _a = i.comments; _i < _a.length; _i++) {
200
+ var value = _a[_i];
201
+ if (!typeGuard.v1.comment(value))
203
202
  return false;
204
203
  }
205
204
  return true;
@@ -346,16 +345,17 @@
346
345
  var fromXMLDocument = function (data) {
347
346
  var _a;
348
347
  var data_ = [], userList = [];
348
+ var index = Array.from(data.documentElement.children).length;
349
349
  for (var _i = 0, _b = Array.from(data.documentElement.children); _i < _b.length; _i++) {
350
350
  var item = _b[_i];
351
351
  if (item.nodeName !== "chat")
352
352
  continue;
353
353
  var tmpParam = {
354
- id: Number(item.getAttribute("no")),
354
+ id: Number(item.getAttribute("no")) || index++,
355
355
  vpos: Number(item.getAttribute("vpos")),
356
356
  content: item.innerHTML,
357
- date: Number(item.getAttribute("date")),
358
- date_usec: Number(item.getAttribute("date_usec")),
357
+ date: Number(item.getAttribute("date")) || 0,
358
+ date_usec: Number(item.getAttribute("date_usec")) || 0,
359
359
  owner: !item.getAttribute("user_id"),
360
360
  premium: item.getAttribute("premium") === "1",
361
361
  mail: [],
@@ -396,9 +396,9 @@
396
396
  };
397
397
  var fromLegacy = function (data) {
398
398
  var data_ = [], userList = [];
399
- for (var i = 0; i < data.length; i++) {
400
- var val = data[i];
401
- if (!val || !typeGuard.legacy.apiChat(val === null || val === void 0 ? void 0 : val.chat))
399
+ for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
400
+ var val = data_1[_i];
401
+ if (!typeGuard.legacy.apiChat(val.chat))
402
402
  continue;
403
403
  var value = val.chat;
404
404
  if (value.deleted !== 1) {
@@ -497,14 +497,11 @@
497
497
  };
498
498
  var fromV1 = function (data) {
499
499
  var data_ = [], userList = [];
500
- for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
501
- var item = data_1[_i];
500
+ for (var _i = 0, data_2 = data; _i < data_2.length; _i++) {
501
+ var item = data_2[_i];
502
502
  var val = item.comments, forkName = item.fork;
503
- for (var _a = 0, _b = Object.keys(val); _a < _b.length; _a++) {
504
- var key = _b[_a];
505
- var value = val[key];
506
- if (!value)
507
- continue;
503
+ for (var _a = 0, val_1 = val; _a < val_1.length; _a++) {
504
+ var value = val_1[_a];
508
505
  var tmpParam = {
509
506
  id: value.no,
510
507
  vpos: Math.floor(value.vposMs / 10),
@@ -1282,6 +1279,7 @@
1282
1279
  fontSize: undefined,
1283
1280
  color: undefined,
1284
1281
  strokeColor: undefined,
1282
+ wakuColor: undefined,
1285
1283
  font: undefined,
1286
1284
  full: false,
1287
1285
  ender: false,
@@ -1305,6 +1303,15 @@
1305
1303
  result.strokeColor = match[1].slice(1);
1306
1304
  }
1307
1305
  }
1306
+ else if (result.wakuColor === undefined &&
1307
+ (match = command.match(/^nico:waku:(.+)$/))) {
1308
+ if (typeGuard.comment.color(match[1])) {
1309
+ result.wakuColor = colors[match[1]];
1310
+ }
1311
+ else if (typeGuard.comment.colorCode(match[1])) {
1312
+ result.wakuColor = match[1].slice(1);
1313
+ }
1314
+ }
1308
1315
  else if (result.loc === undefined && typeGuard.comment.loc(command)) {
1309
1316
  result.loc = command;
1310
1317
  }
@@ -1363,6 +1370,49 @@
1363
1370
  }
1364
1371
  return true;
1365
1372
  };
1373
+ var getFlashFontIndex = function (part) {
1374
+ var regex = {
1375
+ simsunStrong: new RegExp(config.flashChar.simsunStrong),
1376
+ simsunWeak: new RegExp(config.flashChar.simsunWeak),
1377
+ gulim: new RegExp(config.flashChar.gulim),
1378
+ gothic: new RegExp(config.flashChar.gothic),
1379
+ };
1380
+ var index = [];
1381
+ var match;
1382
+ if ((match = regex.simsunStrong.exec(part)) !== null) {
1383
+ index.push({ font: "simsunStrong", index: match.index });
1384
+ }
1385
+ if ((match = regex.simsunWeak.exec(part)) !== null) {
1386
+ index.push({ font: "simsunWeak", index: match.index });
1387
+ }
1388
+ if ((match = regex.gulim.exec(part)) !== null) {
1389
+ index.push({ font: "gulim", index: match.index });
1390
+ }
1391
+ if ((match = regex.gothic.exec(part)) !== null) {
1392
+ index.push({ font: "gothic", index: match.index });
1393
+ }
1394
+ return index;
1395
+ };
1396
+ var getFlashFontName = function (font) {
1397
+ if (font.match("^simsun.+"))
1398
+ return "simsun";
1399
+ if (font === "gothic")
1400
+ return "defont";
1401
+ return font;
1402
+ };
1403
+ var nativeSort = function (getter) {
1404
+ return function (a, b) {
1405
+ if (getter(a) > getter(b)) {
1406
+ return 1;
1407
+ }
1408
+ else if (getter(a) < getter(b)) {
1409
+ return -1;
1410
+ }
1411
+ else {
1412
+ return 0;
1413
+ }
1414
+ };
1415
+ };
1366
1416
 
1367
1417
  var getLineHeight = function (fontSize, isFlash, resized) {
1368
1418
  if (resized === void 0) { resized = false; }
@@ -1387,10 +1437,8 @@
1387
1437
  var _a = getFontSizeAndScale(comment.charSize), fontSize = _a.fontSize, scale = _a.scale, lineWidth = [], itemWidth = [];
1388
1438
  context.font = parseFont(comment.font, fontSize);
1389
1439
  var currentWidth = 0;
1390
- for (var i = 0; i < comment.content.length; i++) {
1391
- var item = comment.content[i];
1392
- if (item === undefined)
1393
- continue;
1440
+ for (var _i = 0, _b = comment.content; _i < _b.length; _i++) {
1441
+ var item = _b[_i];
1394
1442
  var lines = item.content.split("\n");
1395
1443
  context.font = parseFont(item.font || comment.font, fontSize);
1396
1444
  var width = [];
@@ -1706,6 +1754,10 @@
1706
1754
  }
1707
1755
  this.context.drawImage(this.image, posX, posY);
1708
1756
  }
1757
+ if (this.comment.wakuColor) {
1758
+ this.context.strokeStyle = this.comment.wakuColor;
1759
+ this.context.strokeRect(posX, posY, this.comment.width, this.comment.height);
1760
+ }
1709
1761
  if (showCollision) {
1710
1762
  var scale = getConfig(config.commentScale, false);
1711
1763
  this.context.strokeStyle = "rgba(0,255,255,1)";
@@ -1776,10 +1828,8 @@
1776
1828
  var leftOffset = 0, lineCount = 0;
1777
1829
  var paddingTop = (10 - scale * 10) *
1778
1830
  (this.comment.lineCount / config.hiResCommentCorrection);
1779
- for (var i = 0; i < this.comment.content.length; i++) {
1780
- var item = this.comment.content[i];
1781
- if (!item)
1782
- continue;
1831
+ for (var _i = 0, _c = this.comment.content; _i < _c.length; _i++) {
1832
+ var item = _c[_i];
1783
1833
  var lines = item.content.split("\n");
1784
1834
  for (var j = 0; j < lines.length; j++) {
1785
1835
  var line = lines[j];
@@ -1904,59 +1954,6 @@
1904
1954
  enumerable: false,
1905
1955
  configurable: true
1906
1956
  });
1907
- FlashComment.prototype.parseCommand = function (comment) {
1908
- var metadata = comment.mail;
1909
- var result = {
1910
- loc: undefined,
1911
- size: undefined,
1912
- fontSize: undefined,
1913
- color: undefined,
1914
- font: undefined,
1915
- full: false,
1916
- ender: false,
1917
- _live: false,
1918
- invisible: false,
1919
- long: undefined,
1920
- };
1921
- for (var _i = 0, metadata_1 = metadata; _i < metadata_1.length; _i++) {
1922
- var command = metadata_1[_i];
1923
- command = command.toLowerCase();
1924
- var match = command.match(/^@([0-9.]+)/);
1925
- if (match && match[1]) {
1926
- result.long = Number(match[1]);
1927
- }
1928
- else if (result.loc === undefined && typeGuard.comment.loc(command)) {
1929
- result.loc = command;
1930
- }
1931
- else if (result.size === undefined && typeGuard.comment.size(command)) {
1932
- result.size = command;
1933
- result.fontSize = getConfig(config.fontSize, true)[command].default;
1934
- }
1935
- else {
1936
- if (result.color === undefined) {
1937
- var color = config.colors[command];
1938
- if (color) {
1939
- result.color = color;
1940
- continue;
1941
- }
1942
- else {
1943
- var match_1 = command.match(/#[0-9a-z]{3,6}/);
1944
- if (match_1 && match_1[0] && comment.premium) {
1945
- result.color = match_1[0].toUpperCase();
1946
- continue;
1947
- }
1948
- }
1949
- }
1950
- if (result.font === undefined && typeGuard.comment.font(command)) {
1951
- result.font = command;
1952
- }
1953
- else if (typeGuard.comment.command.key(command)) {
1954
- result[command] = true;
1955
- }
1956
- }
1957
- }
1958
- return result;
1959
- };
1960
1957
  FlashComment.prototype.parseCommandAndNicoscript = function (comment) {
1961
1958
  var _a, _b;
1962
1959
  var data = parseCommandAndNicoScript(comment);
@@ -1964,19 +1961,6 @@
1964
1961
  var parts = (comment.content.match(/\n|[^\n]+/g) || []).map(function (val) {
1965
1962
  return Array.from(val.match(/[ -~。-゚]+|[^ -~。-゚]+/g) || []);
1966
1963
  });
1967
- var regex = {
1968
- simsunStrong: new RegExp(config.flashChar.simsunStrong),
1969
- simsunWeak: new RegExp(config.flashChar.simsunWeak),
1970
- gulim: new RegExp(config.flashChar.gulim),
1971
- gothic: new RegExp(config.flashChar.gothic),
1972
- };
1973
- var getFontName = function (font) {
1974
- return font.match("^simsun.+")
1975
- ? "simsun"
1976
- : font === "gothic"
1977
- ? "defont"
1978
- : font;
1979
- };
1980
1964
  var _loop_1 = function (line) {
1981
1965
  var lineContent = [];
1982
1966
  for (var _c = 0, line_1 = line; _c < line_1.length; _c++) {
@@ -1985,38 +1969,18 @@
1985
1969
  lineContent.push({ content: part });
1986
1970
  continue;
1987
1971
  }
1988
- var index = [];
1989
- var match = void 0;
1990
- if ((match = regex.simsunStrong.exec(part)) !== null) {
1991
- index.push({ font: "simsunStrong", index: match.index });
1992
- }
1993
- if ((match = regex.simsunWeak.exec(part)) !== null) {
1994
- index.push({ font: "simsunWeak", index: match.index });
1995
- }
1996
- if ((match = regex.gulim.exec(part)) !== null) {
1997
- index.push({ font: "gulim", index: match.index });
1998
- }
1999
- if ((match = regex.gothic.exec(part)) !== null) {
2000
- index.push({ font: "gothic", index: match.index });
2001
- }
1972
+ var index = getFlashFontIndex(part);
2002
1973
  if (index.length === 0) {
2003
1974
  lineContent.push({ content: part });
2004
1975
  }
2005
1976
  else if (index.length === 1 && index[0]) {
2006
- lineContent.push({ content: part, font: getFontName(index[0].font) });
1977
+ lineContent.push({
1978
+ content: part,
1979
+ font: getFlashFontName(index[0].font),
1980
+ });
2007
1981
  }
2008
1982
  else {
2009
- index.sort(function (a, b) {
2010
- if (a.index > b.index) {
2011
- return 1;
2012
- }
2013
- else if (a.index < b.index) {
2014
- return -1;
2015
- }
2016
- else {
2017
- return 0;
2018
- }
2019
- });
1983
+ index.sort(nativeSort(function (val) { return val.index; }));
2020
1984
  if (config.flashMode === "xp") {
2021
1985
  var offset = 0;
2022
1986
  for (var i = 1; i < index.length; i++) {
@@ -2025,7 +1989,7 @@
2025
1989
  continue;
2026
1990
  lineContent.push({
2027
1991
  content: part.slice(offset, currentVal.index),
2028
- font: getFontName(lastVal.font),
1992
+ font: getFlashFontName(lastVal.font),
2029
1993
  });
2030
1994
  offset = currentVal.index;
2031
1995
  }
@@ -2033,7 +1997,7 @@
2033
1997
  if (val_1)
2034
1998
  lineContent.push({
2035
1999
  content: part.slice(offset),
2036
- font: getFontName(val_1.font),
2000
+ font: getFlashFontName(val_1.font),
2037
2001
  });
2038
2002
  }
2039
2003
  else {
@@ -2045,17 +2009,17 @@
2045
2009
  if (firstVal.font !== "gothic") {
2046
2010
  lineContent.push({
2047
2011
  content: part,
2048
- font: getFontName(firstVal.font),
2012
+ font: getFlashFontName(firstVal.font),
2049
2013
  });
2050
2014
  }
2051
2015
  else {
2052
2016
  lineContent.push({
2053
2017
  content: part.slice(0, secondVal.index),
2054
- font: getFontName(firstVal.font),
2018
+ font: getFlashFontName(firstVal.font),
2055
2019
  });
2056
2020
  lineContent.push({
2057
2021
  content: part.slice(secondVal.index),
2058
- font: getFontName(secondVal.font),
2022
+ font: getFlashFontName(secondVal.font),
2059
2023
  });
2060
2024
  }
2061
2025
  }
@@ -2111,15 +2075,13 @@
2111
2075
  }
2112
2076
  var width_arr = [], spacedWidth_arr = [];
2113
2077
  var currentWidth = 0, spacedWidth = 0;
2114
- for (var i = 0; i < comment.content.length; i++) {
2115
- var item = comment.content[i];
2116
- if (item === undefined)
2117
- continue;
2078
+ for (var _i = 0, _a = comment.content; _i < _a.length; _i++) {
2079
+ var item = _a[_i];
2118
2080
  var lines = item.content.split("\n");
2119
2081
  var widths = [];
2120
2082
  this.context.font = parseFont(item.font || comment.font, comment.fontSize);
2121
- for (var i_1 = 0; i_1 < lines.length; i_1++) {
2122
- var value = lines[i_1];
2083
+ for (var i = 0; i < lines.length; i++) {
2084
+ var value = lines[i];
2123
2085
  if (value === undefined)
2124
2086
  continue;
2125
2087
  var measure = this.context.measureText(value);
@@ -2127,7 +2089,7 @@
2127
2089
  spacedWidth +=
2128
2090
  measure.width + Math.max(value.length - 1, 0) * config.letterSpacing;
2129
2091
  widths.push(measure.width);
2130
- if (i_1 < lines.length - 1) {
2092
+ if (i < lines.length - 1) {
2131
2093
  width_arr.push(currentWidth);
2132
2094
  spacedWidth_arr.push(spacedWidth);
2133
2095
  spacedWidth = 0;
@@ -2255,6 +2217,10 @@
2255
2217
  }
2256
2218
  this.context.drawImage(this.image, posX, posY);
2257
2219
  }
2220
+ if (this.comment.wakuColor) {
2221
+ this.context.strokeStyle = this.comment.wakuColor;
2222
+ this.context.strokeRect(posX, posY, this.comment.width, this.comment.height);
2223
+ }
2258
2224
  if (showCollision) {
2259
2225
  this.context.strokeStyle = "rgba(255,0,255,1)";
2260
2226
  this.context.strokeRect(posX, posY, this.comment.width, this.comment.height);
@@ -2322,10 +2288,8 @@
2322
2288
  context.fillStyle = this.comment.color;
2323
2289
  var lineOffset = this.comment.lineOffset;
2324
2290
  var lastFont = this.comment.font, leftOffset = 0, lineCount = 0;
2325
- for (var i = 0; i < this.comment.content.length; i++) {
2326
- var item = this.comment.content[i];
2327
- if (!item)
2328
- continue;
2291
+ for (var _i = 0, _a = this.comment.content; _i < _a.length; _i++) {
2292
+ var item = _a[_i];
2329
2293
  if (lastFont !== (item.font || this.comment.font)) {
2330
2294
  lastFont = item.font || this.comment.font;
2331
2295
  context.font = parseFont(lastFont, this.comment.fontSize);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xpadev-net/niconicomments",
3
- "version": "0.2.44",
3
+ "version": "0.2.46",
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",