2017-03-18 08:35:17 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-16 17:24:03 +00:00
|
|
|
import anyIntersection from "./anyIntersection";
|
|
|
|
import fill from "./fill";
|
2017-03-18 08:35:17 +00:00
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Merge text part information within a styling fragment
|
2017-03-18 08:35:17 +00:00
|
|
|
function assign(textPart, fragment) {
|
|
|
|
const fragStart = fragment.start;
|
|
|
|
const start = Math.max(fragment.start, textPart.start);
|
|
|
|
const end = Math.min(fragment.end, textPart.end);
|
2018-03-05 00:59:16 +00:00
|
|
|
const text = fragment.text.slice(start - fragStart, end - fragStart);
|
2017-03-18 08:35:17 +00:00
|
|
|
|
2018-03-05 00:59:16 +00:00
|
|
|
return Object.assign({}, fragment, {start, end, text});
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 16:00:46 +00:00
|
|
|
function sortParts(a, b) {
|
|
|
|
return a.start - b.start || b.end - a.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the style fragments within the text parts, taking into account
|
2017-04-04 04:36:03 +00:00
|
|
|
// boundaries and text sections that have not matched to links or channels.
|
|
|
|
// For example, given a string "foobar" where "foo" and "bar" have been
|
|
|
|
// identified as parts (channels, links, etc.) and "fo", "ob" and "ar" have 3
|
|
|
|
// different styles, the first resulting part will contain fragments "fo" and
|
|
|
|
// "o", and the second resulting part will contain "b" and "ar". "o" and "b"
|
|
|
|
// fragments will contain duplicate styling attributes.
|
2018-04-19 16:01:20 +00:00
|
|
|
function merge(textParts, styleFragments, cleanText) {
|
2018-05-07 18:19:54 +00:00
|
|
|
// Remove overlapping parts
|
2019-07-17 09:33:59 +00:00
|
|
|
textParts = textParts.sort(sortParts).reduce((prev, curr) => {
|
|
|
|
const intersection = prev.some((p) => anyIntersection(p, curr));
|
2018-04-19 16:00:46 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (intersection) {
|
|
|
|
return prev;
|
|
|
|
}
|
2018-04-19 16:00:46 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
return prev.concat([curr]);
|
|
|
|
}, []);
|
2017-03-18 08:35:17 +00:00
|
|
|
|
2018-05-07 18:19:54 +00:00
|
|
|
// Every section of the original text that has not been captured in a "part"
|
|
|
|
// is filled with "text" parts, dummy objects with start/end but no extra
|
|
|
|
// metadata.
|
2019-07-17 09:33:59 +00:00
|
|
|
const allParts = textParts.concat(fill(textParts, cleanText)).sort(sortParts); // Sort all parts identified based on their position in the original text
|
2018-05-07 18:19:54 +00:00
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Distribute the style fragments within the text parts
|
2017-04-08 12:34:31 +00:00
|
|
|
return allParts.map((textPart) => {
|
2017-03-18 08:35:17 +00:00
|
|
|
textPart.fragments = styleFragments
|
2017-04-08 12:34:31 +00:00
|
|
|
.filter((fragment) => anyIntersection(textPart, fragment))
|
|
|
|
.map((fragment) => assign(textPart, fragment));
|
2017-03-18 08:35:17 +00:00
|
|
|
|
|
|
|
return textPart;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-16 17:24:03 +00:00
|
|
|
export default merge;
|