{"version":3,"file":"autolink.cjs","sources":["../../../../../components/rich_text_editor/extensions/custom_link/autolink.js"],"sourcesContent":["import {\n  combineTransactionSteps,\n  findChildrenInRange,\n  getChangedRanges,\n} from '@tiptap/core';\nimport {\n  Plugin,\n  PluginKey,\n} from '@tiptap/pm/state';\nimport {\n  addMarks,\n  removeMarks,\n} from './utils';\n\n/**\n * Plugin to automatically add links into content.\n */\nexport function autolink (options) {\n  // Flag to see if we've loaded this plugin once already. This is used to run\n  // the initial content through the plugin if the editor was mounted with some.\n  let hasInitialized = false;\n\n  return new Plugin({\n    key: new PluginKey('autolink'),\n\n    appendTransaction: (transactions, oldState, newState) => {\n      const contentChanged = transactions.some(tr => tr.docChanged) &&\n        !oldState.doc.eq(newState.doc);\n\n      // Every interaction with the editor is a transaction, but we only care\n      // about the ones with content changes.\n      if (hasInitialized && !contentChanged) {\n        return;\n      }\n\n      // The original transaction that we're manipulating.\n      const { tr } = newState;\n\n      // Text content after the original transaction.\n      const { textContent } = newState.doc;\n\n      // When the editor is initialized we want to add links to it.\n      if (!hasInitialized) {\n        addMarks(textContent, 0, 0, textContent.length, tr, options.type);\n      }\n\n      hasInitialized = true;\n\n      // The transformed state of the document.\n      const transform = combineTransactionSteps(\n        oldState.doc,\n        [...transactions],\n      );\n\n      // All the changes within the document.\n      const changes = getChangedRanges(transform);\n\n      changes.forEach(({ oldRange, newRange }) => {\n        // Remove all link marks in the changed range since we'll add them\n        // right back if they're still valid links.\n        removeMarks(newRange, newState.doc, tr, options.type);\n\n        // Find all paragraphs (Textblocks) that were affected since we want to\n        // handle matches in each paragraph separately.\n        const paragraphs = findChildrenInRange(\n          newState.doc,\n          newRange,\n          node => node.isTextblock,\n        );\n\n        paragraphs.forEach(({ node, pos }) => {\n          addMarks(\n            node.textContent,\n            pos,\n            oldRange.from,\n            newRange.to,\n            tr,\n            options.type,\n          );\n        });\n      });\n\n      // Return the modified transaction or the changes above wont have effect.\n      return tr;\n    },\n  });\n}\n"],"names":["Plugin","PluginKey","tr","addMarks","combineTransactionSteps","getChangedRanges","removeMarks","findChildrenInRange"],"mappings":";;;;;AAiBO,SAAS,SAAU,SAAS;AAGjC,MAAI,iBAAiB;AAErB,SAAO,IAAIA,MAAAA,OAAO;AAAA,IAChB,KAAK,IAAIC,MAAS,UAAC,UAAU;AAAA,IAE7B,mBAAmB,CAAC,cAAc,UAAU,aAAa;AACvD,YAAM,iBAAiB,aAAa,KAAK,CAAAC,QAAMA,IAAG,UAAU,KAC1D,CAAC,SAAS,IAAI,GAAG,SAAS,GAAG;AAI/B,UAAI,kBAAkB,CAAC,gBAAgB;AACrC;AAAA,MACD;AAGD,YAAM,EAAE,GAAI,IAAG;AAGf,YAAM,EAAE,YAAW,IAAK,SAAS;AAGjC,UAAI,CAAC,gBAAgB;AACnBC,uBAAS,aAAa,GAAG,GAAG,YAAY,QAAQ,IAAI,QAAQ,IAAI;AAAA,MACjE;AAED,uBAAiB;AAGjB,YAAM,YAAYC,KAAuB;AAAA,QACvC,SAAS;AAAA,QACT,CAAC,GAAG,YAAY;AAAA,MACxB;AAGM,YAAM,UAAUC,sBAAiB,SAAS;AAE1C,cAAQ,QAAQ,CAAC,EAAE,UAAU,SAAQ,MAAO;AAG1CC,cAAW,YAAC,UAAU,SAAS,KAAK,IAAI,QAAQ,IAAI;AAIpD,cAAM,aAAaC,KAAmB;AAAA,UACpC,SAAS;AAAA,UACT;AAAA,UACA,UAAQ,KAAK;AAAA,QACvB;AAEQ,mBAAW,QAAQ,CAAC,EAAE,MAAM,IAAG,MAAO;AACpCJ,gBAAQ;AAAA,YACN,KAAK;AAAA,YACL;AAAA,YACA,SAAS;AAAA,YACT,SAAS;AAAA,YACT;AAAA,YACA,QAAQ;AAAA,UACpB;AAAA,QACA,CAAS;AAAA,MACT,CAAO;AAGD,aAAO;AAAA,IACR;AAAA,EACL,CAAG;AACH;;"}