@antscorp/antsomi-ui 2.0.82 → 2.0.83-text-editor-beta.1

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.
Files changed (213) hide show
  1. package/es/assets/css/main.scss +4 -2
  2. package/es/components/index.scss +1 -0
  3. package/es/components/molecules/AccountSelection/AccountListing.js +14 -3
  4. package/es/components/molecules/EmojiPopover/EmojiPopover.js +5 -1
  5. package/es/components/molecules/FontSizeInput/FontSizeInput.d.ts +3 -0
  6. package/es/components/molecules/FontSizeInput/FontSizeInput.js +77 -0
  7. package/es/components/molecules/FontSizeInput/constants.d.ts +2 -0
  8. package/es/components/molecules/FontSizeInput/constants.js +5 -0
  9. package/es/components/molecules/FontSizeInput/index.d.ts +2 -0
  10. package/es/components/molecules/FontSizeInput/index.js +1 -0
  11. package/es/components/molecules/FontSizeInput/types.d.ts +21 -0
  12. package/es/components/molecules/FontSizeInput/types.js +1 -0
  13. package/es/components/molecules/VirtualizedMenu/VirtualizedMenu.d.ts +1 -2
  14. package/es/components/molecules/VirtualizedMenu/__mocks__/index.js +2550 -938
  15. package/es/components/molecules/VirtualizedMenu/components/Item/Item.d.ts +4 -3
  16. package/es/components/molecules/VirtualizedMenu/components/Item/Item.js +24 -22
  17. package/es/components/molecules/VirtualizedMenu/components/MenuInline/MenuInline.d.ts +1 -2
  18. package/es/components/molecules/VirtualizedMenu/components/MenuInline/MenuInline.js +59 -31
  19. package/es/components/molecules/VirtualizedMenu/components/MenuInline/MenuInline.optimized.d.ts +19 -0
  20. package/es/components/molecules/VirtualizedMenu/components/MenuInline/MenuInline.optimized.js +233 -0
  21. package/es/components/molecules/VirtualizedMenu/components/MenuInline/index.d.ts +1 -1
  22. package/es/components/molecules/VirtualizedMenu/components/MenuInline/index.js +2 -1
  23. package/es/components/molecules/VirtualizedMenu/styled.d.ts +1 -1
  24. package/es/components/molecules/VirtualizedMenu/styled.js +11 -3
  25. package/es/components/molecules/VirtualizedMenu/types.d.ts +1 -8
  26. package/es/components/molecules/VirtualizedMenu/utils.d.ts +7 -5
  27. package/es/components/molecules/VirtualizedMenu/utils.js +12 -18
  28. package/es/components/molecules/index.d.ts +1 -0
  29. package/es/components/molecules/index.js +1 -0
  30. package/es/components/organism/ActivityTimeline/__mocks__/event_tracking.json +1290 -0
  31. package/es/components/organism/ActivityTimeline/__mocks__/timeline.json +3059 -0
  32. package/es/components/organism/TextEditor/TextEditor.d.ts +3 -0
  33. package/es/components/organism/TextEditor/TextEditor.js +239 -0
  34. package/es/components/organism/TextEditor/__mocks__/text-block.settings.json +320 -0
  35. package/es/components/organism/TextEditor/__mocks__/text-contennt.d.ts +1 -0
  36. package/es/components/organism/TextEditor/__mocks__/text-contennt.js +38 -0
  37. package/es/components/organism/TextEditor/constants.d.ts +135 -0
  38. package/es/components/organism/TextEditor/constants.js +280 -0
  39. package/es/components/organism/TextEditor/extensions/BackgroundColor.d.ts +25 -0
  40. package/es/components/organism/TextEditor/extensions/BackgroundColor.js +41 -0
  41. package/es/components/organism/TextEditor/extensions/BubbleMenu/bubble-menu-plugin.d.ts +130 -0
  42. package/es/components/organism/TextEditor/extensions/BubbleMenu/bubble-menu-plugin.js +247 -0
  43. package/es/components/organism/TextEditor/extensions/BubbleMenu/bubble-menu.d.ts +15 -0
  44. package/es/components/organism/TextEditor/extensions/BubbleMenu/bubble-menu.js +31 -0
  45. package/es/components/organism/TextEditor/extensions/BubbleMenu/index.d.ts +2 -0
  46. package/es/components/organism/TextEditor/extensions/BubbleMenu/index.js +2 -0
  47. package/es/components/organism/TextEditor/extensions/Color.d.ts +6 -0
  48. package/es/components/organism/TextEditor/extensions/Color.js +19 -0
  49. package/es/components/organism/TextEditor/extensions/Emoji.d.ts +57 -0
  50. package/es/components/organism/TextEditor/extensions/Emoji.js +184 -0
  51. package/es/components/organism/TextEditor/extensions/FontFamily.d.ts +6 -0
  52. package/es/components/organism/TextEditor/extensions/FontFamily.js +43 -0
  53. package/es/components/organism/TextEditor/extensions/FontSize.d.ts +32 -0
  54. package/es/components/organism/TextEditor/extensions/FontSize.js +47 -0
  55. package/es/components/organism/TextEditor/extensions/FontWeight.d.ts +23 -0
  56. package/es/components/organism/TextEditor/extensions/FontWeight.js +41 -0
  57. package/es/components/organism/TextEditor/extensions/Highlight.d.ts +1 -0
  58. package/es/components/organism/TextEditor/extensions/Highlight.js +14 -0
  59. package/es/components/organism/TextEditor/extensions/Indent.d.ts +28 -0
  60. package/es/components/organism/TextEditor/extensions/Indent.js +66 -0
  61. package/es/components/organism/TextEditor/extensions/LineHeight.d.ts +20 -0
  62. package/es/components/organism/TextEditor/extensions/LineHeight.js +36 -0
  63. package/es/components/organism/TextEditor/extensions/Link.d.ts +3 -0
  64. package/es/components/organism/TextEditor/extensions/Link.js +47 -0
  65. package/es/components/organism/TextEditor/extensions/ListItemMarker.d.ts +13 -0
  66. package/es/components/organism/TextEditor/extensions/ListItemMarker.js +174 -0
  67. package/es/components/organism/TextEditor/extensions/Selection.d.ts +6 -0
  68. package/es/components/organism/TextEditor/extensions/Selection.js +40 -0
  69. package/es/components/organism/TextEditor/extensions/SmartTag.d.ts +33 -0
  70. package/es/components/organism/TextEditor/extensions/SmartTag.js +162 -0
  71. package/es/components/organism/TextEditor/extensions/StyleMemory.d.ts +36 -0
  72. package/es/components/organism/TextEditor/extensions/StyleMemory.js +163 -0
  73. package/es/components/organism/TextEditor/extensions/TextTransform.d.ts +31 -0
  74. package/es/components/organism/TextEditor/extensions/TextTransform.js +35 -0
  75. package/es/components/organism/TextEditor/hooks/index.d.ts +6 -0
  76. package/es/components/organism/TextEditor/hooks/index.js +6 -0
  77. package/es/components/organism/TextEditor/hooks/useDocumentState.d.ts +18 -0
  78. package/es/components/organism/TextEditor/hooks/useDocumentState.js +42 -0
  79. package/es/components/organism/TextEditor/hooks/useLinkHandler.d.ts +27 -0
  80. package/es/components/organism/TextEditor/hooks/useLinkHandler.js +279 -0
  81. package/es/components/organism/TextEditor/hooks/useMarkTracking.d.ts +26 -0
  82. package/es/components/organism/TextEditor/hooks/useMarkTracking.js +68 -0
  83. package/es/components/organism/TextEditor/hooks/usePersistence.d.ts +31 -0
  84. package/es/components/organism/TextEditor/hooks/usePersistence.js +169 -0
  85. package/es/components/organism/TextEditor/hooks/useStyleMemory.d.ts +6 -0
  86. package/es/components/organism/TextEditor/hooks/useStyleMemory.js +42 -0
  87. package/es/components/organism/TextEditor/hooks/useStylePresets.d.ts +34 -0
  88. package/es/components/organism/TextEditor/hooks/useStylePresets.js +83 -0
  89. package/es/components/organism/TextEditor/index.d.ts +5 -0
  90. package/es/components/organism/TextEditor/index.js +2 -0
  91. package/es/components/organism/TextEditor/index.scss +26 -0
  92. package/es/components/organism/TextEditor/provider.d.ts +10 -0
  93. package/es/components/organism/TextEditor/provider.js +20 -0
  94. package/es/components/organism/TextEditor/store.d.ts +11 -0
  95. package/es/components/organism/TextEditor/store.js +12 -0
  96. package/es/components/organism/TextEditor/styled.d.ts +5 -0
  97. package/es/components/organism/TextEditor/styled.js +69 -0
  98. package/es/components/organism/TextEditor/types.d.ts +95 -0
  99. package/es/components/organism/TextEditor/types.js +1 -0
  100. package/es/components/organism/TextEditor/ui/BubbleMenu/BubbleMenu.d.ts +6 -0
  101. package/es/components/organism/TextEditor/ui/BubbleMenu/BubbleMenu.js +78 -0
  102. package/es/components/organism/TextEditor/ui/BubbleMenu/index.d.ts +1 -0
  103. package/es/components/organism/TextEditor/ui/BubbleMenu/index.js +1 -0
  104. package/es/components/organism/TextEditor/ui/ColorPicker/ColorPicker.d.ts +43 -0
  105. package/es/components/organism/TextEditor/ui/ColorPicker/ColorPicker.js +120 -0
  106. package/es/components/organism/TextEditor/ui/ColorPicker/index.d.ts +1 -0
  107. package/es/components/organism/TextEditor/ui/ColorPicker/index.js +1 -0
  108. package/es/components/organism/TextEditor/ui/Emoji/EmojiList.d.ts +11 -0
  109. package/es/components/organism/TextEditor/ui/Emoji/EmojiList.js +66 -0
  110. package/es/components/organism/TextEditor/ui/Emoji/index.d.ts +2 -0
  111. package/es/components/organism/TextEditor/ui/Emoji/index.js +2 -0
  112. package/es/components/organism/TextEditor/ui/Emoji/suggestion.d.ts +4 -0
  113. package/es/components/organism/TextEditor/ui/Emoji/suggestion.js +71 -0
  114. package/es/components/organism/TextEditor/ui/FontPopover/FontPopover.d.ts +11 -0
  115. package/es/components/organism/TextEditor/ui/FontPopover/FontPopover.js +72 -0
  116. package/es/components/organism/TextEditor/ui/FontPopover/styled.d.ts +1 -0
  117. package/es/components/organism/TextEditor/ui/FontPopover/styled.js +51 -0
  118. package/es/components/organism/TextEditor/ui/Popover/Popover.d.ts +6 -0
  119. package/es/components/organism/TextEditor/ui/Popover/Popover.js +7 -0
  120. package/es/components/organism/TextEditor/ui/Popover/index.d.ts +1 -0
  121. package/es/components/organism/TextEditor/ui/Popover/index.js +1 -0
  122. package/es/components/organism/TextEditor/ui/Select/Select.d.ts +4 -0
  123. package/es/components/organism/TextEditor/ui/Select/Select.js +7 -0
  124. package/es/components/organism/TextEditor/ui/Select/index.d.ts +1 -0
  125. package/es/components/organism/TextEditor/ui/Select/index.js +1 -0
  126. package/es/components/organism/TextEditor/ui/TextAlignSelect/TextAlignSelect.d.ts +30 -0
  127. package/es/components/organism/TextEditor/ui/TextAlignSelect/TextAlignSelect.js +49 -0
  128. package/es/components/organism/TextEditor/ui/TextAlignSelect/index.d.ts +1 -0
  129. package/es/components/organism/TextEditor/ui/TextAlignSelect/index.js +1 -0
  130. package/es/components/organism/TextEditor/ui/Toolbar/Toolbar.d.ts +14 -0
  131. package/es/components/organism/TextEditor/ui/Toolbar/Toolbar.js +42 -0
  132. package/es/components/organism/TextEditor/ui/Toolbar/actions/BoldAction.d.ts +5 -0
  133. package/es/components/organism/TextEditor/ui/Toolbar/actions/BoldAction.js +7 -0
  134. package/es/components/organism/TextEditor/ui/Toolbar/actions/BulletListAction.d.ts +5 -0
  135. package/es/components/organism/TextEditor/ui/Toolbar/actions/BulletListAction.js +7 -0
  136. package/es/components/organism/TextEditor/ui/Toolbar/actions/ClearFormattingAction.d.ts +5 -0
  137. package/es/components/organism/TextEditor/ui/Toolbar/actions/ClearFormattingAction.js +18 -0
  138. package/es/components/organism/TextEditor/ui/Toolbar/actions/EmojiAction.d.ts +4 -0
  139. package/es/components/organism/TextEditor/ui/Toolbar/actions/EmojiAction.js +13 -0
  140. package/es/components/organism/TextEditor/ui/Toolbar/actions/FontFamilyAction.d.ts +7 -0
  141. package/es/components/organism/TextEditor/ui/Toolbar/actions/FontFamilyAction.js +18 -0
  142. package/es/components/organism/TextEditor/ui/Toolbar/actions/FontSizeAction.d.ts +6 -0
  143. package/es/components/organism/TextEditor/ui/Toolbar/actions/FontSizeAction.js +42 -0
  144. package/es/components/organism/TextEditor/ui/Toolbar/actions/HighlightAction.d.ts +5 -0
  145. package/es/components/organism/TextEditor/ui/Toolbar/actions/HighlightAction.js +7 -0
  146. package/es/components/organism/TextEditor/ui/Toolbar/actions/IndentAction.d.ts +5 -0
  147. package/es/components/organism/TextEditor/ui/Toolbar/actions/IndentAction.js +7 -0
  148. package/es/components/organism/TextEditor/ui/Toolbar/actions/ItalicAction.d.ts +5 -0
  149. package/es/components/organism/TextEditor/ui/Toolbar/actions/ItalicAction.js +7 -0
  150. package/es/components/organism/TextEditor/ui/Toolbar/actions/LinkAction.d.ts +6 -0
  151. package/es/components/organism/TextEditor/ui/Toolbar/actions/LinkAction.js +4 -0
  152. package/es/components/organism/TextEditor/ui/Toolbar/actions/OrderedListAction.d.ts +5 -0
  153. package/es/components/organism/TextEditor/ui/Toolbar/actions/OrderedListAction.js +7 -0
  154. package/es/components/organism/TextEditor/ui/Toolbar/actions/OutdentAction.d.ts +5 -0
  155. package/es/components/organism/TextEditor/ui/Toolbar/actions/OutdentAction.js +7 -0
  156. package/es/components/organism/TextEditor/ui/Toolbar/actions/SmartTagAction.d.ts +7 -0
  157. package/es/components/organism/TextEditor/ui/Toolbar/actions/SmartTagAction.js +9 -0
  158. package/es/components/organism/TextEditor/ui/Toolbar/actions/SpacingAction.d.ts +9 -0
  159. package/es/components/organism/TextEditor/ui/Toolbar/actions/SpacingAction.js +22 -0
  160. package/es/components/organism/TextEditor/ui/Toolbar/actions/StrikeAction.d.ts +5 -0
  161. package/es/components/organism/TextEditor/ui/Toolbar/actions/StrikeAction.js +7 -0
  162. package/es/components/organism/TextEditor/ui/Toolbar/actions/SubscriptAction.d.ts +5 -0
  163. package/es/components/organism/TextEditor/ui/Toolbar/actions/SubscriptAction.js +13 -0
  164. package/es/components/organism/TextEditor/ui/Toolbar/actions/SuperscriptAction.d.ts +5 -0
  165. package/es/components/organism/TextEditor/ui/Toolbar/actions/SuperscriptAction.js +13 -0
  166. package/es/components/organism/TextEditor/ui/Toolbar/actions/TextAlignAction.d.ts +5 -0
  167. package/es/components/organism/TextEditor/ui/Toolbar/actions/TextAlignAction.js +3 -0
  168. package/es/components/organism/TextEditor/ui/Toolbar/actions/TextBackgroundColorAction.d.ts +7 -0
  169. package/es/components/organism/TextEditor/ui/Toolbar/actions/TextBackgroundColorAction.js +19 -0
  170. package/es/components/organism/TextEditor/ui/Toolbar/actions/TextColorAction.d.ts +15 -0
  171. package/es/components/organism/TextEditor/ui/Toolbar/actions/TextColorAction.js +14 -0
  172. package/es/components/organism/TextEditor/ui/Toolbar/actions/TextTransformAction.d.ts +5 -0
  173. package/es/components/organism/TextEditor/ui/Toolbar/actions/TextTransformAction.js +28 -0
  174. package/es/components/organism/TextEditor/ui/Toolbar/actions/UnderlineAction.d.ts +5 -0
  175. package/es/components/organism/TextEditor/ui/Toolbar/actions/UnderlineAction.js +5 -0
  176. package/es/components/organism/TextEditor/ui/Toolbar/actions/UnsetLink.d.ts +6 -0
  177. package/es/components/organism/TextEditor/ui/Toolbar/actions/UnsetLink.js +10 -0
  178. package/es/components/organism/TextEditor/ui/Toolbar/actions/index.d.ts +20 -0
  179. package/es/components/organism/TextEditor/ui/Toolbar/actions/index.js +20 -0
  180. package/es/components/organism/TextEditor/ui/Toolbar/index.d.ts +1 -0
  181. package/es/components/organism/TextEditor/ui/Toolbar/index.js +1 -0
  182. package/es/components/organism/TextEditor/utils/documentState.d.ts +57 -0
  183. package/es/components/organism/TextEditor/utils/documentState.js +100 -0
  184. package/es/components/organism/TextEditor/utils/font.d.ts +84 -0
  185. package/es/components/organism/TextEditor/utils/font.js +175 -0
  186. package/es/components/organism/TextEditor/utils/htmlProcessing.d.ts +62 -0
  187. package/es/components/organism/TextEditor/utils/htmlProcessing.js +304 -0
  188. package/es/components/organism/TextEditor/utils/index.d.ts +8 -0
  189. package/es/components/organism/TextEditor/utils/index.js +16 -0
  190. package/es/components/organism/TextEditor/utils/link.d.ts +100 -0
  191. package/es/components/organism/TextEditor/utils/link.js +149 -0
  192. package/es/components/organism/TextEditor/utils/menu.d.ts +134 -0
  193. package/es/components/organism/TextEditor/utils/menu.js +317 -0
  194. package/es/components/organism/TextEditor/utils/selection.d.ts +25 -0
  195. package/es/components/organism/TextEditor/utils/selection.js +57 -0
  196. package/es/components/organism/TextEditor/utils/smartTag.d.ts +49 -0
  197. package/es/components/organism/TextEditor/utils/smartTag.js +89 -0
  198. package/es/components/organism/TextEditor/utils/style.d.ts +78 -0
  199. package/es/components/organism/TextEditor/utils/style.js +193 -0
  200. package/es/components/organism/index.d.ts +1 -0
  201. package/es/components/organism/index.js +1 -0
  202. package/es/components/organism/index.scss +1 -0
  203. package/es/hooks/useBroadcastedLocalStorage.d.ts +5 -0
  204. package/es/hooks/useBroadcastedLocalStorage.js +71 -0
  205. package/es/utils/common.d.ts +6 -9
  206. package/es/utils/common.js +44 -23
  207. package/es/utils/index.d.ts +1 -0
  208. package/es/utils/index.js +1 -0
  209. package/es/utils/tree.d.ts +225 -0
  210. package/es/utils/tree.js +469 -0
  211. package/es/utils/web.d.ts +4 -0
  212. package/es/utils/web.js +25 -0
  213. package/package.json +29 -3
