@antv/x6
Version:
JavaScript diagramming library that uses SVG and HTML for rendering
104 lines • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.annotate = annotate;
exports.findAnnotationsAtIndex = findAnnotationsAtIndex;
exports.findAnnotationsBetweenIndexes = findAnnotationsBetweenIndexes;
exports.shiftAnnotations = shiftAnnotations;
const object_1 = require("../object");
const attr_1 = require("../dom/attr");
function annotate(t, annotations, opt = {}) {
const offset = opt.offset || 0;
const compacted = [];
const ret = [];
let curr;
let prev;
let batch = null;
for (let i = 0; i < t.length; i += 1) {
curr = ret[i] = t[i];
for (let j = 0, jj = annotations.length; j < jj; j += 1) {
const annotation = annotations[j];
const start = annotation.start + offset;
const end = annotation.end + offset;
if (i >= start && i < end) {
if (typeof curr === 'string') {
curr = ret[i] = {
t: t[i],
attrs: annotation.attrs,
};
}
else {
curr.attrs = (0, attr_1.mergeAttrs)((0, attr_1.mergeAttrs)({}, curr.attrs), annotation.attrs);
}
if (opt.includeAnnotationIndices) {
if (curr.annotations == null) {
curr.annotations = [];
}
curr.annotations.push(j);
}
}
}
prev = ret[i - 1];
if (!prev) {
batch = curr;
}
else if (object_1.ObjectExt.isObject(curr) && object_1.ObjectExt.isObject(prev)) {
batch = batch;
// Both previous item and the current one are annotations.
// If the attributes didn't change, merge the text.
if (JSON.stringify(curr.attrs) === JSON.stringify(prev.attrs)) {
batch.t += curr.t;
}
else {
compacted.push(batch);
batch = curr;
}
}
else if (object_1.ObjectExt.isObject(curr)) {
// Previous item was a string, current item is an annotation.
batch = batch;
compacted.push(batch);
batch = curr;
}
else if (object_1.ObjectExt.isObject(prev)) {
// Previous item was an annotation, current item is a string.
batch = batch;
compacted.push(batch);
batch = curr;
}
else {
// Both previous and current item are strings.
batch = String(batch || '') + String(curr);
}
}
if (batch != null) {
compacted.push(batch);
}
return compacted;
}
function findAnnotationsAtIndex(annotations, index) {
return annotations
? annotations.filter((a) => a.start < index && index <= a.end)
: [];
}
function findAnnotationsBetweenIndexes(annotations, start, end) {
return annotations
? annotations.filter((a) => (start >= a.start && start < a.end) ||
(end > a.start && end <= a.end) ||
(a.start >= start && a.end < end))
: [];
}
function shiftAnnotations(annotations, index, offset) {
if (annotations) {
annotations.forEach((a) => {
if (a.start < index && a.end >= index) {
a.end += offset;
}
else if (a.start >= index) {
a.start += offset;
a.end += offset;
}
});
}
return annotations;
}
//# sourceMappingURL=annotate.js.map