-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighlighter.js
More file actions
169 lines (142 loc) · 4.65 KB
/
Highlighter.js
File metadata and controls
169 lines (142 loc) · 4.65 KB
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
import React, { Children, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { defaultStyle } from './utils';
import { iterateMentionsMarkup, mapPlainTextIndex, readConfigFromChildren, isNumber } from './utils';
const _generateComponentKey = (usedKeys, id) => {
if (!usedKeys.hasOwnProperty(id)) {
usedKeys[id] = 0;
} else {
usedKeys[id]++;
}
return id + '_' + usedKeys[id];
};
function Highlighter({
selectionStart,
selectionEnd,
value = '',
onCaretPositionChange,
containerRef,
children,
singleLine,
style,
}) {
const [position, setPosition] = useState({
left: undefined,
top: undefined,
});
const [caretElement, setCaretElement] = useState();
useEffect(() => {
notifyCaretPosition();
});
const notifyCaretPosition = () => {
if (!caretElement) {
return;
}
const { offsetLeft, offsetTop } = caretElement;
if (position.left === offsetLeft && position.top === offsetTop) {
return;
}
const newPosition = { left: offsetLeft, top: offsetTop };
setPosition(newPosition);
onCaretPositionChange(newPosition);
};
const config = readConfigFromChildren(children);
let caretPositionInMarkup;
if (selectionEnd === selectionStart) {
caretPositionInMarkup = mapPlainTextIndex(value, config, selectionStart, 'START');
}
const resultComponents = [];
const componentKeys = {};
let components = resultComponents;
let substringComponentKey = 0;
const textIteratee = (substr, index, indexInPlainText) => {
// check whether the caret element has to be inserted inside the current plain substring
if (
isNumber(caretPositionInMarkup) &&
caretPositionInMarkup >= index &&
caretPositionInMarkup <= index + substr.length
) {
// if yes, split substr at the caret position and insert the caret component
const splitIndex = caretPositionInMarkup - index;
components.push(renderSubstring(substr.substring(0, splitIndex), substringComponentKey));
// add all following substrings and mention components as children of the caret component
components = [renderSubstring(substr.substring(splitIndex), substringComponentKey)];
} else {
components.push(renderSubstring(substr, substringComponentKey));
}
substringComponentKey++;
};
const mentionIteratee = (markup, index, indexInPlainText, id, display, mentionChildIndex, lastMentionEndIndex) => {
const key = _generateComponentKey(componentKeys, id);
components.push(getMentionComponentForMatch(id, display, mentionChildIndex, key));
};
const renderSubstring = (string, key) => {
// set substring span to hidden, so that Emojis are not shown double in Mobile Safari
return (
<span {...style('substring')} key={key}>
{string}
</span>
);
};
const getMentionComponentForMatch = (id, display, mentionChildIndex, key) => {
const props = { id, display, key };
const child = Children.toArray(children)[mentionChildIndex];
return React.cloneElement(child, props);
};
const renderHighlighterCaret = (children) => {
return (
<span {...style('caret')} ref={setCaretElement} key='caret'>
{children}
</span>
);
};
iterateMentionsMarkup(value, config, mentionIteratee, textIteratee);
// append a span containing a space, to ensure the last text line has the correct height
components.push(' ');
if (components !== resultComponents) {
// if a caret component is to be rendered, add all components that followed as its children
resultComponents.push(renderHighlighterCaret(components));
}
return (
<div {...style} ref={containerRef}>
{resultComponents}
</div>
);
}
Highlighter.propTypes = {
selectionStart: PropTypes.number,
selectionEnd: PropTypes.number,
value: PropTypes.string.isRequired,
onCaretPositionChange: PropTypes.func.isRequired,
containerRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: typeof Element === 'undefined' ? PropTypes.any : PropTypes.instanceOf(Element),
}),
]),
children: PropTypes.oneOfType([PropTypes.element, PropTypes.arrayOf(PropTypes.element)]).isRequired,
};
const styled = defaultStyle(
{
'position': 'relative',
'boxSizing': 'border-box',
'width': '100%',
'color': 'transparent',
'overflow': 'hidden',
'whiteSpace': 'pre-wrap',
'wordWrap': 'break-word',
'border': '1px solid transparent',
'textAlign': 'start',
'&singleLine': {
whiteSpace: 'pre',
wordWrap: null,
},
'substring': {
visibility: 'hidden',
},
},
(props) => ({
'&singleLine': props.singleLine,
}),
);
export default styled(Highlighter);