@zzish/math-rich-input 0.1.25 → 0.1.27
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/index.js +1003 -299
- package/package.json +1 -1
- package/src/MathRichInput.jsx +58 -64
- package/src/mathRichInputHelper.js +28 -5
package/package.json
CHANGED
package/src/MathRichInput.jsx
CHANGED
|
@@ -461,11 +461,12 @@ export default class MathRichInput extends React.Component {
|
|
|
461
461
|
}
|
|
462
462
|
|
|
463
463
|
if (deleteToEmpty) {
|
|
464
|
+
console.log("deleteToEmpty")
|
|
464
465
|
let new_rangeParams = {
|
|
465
466
|
startNodeIndex: 0,
|
|
466
|
-
startOffset:
|
|
467
|
+
startOffset: 1,
|
|
467
468
|
endNodeIndex: 0,
|
|
468
|
-
endOffset:
|
|
469
|
+
endOffset: 1,
|
|
469
470
|
startGlobalOffset: 0,
|
|
470
471
|
endGlobalOffset: 0,
|
|
471
472
|
};
|
|
@@ -677,9 +678,8 @@ export default class MathRichInput extends React.Component {
|
|
|
677
678
|
new_offset = 0;
|
|
678
679
|
}
|
|
679
680
|
|
|
680
|
-
if (new_index === -1 || new_offset === -1)
|
|
681
|
-
|
|
682
|
-
} else {
|
|
681
|
+
if (new_index === -1 || new_offset === -1) return;
|
|
682
|
+
else {
|
|
683
683
|
let new_rangeParams = {
|
|
684
684
|
startNodeIndex: new_index,
|
|
685
685
|
startOffset: new_offset,
|
|
@@ -691,16 +691,6 @@ export default class MathRichInput extends React.Component {
|
|
|
691
691
|
this._setRangeParams(new_rangeParams);
|
|
692
692
|
e.preventDefault();
|
|
693
693
|
}
|
|
694
|
-
|
|
695
|
-
let new_rangeParams = {
|
|
696
|
-
startNodeIndex: new_index,
|
|
697
|
-
startOffset: new_offset,
|
|
698
|
-
endNodeIndex: new_index,
|
|
699
|
-
endOffset: new_offset,
|
|
700
|
-
};
|
|
701
|
-
this._setRangeParams(new_rangeParams);
|
|
702
|
-
|
|
703
|
-
e.preventDefault();
|
|
704
694
|
};
|
|
705
695
|
|
|
706
696
|
handleKeyDownArrowRight = (e) => {
|
|
@@ -1204,15 +1194,15 @@ export default class MathRichInput extends React.Component {
|
|
|
1204
1194
|
}
|
|
1205
1195
|
}
|
|
1206
1196
|
|
|
1207
|
-
if (this.handleKeyDownAccentHelper(e))
|
|
1197
|
+
if (this.handleKeyDownAccentHelper(e))
|
|
1198
|
+
return;
|
|
1208
1199
|
|
|
1209
1200
|
// Check if inserting character in element that starts with small space
|
|
1210
|
-
if (
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
this.replaceNextLetter(e.key);
|
|
1201
|
+
if (e.key.length === 1 && // ie, it's not something like "arrowLeft"
|
|
1202
|
+
params.startOffset === 0 &&
|
|
1203
|
+
this.nodeStartsWithSmallSpace(startNode)) {
|
|
1204
|
+
console.log("Handling character input at start of text node with small space")
|
|
1205
|
+
this.insertAfterSmallSpace(e.key, params);
|
|
1216
1206
|
e.preventDefault();
|
|
1217
1207
|
return;
|
|
1218
1208
|
}
|
|
@@ -1921,6 +1911,7 @@ export default class MathRichInput extends React.Component {
|
|
|
1921
1911
|
console.error(error);
|
|
1922
1912
|
}
|
|
1923
1913
|
};
|
|
1914
|
+
|
|
1924
1915
|
// https://stackoverflow.com/questions/6690752/insert-html-at-caret-in-a-contenteditable-div
|
|
1925
1916
|
pasteHtmlAtCaret = (html, selectPastedContent) => {
|
|
1926
1917
|
var sel, range;
|
|
@@ -1968,6 +1959,7 @@ export default class MathRichInput extends React.Component {
|
|
|
1968
1959
|
}
|
|
1969
1960
|
}
|
|
1970
1961
|
};
|
|
1962
|
+
|
|
1971
1963
|
insertHtml = (new_html) => {
|
|
1972
1964
|
try {
|
|
1973
1965
|
// console.log("New html: "+new_html)
|
|
@@ -2165,37 +2157,40 @@ export default class MathRichInput extends React.Component {
|
|
|
2165
2157
|
}
|
|
2166
2158
|
};
|
|
2167
2159
|
|
|
2168
|
-
|
|
2160
|
+
insertAfterSmallSpace(new_char, rangeParams) {
|
|
2169
2161
|
try {
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
let offset = rangeParams.endOffset + 1; // next letter
|
|
2162
|
+
if (rangeParams === null || rangeParams === undefined)
|
|
2163
|
+
rangeParams = this._getRangeParams();
|
|
2173
2164
|
|
|
2165
|
+
if (rangeParams.startOffset !== 0)
|
|
2166
|
+
console.warn("Unexpected startOffset "+rangeParams.startOffset+" in insertAfterSmallSpace")
|
|
2167
|
+
|
|
2168
|
+
let node = this._findNodeWithIndex(rangeParams.startNodeIndex);
|
|
2174
2169
|
let raw_text = elementToMarkedRawText(
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
let
|
|
2182
|
-
|
|
2183
|
-
insertCharacterBeforeMarks(raw_text, new_char)
|
|
2184
|
-
);
|
|
2185
|
-
if (
|
|
2186
|
-
rangeParams.startNodeIndex === rangeParams.endNodeIndex &&
|
|
2187
|
-
rangeParams.startOffset === rangeParams.endOffset
|
|
2188
|
-
)
|
|
2189
|
-
new_rangeParams.startOffset =
|
|
2190
|
-
new_rangeParams.startOffset + new_char.length + 1;
|
|
2191
|
-
new_rangeParams.endOffset =
|
|
2192
|
-
new_rangeParams.endOffset + new_char.length + 1;
|
|
2170
|
+
this.editableDiv,
|
|
2171
|
+
node,
|
|
2172
|
+
1, // after small space
|
|
2173
|
+
this.enableHtml()
|
|
2174
|
+
);
|
|
2175
|
+
|
|
2176
|
+
let new_raw_text = removeMarks(
|
|
2177
|
+
insertCharacterBeforeMarks(raw_text, new_char))
|
|
2193
2178
|
|
|
2179
|
+
let new_rangeParams = {
|
|
2180
|
+
startNodeIndex:rangeParams.startNodeIndex,
|
|
2181
|
+
startNodeOffset:1+new_char.length,
|
|
2182
|
+
endNodeIndex:rangeParams.startNodeIndex,
|
|
2183
|
+
endNodeOffset:1+new_char.length,
|
|
2184
|
+
startGlobalOffset:rangeParams.startGlobalOffset+new_char.length,
|
|
2185
|
+
endGlobalOffset:rangeParams.endGlobalOffset+new_char.length,
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2194
2188
|
new_raw_text = removeEncodingIfPlainText(
|
|
2195
2189
|
new_raw_text,
|
|
2196
2190
|
this.props.mimeType,
|
|
2197
2191
|
this.enableHtml()
|
|
2198
2192
|
);
|
|
2193
|
+
|
|
2199
2194
|
this.applyChangesToComponent(
|
|
2200
2195
|
new_raw_text,
|
|
2201
2196
|
this.props.mimeType,
|
|
@@ -2211,8 +2206,8 @@ export default class MathRichInput extends React.Component {
|
|
|
2211
2206
|
insertAccentLetter = (event, old_char, new_char) => {
|
|
2212
2207
|
try {
|
|
2213
2208
|
let rangeParams = this._getRangeParams();
|
|
2214
|
-
let node = this._findNodeWithIndex(rangeParams.
|
|
2215
|
-
let offset = rangeParams.
|
|
2209
|
+
let node = this._findNodeWithIndex(rangeParams.startNodeIndex);
|
|
2210
|
+
let offset = rangeParams.startOffset;
|
|
2216
2211
|
|
|
2217
2212
|
let raw_text = elementToMarkedRawText(
|
|
2218
2213
|
this.editableDiv,
|
|
@@ -2220,32 +2215,31 @@ export default class MathRichInput extends React.Component {
|
|
|
2220
2215
|
offset,
|
|
2221
2216
|
this.enableHtml()
|
|
2222
2217
|
);
|
|
2218
|
+
|
|
2223
2219
|
let new_raw_text = null;
|
|
2224
|
-
let new_rangeParams =
|
|
2225
|
-
new_rangeParams.startGlobalOffset++;
|
|
2226
|
-
new_rangeParams.endGlobalOffset++;
|
|
2220
|
+
let new_rangeParams = null;
|
|
2227
2221
|
|
|
2228
|
-
if (old_char === null) {
|
|
2229
|
-
new_raw_text = removeMarks(
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
rangeParams.startNodeIndex
|
|
2234
|
-
rangeParams.startOffset
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
);
|
|
2222
|
+
if (old_char === null) { // insert new char
|
|
2223
|
+
new_raw_text = removeMarks(insertCharacterBeforeMarks(raw_text, new_char));
|
|
2224
|
+
new_rangeParams = {
|
|
2225
|
+
startNodeIndex:rangeParams.startNodeIndex,
|
|
2226
|
+
startOffset:rangeParams.startOffset + new_char.length,
|
|
2227
|
+
endNodeIndex:rangeParams.startNodeIndex,
|
|
2228
|
+
endOffset:rangeParams.startOffset + new_char.length,
|
|
2229
|
+
startGlobalOffset:rangeParams.startGlobalOffset + new_char.length,
|
|
2230
|
+
endGlobalOffset:rangeParams.startGlobalOffset + new_char.length
|
|
2231
|
+
}
|
|
2232
|
+
} else { // replace old char
|
|
2233
|
+
new_raw_text = removeMarks(replaceCharacterBeforeMarks(raw_text, new_char));
|
|
2234
|
+
new_rangeParams = { ...rangeParams };
|
|
2235
|
+
}
|
|
2243
2236
|
|
|
2244
2237
|
new_raw_text = removeEncodingIfPlainText(
|
|
2245
2238
|
new_raw_text,
|
|
2246
2239
|
this.props.mimeType,
|
|
2247
2240
|
this.enableHtml()
|
|
2248
2241
|
);
|
|
2242
|
+
|
|
2249
2243
|
this.applyChangesToComponent(
|
|
2250
2244
|
new_raw_text,
|
|
2251
2245
|
this.props.mimeType,
|
|
@@ -729,6 +729,7 @@ function findNodeAndOffsetForGlobalOffset(node, globalOffset, result=null) {
|
|
|
729
729
|
}
|
|
730
730
|
if (isTextNode(node)) {
|
|
731
731
|
result.node = node
|
|
732
|
+
result.lastTextNode = node
|
|
732
733
|
result.offset += node.nodeValue.length
|
|
733
734
|
if (nodeStartsWithSmallSpace(node))
|
|
734
735
|
result.offset -= 1
|
|
@@ -859,13 +860,35 @@ function setRangeParamsFromGlobalOffsets(editableDiv, params) {
|
|
|
859
860
|
findNodeAndOffsetForGlobalOffset(editableDiv, params.endGlobalOffset, endResult)
|
|
860
861
|
// console.log(endResult.node.nodeType+":"+endResult.node.tagName+":"+
|
|
861
862
|
// endResult.node.nodeValue+":"+endResult.offset)
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
863
|
+
|
|
864
|
+
// So the next bit of code should never be called in principle, but if we have
|
|
865
|
+
// failed to find an appropriate node (this could happen if the global offset is bigger
|
|
866
|
+
// than the length of the text in the node), then lets try and fix it
|
|
867
|
+
if (startResult.node === null || endResult.node === null) {
|
|
868
|
+
console.warn("Can't set range from global offsets, as could not find text nodes")
|
|
865
869
|
console.log(startResult)
|
|
866
|
-
console.log(endResult)
|
|
867
|
-
|
|
870
|
+
console.log(endResult)
|
|
871
|
+
|
|
872
|
+
if (startResult.node === null) {
|
|
873
|
+
if (params.startGlobalOffset > 0 && startResult.lastTextNode !== null) {
|
|
874
|
+
startResult.node = startResult.lastTextNode
|
|
875
|
+
startResult.offset = startResult.lastTextNode.nodeValue.length
|
|
876
|
+
} else {
|
|
877
|
+
startResult.node = findFirstTextNode(editableDiv)
|
|
878
|
+
startResult.offset = 0
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
if (endResult.node === null) {
|
|
882
|
+
if (params.endGlobalOffset > 0 && endResult.lastTextNode !== null) {
|
|
883
|
+
endResult.node = endResult.lastTextNode
|
|
884
|
+
endResult.offset = endResult.lastTextNode.nodeValue.length
|
|
885
|
+
} else {
|
|
886
|
+
endResult.node = findFirstTextNode(editableDiv)
|
|
887
|
+
endResult.offset = 0
|
|
888
|
+
}
|
|
889
|
+
}
|
|
868
890
|
}
|
|
891
|
+
|
|
869
892
|
if (startResult.offset === 0 &&
|
|
870
893
|
startResult.node.nodeValue.length > 0 &&
|
|
871
894
|
nodeStartsWithSmallSpace(startResult.node)) {
|