@zzish/math-rich-input 0.1.28 → 0.1.29

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 CHANGED
@@ -21352,9 +21352,16 @@ function findNodeAndOffsetForGlobalOffset(node, globalOffset) {
21352
21352
  if (isTextNode(node)) {
21353
21353
  result.node = node;
21354
21354
  result.lastTextNode = node;
21355
+ console.log("Found text node \'" + node.nodeValue + "\' of length " + node.nodeValue.length);
21356
+ console.log("result.offset was: " + result.offset);
21355
21357
  result.offset += node.nodeValue.length;
21356
- if (nodeStartsWithSmallSpace(node)) result.offset -= 1; // console.log("result.offset now: "+result.offset)
21357
21358
 
21359
+ if (nodeStartsWithSmallSpace(node)) {
21360
+ console.log("Text node starts with small space");
21361
+ result.offset -= 1;
21362
+ }
21363
+
21364
+ console.log("result.offset now: " + result.offset);
21358
21365
  return false;
21359
21366
  }
21360
21367
 
@@ -21374,8 +21381,10 @@ function findNodeAndOffsetForGlobalOffset(node, globalOffset) {
21374
21381
  }
21375
21382
 
21376
21383
  if (node.nodeType === 1 && node.tagName === "P") {
21384
+ console.log("Leaving p node");
21377
21385
  result.node = null;
21378
21386
  result.offset++;
21387
+ console.log("result.offset now: " + result.offset);
21379
21388
  }
21380
21389
 
21381
21390
  return false;
@@ -21397,6 +21406,7 @@ function findNodeAndOffsetForGlobalOffsetInEditableDiv(editableDiv, globalOffset
21397
21406
  var result = {
21398
21407
  offset: 0
21399
21408
  };
21409
+ console.log("Finding node and offset within: " + editableDiv.outerHTML);
21400
21410
  findNodeAndOffsetForGlobalOffset(editableDiv, globalOffset, result);
21401
21411
  console.log(result);
21402
21412
 
@@ -21411,20 +21421,28 @@ function findNodeAndOffsetForGlobalOffsetInEditableDiv(editableDiv, globalOffset
21411
21421
  result.node = findFirstTextNode(editableDiv);
21412
21422
  result.offset = 0;
21413
21423
  }
21414
- } // if (result.offset === 0 &&
21415
- // result.node.nodeValue.length > 0 &&
21416
- // nodeStartsWithSmallSpace(result.node)) {
21417
- // result.offset = 1
21418
- // }
21424
+ }
21425
+
21426
+ if (result.offset === 0 && result.node.nodeValue.length > 0 && nodeStartsWithSmallSpace(result.node)) {
21427
+ result.offset = 1;
21428
+ }
21419
21429
 
21430
+ if (result.offset > result.node.nodeValue.length) {
21431
+ console.warn("Unexpected offset when converting global offset to node and offset");
21432
+ console.log("Global offset: " + globalOffset);
21433
+ console.log("Result:");
21434
+ console.log(result);
21435
+ console.log("result.offset > result.node.nodeValue.length");
21436
+ result.offset = result.node.nodeValue.length;
21437
+ }
21420
21438
 
21421
21439
  return result;
21422
21440
  } // https://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html
21423
21441
 
21424
21442
 
