UNPKG

@edifice.io/tiptap-extensions

Version:
1 lines 4.14 kB
{"version":3,"file":"conversation-history.cjs","sources":["../../src/conversation-history/conversation-history.ts"],"sourcesContent":["/**\n * A Tiptap Node extension that creates a conversation history container.\n * This extension groups content blocks that appear after a horizontal rule\n * into a conversation history section.\n *\n * @extends Node\n *\n * @remarks\n * The node is rendered as a div element with the class 'conversation-history'.\n * It can parse HTML elements that match the same structure.\n *\n * For retrocompatibility, this extension also includes a ProseMirror plugin\n * that handles the grouping all the content after an horizontal rules into the\n * conversation-history children block.\n *\n * Usage:\n * ```\n * // Add to your Tiptap editor extensions\n * import { ConversationHistory } from './conversation-history'\n *\n * new Editor({\n * extensions: [\n * ConversationHistory,\n * // ... other extensions\n * ],\n * })\n * ```\n */\nimport { mergeAttributes, Node } from '@tiptap/core';\nimport { Plugin } from 'prosemirror-state';\n\nexport const ConversationHistory = Node.create({\n name: 'converstationHistory',\n group: 'block',\n content: 'block+',\n\n parseHTML() {\n return [\n {\n tag: 'div.conversation-history',\n },\n ];\n },\n\n renderHTML({ HTMLAttributes }) {\n return [\n 'div',\n mergeAttributes(HTMLAttributes, { class: 'conversation-history' }),\n 0,\n ];\n },\n\n /**\n * Adds custom ProseMirror plugins to the editor.\n * When a horizontal rule is encountered, this plugin collects all nodes\n * following the horizontal rule until the end of the document.\n * These nodes are then grouped together and replaced with\n * a single node of the same type as the plugin's type.\n *\n * @returns {Plugin[]} An array of ProseMirror plugins.\n */\n addProseMirrorPlugins() {\n return [\n new Plugin({\n appendTransaction: (transactions, oldState, newState) => {\n const tr = newState.tr;\n let modified = false;\n const nodesAfterHr = [];\n\n newState.doc.descendants((node, pos, parent) => {\n if (\n node.type.name === 'horizontalRule' &&\n parent.type.name === 'doc'\n ) {\n const start = pos;\n const end = newState.doc.content.size;\n\n newState.doc.nodesBetween(start, end, (n, p, parent) => {\n if (\n n.type.name !== 'horizontalRule' &&\n parent.type.name === 'doc'\n ) {\n nodesAfterHr.push({ node: n, pos: p });\n } else {\n return false;\n }\n });\n\n if (nodesAfterHr.length > 0) {\n const groupNode = this.type.create(\n {},\n nodesAfterHr.map((n) => n.node),\n );\n tr.replaceWith(start, end, groupNode);\n modified = true;\n }\n return false;\n }\n });\n\n return modified ? tr : null;\n },\n }),\n ];\n },\n});\n"],"names":["Node","mergeAttributes","Plugin","parent"],"mappings":"iKA+Ba,oBAAsBA,KAAAA,KAAK,OAAO,CAC7C,KAAM,uBACN,MAAO,QACP,QAAS,SAET,WAAY,CACV,MAAO,CACL,CACE,IAAK,0BAAA,CACP,CAEJ,EAEA,WAAW,CAAE,gBAAkB,CAC7B,MAAO,CACL,MACAC,KAAAA,gBAAgB,eAAgB,CAAE,MAAO,uBAAwB,EACjE,CAAA,CAEJ,EAWA,uBAAwB,CACtB,MAAO,CACL,IAAIC,wBAAO,CACT,kBAAmB,CAAC,aAAc,SAAU,WAAa,CACvD,MAAM,GAAK,SAAS,GACpB,IAAI,SAAW,GACf,MAAM,aAAe,CAAA,EAErB,gBAAS,IAAI,YAAY,CAAC,KAAM,IAAK,SAAW,CAC9C,GACE,KAAK,KAAK,OAAS,kBACnB,OAAO,KAAK,OAAS,MACrB,CACA,MAAM,MAAQ,IACR,IAAM,SAAS,IAAI,QAAQ,KAajC,GAXA,SAAS,IAAI,aAAa,MAAO,IAAK,CAAC,EAAG,EAAGC,UAAW,CACtD,GACE,EAAE,KAAK,OAAS,kBAChBA,QAAO,KAAK,OAAS,MAErB,aAAa,KAAK,CAAE,KAAM,EAAG,IAAK,EAAG,MAErC,OAAO,EAEX,CAAC,EAEG,aAAa,OAAS,EAAG,CAC3B,MAAM,UAAY,KAAK,KAAK,OAC1B,CAAA,EACA,aAAa,IAAK,GAAM,EAAE,IAAI,CAAA,EAEhC,GAAG,YAAY,MAAO,IAAK,SAAS,EACpC,SAAW,EACb,CACA,MAAO,EACT,CACF,CAAC,EAEM,SAAW,GAAK,IACzB,CAAA,CACD,CAAA,CAEL,CACF,CAAC"}