{"version":3,"sources":["../src/lib/normalize-types/NormalizeTypesPlugin.ts","../src/lib/normalize-types/withNormalizeTypes.ts","../src/lib/remove-empty-nodes/RemoveEmptyNodesPlugin.ts","../src/lib/remove-empty-nodes/withRemoveEmptyNodes.ts"],"sourcesContent":["import {\n  type Path,\n  type PluginConfig,\n  createTSlatePlugin,\n} from '@udecode/plate';\n\nimport { withNormalizeTypes } from './withNormalizeTypes';\n\nexport type NormalizeTypesConfig = PluginConfig<\n  'normalizeTypes',\n  {\n    /**\n     * Set of rules for the types. For each rule, provide a `path` and either\n     * `strictType` or `type`. If there is no node existing at `path`: insert a\n     * node with `strictType`. If there is a node existing at `path` but its\n     * type is not `strictType` or `type`: set the node type to `strictType` or\n     * `type`.\n     */\n    rules?: Rule[];\n    onError?: (err: any) => void;\n  }\n>;\n\ninterface Rule {\n  /** Path where the rule applies */\n  path: Path;\n  /** Force the type of the node at the given path */\n  strictType?: string;\n  /** Type of the inserted node at the given path if `strictType` is not provided */\n  type?: string;\n}\n\n/** @see {@link withNormalizeTypes} */\nexport const NormalizeTypesPlugin = createTSlatePlugin<NormalizeTypesConfig>({\n  key: 'normalizeTypes',\n  options: {\n    rules: [],\n  },\n}).overrideEditor(withNormalizeTypes);\n","import {\n  type OverrideEditor,\n  type TElement,\n  ElementApi,\n  NodeApi,\n} from '@udecode/plate';\n\nimport type { NormalizeTypesConfig } from './NormalizeTypesPlugin';\n\nexport const withNormalizeTypes: OverrideEditor<NormalizeTypesConfig> = ({\n  editor,\n  getOptions,\n  tf: { normalizeNode },\n}) => ({\n  transforms: {\n    normalizeNode([currentNode, currentPath]) {\n      const { rules, onError } = getOptions();\n\n      if (currentPath.length === 0) {\n        const endCurrentNormalizationPass = rules!.some(\n          ({ path, strictType, type }) => {\n            const node = NodeApi.get<TElement>(editor, path);\n\n            if (node) {\n              if (\n                strictType &&\n                ElementApi.isElement(node) &&\n                node.type !== strictType\n              ) {\n                const { children, ...props } = editor.api.create.block({\n                  type: strictType,\n                });\n                editor.tf.setNodes(props, {\n                  at: path,\n                });\n\n                return true;\n              }\n            } else {\n              try {\n                editor.tf.insertNodes(\n                  editor.api.create.block({ type: strictType ?? type! }),\n                  { at: path }\n                );\n\n                return true;\n              } catch (error) {\n                onError?.(error);\n              }\n            }\n\n            return false;\n          }\n        );\n\n        if (endCurrentNormalizationPass) {\n          return;\n        }\n      }\n\n      return normalizeNode([currentNode, currentPath]);\n    },\n  },\n});\n","import { type PluginConfig, createTSlatePlugin } from '@udecode/plate';\n\nimport { withRemoveEmptyNodes } from './withRemoveEmptyNodes';\n\nexport type RemoveEmptyNodesConfig = PluginConfig<\n  'removeEmptyNodes',\n  {\n    types?: string[] | string;\n  }\n>;\n\n/** @see {@link withRemoveEmptyNodes} */\nexport const RemoveEmptyNodesPlugin =\n  createTSlatePlugin<RemoveEmptyNodesConfig>({\n    key: 'removeEmptyNodes',\n  }).overrideEditor(withRemoveEmptyNodes);\n","import { type OverrideEditor, ElementApi, NodeApi } from '@udecode/plate';\nimport castArray from 'lodash/castArray.js';\n\nimport type { RemoveEmptyNodesConfig } from './RemoveEmptyNodesPlugin';\n\n/** Remove nodes with empty text. */\nexport const withRemoveEmptyNodes: OverrideEditor<RemoveEmptyNodesConfig> = ({\n  editor,\n  getOptions,\n  tf: { normalizeNode },\n}) => ({\n  transforms: {\n    normalizeNode([node, path]) {\n      const types = castArray(getOptions().types ?? []);\n\n      if (\n        ElementApi.isElement(node) &&\n        node.type &&\n        types.includes(node.type) &&\n        NodeApi.string(node) === ''\n      ) {\n        editor.tf.removeNodes({ at: path });\n\n        return;\n      }\n\n      normalizeNode([node, path]);\n    },\n  },\n});\n"],"mappings":";AAAA;AAAA,EAGE;AAAA,OACK;;;ACJP;AAAA,EAGE;AAAA,EACA;AAAA,OACK;AAIA,IAAM,qBAA2D,CAAC;AAAA,EACvE;AAAA,EACA;AAAA,EACA,IAAI,EAAE,cAAc;AACtB,OAAO;AAAA,EACL,YAAY;AAAA,IACV,cAAc,CAAC,aAAa,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