| 2664 | * @internal |
| 2665 | */ |
| 2666 | export function use( |
| 2667 | node: FormKitNode, |
| 2668 | context: FormKitContext, |
| 2669 | plugin: FormKitPlugin | FormKitPlugin[] | Set<FormKitPlugin>, |
| 2670 | run = true, |
| 2671 | library = true |
| 2672 | ): FormKitNode { |
| 2673 | if (Array.isArray(plugin) || plugin instanceof Set) { |
| 2674 | plugin.forEach((p: FormKitPlugin) => use(node, context, p)) |
| 2675 | return node |
| 2676 | } |
| 2677 | if (!context.plugins.has(plugin)) { |
| 2678 | if (library && typeof plugin.library === 'function') plugin.library(node) |
| 2679 | // When plugins return false, they are never added as to the plugins Set |
| 2680 | // meaning they only ever have access to the single node they were added on. |
| 2681 | if (run && plugin(node) !== false) { |
| 2682 | context.plugins.add(plugin) |
| 2683 | node.children.forEach((child) => child.use(plugin)) |
| 2684 | } |
| 2685 | } |
| 2686 | return node |
| 2687 | } |
| 2688 | |
| 2689 | /** |
| 2690 | * Moves a node in the parent’s children to the given index. |