@antscorp/antsomi-ui 2.0.45 → 2.0.46

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.
@@ -84,7 +84,8 @@ const TagifyInput = forwardRef((props, ref) => {
84
84
  throw new Error('Tagify instance is not initialized');
85
85
  }
86
86
  const { scope } = tagifyRef.current?.DOM;
87
- const isValidSelection = isAnchorNodeChildOfElement(scope);
87
+ const anchorNodeInstance = _.get(tagifyRef.current, 'state.selection.anchorNode', null);
88
+ const isValidSelection = isAnchorNodeChildOfElement(scope, anchorNodeInstance);
88
89
  if (isValidSelection) {
89
90
  const selection = window.getSelection();
90
91
  if (!selection?.rangeCount)
@@ -113,17 +114,20 @@ const TagifyInput = forwardRef((props, ref) => {
113
114
  }
114
115
  const { settings, DOM } = tagifyRef.current;
115
116
  const rangeInstance = _.get(tagifyRef.current, 'state.selection.range', null);
117
+ const anchorNodeInstance = _.get(tagifyRef.current, 'state.selection.anchorNode', null);
116
118
  const { empty } = settings.classNames;
117
- const isValidSelection = isAnchorNodeChildOfElement(DOM.scope);
119
+ const isValidAnchor = isAnchorNodeChildOfElement(DOM.scope, anchorNodeInstance);
118
120
  const selection = window.getSelection();
121
+ const isValidRange = selection?.rangeCount && rangeInstance;
122
+ const hasValidSelection = isValidRange && isValidAnchor;
119
123
  // In case not have the selection yet or lost the selection,
120
- if (!selection?.rangeCount || !isValidSelection || !rangeInstance) {
124
+ if (!hasValidSelection) {
121
125
  // need to restore the last range before inject a new tag if the last range exists
122
126
  if (lastRange.current) {
123
127
  selection?.removeAllRanges();
124
128
  selection?.addRange(lastRange.current);
125
129
  }
126
- else {
130
+ else if (!isValidRange) {
127
131
  // If the last range is lost, need to select the last text node of the input
128
132
  const { input: inputElement } = tagifyRef.current.DOM;
129
133
  if (inputElement) {
@@ -179,6 +179,7 @@ export declare const getAttributesString: (map: Map<string, string | boolean | n
179
179
  * is a child of a specific DOM element.
180
180
  *
181
181
  * @param {HTMLElement} element - The parent element to check against
182
+ * @param {HTMLElement} anchorNodeInstance - The anchor node of the Tagify instance
182
183
  * @returns {boolean} True if the anchor node is a child of the element, false otherwise
183
184
  */
184
- export declare const isAnchorNodeChildOfElement: (element: Node) => boolean;
185
+ export declare const isAnchorNodeChildOfElement: (element: Node, anchorNodeInstance: Node | null) => boolean;
@@ -622,9 +622,10 @@ export const getAttributesString = (map) => Array.from(map.entries())
622
622
  * is a child of a specific DOM element.
623
623
  *
624
624
  * @param {HTMLElement} element - The parent element to check against
625
+ * @param {HTMLElement} anchorNodeInstance - The anchor node of the Tagify instance
625
626
  * @returns {boolean} True if the anchor node is a child of the element, false otherwise
626
627
  */
627
- export const isAnchorNodeChildOfElement = (element) => {
628
+ export const isAnchorNodeChildOfElement = (element, anchorNodeInstance) => {
628
629
  // Get the current selection
629
630
  const selection = window.getSelection();
630
631
  // Check if there's an active selection
@@ -638,5 +639,5 @@ export const isAnchorNodeChildOfElement = (element) => {
638
639
  return false;
639
640
  }
640
641
  // Use Node.contains() to check if the element contains the anchor node
641
- return element.contains(anchorNode);
642
+ return element.contains(anchorNode) && element.contains(anchorNodeInstance);
642
643
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antscorp/antsomi-ui",
3
- "version": "2.0.45",
3
+ "version": "2.0.46",
4
4
  "description": "An enterprise-class UI design language and React UI library.",
5
5
  "sideEffects": [
6
6
  "dist/*",