annotate.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.shiftAnnotations = exports.findAnnotationsBetweenIndexes = exports.findAnnotationsAtIndex = exports.annotate = void 0;
  4. const object_1 = require("../object");
  5. const attr_1 = require("../dom/attr");
  6. function annotate(t, annotations, opt = {}) {
  7. const offset = opt.offset || 0;
  8. const compacted = [];
  9. const ret = [];
  10. let curr;
  11. let prev;
  12. let batch = null;
  13. for (let i = 0; i < t.length; i += 1) {
  14. curr = ret[i] = t[i];
  15. for (let j = 0, jj = annotations.length; j < jj; j += 1) {
  16. const annotation = annotations[j];
  17. const start = annotation.start + offset;
  18. const end = annotation.end + offset;
  19. if (i >= start && i < end) {
  20. if (typeof curr === 'string') {
  21. curr = ret[i] = {
  22. t: t[i],
  23. attrs: annotation.attrs,
  24. };
  25. }
  26. else {
  27. curr.attrs = (0, attr_1.mergeAttrs)((0, attr_1.mergeAttrs)({}, curr.attrs), annotation.attrs);
  28. }
  29. if (opt.includeAnnotationIndices) {
  30. if (curr.annotations == null) {
  31. curr.annotations = [];
  32. }
  33. curr.annotations.push(j);
  34. }
  35. }
  36. }
  37. prev = ret[i - 1];
  38. if (!prev) {
  39. batch = curr;
  40. }
  41. else if (object_1.ObjectExt.isObject(curr) && object_1.ObjectExt.isObject(prev)) {
  42. batch = batch;
  43. // Both previous item and the current one are annotations.
  44. // If the attributes didn't change, merge the text.
  45. if (JSON.stringify(curr.attrs) === JSON.stringify(prev.attrs)) {
  46. batch.t += curr.t;
  47. }
  48. else {
  49. compacted.push(batch);
  50. batch = curr;
  51. }
  52. }
  53. else if (object_1.ObjectExt.isObject(curr)) {
  54. // Previous item was a string, current item is an annotation.
  55. batch = batch;
  56. compacted.push(batch);
  57. batch = curr;
  58. }
  59. else if (object_1.ObjectExt.isObject(prev)) {
  60. // Previous item was an annotation, current item is a string.
  61. batch = batch;
  62. compacted.push(batch);
  63. batch = curr;
  64. }
  65. else {
  66. // Both previous and current item are strings.
  67. batch = (batch || '') + curr;
  68. }
  69. }
  70. if (batch != null) {
  71. compacted.push(batch);
  72. }
  73. return compacted;
  74. }
  75. exports.annotate = annotate;
  76. function findAnnotationsAtIndex(annotations, index) {
  77. return annotations
  78. ? annotations.filter((a) => a.start < index && index <= a.end)
  79. : [];
  80. }
  81. exports.findAnnotationsAtIndex = findAnnotationsAtIndex;
  82. function findAnnotationsBetweenIndexes(annotations, start, end) {
  83. return annotations
  84. ? annotations.filter((a) => (start >= a.start && start < a.end) ||
  85. (end > a.start && end <= a.end) ||
  86. (a.start >= start && a.end < end))
  87. : [];
  88. }
  89. exports.findAnnotationsBetweenIndexes = findAnnotationsBetweenIndexes;
  90. function shiftAnnotations(annotations, index, offset) {
  91. if (annotations) {
  92. annotations.forEach((a) => {
  93. if (a.start < index && a.end >= index) {
  94. a.end += offset;
  95. }
  96. else if (a.start >= index) {
  97. a.start += offset;
  98. a.end += offset;
  99. }
  100. });
  101. }
  102. return annotations;
  103. }
  104. exports.shiftAnnotations = shiftAnnotations;
  105. //# sourceMappingURL=annotate.js.map