react-text-range 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +32 -24
- package/dist/esm/index.js +32 -24
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -70,6 +70,17 @@ const getHandlerRect = (node, left) => {
|
|
|
70
70
|
return null;
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
|
+
const createTextNodes = (div) => {
|
|
74
|
+
if (div.childNodes[1] && !div.childNodes[1].firstChild) {
|
|
75
|
+
div.childNodes[1].appendChild(document.createTextNode(''));
|
|
76
|
+
}
|
|
77
|
+
if (div.childNodes[3] && !div.childNodes[3].firstChild) {
|
|
78
|
+
div.childNodes[3].appendChild(document.createTextNode(''));
|
|
79
|
+
}
|
|
80
|
+
if (div.childNodes[5] && !div.childNodes[5].firstChild) {
|
|
81
|
+
div.childNodes[5].appendChild(document.createTextNode(''));
|
|
82
|
+
}
|
|
83
|
+
};
|
|
73
84
|
const getNodeAndOffsetFromPoint = (x, y) => {
|
|
74
85
|
let range;
|
|
75
86
|
let textNode;
|
|
@@ -158,29 +169,27 @@ const useTextSelectionEditor = (initLeftPos, initRightPos, leftDrag, rightDrag,
|
|
|
158
169
|
// mouse move handler
|
|
159
170
|
// left handler
|
|
160
171
|
const leftMoveHandler = React.useCallback((e) => {
|
|
161
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
|
|
162
172
|
const sm = getNodeAndOffsetFromPoint(e.clientX, e.clientY);
|
|
163
173
|
if (!sm)
|
|
164
174
|
return;
|
|
175
|
+
if (!textDiv.current)
|
|
176
|
+
return;
|
|
165
177
|
let posToSet = currentLeftPos;
|
|
166
|
-
if (sm.node ===
|
|
178
|
+
if (sm.node === textDiv.current.childNodes[1].firstChild) {
|
|
167
179
|
posToSet = sm.offset;
|
|
168
180
|
}
|
|
169
|
-
else if (sm.node ===
|
|
181
|
+
else if (sm.node === textDiv.current.childNodes[3].firstChild) {
|
|
170
182
|
posToSet = currentLeftPos + sm.offset;
|
|
171
183
|
}
|
|
172
184
|
if (posToSet !== currentLeftPos) {
|
|
173
|
-
|
|
174
|
-
const
|
|
185
|
+
createTextNodes(textDiv.current);
|
|
186
|
+
const headText = textDiv.current.childNodes[1].firstChild.nodeValue;
|
|
187
|
+
const selText = textDiv.current.childNodes[3].firstChild.nodeValue;
|
|
175
188
|
const full = headText + selText;
|
|
176
|
-
const nodeChild1 =
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const nodeChild3 = (_p = (_o = textDiv.current) === null || _o === void 0 ? void 0 : _o.childNodes[3]) === null || _p === void 0 ? void 0 : _p.firstChild;
|
|
181
|
-
if (nodeChild3) {
|
|
182
|
-
nodeChild3.nodeValue = full.substring(posToSet);
|
|
183
|
-
}
|
|
189
|
+
const nodeChild1 = textDiv.current.childNodes[1].firstChild;
|
|
190
|
+
nodeChild1.nodeValue = full.substring(0, posToSet);
|
|
191
|
+
const nodeChild3 = textDiv.current.childNodes[3].firstChild;
|
|
192
|
+
nodeChild3.nodeValue = full.substring(posToSet);
|
|
184
193
|
setCurrentLeftPos(posToSet);
|
|
185
194
|
}
|
|
186
195
|
}, [currentLeftPos, textDiv.current]);
|
|
@@ -200,10 +209,12 @@ const useTextSelectionEditor = (initLeftPos, initRightPos, leftDrag, rightDrag,
|
|
|
200
209
|
}, [initLeftPos]);
|
|
201
210
|
// right handler
|
|
202
211
|
const rightMoveHandler = React.useCallback((e) => {
|
|
203
|
-
var _a, _b
|
|
212
|
+
var _a, _b;
|
|
204
213
|
const sm = getNodeAndOffsetFromPoint(e.clientX, e.clientY);
|
|
205
214
|
if (!sm)
|
|
206
215
|
return;
|
|
216
|
+
if (!textDiv.current)
|
|
217
|
+
return;
|
|
207
218
|
let posToSet = currentRightPos;
|
|
208
219
|
if (sm.node === ((_a = textDiv.current) === null || _a === void 0 ? void 0 : _a.childNodes[3].firstChild)) {
|
|
209
220
|
posToSet = currentLeftPos + sm.offset;
|
|
@@ -212,17 +223,14 @@ const useTextSelectionEditor = (initLeftPos, initRightPos, leftDrag, rightDrag,
|
|
|
212
223
|
posToSet = currentRightPos + sm.offset;
|
|
213
224
|
}
|
|
214
225
|
if (posToSet !== currentRightPos) {
|
|
215
|
-
|
|
216
|
-
const
|
|
226
|
+
createTextNodes(textDiv.current);
|
|
227
|
+
const selText = textDiv.current.childNodes[3].firstChild.nodeValue;
|
|
228
|
+
const tailText = textDiv.current.childNodes[5].firstChild.nodeValue;
|
|
217
229
|
const full = selText + tailText;
|
|
218
|
-
const nodeChild3 =
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const nodeChild5 = (_p = (_o = textDiv.current) === null || _o === void 0 ? void 0 : _o.childNodes[5]) === null || _p === void 0 ? void 0 : _p.firstChild;
|
|
223
|
-
if (nodeChild5) {
|
|
224
|
-
nodeChild5.nodeValue = full.substring(posToSet - currentLeftPos);
|
|
225
|
-
}
|
|
230
|
+
const nodeChild3 = textDiv.current.childNodes[3].firstChild;
|
|
231
|
+
nodeChild3.nodeValue = full.substring(0, posToSet - currentLeftPos);
|
|
232
|
+
const nodeChild5 = textDiv.current.childNodes[5].firstChild;
|
|
233
|
+
nodeChild5.nodeValue = full.substring(posToSet - currentLeftPos);
|
|
226
234
|
setCurrentRightPos(posToSet);
|
|
227
235
|
}
|
|
228
236
|
}, [currentLeftPos, currentRightPos, textDiv.current]);
|
package/dist/esm/index.js
CHANGED
|
@@ -50,6 +50,17 @@ const getHandlerRect = (node, left) => {
|
|
|
50
50
|
return null;
|
|
51
51
|
}
|
|
52
52
|
};
|
|
53
|
+
const createTextNodes = (div) => {
|
|
54
|
+
if (div.childNodes[1] && !div.childNodes[1].firstChild) {
|
|
55
|
+
div.childNodes[1].appendChild(document.createTextNode(''));
|
|
56
|
+
}
|
|
57
|
+
if (div.childNodes[3] && !div.childNodes[3].firstChild) {
|
|
58
|
+
div.childNodes[3].appendChild(document.createTextNode(''));
|
|
59
|
+
}
|
|
60
|
+
if (div.childNodes[5] && !div.childNodes[5].firstChild) {
|
|
61
|
+
div.childNodes[5].appendChild(document.createTextNode(''));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
53
64
|
const getNodeAndOffsetFromPoint = (x, y) => {
|
|
54
65
|
let range;
|
|
55
66
|
let textNode;
|
|
@@ -138,29 +149,27 @@ const useTextSelectionEditor = (initLeftPos, initRightPos, leftDrag, rightDrag,
|
|
|
138
149
|
// mouse move handler
|
|
139
150
|
// left handler
|
|
140
151
|
const leftMoveHandler = useCallback((e) => {
|
|
141
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
|
|
142
152
|
const sm = getNodeAndOffsetFromPoint(e.clientX, e.clientY);
|
|
143
153
|
if (!sm)
|
|
144
154
|
return;
|
|
155
|
+
if (!textDiv.current)
|
|
156
|
+
return;
|
|
145
157
|
let posToSet = currentLeftPos;
|
|
146
|
-
if (sm.node ===
|
|
158
|
+
if (sm.node === textDiv.current.childNodes[1].firstChild) {
|
|
147
159
|
posToSet = sm.offset;
|
|
148
160
|
}
|
|
149
|
-
else if (sm.node ===
|
|
161
|
+
else if (sm.node === textDiv.current.childNodes[3].firstChild) {
|
|
150
162
|
posToSet = currentLeftPos + sm.offset;
|
|
151
163
|
}
|
|
152
164
|
if (posToSet !== currentLeftPos) {
|
|
153
|
-
|
|
154
|
-
const
|
|
165
|
+
createTextNodes(textDiv.current);
|
|
166
|
+
const headText = textDiv.current.childNodes[1].firstChild.nodeValue;
|
|
167
|
+
const selText = textDiv.current.childNodes[3].firstChild.nodeValue;
|
|
155
168
|
const full = headText + selText;
|
|
156
|
-
const nodeChild1 =
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const nodeChild3 = (_p = (_o = textDiv.current) === null || _o === void 0 ? void 0 : _o.childNodes[3]) === null || _p === void 0 ? void 0 : _p.firstChild;
|
|
161
|
-
if (nodeChild3) {
|
|
162
|
-
nodeChild3.nodeValue = full.substring(posToSet);
|
|
163
|
-
}
|
|
169
|
+
const nodeChild1 = textDiv.current.childNodes[1].firstChild;
|
|
170
|
+
nodeChild1.nodeValue = full.substring(0, posToSet);
|
|
171
|
+
const nodeChild3 = textDiv.current.childNodes[3].firstChild;
|
|
172
|
+
nodeChild3.nodeValue = full.substring(posToSet);
|
|
164
173
|
setCurrentLeftPos(posToSet);
|
|
165
174
|
}
|
|
166
175
|
}, [currentLeftPos, textDiv.current]);
|
|
@@ -180,10 +189,12 @@ const useTextSelectionEditor = (initLeftPos, initRightPos, leftDrag, rightDrag,
|
|
|
180
189
|
}, [initLeftPos]);
|
|
181
190
|
// right handler
|
|
182
191
|
const rightMoveHandler = useCallback((e) => {
|
|
183
|
-
var _a, _b
|
|
192
|
+
var _a, _b;
|
|
184
193
|
const sm = getNodeAndOffsetFromPoint(e.clientX, e.clientY);
|
|
185
194
|
if (!sm)
|
|
186
195
|
return;
|
|
196
|
+
if (!textDiv.current)
|
|
197
|
+
return;
|
|
187
198
|
let posToSet = currentRightPos;
|
|
188
199
|
if (sm.node === ((_a = textDiv.current) === null || _a === void 0 ? void 0 : _a.childNodes[3].firstChild)) {
|
|
189
200
|
posToSet = currentLeftPos + sm.offset;
|
|
@@ -192,17 +203,14 @@ const useTextSelectionEditor = (initLeftPos, initRightPos, leftDrag, rightDrag,
|
|
|
192
203
|
posToSet = currentRightPos + sm.offset;
|
|
193
204
|
}
|
|
194
205
|
if (posToSet !== currentRightPos) {
|
|
195
|
-
|
|
196
|
-
const
|
|
206
|
+
createTextNodes(textDiv.current);
|
|
207
|
+
const selText = textDiv.current.childNodes[3].firstChild.nodeValue;
|
|
208
|
+
const tailText = textDiv.current.childNodes[5].firstChild.nodeValue;
|
|
197
209
|
const full = selText + tailText;
|
|
198
|
-
const nodeChild3 =
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
const nodeChild5 = (_p = (_o = textDiv.current) === null || _o === void 0 ? void 0 : _o.childNodes[5]) === null || _p === void 0 ? void 0 : _p.firstChild;
|
|
203
|
-
if (nodeChild5) {
|
|
204
|
-
nodeChild5.nodeValue = full.substring(posToSet - currentLeftPos);
|
|
205
|
-
}
|
|
210
|
+
const nodeChild3 = textDiv.current.childNodes[3].firstChild;
|
|
211
|
+
nodeChild3.nodeValue = full.substring(0, posToSet - currentLeftPos);
|
|
212
|
+
const nodeChild5 = textDiv.current.childNodes[5].firstChild;
|
|
213
|
+
nodeChild5.nodeValue = full.substring(posToSet - currentLeftPos);
|
|
206
214
|
setCurrentRightPos(posToSet);
|
|
207
215
|
}
|
|
208
216
|
}, [currentLeftPos, currentRightPos, textDiv.current]);
|