UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

50 lines (49 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const core = require("@tiptap/core"); const state = require("@tiptap/pm/state"); const utils = require("./utils.cjs"); function autolink(options) { let hasInitialized = false; return new state.Plugin({ key: new state.PluginKey("autolink"), appendTransaction: (transactions, oldState, newState) => { const contentChanged = transactions.some((tr2) => tr2.docChanged) && !oldState.doc.eq(newState.doc); if (hasInitialized && !contentChanged) { return; } const { tr } = newState; const { textContent } = newState.doc; if (!hasInitialized) { utils.addMarks(textContent, 0, 0, textContent.length, tr, options.type); } hasInitialized = true; const transform = core.combineTransactionSteps( oldState.doc, [...transactions] ); const changes = core.getChangedRanges(transform); changes.forEach(({ oldRange, newRange }) => { utils.removeMarks(newRange, newState.doc, tr, options.type); const paragraphs = core.findChildrenInRange( newState.doc, newRange, (node) => node.isTextblock ); paragraphs.forEach(({ node, pos }) => { utils.addMarks( node.textContent, pos, oldRange.from, newRange.to, tr, options.type ); }); }); return tr; } }); } exports.autolink = autolink; //# sourceMappingURL=autolink.cjs.map