@@ -430,35 +430,56 @@ export function generateKey() {
430
430
  }
431
431
  export const formatUserId = (userId) => String(userId || '').replace(/(\d{3})(\d{3})(\d{0,})/, `$1-$2-$3`);
432
432
  /**
433
- * Function to remove accent
434
- * @param str - String want to remove accent
435
- * @returns {string} - Result after remove accent
433
+ * Normalizes a string for searching by:
434
+ * 1. Converting to NFD Unicode form and removing diacritics.
435
+ * 2. Converting to lowercase.
436
+ * 3. Removing all whitespace.
437
+ * @param str The string to normalize.
438
+ * @returns The normalized string.
436
439
  */
437
- export const removeAccent = (str) => {
438
- str = str.replace(/[àáạảãâầấậẩẫăằắặẳẵ]/g, 'a');
439
- str = str.replace(/[èéẹẻẽêềếệểễ]/g, 'e');
440
- str = str.replace(/[ìíịỉĩ]/g, 'i');
441
- str = str.replace(/[òóọỏõôồốộổỗơờớợởỡ]/g, 'o');
442
- str = str.replace(/[ùúụủũưừứựửữ]/g, 'u');
443
- str = str.replace(/[ỳýỵỷỹ]/g, 'y');
444
- str = str.replace(/đ/g, 'd');
445
- str = str.replace(/[ÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴ]/g, 'A');
446
- str = str.replace(/[ÈÉẸẺẼÊỀẾỆỂỄ]/g, 'E');
447
- str = str.replace(/[ÌÍỊỈĨ]/g, 'I');
448
- str = str.replace(/[ÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠ]/g, 'O');
449
- str = str.replace(/Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ/g, 'U');
450
- str = str.replace(/Ỳ|Ý|Ỵ|Ỷ|Ỹ/g, 'Y');
451
- str = str.replace(/Đ/g, 'D');
452
- return str;
453
- };
454
- /** Remove all accents and convert string to lower case, remove leading and trailing white space */
455
- export const simplifyString = (str) => removeAccent(`${str || ''}`.toLowerCase().trim()) || '';
440
+ function normalizeStringForSearch(str) {
441
+ if (!str) {
442
+ return ''; // Handle null, undefined, or empty string inputs
443
+ }
444
+ return str
445
+ .normalize('NFD') // Decompose accented characters (e.g., "á" -> "a" + "´")
446
+ .replace(/[\u0300-\u036f]/g, '') // Remove diacritical marks
447
+ .toLowerCase() // Convert to lowercase
448
+ .replace(/\s+/g, ''); // Remove all whitespace
449
+ }
456
450
  /**
451
+ * Checks string matching based on a query, supporting fuzzy matching,
452
+ * where the query characters must appear in order within the target string.
453
+ * Allows for missing characters in the query relative to the target,
454
+ * and ignores case, diacritics, and spaces.
455
+ *
457
456
  * @param str target string
458
457
  * @param queryStr search query
459
458
  * @returns true if target string includes search query
460
459
  */
