UNPKG

6.18 kBSource Map (JSON)View Raw
1{"version":3,"file":"embed.js","names":["EmbedBlot","TextBlot","GUARD_TEXT","Embed","constructor","scroll","node","contentNode","document","createElement","setAttribute","Array","from","domNode","childNodes","forEach","childNode","appendChild","leftGuard","createTextNode","rightGuard","index","offset","restore","range","textNode","text","data","split","join","prev","prevLength","length","insertAt","startNode","startOffset","parent","insertBefore","create","next","update","mutations","context","mutation","type","target"],"sources":["../../src/blots/embed.ts"],"sourcesContent":["import type { ScrollBlot } from 'parchment';\nimport { EmbedBlot } from 'parchment';\nimport TextBlot from './text.js';\n\nconst GUARD_TEXT = '\\uFEFF';\n\nexport interface EmbedContextRange {\n startNode: Node | Text;\n startOffset: number;\n endNode?: Node | Text;\n endOffset?: number;\n}\n\nclass Embed extends EmbedBlot {\n contentNode: HTMLSpanElement;\n leftGuard: Text;\n rightGuard: Text;\n\n constructor(scroll: ScrollBlot, node: Node) {\n super(scroll, node);\n this.contentNode = document.createElement('span');\n this.contentNode.setAttribute('contenteditable', 'false');\n Array.from(this.domNode.childNodes).forEach((childNode) => {\n this.contentNode.appendChild(childNode);\n });\n this.leftGuard = document.createTextNode(GUARD_TEXT);\n this.rightGuard = document.createTextNode(GUARD_TEXT);\n this.domNode.appendChild(this.leftGuard);\n this.domNode.appendChild(this.contentNode);\n this.domNode.appendChild(this.rightGuard);\n }\n\n index(node: Node, offset: number) {\n if (node === this.leftGuard) return 0;\n if (node === this.rightGuard) return 1;\n return super.index(node, offset);\n }\n\n restore(node: Text): EmbedContextRange | null {\n let range: EmbedContextRange | null = null;\n let textNode: Text;\n const text = node.data.split(GUARD_TEXT).join('');\n if (node === this.leftGuard) {\n if (this.prev instanceof TextBlot) {\n const prevLength = this.prev.length();\n this.prev.insertAt(prevLength, text);\n range = {\n startNode: this.prev.domNode,\n startOffset: prevLength + text.length,\n };\n } else {\n textNode = document.createTextNode(text);\n this.parent.insertBefore(this.scroll.create(textNode), this);\n range = {\n startNode: textNode,\n startOffset: text.length,\n };\n }\n } else if (node === this.rightGuard) {\n if (this.next instanceof TextBlot) {\n this.next.insertAt(0, text);\n range = {\n startNode: this.next.domNode,\n startOffset: text.length,\n };\n } else {\n textNode = document.createTextNode(text);\n this.parent.insertBefore(this.scroll.create(textNode), this.next);\n range = {\n startNode: textNode,\n startOffset: text.length,\n };\n }\n }\n node.data = GUARD_TEXT;\n return range;\n }\n\n update(mutations: MutationRecord[], context: Record<string, unknown>) {\n mutations.forEach((mutation) => {\n if (\n mutation.type === 'characterData' &&\n (mutation.target === this.leftGuard ||\n mutation.target === this.rightGuard)\n ) {\n const range = this.restore(mutation.target as Text);\n if (range) context.range = range;\n }\n });\n }\n}\n\nexport default Embed;\n"],"mappings":"AACA,SAASA,SAAS,QAAQ,WAAW;AACrC,OAAOC,QAAQ,MAAM,WAAW;AAEhC,MAAMC,UAAU,GAAG,QAAQ;AAS3B,MAAMC,KAAK,SAASH,SAAS,CAAC;EAK5BI,WAAWA,CAACC,MAAkB,EAAEC,IAAU,EAAE;IAC1C,KAAK,CAACD,MAAM,EAAEC,IAAI,CAAC;IACnB,IAAI,CAACC,WAAW,GAAGC,QAAQ,CAACC,aAAa,CAAC,MAAM,CAAC;IACjD,IAAI,CAACF,WAAW,CAACG,YAAY,CAAC,iBAAiB,EAAE,OAAO,CAAC;IACzDC,KAAK,CAACC,IAAI,CAAC,IAAI,CAACC,OAAO,CAACC,UAAU,CAAC,CAACC,OAAO,CAAEC,SAAS,IAAK;MACzD,IAAI,CAACT,WAAW,CAACU,WAAW,CAACD,SAAS,CAAC;IACzC,CAAC,CAAC;IACF,IAAI,CAACE,SAAS,GAAGV,QAAQ,CAACW,cAAc,CAACjB,UAAU,CAAC;IACpD,IAAI,CAACkB,UAAU,GAAGZ,QAAQ,CAACW,cAAc,CAACjB,UAAU,CAAC;IACrD,IAAI,CAACW,OAAO,CAACI,WAAW,CAAC,IAAI,CAACC,SAAS,CAAC;IACxC,IAAI,CAACL,OAAO,CAACI,WAAW,CAAC,IAAI,CA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