-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordGrabber.js
167 lines (145 loc) · 5.54 KB
/
wordGrabber.js
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
/*
Функция getWordFromEvent(e) создает range по клику и возвращает слово по координатам клика
Три варианта, для Chrome + Safari, для IE и для Mozilla
При клике мы ловим ноду, в которую был клик, получаем смещение слева от края ноды и в случае с FF и Chrome,
проверям символы слева и справа от этой точки на наличие разделителей, чтобы найти конец слова, IE умеет делать это сам.
*/
export function getWordFromEvent(e) {
// для Chrome, Safari
if (global.document.caretRangeFromPoint) {
return getRangeChrome(e);
}
//IE
if (global.document.body.createTextRange) {
return getWordFromEventIE(e);
}
//FF
if (global.document.createRange) {
return getRangeFF(e);
}
return null;
}
function getRangeChrome(e) {
const x = e.clientX;
const y = e.clientY;
const range = global.document.caretRangeFromPoint(x, y);
return getWordFromRange(range, e);
}
function getWordFromEventIE(e) {
const x = e.clientX;
const y = e.clientY;
const innerText = e.target && e.target.innerText;
const separators = /([\s&^:;,!?(){}])+/;
const IErange = global.document.body.createTextRange();
try {
IErange.moveToElementText(e.target);
IErange.collapse(true);
let wholeSentenceLength = 0;
const reqIEcharTest = () => {
do {
IErange.expand("character");
wholeSentenceLength += 1;
}
while (!separators.test(IErange.text.slice(-1)) && wholeSentenceLength <= innerText.length);
const {boundingLeft, boundingTop, boundingWidth, boundingHeight, text} = IErange;
if (boundingLeft <= x && x <= (boundingLeft + boundingWidth)
&& boundingTop <= y && y <= (boundingTop + boundingHeight)) {
if (wholeSentenceLength <= innerText.length && !separators.test(text.slice(-1)) ) {
return text;
}
return text.substr(0, text.length - 1);
}
IErange.collapse(false);
return reqIEcharTest();
};
const text = reqIEcharTest().trim();
const innerTextArr = innerText.split(text);
const innerTextLeft = innerTextArr[0].split(separators);
const innerTextRight = innerTextArr[1].split(separators);
let leftPart;
if (innerTextLeft <= 1) {
leftPart = recursionWordGet(e.target, 'left') + innerTextLeft.slice(-1)[0];
} else {
leftPart = innerTextLeft.slice(-1)[0];
}
let rightPart;
if (innerTextRight <= 1) {
rightPart = innerTextRight[0] + recursionWordGet(e.target, 'right');
} else {
rightPart = innerTextRight[0];
}
return leftPart + text + rightPart;
} catch (err) {
console.log('>>>>>>>>>>>>>>>>>> text', err);
}
}
function getRangeFF(e) {
const x = e.clientX;
const y = e.clientY;
const range = global.document.caretPositionFromPoint(x, y);
return getWordFromRange(range, e);
}
function getWordFromRange(range, e) {
const text = e && e.target && (e.target.innerText || e.target.textContent) || '';
const wordClickPoint = range && range.startOffset || range && range.offset; //ff
const separators = /([\s&^:;,!?(){}])+/;
const textLeft = text.substr(0, wordClickPoint || 0);
const textRight = text.substr(wordClickPoint || 0);
const textLeftSplit = textLeft.split(separators);
const textRightSplit = textRight.split(separators);
let rightPart;
if (textRightSplit.length <= 1) {
rightPart = textRightSplit[0] + recursionWordGet(e.target, 'right');
} else {
rightPart = textRightSplit[0];
}
let leftPart;
if (textLeftSplit.length <= 1) {
leftPart = recursionWordGet(e.target, 'left') + textLeftSplit.slice(-1)[0];
} else {
leftPart = textLeftSplit.slice(-1)[0];
}
return (leftPart + rightPart).trim();
}
function recursionWordGet(target, option) {
const separators = /([\s&^:;,!?(){}])+/;
const uniqString = Date.now();
target.setAttribute("data-target", uniqString);
const {parentNode} = target;
const copyNode = parentNode.cloneNode(true);
// const targetNodeText = target.innerText;
//поменяем текст в скопированной ноде, чтобы потом найти его парсингом
const dataTargetNode = copyNode.querySelector(`[data-target="${uniqString}"]`);
if (dataTargetNode.hasOwnProperty('innerText')) {
dataTargetNode.innerText = uniqString;
} else {
dataTargetNode.textContent = uniqString;
}
const tagName = copyNode.tagName;
//возьмем весь текст
const text = copyNode.innerText || copyNode.textContent;
const textArr = text.split(uniqString);
const textLeftPartArr = textArr[0].split(separators);
const textRightPartArr = textArr[1].split(separators);
if (option === 'right') {
let returnText;
if (textRightPartArr.length <= 1 && tagName === 'span') {
//разделителей нет, нужно подниматься на уровень выше
returnText = textRightPartArr[0] + recursionWordGet(parentNode, 'right');
} else {
returnText = textRightPartArr[0];
}
return returnText;
}
if (option === 'left') {
let returnText;
if (textLeftPartArr <= 1 && tagName === 'span') {
//разделителей нет, нужно подниматься на уровень выше
returnText = recursionWordGet(parentNode, 'left') + textLeftPartArr.slice(-1)[0];
} else {
returnText = textLeftPartArr.slice(-1)[0];
}
return returnText;
}
return '';
}