461
- export const searchStringQuery = (str, queryStr) => simplifyString(str).includes(simplifyString(queryStr || ''));
460
+ export const searchStringQuery = (str, queryStr) => {
461
+ const normalizedQuery = normalizeStringForSearch(queryStr);
462
+ const normalizedItem = normalizeStringForSearch(str);
463
+ // Optimization: if normalized query is longer than normalized item,
464
+ // it cannot be a subsequence.
465
+ if (normalizedQuery.length > normalizedItem.length) {
466
+ return false;
467
+ }
468
+ // Subsequence check:
469
+ // Iterate through both normalized strings.
470
+ // If characters match, advance the query pointer.
471
+ // Always advance the item pointer.
472
+ let queryIndex = 0;
473
+ let itemIndex = 0;
474
+ while (queryIndex < normalizedQuery.length && itemIndex < normalizedItem.length) {
475
+ if (normalizedQuery[queryIndex] === normalizedItem[itemIndex]) {
476
+ queryIndex++;
477
+ }
478
+ itemIndex++;
479
+ }
480
+ // If queryIndex reached the end of normalizedQuery, all its characters were found in order.
481
+ return queryIndex === normalizedQuery.length;
482
+ };
462
483
  /**
463
484
  * Checks if any of the provided values match the search value.
464
485
  *
@@ -9,3 +9,4 @@ export * from './actionButtons';
9
9
  export * from './dataTable';
10
10
  export * from './cookie';
11
11
  export * from './attribute';
12
+ export * from './web';
package/es/utils/index.js CHANGED
@@ -9,3 +9,4 @@ export * from './actionButtons';
9
9
  export * from './dataTable';
10
10
  export * from './cookie';
11
11
  export * from './attribute';
12
+ export * from './web';
@@ -0,0 +1,225 @@
1
+ /**
2
+ * Tree utilities with performance optimizations using level-based operations.
3
+ *
4
+ * Key performance improvements:
5
+ * - Level property enables O(1) depth checks and early exit conditions
6
+ * - Pre-allocated arrays for path operations avoid costly array resizing
7
+ * - Level-based filtering for efficient node selection
8
+ * - Optimized tree traversal with level-aware recursion control
9
+ *
10
+ * @file src/utils/tree.ts
11
+ */
12
+ /**
13
+ * Represents the structure of a tree node before normalization.
14
+ * @template T The type of data stored in the node.
15
+ */
16
+ export interface TreeNode<T> {
17
+ /** The unique identifier for the node. */
18
+ id: string;
19
+ /** The data payload of the node. */
20
+ data: T;
21
+ /** Optional array of child nodes. Children are full TreeNode objects. */
22
+ children?: TreeNode<T>[];
23
+ }
24
+ /**
25
+ * Represents the structure of a normalized tree node, as stored in the `items` map.
26
+ * @template T The type of data stored in the node.
27
+ */
28
+ export interface NormalizedNode<T> {
29
+ /** The unique identifier for the node. */
30
+ id: string;
31
+ /** The data payload of the node. */
32
+ data: T;
33
+ /** The ID of the parent node, or null if it's a root node. */
34
+ parentId: string | null;
35
+ /** An array of IDs of the direct children of this node. */
36
+ childIds: string[];
37
+ /** The depth/level of this node in the tree (0 for root nodes). */
38
+ level: number;
39
+ }
40
+ /**
41
+ * Represents the overall shape of the normalized tree data store.
42
+ * @template T The type of data stored in the tree nodes.
43
+ */
44
+ export interface NormalizedTreeState<T> {
45
+ /** A Map where keys are item IDs (strings) and values are NormalizedNode objects. */
46
+ items: Map<string, NormalizedNode<T>>;
47
+ /** A Set of IDs for the top-level (root) nodes. */
48
+ rootItemIds: Set<string>;
49
+ }
50
+ /**
51
+ * Normalizes a tree structure from a nested format to a flat, relational format.
52
+ * Each node is stored in a map by its ID, and relationships are maintained via `parentId` and `childIds`.
53
+ *
54
+ * @template T The type of data in the tree nodes.
55
+ * @param {TreeNode<T>[]} nodes - An array of root tree nodes in their original nested structure.
56
+ * @returns {NormalizedTreeState<T>} The normalized tree state, containing a map of items and a set of root item IDs.
57
+ */
58
+ export declare function normalizeTreeData<T>(nodes: TreeNode<T>[]): NormalizedTreeState<T>;
59
+ /**
60
+ * Retrieves all normalized nodes from the items store as an array.
61
+ *
62
+ * @template T The type of data in the nodes.
63
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
64
+ * @returns {NormalizedNode<T>[]} An array of all normalized nodes.
65
+ */
66
+ export declare function getAllItems<T>(itemsStore: Map<string, NormalizedNode<T>>): NormalizedNode<T>[];
67
+ /**
68
+ * Recursively reconstructs a subtree (or a portion of it) from the normalized data,
69
+ * starting from a given item ID.
70
+ *
71
+ * @template T The type of data in the nodes.
72
+ * @param {string} itemId - The ID of the node from which to start reconstructing the subtree.
73
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
74
+ * @param {number} [maxLevels=Infinity] - The maximum number of levels of children to retrieve.
75
+ * @param {number} [startLevel] - The starting level for depth calculation (used internally).
76
+ * @returns {TreeNode<T>[]} An array of reconstructed tree nodes (children of the `itemId` node).
77
+ * Returns an empty array if the item has no children or `maxLevels` is reached.
78
+ */
79
+ export declare function getChildrenRecursively<T>(itemId: string, itemsStore: Map<string, NormalizedNode<T>>, maxLevels?: number, startLevel?: number): TreeNode<T>[];
80
+ /**
81
+ * Retrieves a single normalized node by its ID from the items store.
82
+ *
83
+ * @template T The type of data in the node.
84
+ * @param {string} itemId - The ID of the node to retrieve.
85
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
86
+ * @returns {NormalizedNode<T> | undefined} The normalized node if found, otherwise undefined.
87
+ */
88
+ export declare function getNode<T>(itemId: string, itemsStore: Map<string, NormalizedNode<T>>): NormalizedNode<T> | undefined;
89
+ /**
90
+ * Retrieves the parent of a given node from the items store.
91
+ *
92
+ * @template T The type of data in the nodes.
93
+ * @param {string} itemId - The ID of the node whose parent is to be retrieved.
94
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
95
+ * @returns {NormalizedNode<T> | undefined} The parent node if found, otherwise undefined.
96
+ */
97
+ export declare function getParent<T>(itemId: string, itemsStore: Map<string, NormalizedNode<T>>): NormalizedNode<T> | undefined;
98
+ /**
99
+ * Retrieves the path (an array of nodes) from the root to a specified node.
100
+ * The path includes the target node itself as the last element.
101
+ * Performance optimized using the level property to pre-allocate array size.
102
+ *
103
+ * @template T The type of data in the nodes.
104
+ * @param {string} itemId - The ID of the target node.
105
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
106
+ * @returns {NormalizedNode<T>[]} An array of nodes representing the path from a root to the target node.
107
+ * Returns an empty array if the node is not found.
108
+ */
109
+ export declare function getPathToNode<T>(itemId: string, itemsStore: Map<string, NormalizedNode<T>>): NormalizedNode<T>[];
110
+ /**
111
+ * Finds all nodes in the items store that satisfy a given predicate function.
112
+ *
113
+ * @template T The type of data in the nodes.
114
+ * @param {(node: NormalizedNode<T>) => boolean} predicate - A function that takes a node and returns true if it matches the criteria.
115
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
116
+ * @returns {NormalizedNode<T>[]} An array of nodes that satisfy the predicate.
117
+ */
118
+ export declare function findNodes<T>(predicate: (node: NormalizedNode<T>) => boolean, itemsStore: Map<string, NormalizedNode<T>>): NormalizedNode<T>[];
119
+ /**
120
+ * Checks if a node is a descendant of another node.
121
+ * Performance optimized using level comparison for early exit.
122
+ *
123
+ * @template T The type of data in the nodes.
124
+ * @param {string} potentialDescendantId - The ID of the node to check if it's a descendant.
125
+ * @param {string} ancestorId - The ID of the potential ancestor node.
126
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
127
+ * @returns {boolean} True if `potentialDescendantId` is a descendant of `ancestorId`, false otherwise.
128
+ */
129
+ export declare function isDescendant<T>(potentialDescendantId: string, ancestorId: string, itemsStore: Map<string, NormalizedNode<T>>): boolean;
130
+ /**
131
+ * Updates the data of a specific node in an immutable way.
132
+ *
133
+ * @template T The type of data in the node.
134
+ * @param {string} itemId - The ID of the node to update.
135
+ * @param {Partial<T>} newData - An object containing the new data fields to merge into the existing node's data.
136
+ * @param {NormalizedTreeState<T>} currentState - The current normalized tree state.
137
+ * @returns {NormalizedTreeState<T>} A new tree state with the specified node's data updated.
138
+ * Returns the original state if the node is not found.
139
+ */
140
+ export declare function updateNodeData<T>(itemId: string, newData: Partial<T>, currentState: NormalizedTreeState<T>): NormalizedTreeState<T>;
141
+ /**
142
+ * Adds a new node to the tree in an immutable way.
143
+ *
144
+ * @template T The type of data for the new node.
145
+ * @param {string} newNodeId - The ID for the new node. Must be unique.
146
+ * @param {T} nodeData - The data payload for the new node.
147
+ * @param {string | null} parentId - The ID of the parent node. If null, the new node is added as a root.
148
+ * @param {NormalizedTreeState<T>} currentState - The current normalized tree state.
149
+ * @returns {NormalizedTreeState<T>} A new tree state with the new node added.
150
+ * If `newNodeId` already exists, it logs a warning and returns the original state.
151
+ * If `parentId` is provided but not found, the node is added as a root.
152
+ */
153
+ export declare function addNode<T>(newNodeId: string, nodeData: T, parentId: string | null, currentState: NormalizedTreeState<T>): NormalizedTreeState<T>;
154
+ /**
155
+ * Helper function to recursively get all descendant IDs of a given node,
156
+ * including the starting node's ID itself.
157
+ * Uses a Set for efficient addition and lookup, and to avoid reprocessing.
158
+ *
159
+ * @template T The type of data in the nodes (not directly used but good for consistency).
160
+ * @param {string} itemId - The ID of the starting node.
161
+ * @param {Map<string, NormalizedNode<T>>} itemsMap - The map of all normalized nodes.
162
+ * @param {Set<string>} [idsFound=new Set()] - Accumulator for descendant IDs (used internally for recursion).
163
+ * @returns {Set<string>} A Set containing the IDs of the starting node and all its descendants.
164
+ */
165
+ export declare function getAllDescendantIdsRecursive<T>(itemId: string, itemsMap: Map<string, NormalizedNode<T>>, idsFound?: Set<string>): Set<string>;
166
+ /**
167
+ * Removes a node and all its descendants from the tree in an immutable way.
168
+ *
169
+ * @template T The type of data in the nodes.
170
+ * @param {string} itemIdToRemove - The ID of the node to remove.
171
+ * @param {NormalizedTreeState<T>} currentState - The current normalized tree state.
172
+ * @returns {NormalizedTreeState<T>} A new tree state with the specified node and its descendants removed.
173
+ * Returns the original state if the node to remove is not found.
174
+ */
175
+ export declare function removeNode<T>(itemIdToRemove: string, currentState: NormalizedTreeState<T>): NormalizedTreeState<T>;
176
+ /**
177
+ * Gets all nodes at a specific level in the tree.
178
+ * Performance optimized using the level property.
179
+ *
180
+ * @template T The type of data in the nodes.
181
+ * @param {number} targetLevel - The level to retrieve nodes from (0 for root nodes).
182
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
183
+ * @returns {NormalizedNode<T>[]} An array of nodes at the specified level.
184
+ */
185
+ export declare function getNodesAtLevel<T>(targetLevel: number, itemsStore: Map<string, NormalizedNode<T>>): NormalizedNode<T>[];
186
+ /**
187
+ * Gets the maximum depth/level of the tree.
188
+ * Performance optimized using the level property.
189
+ *
190
+ * @template T The type of data in the nodes.
191
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
192
+ * @returns {number} The maximum level in the tree, or -1 if the tree is empty.
193
+ */
194
+ export declare function getTreeDepth<T>(itemsStore: Map<string, NormalizedNode<T>>): number;
195
+ /**
196
+ * Gets all nodes within a specified level range (inclusive).
197
+ * Performance optimized using the level property.
198
+ *
199
+ * @template T The type of data in the nodes.
200
+ * @param {number} minLevel - The minimum level (inclusive).
201
+ * @param {number} maxLevel - The maximum level (inclusive).
202
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
203
+ * @returns {NormalizedNode<T>[]} An array of nodes within the specified level range.
204
+ */
205
+ export declare function getNodesInLevelRange<T>(minLevel: number, maxLevel: number, itemsStore: Map<string, NormalizedNode<T>>): NormalizedNode<T>[];
206
+ /**
207
+ * Gets all ancestor nodes of a specified node.
208
+ * Performance optimized using the level property for early termination.
209
+ *
210
+ * @template T The type of data in the nodes.
211
+ * @param {string} itemId - The ID of the node to get ancestors for.
212
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
213
+ * @returns {NormalizedNode<T>[]} An array of ancestor nodes, ordered from root to immediate parent.
214
+ */
215
+ export declare function getAncestors<T>(itemId: string, itemsStore: Map<string, NormalizedNode<T>>): NormalizedNode<T>[];
216
+ /**
217
+ * Checks if a node is at a leaf level (has no children).
218
+ * This is a convenience function that leverages the existing childIds property.
219
+ *
220
+ * @template T The type of data in the nodes.
221
+ * @param {string} itemId - The ID of the node to check.
222
+ * @param {Map<string, NormalizedNode<T>>} itemsStore - The map of normalized nodes.
223
+ * @returns {boolean} True if the node is a leaf (has no children), false otherwise.
224
+ */
225
+ export declare function isLeafNode<T>(itemId: string, itemsStore: Map<string, NormalizedNode<T>>): boolean;