UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

50 lines (49 loc) 1.52 kB
import { combineTransactionSteps, getChangedRanges, findChildrenInRange } from "@tiptap/core"; import { Plugin, PluginKey } from "@tiptap/pm/state"; import { addMarks, removeMarks } from "./utils.js"; function autolink(options) { let hasInitialized = false; return new Plugin({ key: new 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) { addMarks(textContent, 0, 0, textContent.length, tr, options.type); } hasInitialized = true; const transform = combineTransactionSteps( oldState.doc, [...transactions] ); const changes = getChangedRanges(transform); changes.forEach(({ oldRange, newRange }) => { removeMarks(newRange, newState.doc, tr, options.type); const paragraphs = findChildrenInRange( newState.doc, newRange, (node) => node.isTextblock ); paragraphs.forEach(({ node, pos }) => { addMarks( node.textContent, pos, oldRange.from, newRange.to, tr, options.type ); }); }); return tr; } }); } export { autolink }; //# sourceMappingURL=autolink.js.map