{"version":3,"file":"index.cjs","names":["Extension","PluginKey","Plugin"],"sources":["../../src/trailing-node/trailing-node.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport type { Node, NodeType } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\n\nexport const skipTrailingNodeMeta = 'skipTrailingNode'\n\nfunction nodeEqualsType({\n types,\n node,\n}: {\n types: NodeType | NodeType[]\n node: Node | null | undefined\n}) {\n return (node && Array.isArray(types) && types.includes(node.type)) || node?.type === types\n}\n\n/**\n * Extension based on:\n * - https://github.com/ueberdosis/tiptap/blob/v1/packages/tiptap-extensions/src/extensions/TrailingNode.js\n * - https://github.com/remirror/remirror/blob/e0f1bec4a1e8073ce8f5500d62193e52321155b9/packages/prosemirror-trailing-node/src/trailing-node-plugin.ts\n */\n\nexport interface TrailingNodeOptions {\n /**\n * The node type that should be inserted at the end of the document.\n * @note the node will always be added to the `notAfter` lists to\n * prevent an infinite loop.\n * @default undefined\n */\n node?: string\n /**\n * The node types after which the trailing node should not be inserted.\n * @default ['paragraph']\n */\n notAfter?: string | string[]\n}\n\n/**\n * This extension allows you to add an extra node at the end of the document.\n * @see https://www.tiptap.dev/api/extensions/trailing-node\n */\nexport const TrailingNode = Extension.create({\n name: 'trailingNode',\n\n addOptions() {\n return {\n node: undefined,\n notAfter: [],\n }\n },\n\n addProseMirrorPlugins() {\n const plugin = new PluginKey(this.name)\n const defaultNode =\n this.options.node ||\n this.editor.schema.topNodeType.contentMatch.defaultType?.name ||\n 'paragraph'\n\n const disabledNodes = Object.entries(this.editor.schema.nodes)\n .map(([, value]) => value)\n .filter(node => (this.options.notAfter || []).concat(defaultNode).includes(node.name))\n\n return [\n new Plugin({\n key: plugin,\n appendTransaction: (transactions, __, state) => {\n const { doc, tr, schema } = state\n const shouldInsertNodeAtEnd = plugin.getState(state)\n const endPosition = doc.content.size\n const type = schema.nodes[defaultNode]\n\n if (transactions.some(transaction => transaction.getMeta(skipTrailingNodeMeta))) {\n return\n }\n\n if (!shouldInsertNodeAtEnd) {\n return\n }\n\n return tr.insert(endPosition, type.create())\n },\n state: {\n init: (_, state) => {\n const lastNode = state.tr.doc.lastChild\n\n return !nodeEqualsType({ node: lastNode, types: disabledNodes })\n },\n apply: (tr, value) => {\n if (!tr.docChanged) {\n return value\n }\n\n // Ignore transactions from UniqueID extension to prevent infinite loops\n // when UniqueID adds IDs to newly inserted trailing nodes\n if (tr.getMeta('__uniqueIDTransaction')) {\n return value\n }\n\n const lastNode = tr.doc.lastChild\n\n return !nodeEqualsType({ node: lastNode, types: disabledNodes })\n },\n },\n }),\n ]\n },\n})\n"],"mappings":";;;;AAIA,MAAa,uBAAuB;AAEpC,SAAS,eAAe,EACtB,OACA,QAIC;CACD,OAAQ,QAAQ,MAAM,QAAQ,KAAK,KAAK,MAAM,SAAS,KAAK,IAAI,MAAA,SAAA,QAAA,SAAA,KAAA,IAAA,KAAA,IAAM,KAAM,UAAS;AACvF;;;;;AA2BA,MAAa,eAAeA,aAAAA,UAAU,OAA4B;CAChE,MAAM;CAEN,aAAa;EACX,OAAO;GACL,MAAM,KAAA;GACN,UAAU,CAAC;EACb;CACF;CAEA,wBAAwB;;EACtB,MAAM,SAAS,IAAIC,iBAAAA,UAAU,KAAK,IAAI;EACtC,MAAM,cACJ,KAAK,QAAQ,UAAA,wBACb,KAAK,OAAO,OAAO,YAAY,aAAa,iBAAA,QAAA,0BAAA,KAAA,IAAA,KAAA,IAAA,sBAAa,SACzD;EAEF,MAAM,gBAAgB,OAAO,QAAQ,KAAK,OAAO,OAAO,KAAK,CAAC,CAC3D,KAAK,GAAG,WAAW,KAAK,CAAC,CACzB,QAAO,UAAS,KAAK,QAAQ,YAAY,CAAC,EAAA,CAAG,OAAO,WAAW,CAAC,CAAC,SAAS,KAAK,IAAI,CAAC;EAEvF,OAAO,CACL,IAAIC,iBAAAA,OAAO;GACT,KAAK;GACL,oBAAoB,cAAc,IAAI,UAAU;IAC9C,MAAM,EAAE,KAAK,IAAI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