{"version":3,"sources":["../src/lib/BaseResetNodePlugin.ts"],"sourcesContent":["import {\n  type PluginConfig,\n  type TElement,\n  createTSlatePlugin,\n  NodeApi,\n  PointApi,\n  RangeApi,\n} from '@udecode/plate';\n\nimport type { ResetNodePluginRule } from './types';\n\nexport type ResetNodeConfig = PluginConfig<\n  'resetNode',\n  {\n    disableEditorReset?: boolean;\n    disableFirstBlockReset?: boolean;\n    rules?: ResetNodePluginRule[];\n  }\n>;\n\n/** Enables support for resetting block type from rules. */\nexport const BaseResetNodePlugin = createTSlatePlugin<ResetNodeConfig>({\n  key: 'resetNode',\n  editOnly: true,\n  options: {\n    rules: [],\n  },\n}).overrideEditor(\n  ({ editor, getOptions, tf: { deleteBackward, deleteFragment } }) => ({\n    transforms: {\n      deleteBackward(unit) {\n        if (!getOptions().disableFirstBlockReset) {\n          const { selection } = editor;\n\n          if (selection && editor.api.isCollapsed()) {\n            const start = editor.api.start([])!;\n\n            if (PointApi.equals(selection.anchor, start)) {\n              const node = NodeApi.get<TElement>(editor, [0])!;\n\n              const { children, ...props } = editor.api.create.block({}, [0]);\n\n              // replace props\n              editor.tf.withoutNormalizing(() => {\n                // missing id will cause block selection not working and other issues\n                const { id, ...nodeProps } = NodeApi.extractProps(node);\n\n                editor.tf.unsetNodes(Object.keys(nodeProps), { at: [0] });\n                editor.tf.setNodes(props, { at: [0] });\n              });\n\n              return;\n            }\n          }\n        }\n\n        deleteBackward(unit);\n      },\n\n      deleteFragment(direction) {\n        const deleteFragmentPlugin = () => {\n          const { selection } = editor;\n\n          if (!selection) return;\n          if (RangeApi.equals(selection, editor.api.range([])!)) {\n            editor.tf.reset({\n              children: true,\n              select: true,\n            });\n\n            return true;\n          }\n        };\n\n        if (!getOptions().disableEditorReset && deleteFragmentPlugin()) return;\n\n        deleteFragment(direction);\n      },\n    },\n  })\n);\n"],"mappings":";AAAA;AAAA,EAGE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAcA,IAAM,sBAAsB,mB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