21425
21443
  function getRangeParams(editableDiv) {
21426
- // console.log ("getRangeParams")
21427
- // console.log("Inner html: "+editableDiv.innerHTML)
21444
+ console.log("getRangeParams"); // console.log("Inner html: "+editableDiv.innerHTML)
21445
+
21428
21446
  var isSupported = typeof window.getSelection !== "undefined";
21429
21447
 
21430
21448
  if (!isSupported) {
@@ -21543,17 +21561,18 @@ function setRangeParams(editableDiv, params) {
21543
21561
 
21544
21562
 
21545
21563
  if (params.startGlobalOffset !== null && params.startGlobalOffset !== undefined) {
21564
+ console.log("Setting range from global offsets");
21546
21565
  setRangeParamsFromGlobalOffsets(editableDiv, params);
21547
21566
  return;
21548
21567
  } // Otherwise use the old way of doing things
21549
21568
 
21550
21569
 
21570
+ console.log("Setting range from node index and local offset");
21571
+
21551
21572
  if (params.startNodeIndex === null || params.startNodeIndex === undefined) {
21552
21573
  console.error("Can't set range params: params.startNodeIndex === " + params.startNodeIndex);
21553
21574
  return;
21554
- } // console.log("Setting cursor to range params: "+params.startNodeIndex+"->"+params.startOffset)
21555
- // console.log(params)
21556
-
21575
+ }
21557
21576
 
21558
21577
  if (params.startNodeIndex < 0) {
21559
21578
  console.error("Can't set range params: params.startNodeIndex < 0");
@@ -21563,34 +21582,32 @@ function setRangeParams(editableDiv, params) {
21563
21582
  var nodes = editableDiv.childNodes;
21564
21583
 
21565
21584
  if (params.startNodeIndex === 0 && params.startOffset === 0 && nodes.length === 0) {
21566
- // console.log("Not setting range params")
21567
21585
  return;
21568
- } // console.log("Node["+params.startNodeIndex+"]:")
21569
- // console.log(nodes[params.startNodeIndex])
21586
+ } // Set the offsets within the nodes
21570
21587
 
21571
21588
 
21572
21589
  var startOffset = params.startOffset;
21573
21590
  var endOffset = params.endOffset; // Handle special case of auto inserted SMALL CHAR at beginning of input
21591
+ // TODO This sectin may no longer be neceesary and may be introducing a bug
21574
21592
 
21575
21593
  if (params.startNodeIndex === 0 && params.startOffset === 1 && isSmallSpace(nodes[0])) startOffset = 0;
21576
- if (params.endNodeIndex === 0 && params.endOffset === 1 && isSmallSpace(nodes[0])) endOffset = 0;
21594
+ if (params.endNodeIndex === 0 && params.endOffset === 1 && isSmallSpace(nodes[0])) endOffset = 0; // Find the nodes
21595
+
21577
21596
  var startNode = findNodeWithIndex(editableDiv, params.startNodeIndex);
21578
- var endNode = findNodeWithIndex(editableDiv, params.endNodeIndex); // Lets make sure we are setting
21579
- // if (params.startNodeIndex !== 0 &&
21580
- // params.startOffset === 0) {
21581
- // if (getNodeText(startNode).charAt(0) === SMALL_SPACE)
21582
- // startOffset = SMALL_SPACE.length
21583
- // }
21584
- // if (params.endNodeIndex !== 0 &&
21585
- // params.endOffset === 0) {
21586
- // if (getNodeText(endNode).charAt(0) === SMALL_SPACE)
21587
- // endOffset = SMALL_SPACE.length
21588
- // }
21589
- // console.log("End: "+params.endNodeIndex+"->"+endOffset)
21590
- // Set range in document
21597
+ var endNode = findNodeWithIndex(editableDiv, params.endNodeIndex);
21598
+ console.log("Setting range to start in node:");
21599
+ console.log(" node type: " + startNode.nodeType);
21600
+ console.log(" tag name: " + startNode.tagName);
21601
+ console.log(" node value: " + startNode.nodeValue);
21602
+ console.log(" offset:" + startOffset);
21603
+ console.log("Setting range to end in node:");
21604
+ console.log(" node type: " + endNode.nodeType);
21605
+ console.log(" tag name: " + endNode.tagName);
21606
+ console.log(" node value: " + endNode.nodeValue);
21607
+ console.log(" offset:" + endOffset); // Set range in document
21591
21608
 
21592
21609
  if (startNode === null) {
21593
- console.error("Could not set range to start node with index " + params.startNodeIndex);
21610
+ console.warn("Could not set range to start node with index " + params.startNodeIndex);
21594
21611
  startNode = findFirstTextNode(editableDiv);
21595
21612
  startOffset = 0;
21596
21613
  }
@@ -21606,7 +21623,7 @@ function setRangeParams(editableDiv, params) {
21606
21623
  }
21607
21624
 
21608
21625
  if (endNode === null) {
21609
- console.error("Could not set range to end node with index " + params.endNodeIndex);
21626
+ console.warn("Could not set range to end node with index " + params.endNodeIndex);
21610
21627
  endNode = findFirstTextNode(editableDiv);
21611
21628
  endOffset = 0;
21612
21629
  }
@@ -21622,11 +21639,17 @@ function setRangeParams(editableDiv, params) {
21622
21639
  }
21623
21640
 
21624
21641
  new_range = document.createRange();
21642
+
21643
+ if (startOffset > startNode.nodeValue.length) {
21644
+ console.warn("endOffset > endNode.nodeValue.length when setting range: " + startOffset + " > " + startNode.nodeValue.length + " - " + stateNode.nodeType + " - " + startNode.nodeValue);
21645
+ startOffset = startNode.nodeValue.length;
21646
+ }
21647
+
21625
21648
  new_range.setStart(startNode, startOffset);
21626
21649
 
21627
- if (endOffset > endNode.length) {
21628
- console.warn("endOffset > endNode.length when setting range: " + endOffset + " > " + endNode.length + " - " + endNode.nodeType + " - " + endNode.nodeValue);
21629
- endOffset = endNode.length;
21650
+ if (endOffset > endNode.nodeValue.length) {
21651
+ console.warn("endOffset > endNode.nodeValue.length when setting range: " + endOffset + " > " + endNode.nodeValue.length + " - " + endNode.nodeType + " - " + endNode.nodeValue);
21652
+ endOffset = endNode.nodeValue.length;
21630
21653
  }
21631
21654
 
21632
21655
  new_range.setEnd(endNode, endOffset); // console.log("Setting range")
@@ -22187,17 +22210,27 @@ var MathRichInput = /*#__PURE__*/function (_React$Component) {
22187
22210
  rangeParams.startOffset;
22188
22211
  console.log("handleKeyDownArrowLeft");
22189
22212
  console.log(rangeParams);
22190
- var new_globalOffset = rangeParams.startGlobalOffset - 1;
22191
- var new_rangeParams = {
22192
- startGlobalOffset: new_globalOffset,
22193
- endGlobalOffset: new_globalOffset
22194
- };
22213
+ var new_globalOffset = rangeParams.endGlobalOffset - 1;
22214
+ var new_rangeParams = null;
22215
+
22216
+ if (e.shiftKey && new_globalOffset > rangeParams.startGlobalOffset) {
22217
+ new_rangeParams = {
22218
+ startGlobalOffset: rangeParams.startGlobalOffset,
22219
+ endGlobalOffset: new_globalOffset
22220
+ };
22221
+ } else {
22222
+ new_rangeParams = {
22223
+ startGlobalOffset: new_globalOffset,
22224
+ endGlobalOffset: new_globalOffset
22225
+ };
22226
+ }
22227
+
22195
22228
  console.log(new_rangeParams);
22196
22229
  e.preventDefault();
22197
22230
 
22198
22231
  _this2._setRangeParams(new_rangeParams);
22199
22232
 
22200
- return; // console.log("Old: "+index+"->"+offset)
22233
+ return;
22201
22234
  });
22202
22235
 
22203
22236
  _defineProperty(_assertThisInitialized(_this2), "handleKeyDownArrowRight", function (e) {
@@ -22206,11 +22239,21 @@ var MathRichInput = /*#__PURE__*/function (_React$Component) {
22206
22239
 
22207
22240
  rangeParams.startNodeIndex;
22208
22241
  rangeParams.startOffset;
22209
- var new_globalOffset = rangeParams.startGlobalOffset + 1;
22210
- var new_rangeParams = {
22211
- startGlobalOffset: new_globalOffset,
22212
- endGlobalOffset: new_globalOffset
22213
- };
22242
+ var new_rangeParams = null;
22243
+ var new_globalOffset = rangeParams.endGlobalOffset + 1;
22244
+
22245
+ if (e.shiftKey && new_globalOffset > rangeParams.startGlobalOffset) {
22246
+ new_rangeParams = {
22247
+ startGlobalOffset: rangeParams.startGlobalOffset,
22248
+ endGlobalOffset: new_globalOffset
22249
+ };
22250
+ } else {
22251
+ new_rangeParams = {
22252
+ startGlobalOffset: new_globalOffset,
22253
+ endGlobalOffset: new_globalOffset
22254
+ };
22255
+ }
22256
+
22214
22257
  e.preventDefault();
22215
22258
 
22216
22259
  _this2._setRangeParams(new_rangeParams);
@@ -22630,30 +22673,30 @@ var MathRichInput = /*#__PURE__*/function (_React$Component) {
22630
22673
 
22631
22674
  if (_index >= 0) {
22632
22675
  if (isKatexNode(_this2._findNodeWithIndex(_index))) {
22633
- var _new_index4 = 0;
22634
- var _new_offset4 = 0;
22676
+ var _new_index = 0;
22677
+ var _new_offset = 0;
22635
22678
 
22636
22679
  if (_index - 1 >= 0) {
22637
- _new_index4 = _index - 1;
22638
- _new_offset4 = getNodeTextLength(_this2._findNodeWithIndex(_new_index4));
22680
+ _new_index = _index - 1;
22681
+ _new_offset = getNodeTextLength(_this2._findNodeWithIndex(_new_index));
22639
22682
 
22640
- if (_new_offset4 < 0) {
22683
+ if (_new_offset < 0) {
22641
22684
  console.error("Failed to get node text length");
22642
- _new_offset4 = 0;
22685
+ _new_offset = 0;
22643
22686
  }
22644
22687
  } else {
22645
- _new_index4 = _index + 1;
22646
- _new_offset4 = SMALL_SPACE_LENGTH;
22688
+ _new_index = _index + 1;
22689
+ _new_offset = SMALL_SPACE_LENGTH;
22647
22690
  }
22648
22691
 
22649
- var _new_rangeParams7 = {
22650
- startNodeIndex: _new_index4,
22651
- startOffset: _new_offset4,
22652
- endNodeIndex: _new_index4,
22653
- endOffset: _new_offset4
22692
+ var _new_rangeParams2 = {
22693
+ startNodeIndex: _new_index,
22694
+ startOffset: _new_offset,
22695
+ endNodeIndex: _new_index,
22696
+ endOffset: _new_offset
22654
22697
  }; // console.log(new_rangeParams)
22655
22698
 
22656
- _this2._setRangeParams(_new_rangeParams7);
22699
+ _this2._setRangeParams(_new_rangeParams2);
22657
22700
  }
22658
22701
  }
22659
22702
  }
@@ -22690,6 +22733,8 @@ var MathRichInput = /*#__PURE__*/function (_React$Component) {
22690
22733
 
22691
22734
  _defineProperty(_assertThisInitialized(_this2), "updateActiveButtons", function () {
22692
22735
  try {
22736
+ console.log("updateActiveButtons");
22737
+
22693
22738
  var rangeParams = _this2._getRangeParams();
22694
22739
 
22695
22740
  if (rangeParams === null || rangeParams === undefined) {
@@ -23000,14 +23045,14 @@ var MathRichInput = /*#__PURE__*/function (_React$Component) {
23000
23045
  }
23001
23046
  }
23002
23047
 
23003
- var _new_rangeParams8 = {
23048
+ var _new_rangeParams3 = {
23004
23049
  startNodeIndex: new_index,
23005
23050
  startOffset: new_offset,
23006
23051
  endNodeIndex: new_index,
23007
23052
  endOffset: new_offset
23008
23053
  };
23009
23054
 
23010
- _this2.setOldRangeParams(_new_rangeParams8);
23055
+ _this2.setOldRangeParams(_new_rangeParams3);
23011
23056
 
23012
23057
  var _nodeCounts = _this2._countNodeTypes();
23013
23058
 
@@ -23015,7 +23060,7 @@ var MathRichInput = /*#__PURE__*/function (_React$Component) {
23015
23060
  if (_this2.editingExistingEquation) _mimeType = _this2.getMimeType(_nodeCounts.html > 0, _nodeCounts.katex > 1);else _mimeType = _this2.getMimeType(_nodeCounts.html > 0, _nodeCounts.katex > 0);
23016
23061
  _new_raw_text = removeEncodingIfPlainText(_new_raw_text, _mimeType, _this2.enableHtml());
23017
23062
 
23018
- _this2.applyChangesToComponent(_new_raw_text, _mimeType, _new_rangeParams8, _this2.props.useExpertMode, _this2.props.selectedTab);
23063
+ _this2.applyChangesToComponent(_new_raw_text, _mimeType, _new_rangeParams3, _this2.props.useExpertMode, _this2.props.selectedTab);
23019
23064
 
23020
23065
  _this2.setState({
23021
23066
  showEquationEditor: false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zzish/math-rich-input",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "React component for user to enter rich text with embedded math equations.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -592,110 +592,23 @@ export default class MathRichInput extends React.Component {
592
592
 
593
593
  console.log("handleKeyDownArrowLeft")
594
594
  console.log(rangeParams)
595
- let new_globalOffset = rangeParams.startGlobalOffset - 1;
596
-
597
- let new_rangeParams = {
598
- startGlobalOffset: new_globalOffset,
599
- endGlobalOffset: new_globalOffset,
600
- };
601
- console.log(new_rangeParams)
602
- e.preventDefault();
603
- this._setRangeParams(new_rangeParams);
604
- return;
605
-
606
- // console.log("Old: "+index+"->"+offset)
607
-
608
- // The user shouldn't be in a katex node, but lets deal with it anyway
609
- let node = this._findNodeWithIndex(index);
610
- if (isKatexNode(node)) {
611
- console.warn("Cursor is inside katex node");
612
- let new_index = index - 1; // Jump out of the latex node
613
- let prev_node = this._findNodeWithIndex(index - 1);
614
- let new_offset = getNodeTextLength(prev_node);
615
-
616
- let new_rangeParams = {
617
- startNodeIndex: new_index,
618
- startOffset: new_offset,
619
- endNodeIndex: new_index,
620
- endOffset: new_offset,
621
- startGlobalOffset: new_globalOffset,
622
- endGlobalOffset: new_globalOffset,
623
- };
624
- this._setRangeParams(new_rangeParams);
625
- e.preventDefault();
626
- return;
627
- }
628
-
629
- if (offset > 2) return;
630
-
631
- let new_index = -1;
632
- let new_offset = -1;
633
-
634
- // Deal with case of inserted small space at start of <p> element or after katex node
635
- if (offset === 2) {
636
- new_index = index;
637
- if (this.nodeStartsWithSmallSpace(node)) {
638
- if (index > 0 && isKatexNode(this._findNodeWithIndex(index - 1)))
639
- new_offset = 1;
640
- else new_offset = 0;
641
- } else new_offset = 1;
642
- }
643
-
644
- if (offset === 1) {
645
- if (this.nodeStartsWithSmallSpace(node)) {
646
- if (index === 0) {
647
- e.preventDefault();
648
- return;
649
- }
650
- let prev_node = this._findNodeWithIndex(index - 1);
651
- if (index >= 2 && isKatexNode(prev_node)) new_index = index - 2;
652
- else new_index = index - 1;
653
-
654
- let new_node = this._findNodeWithIndex(new_index);
655
- new_offset = getNodeTextLength(new_node);
656
- } else {
657
- new_index = index;
658
- new_offset = 0;
659
- }
660
- }
661
-
662
- if (offset === 0) {
663
- let prev_node = this._findNodeWithIndex(index - 1);
664
- if (index >= 2 && isKatexNode(prev_node)) new_index = index - 2;
665
- else new_index = index - 1;
666
- let new_node = this._findNodeWithIndex(new_index);
667
- new_offset = getNodeTextLength(new_node);
668
- if (new_offset === 1) {
669
- if (this.nodeStartsWithSmallSpace(new_node)) {
670
- if (index > 0 && isKatexNode(this._findNodeWithIndex(index - 1)))
671
- new_offset = 1;
672
- else new_offset = 0;
595
+ let new_globalOffset = rangeParams.endGlobalOffset - 1;
596
+ let new_rangeParams = null
597
+ if (e.shiftKey && new_globalOffset > rangeParams.startGlobalOffset) {
598
+ new_rangeParams = {
599
+ startGlobalOffset: rangeParams.startGlobalOffset,
600
+ endGlobalOffset: new_globalOffset,
673
601
  }
674
- }
675
- }
676
-
677
- // Move cursor to start of field if in starting small space
678
- if (
679
- new_index === 0 &&
680
- new_offset === 1 &&
681
- this.inputStartsWithSmallSpace()
682
- ) {
683
- new_offset = 0;
684
- }
685
-
686
- if (new_index === -1 || new_offset === -1) return;
687
- else {
688
- let new_rangeParams = {
689
- startNodeIndex: new_index,
690
- startOffset: new_offset,
691
- endNodeIndex: new_index,
692
- endOffset: new_offset,
602
+ } else {
603
+ new_rangeParams = {
693
604
  startGlobalOffset: new_globalOffset,
694
605
  endGlobalOffset: new_globalOffset,
695
606
  };
696
- this._setRangeParams(new_rangeParams);
697
- e.preventDefault();
698
607
  }
608
+ console.log(new_rangeParams)
609
+ e.preventDefault();
610
+ this._setRangeParams(new_rangeParams);
611
+ return;
699
612
  };
700
613
 
701
614
  handleKeyDownArrowRight = (e) => {
@@ -704,103 +617,22 @@ export default class MathRichInput extends React.Component {
704
617
  let index = rangeParams.startNodeIndex;
705
618
  let offset = rangeParams.startOffset;
706
619
 
707
- let new_globalOffset = rangeParams.startGlobalOffset + 1;
708
- let new_rangeParams = {
709
- startGlobalOffset: new_globalOffset,
710
- endGlobalOffset: new_globalOffset,
711
- };
712
- e.preventDefault();
713
- this._setRangeParams(new_rangeParams);
714
- return;
715
-
716
- if (index < 0)
717
- // Empty input field
718
- return;
719
-
720
- let node = this._findNodeWithIndex(index);
721
- let length = getNodeTextLength(node);
722
-
723
- // The user shouldn't be in a katex node, but lets deal with it anyway
724
- if (isKatexNode(node)) {
725
- console.warn("Cursor is inside katex node");
726
- let new_index = index + 1; // Jump out of the latex node
727
- let new_offset = SMALL_SPACE_LENGTH; // As there will be a SMALL_SPACE at the start of the text node
728
-
729
- let new_rangeParams = {
730
- startNodeIndex: new_index,
731
- startOffset: new_offset,
732
- endNodeIndex: new_index,
733
- endOffset: new_offset,
620
+ let new_rangeParams = null
621
+ let new_globalOffset = rangeParams.endGlobalOffset + 1;
622
+ if (e.shiftKey && new_globalOffset > rangeParams.startGlobalOffset) {
623
+ new_rangeParams = {
624
+ startGlobalOffset: rangeParams.startGlobalOffset,
625
+ endGlobalOffset: new_globalOffset,
734
626
  };
735
- this._setRangeParams(new_rangeParams);
736
- e.preventDefault();
737
- return;
738
- }
739
-
740
- // Deal with case of inserted small space at start of <p> element
741
- if (offset === 0) {
742
- if (this.nodeStartsWithSmallSpace(node)) {
743
- // console.log("Offset = 0 and node starts with small space");
744
- let new_index = -1;
745
- let new_offset = -1;
746
-
747
- if (node.nodeValue.length > 1) {
748
- new_index = index;
749
- new_offset = SMALL_SPACE_LENGTH + 1;
750
- } else {
751
- new_index = index + 1;
752
- let next_node = this._findNodeWithIndex(new_index);
753
- if (next_node !== null) {
754
- if (isKatexNode(next_node)) {
755
- new_index = index + 2;
756
- new_offset = SMALL_SPACE_LENGTH;
757
- } else {
758
- new_index = index + 1;
759
- new_offset = 0;
760
- }
761
- } else {
762
- new_index = index;
763
- new_offset = offset;
764
- }
765
- }
766
- let new_rangeParams = {
767
- startNodeIndex: new_index,
768
- startOffset: new_offset,
769
- endNodeIndex: new_index,
770
- endOffset: new_offset,
771
- };
772
- this._setRangeParams(new_rangeParams);
773
-
774
- e.preventDefault();
775
- return;
776
- }
777
- }
778
-
779
- // If at the end of a text node
780
- if (
781
- offset === length ||
782
- (index === 0 && this.inputStartsWithSmallSpace())
783
- ) {
784
- let next_node = this._findNodeWithIndex(index + 1);
785
-
786
- // If there is no next node, then we can't move, so do the default action
787
- // If the next node is a normal node, do the default action
788
- if (next_node === null || !isKatexNode(next_node)) return;
789
-
790
- let new_index = index + 2; // Jump over the latex node
791
- let new_offset = SMALL_SPACE_LENGTH; // As there will be a SMALL_SPACE at the start of the text node
792
-
793
- let new_rangeParams = {
794
- startNodeIndex: new_index,
795
- startOffset: new_offset,
796
- endNodeIndex: new_index,
797
- endOffset: new_offset,
627
+ } else {
628
+ new_rangeParams = {
629
+ startGlobalOffset: new_globalOffset,
630
+ endGlobalOffset: new_globalOffset,
798
631
  };
799
- this._setRangeParams(new_rangeParams);
800
-
801
- e.preventDefault();
802
- return;
803
632
  }
633
+ e.preventDefault();
634
+ this._setRangeParams(new_rangeParams);
635
+ return;
804
636
  };
805
637
 
806
638
  editableDivIsPlainText = () => {
@@ -1357,6 +1189,7 @@ export default class MathRichInput extends React.Component {
1357
1189
 
1358
1190
  updateActiveButtons = () => {
1359
1191
  try {
1192
+ console.log("updateActiveButtons")
1360
1193
  let rangeParams = this._getRangeParams();
1361
1194
  if (rangeParams === null || rangeParams === undefined) {
1362
1195
  console.warn(
@@ -79,10 +79,10 @@ function isBrNode(node) {
79
79
  }
80
80
 
81
81
  function nodeStartsWithSmallSpace(node) {
82
- if (node.nodeType === 3 &&
83
- node.nodeValue.length >= 1 &&
84
- node.nodeValue.charCodeAt(0) === SMALL_SPACE_CHAR_CODE)
85
- return true;
82
+ if (node.nodeType === 3 &&
83
+ node.nodeValue.length >= 1 &&
84
+ node.nodeValue.charCodeAt(0) === SMALL_SPACE_CHAR_CODE)
85
+ return true;
86
86
  return false;
87
87
  }
88
88
 
@@ -722,7 +722,7 @@ function findGlobalOffset(node, nodeToFind, offset, result=null) {
722
722
  result.offset ++
723
723
  }
724
724
 
725
- return false;
725
+ return false;
726
726
  }
727
727
 
728
728
  function findNodeAndOffsetForGlobalOffset(node, globalOffset, result=null) {
@@ -734,10 +734,14 @@ function findNodeAndOffsetForGlobalOffset(node, globalOffset, result=null) {
734
734
  if (isTextNode(node)) {
735
735
  result.node = node
736
736
  result.lastTextNode = node
737
+ console.log("Found text node \'"+node.nodeValue+"\' of length "+node.nodeValue.length)
738
+ console.log("result.offset was: "+result.offset)
737
739
  result.offset += node.nodeValue.length
738
- if (nodeStartsWithSmallSpace(node))
740
+ if (nodeStartsWithSmallSpace(node)) {
741
+ console.log("Text node starts with small space")
739
742
  result.offset -= 1
740
- // console.log("result.offset now: "+result.offset)
743
+ }
744
+ console.log("result.offset now: "+result.offset)
741
745
  return false
742
746
  }
743
747
  const childNodes = node.childNodes
@@ -758,8 +762,10 @@ function findNodeAndOffsetForGlobalOffset(node, globalOffset, result=null) {
758
762
  }
759
763
 
760
764
  if (node.nodeType === 1 && node.tagName === "P") {
765
+ console.log("Leaving p node")
761
766
  result.node = null
762
767
  result.offset ++
768
+ console.log("result.offset now: "+result.offset)
763
769
  }
764
770
  return false;
765
771
  }
@@ -780,6 +786,7 @@ export function findFirstTextNode(node) {
780
786
 
781
787
  function findNodeAndOffsetForGlobalOffsetInEditableDiv(editableDiv, globalOffset) {
782
788
  let result = {offset:0}
789
+ console.log("Finding node and offset within: "+editableDiv.outerHTML)
783
790
  findNodeAndOffsetForGlobalOffset(editableDiv, globalOffset, result)
784
791
  console.log(result)
785
792
 
@@ -796,18 +803,27 @@ function findNodeAndOffsetForGlobalOffsetInEditableDiv(editableDiv, globalOffset
796
803
  }
797
804
  }
798
805
 
799
- // if (result.offset === 0 &&
800
- // result.node.nodeValue.length > 0 &&
801
- // nodeStartsWithSmallSpace(result.node)) {
802
- // result.offset = 1
803
- // }
806
+ if (result.offset === 0 &&
807
+ result.node.nodeValue.length > 0 &&
808
+ nodeStartsWithSmallSpace(result.node)) {
809
+ result.offset = 1
810
+ }
811
+
812
+ if (result.offset > result.node.nodeValue.length) {
813
+ console.warn("Unexpected offset when converting global offset to node and offset")
814
+ console.log("Global offset: "+globalOffset)
815
+ console.log("Result:")
816
+ console.log(result)
817
+ console.log("result.offset > result.node.nodeValue.length")
818
+ result.offset = result.node.nodeValue.length
819
+ }
804
820
 
805
821
  return result
806
822
  }
807
823
 
808
824
  // https://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html
809
825
  export function getRangeParams(editableDiv) {
810
- // console.log ("getRangeParams")
826
+ console.log ("getRangeParams")
811
827
  // console.log("Inner html: "+editableDiv.innerHTML)
812
828
  const isSupported = typeof window.getSelection !== "undefined";
813
829
  if (!isSupported) {
@@ -822,6 +838,7 @@ export function getRangeParams(editableDiv) {
822
838
  }
823
839
 
824
840
  const range = selection.getRangeAt(0);
841
+
825
842
  // logAllDescendants(editableDiv)
826
843
  // If the range is not within a text node, move it to a text node
827
844
  // This typically happens when the end of a range is at the start of a new line
@@ -927,38 +944,34 @@ export function setRangeParams(editableDiv, params) {
927
944
 
928
945
  // New way of doing things
929
946
  if (params.startGlobalOffset !== null && params.startGlobalOffset !== undefined) {
947
+ console.log("Setting range from global offsets")
930
948
  setRangeParamsFromGlobalOffsets(editableDiv, params)
931
949
  return
932
950
  }
933
951
 
934
952
  // Otherwise use the old way of doing things
953
+ console.log("Setting range from node index and local offset")
935
954
 
936
955
  if (params.startNodeIndex === null || params.startNodeIndex === undefined) {
937
956
  console.error("Can't set range params: params.startNodeIndex === "+params.startNodeIndex)
938
957
  return
939
958
  }
940
-
941
- // console.log("Setting cursor to range params: "+params.startNodeIndex+"->"+params.startOffset)
942
- // console.log(params)
943
-
944
959
  if (params.startNodeIndex < 0) {
945
960
  console.error("Can't set range params: params.startNodeIndex < 0")
946
961
  return
947
962
  }
948
963
 
949
-
950
964
  let nodes = editableDiv.childNodes
951
965
  if (params.startNodeIndex === 0 && params.startOffset === 0 && nodes.length === 0) {
952
- // console.log("Not setting range params")
953
966
  return
954
967
  }
955
968
 
956
- // console.log("Node["+params.startNodeIndex+"]:")
957
- // console.log(nodes[params.startNodeIndex])
969
+ // Set the offsets within the nodes
958
970
  let startOffset = params.startOffset
959
971
  let endOffset = params.endOffset
960
972
 
961
973
  // Handle special case of auto inserted SMALL CHAR at beginning of input
974
+ // TODO This sectin may no longer be neceesary and may be introducing a bug
962
975
  if (params.startNodeIndex === 0 &&
963
976
  params.startOffset === 1 &&
964
977
  isSmallSpace(nodes[0]))
@@ -969,25 +982,26 @@ export function setRangeParams(editableDiv, params) {
969
982
  isSmallSpace(nodes[0]))
970
983
  endOffset = 0
971
984
 
985
+
986
+ // Find the nodes
972
987
  let startNode = findNodeWithIndex(editableDiv, params.startNodeIndex)
973
988
  let endNode = findNodeWithIndex(editableDiv, params.endNodeIndex)
974
989
 
975
- // Lets make sure we are setting
976
- // if (params.startNodeIndex !== 0 &&
977
- // params.startOffset === 0) {
978
- // if (getNodeText(startNode).charAt(0) === SMALL_SPACE)
979
- // startOffset = SMALL_SPACE.length
980
- // }
981
- // if (params.endNodeIndex !== 0 &&
982
- // params.endOffset === 0) {
983
- // if (getNodeText(endNode).charAt(0) === SMALL_SPACE)
984
- // endOffset = SMALL_SPACE.length
985
- // }
986
- // console.log("End: "+params.endNodeIndex+"->"+endOffset)
990
+ console.log("Setting range to start in node:")
991
+ console.log(" node type: "+startNode.nodeType)
992
+ console.log(" tag name: "+startNode.tagName)
993
+ console.log(" node value: "+startNode.nodeValue)
994
+ console.log(" offset:"+startOffset)
995
+
996
+ console.log("Setting range to end in node:")
997
+ console.log(" node type: "+endNode.nodeType)
998
+ console.log(" tag name: "+endNode.tagName)
999
+ console.log(" node value: "+endNode.nodeValue)
1000
+ console.log(" offset:"+endOffset)
987
1001
 
988
1002
  // Set range in document
989
1003
  if (startNode === null) {
990
- console.error("Could not set range to start node with index "+params.startNodeIndex)
1004
+ console.warn("Could not set range to start node with index "+params.startNodeIndex)
991
1005
  startNode = findFirstTextNode(editableDiv)
992
1006
  startOffset = 0
993
1007
  }
@@ -1004,7 +1018,7 @@ export function setRangeParams(editableDiv, params) {
1004
1018
  }
1005
1019
 
1006
1020
  if (endNode === null) {
1007
- console.error("Could not set range to end node with index "+params.endNodeIndex)
1021
+ console.warn("Could not set range to end node with index "+params.endNodeIndex)
1008
1022
  endNode = findFirstTextNode(editableDiv)
1009
1023
  endOffset = 0
1010
1024
  }
@@ -1021,13 +1035,21 @@ export function setRangeParams(editableDiv, params) {
1021
1035
  }
1022
1036
 
1023
1037
  new_range = document.createRange();
1038
+ if (startOffset > startNode.nodeValue.length) {
1039
+ console.warn("endOffset > endNode.nodeValue.length when setting range: "+
1040
+ startOffset+ " > "+startNode.nodeValue.length+
1041
+ " - "+stateNode.nodeType+
1042
+ " - "+startNode.nodeValue)
1043
+ startOffset = startNode.nodeValue.length
1044
+ }
1045
+
1024
1046
  new_range.setStart(startNode, startOffset);
1025
- if (endOffset > endNode.length) {
1026
- console.warn("endOffset > endNode.length when setting range: "+
1027
- endOffset+ " > "+endNode.length+
1047
+ if (endOffset > endNode.nodeValue.length) {
1048
+ console.warn("endOffset > endNode.nodeValue.length when setting range: "+
1049
+ endOffset+ " > "+endNode.nodeValue.length+
1028
1050
  " - "+endNode.nodeType+
1029
1051
  " - "+endNode.nodeValue)
1030
- endOffset = endNode.length
1052
+ endOffset = endNode.nodeValue.length
1031
1053
  }
1032
1054
  new_range.setEnd(endNode, endOffset);
1033
1055