annotate.js 3.2 KB

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