@zzish/math-rich-input 0.1.27 → 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.
@@ -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
 
@@ -360,7 +360,7 @@ function addTextForNode(node, arrayOfText, nodeToMark=null, offset=0, useHtmlMat
360
360
  if (mark)
361
361
  arrayOfText.push(MARK)
362
362
  } else {
363
- console.error("Cant render node in editable div: Unknown node type")
363
+ console.error("Can't render node in editable div: Unknown node type")
364
364
  console.log(node)
365
365
  console.log("Node.type:"+node.type)
366
366
  console.log("Node.tagName:"+node.tagName)
@@ -382,6 +382,10 @@ function addTextForNode(node, arrayOfText, nodeToMark=null, offset=0, useHtmlMat
382
382
  // Marked nodes are useful if a node may need to be deleted and allows the node to be
383
383
  // deleted by stripping it out of the raw text.
384
384
  export function elementToMarkedRawText(element, nodeToMark=null, offset=0, useHtmlMath=true) {
385
+ console.log("elementToMarkedRawText")
386
+ console.log(element.innerHTML)
387
+ if (element.innerHTML === "<p><br></p>")
388
+ throw new Error ("We are here")
385
389
  // console.log("Node to mark:")
386
390
  // console.log(nodeToMark)
387
391
  let nodes = element.childNodes
@@ -718,7 +722,7 @@ function findGlobalOffset(node, nodeToFind, offset, result=null) {
718
722
  result.offset ++
719
723
  }
720
724
 
721
- return false;
725
+ return false;
722
726
  }
723
727
 
724
728
  function findNodeAndOffsetForGlobalOffset(node, globalOffset, result=null) {
@@ -730,10 +734,14 @@ function findNodeAndOffsetForGlobalOffset(node, globalOffset, result=null) {
730
734
  if (isTextNode(node)) {
731
735
  result.node = node
732
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)
733
739
  result.offset += node.nodeValue.length
734
- if (nodeStartsWithSmallSpace(node))
740
+ if (nodeStartsWithSmallSpace(node)) {
741
+ console.log("Text node starts with small space")
735
742
  result.offset -= 1
736
- // console.log("result.offset now: "+result.offset)
743
+ }
744
+ console.log("result.offset now: "+result.offset)
737
745
  return false
738
746
  }
739
747
  const childNodes = node.childNodes
@@ -754,10 +762,12 @@ function findNodeAndOffsetForGlobalOffset(node, globalOffset, result=null) {
754
762
  }
755
763
 
756
764
  if (node.nodeType === 1 && node.tagName === "P") {
765
+ console.log("Leaving p node")
757
766
  result.node = null
758
767
  result.offset ++
768
+ console.log("result.offset now: "+result.offset)
759
769
  }
760
- return false;
770
+ return false;
761
771
  }
762
772
 
763
773
  export function findFirstTextNode(node) {
@@ -774,9 +784,46 @@ export function findFirstTextNode(node) {
774
784
  return null
775
785
  }
776
786
 
787
+ function findNodeAndOffsetForGlobalOffsetInEditableDiv(editableDiv, globalOffset) {
788
+ let result = {offset:0}
789
+ console.log("Finding node and offset within: "+editableDiv.outerHTML)
790
+ findNodeAndOffsetForGlobalOffset(editableDiv, globalOffset, result)
791
+ console.log(result)
792
+
793
+ if (result.node === null) {
794
+ if (globalOffset > 0 && result.lastTextNode !== null) {
795
+ result.node = result.lastTextNode
796
+ result.offset = result.lastTextNode.nodeValue.length
797
+ } else {
798
+ console.warn("Unable to find node and offset for global offset: "+globalOffset)
799
+ console.log(editableDiv.innerHTML)
800
+ console.log(result)
801
+ result.node = findFirstTextNode(editableDiv)
802
+ result.offset = 0
803
+ }
804
+ }
805
+
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
+ }
820
+
821
+ return result
822
+ }
823
+
777
824
  // https://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html
778
825
  export function getRangeParams(editableDiv) {
779
- // console.log ("getRangeParams")
826
+ console.log ("getRangeParams")
780
827
  // console.log("Inner html: "+editableDiv.innerHTML)
781
828
  const isSupported = typeof window.getSelection !== "undefined";
782
829
  if (!isSupported) {
@@ -791,6 +838,7 @@ export function getRangeParams(editableDiv) {
791
838
  }
792
839
 
793
840
  const range = selection.getRangeAt(0);
841
+
794
842
  // logAllDescendants(editableDiv)
795
843
  // If the range is not within a text node, move it to a text node
796
844
  // This typically happens when the end of a range is at the start of a new line
@@ -852,56 +900,23 @@ function setRangeParamsFromGlobalOffsets(editableDiv, params) {
852
900
  try {
853
901
  if (params === null || params === undefined)
854
902
  throw new Error("Can't set range params: params === "+params)
855
- let startResult = {offset:0}
856
- findNodeAndOffsetForGlobalOffset(editableDiv, params.startGlobalOffset, startResult)
857
- // console.log(startResult.node.nodeType+":"+startResult.node.tagName+":"+
858
- // startResult.node.nodeValue+":"+startResult.offset)
859
- let endResult = {offset:0}
860
- findNodeAndOffsetForGlobalOffset(editableDiv, params.endGlobalOffset, endResult)
861
- // console.log(endResult.node.nodeType+":"+endResult.node.tagName+":"+
862
- // endResult.node.nodeValue+":"+endResult.offset)
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")
869
- console.log(startResult)
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
- }
903
+ let startResult =
904
+ findNodeAndOffsetForGlobalOffsetInEditableDiv(editableDiv, params.startGlobalOffset)
905
+
906
+ let endResult =
907
+ findNodeAndOffsetForGlobalOffsetInEditableDiv(editableDiv, params.endGlobalOffset)
908
+
909
+ if (startResult.node !== null) {
910
+ console.log("Setting range to start in node:")
911
+ console.log(" node type: "+startResult.node.nodeType)
912
+ console.log(" tag name: "+startResult.node.tagName)
913
+ console.log(" node value: "+startResult.node.nodeValue)
914
+ console.log(" offset:"+startResult.offset)
915
+ } else {
916
+ console.warn("Can't set range, null start node returned")
917
+ return
890
918
  }
891
919
 
892
- if (startResult.offset === 0 &&
893
- startResult.node.nodeValue.length > 0 &&
894
- nodeStartsWithSmallSpace(startResult.node)) {
895
- startResult.offset = 1
896
- }
897
- if (endResult.offset === 0 &&
898
- endResult.node.nodeValue.length > 0 &&
899
- nodeStartsWithSmallSpace(endResult.node)) {
900
- endResult.offset = 1
901
- }
902
- // console.log("Setting range to:")
903
- // console.log(startResult.node.nodeType+":"+startResult.node.tagName+":"+
904
- // startResult.node.nodeValue+":"+startResult.offset)
905
920
  new_range = document.createRange();
906
921
  new_range.setStart(startResult.node, startResult.offset);
907
922
  new_range.setEnd(endResult.node, endResult.offset);
@@ -926,38 +941,37 @@ export function setRangeParams(editableDiv, params) {
926
941
  console.error("Can't set range params: params === "+params)
927
942
  return
928
943
  }
929
-
930
- if (params.startGlobalOffset) {
944
+
945
+ // New way of doing things
946
+ if (params.startGlobalOffset !== null && params.startGlobalOffset !== undefined) {
947
+ console.log("Setting range from global offsets")
931
948
  setRangeParamsFromGlobalOffsets(editableDiv, params)
932
949
  return
933
950
  }
934
-
951
+
952
+ // Otherwise use the old way of doing things
953
+ console.log("Setting range from node index and local offset")
954
+
935
955
  if (params.startNodeIndex === null || params.startNodeIndex === undefined) {
936
956
  console.error("Can't set range params: params.startNodeIndex === "+params.startNodeIndex)
937
957
  return
938
958
  }
939
-
940
- // console.log("Setting cursor to range params: "+params.startNodeIndex+"->"+params.startOffset)
941
- // console.log(params)
942
-
943
959
  if (params.startNodeIndex < 0) {
944
960
  console.error("Can't set range params: params.startNodeIndex < 0")
945
961
  return
946
962
  }
947
963
 
948
-
949
964
  let nodes = editableDiv.childNodes
950
965
  if (params.startNodeIndex === 0 && params.startOffset === 0 && nodes.length === 0) {
951
- // console.log("Not setting range params")
952
966
  return
953
967
  }
954
968
 
955
- // console.log("Node["+params.startNodeIndex+"]:")
956
- // console.log(nodes[params.startNodeIndex])
969
+ // Set the offsets within the nodes
957
970
  let startOffset = params.startOffset
958
971
  let endOffset = params.endOffset
959
972
 
960
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
961
975
  if (params.startNodeIndex === 0 &&
962
976
  params.startOffset === 1 &&
963
977
  isSmallSpace(nodes[0]))
@@ -968,25 +982,26 @@ export function setRangeParams(editableDiv, params) {
968
982
  isSmallSpace(nodes[0]))
969
983
  endOffset = 0
970
984
 
985
+
986
+ // Find the nodes
971
987
  let startNode = findNodeWithIndex(editableDiv, params.startNodeIndex)
972
988
  let endNode = findNodeWithIndex(editableDiv, params.endNodeIndex)
973
989
 
974
- // Lets make sure we are setting
975
- // if (params.startNodeIndex !== 0 &&
976
- // params.startOffset === 0) {
977
- // if (getNodeText(startNode).charAt(0) === SMALL_SPACE)
978
- // startOffset = SMALL_SPACE.length
979
- // }
980
- // if (params.endNodeIndex !== 0 &&
981
- // params.endOffset === 0) {
982
- // if (getNodeText(endNode).charAt(0) === SMALL_SPACE)
983
- // endOffset = SMALL_SPACE.length
984
- // }
985
- // 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)
986
1001
 
987
1002
  // Set range in document
988
1003
  if (startNode === null) {
989
- 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)
990
1005
  startNode = findFirstTextNode(editableDiv)
991
1006
  startOffset = 0
992
1007
  }
@@ -1003,7 +1018,7 @@ export function setRangeParams(editableDiv, params) {
1003
1018
  }
1004
1019
 
1005
1020
  if (endNode === null) {
1006
- 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)
1007
1022
  endNode = findFirstTextNode(editableDiv)
1008
1023
  endOffset = 0
1009
1024
  }
@@ -1020,13 +1035,21 @@ export function setRangeParams(editableDiv, params) {
1020
1035
  }
1021
1036
 
1022
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
+
1023
1046
  new_range.setStart(startNode, startOffset);
1024
- if (endOffset > endNode.length) {
1025
- console.warn("endOffset > endNode.length when setting range: "+
1026
- endOffset+ " > "+endNode.length+
1047
+ if (endOffset > endNode.nodeValue.length) {
1048
+ console.warn("endOffset > endNode.nodeValue.length when setting range: "+
1049
+ endOffset+ " > "+endNode.nodeValue.length+
1027
1050
  " - "+endNode.nodeType+
1028
1051
  " - "+endNode.nodeValue)
1029
- endOffset = endNode.length
1052
+ endOffset = endNode.nodeValue.length
1030
1053
  }
1031
1054
  new_range.setEnd(endNode, endOffset);
1032
1055