Spaces:
Configuration error
Configuration error
File size: 9,837 Bytes
a7060a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | import emojiRegex from "emoji-regex";
import { Component } from "react";
import { observer } from "mobx-react";
import Utils from "../../utils";
import Range from "./Range";
import { HtxTextNode } from "./Node";
import UrlNode from "./UrlNode";
import EmojiNode from "./EmojiNode";
import styles from "./TextHighlight.module.css";
class TextHighlight extends Component {
constructor() {
super();
this.dismissMouseUp = 0;
}
/**
* Return first ok element
*/
getRange(charIndex) {
if (this.props.ranges && this.props.ranges.length) {
return this.props.ranges.find((range) => charIndex >= range.start && charIndex <= range.end);
}
}
/**
* Function when the user mouse is over an highlighted text
*/
onMouseOverHighlightedWord(range, visible) {
if (visible && this.props.onMouseOverHighlightedWord) {
this.props.onMouseOverHighlightedWord(range);
}
}
getLetterNode(charIndex, range) {
/**
* Current symbol
*/
const char = this.props.text[charIndex];
let nl;
/**
* Line break
*/
if (char && char.charCodeAt()) {
nl = char.charCodeAt(0) === 10;
}
let arrOverlap = [];
if (this.props.ranges) {
this.props.ranges.map((range) => {
if (charIndex >= range.start && charIndex <= range.end) {
return (arrOverlap = [...arrOverlap, range.id]);
}
return arrOverlap;
});
}
return (
<HtxTextNode
id={this.props.id}
overlap={arrOverlap}
range={range}
charIndex={charIndex}
key={`${this.props.id}-${charIndex}`}
highlightStyle={this.props.highlightStyle}
// style={{padding: "2px 0", background: "linear-gradient(180deg, #fdcd3b 60%, #ffed4b 60%, red 9%)"}}
>
{nl ? <br /> : char}
</HtxTextNode>
);
}
getEmojiNode(charIndex, range) {
let arrOverlap = [];
if (this.props.ranges) {
this.props.ranges.map((range) => {
if (charIndex >= range.start && charIndex <= range.end) {
return (arrOverlap = [...arrOverlap, range.id]);
}
return arrOverlap;
});
}
return (
<EmojiNode
text={this.props.text}
id={this.props.id}
overlap={arrOverlap}
range={range}
key={`${this.props.id}-emoji-${charIndex}`}
charIndex={charIndex}
highlightStyle={this.props.highlightStyle}
/>
);
}
getUrlNode(charIndex, range, url) {
let arrOverlap = [];
if (this.props.ranges) {
this.props.ranges.map((range) => {
if (charIndex >= range.start && charIndex <= range.end) {
return (arrOverlap = [...arrOverlap, range.id]);
}
return arrOverlap;
});
}
return (
<UrlNode
url={url}
id={this.props.id}
overlap={arrOverlap}
range={range}
key={`${this.props.id}-url-${charIndex}`}
charIndex={charIndex}
highlightStyle={this.props.highlightStyle}
/>
);
}
mouseEvent() {
if (!this.props.enabled) {
return false;
}
let text = "";
if (window.getSelection) {
/**
* Get highlited text
* Tip: with helper elements (hints)
*/
// text = window.getSelection().toString();
if (window.getSelection().type === "None") return;
/**
* Create clone range
*/
const cloneCont = window.getSelection().getRangeAt(0).cloneRange();
/**
* The Range.cloneContents() returns a DocumentFragment copying the objects of type Node included in the Range.
*/
const selectionContents = cloneCont.cloneContents();
/**
* Create virtual div with text
*/
const virtualDiv = document.createElement("div");
virtualDiv.appendChild(selectionContents);
const elementsWithSup = virtualDiv.getElementsByTagName("sup");
if (elementsWithSup.length > 0) {
for (let i = 0; i < elementsWithSup.length; i++) {
elementsWithSup[i].innerText = "";
}
text = virtualDiv.innerText;
} else {
text = virtualDiv.innerText;
}
} else if (document.selection && document.selection.type !== "Control") {
text = document.selection.createRange().text;
}
if (!text || !text.length) return false;
const range = window.getSelection().getRangeAt(0);
/**
* Check for hint
*/
if (range.startContainer.parentNode.dataset.hint || range.endContainer.parentNode.dataset.hint) return;
/**
* Start position of selected item
*/
let startContainerPosition = Number.parseInt(range.startContainer.parentNode.dataset.position);
/**
* End position of selected item
*/
let endContainerPosition = Number.parseInt(range.endContainer.parentNode.dataset.position);
if (!range.startContainer.parentNode.dataset.position) {
if (!range.startContainer.dataset) return;
startContainerPosition = Number.parseInt(range.startContainer.dataset.position);
}
if (!range.endContainer.parentNode.dataset.position) {
if (!range.endContainer.dataset) return;
endContainerPosition = Number.parseInt(range.endContainer.dataset.position);
}
const startHL = startContainerPosition < endContainerPosition ? startContainerPosition : endContainerPosition;
const endHL = startContainerPosition < endContainerPosition ? endContainerPosition : startContainerPosition;
const rangeObj = new Range(startHL, endHL, text, {
...this.props,
ranges: undefined,
});
this.props.onTextHighlighted(rangeObj);
}
/**
*
* @param {*} event
*/
onMouseUp() {
this.mouseEvent.bind(this)();
}
onMouseDown() {
// console.log(event)
}
onMouseEnter() {
// console.log(event)
}
/**
* Double click on text
* @param {*} event
*/
onDoubleClick() {
// WARN
// event.stopPropagation();
// this.doucleckicked = true;
// this.mouseEvent.bind(this)();
}
/**
* @param {object} letterGroup All marked sections of text
* @param {object} range Range of marked section
* @param {number} textCharIndex The last number of selection
* @param {function} onMouseOverHighlightedWord
*/
rangeRenderer(letterGroup, range, textCharIndex, onMouseOverHighlightedWord) {
if (this.props.rangeRenderer) {
return this.props.rangeRenderer(letterGroup, range, textCharIndex, onMouseOverHighlightedWord);
}
return letterGroup;
}
getNode(i, range, text, url, isEmoji) {
if (url.length) {
return this.getUrlNode(i, range, url);
}
if (isEmoji) {
return this.getEmojiNode(i, range);
}
return this.getLetterNode(i, range);
}
getRanges() {
/**
* Text with nodes
*/
const newText = [];
let lastRange;
/**
* For all the characters on the text
*/
for (let textCharIndex = 0; textCharIndex < this.props.text.length; textCharIndex++) {
/**
* Get range text
*/
const range = this.getRange(textCharIndex);
/**
* Check characters for URL
*/
const url = Utils.Checkers.getUrl(textCharIndex, this.props.text);
/**
* Check characters for emoji
*/
const isEmoji = emojiRegex().test(this.props.text[textCharIndex] + this.props.text[textCharIndex + 1]);
/**
* Get the current character node
*/
const node = this.getNode(textCharIndex, range, this.props.text, url, isEmoji);
/**
* If the next node is an url one, we fast forward to the end of it
*/
if (url.length) {
textCharIndex += url.length - 1;
} else if (isEmoji) {
/**
* Because an emoji is composed of 2 chars
*/
textCharIndex++;
}
if (!range) {
newText.push(node);
continue;
}
/**
* If the char is in range
*/
lastRange = range;
// console.log(this.props.text[lastRange.start], this.props.text[lastRange.end])
/**
* We put the first range node on the array
*/
const letterGroup = [node];
/**
* For all the characters in the highlighted range
*/
let rangeCharIndex = textCharIndex + 1;
// if (rangeCharIndex >= parseInt(range.start) && rangeCharIndex <= parseInt(range.end)) {
// console.log(this.props.text[parseInt(range.end)])
// }
// console.log(textCharIndex, range.start, range.end)
for (; rangeCharIndex < Number.parseInt(range.end) + 1; rangeCharIndex++) {
/**
* Emoji handler
*/
const isEmoji = emojiRegex().test(`${this.props.text[rangeCharIndex]}${this.props.text[rangeCharIndex + 1]}`);
if (isEmoji) {
letterGroup.push(this.getEmojiNode(rangeCharIndex, range));
// Because an emoji is composed of 2 chars
rangeCharIndex++;
} else {
letterGroup.push(this.getLetterNode(rangeCharIndex, range));
}
textCharIndex = rangeCharIndex;
}
newText.push(this.rangeRenderer(letterGroup, range, textCharIndex, this.onMouseOverHighlightedWord.bind(this)));
}
if (lastRange) {
// Callback function
this.onMouseOverHighlightedWord(lastRange, true);
}
return newText;
}
render() {
const newText = this.getRanges();
return (
<div
className={styles.block}
style={this.props.style}
onMouseUp={this.onMouseUp.bind(this)}
onMouseDown={this.onMouseDown.bind(this)}
onMouseEnter={this.onMouseEnter.bind(this)}
onDoubleClick={this.onDoubleClick.bind(this)}
>
{newText}
</div>
);
}
}
export default observer(TextHighlight);
|