{"version":3,"sources":["../../src/color/index.ts","../../src/text-style/index.ts","../../src/color/color.ts"],"sourcesContent":["import { Color } from './color.js'\n\nexport * from './color.js'\n\nexport default Color\n","import { Mark, mergeAttributes } from '@tiptap/core'\n\nimport type { TextStyleAttributes } from '../index.js'\n\nexport interface TextStyleOptions {\n  /**\n   * HTML attributes to add to the span element.\n   * @default {}\n   * @example { class: 'foo' }\n   */\n  HTMLAttributes: Record<string, any>\n  /**\n   * When enabled, merges the styles of nested spans into the child span during HTML parsing.\n   * This prioritizes the style of the child span.\n   * Used when parsing content created in other editors.\n   * (Fix for ProseMirror's default behavior.)\n   * @default true\n   */\n  mergeNestedSpanStyles: boolean\n}\n\ndeclare module '@tiptap/core' {\n  interface Commands<ReturnType> {\n    textStyle: {\n      /**\n       * Remove spans without inline style attributes.\n       * @example editor.commands.removeEmptyTextStyle()\n       */\n      removeEmptyTextStyle: () => ReturnType\n      /**\n       * Toggle a text style\n       * @param attributes The text style attributes\n       * @example editor.commands.toggleTextStyle({ fontWeight: 'bold' })\n       */\n      toggleTextStyle: (attributes?: TextStyleAttributes) => ReturnType\n    }\n  }\n}\n\nconst MAX_FIND_CHILD_SPAN_DEPTH = 20\n\n/**\n * Returns all next child spans, either direct children or nested deeper\n * but won't traverse deeper into child spans found, will only go MAX_FIND_CHILD_SPAN_DEPTH levels deep (default: 20)\n */\nconst findChildSpans = (element: HTMLElement, depth = 0): HTMLElement[] => {\n  const childSpans: HTMLElement[] = []\n\n  if (!element.children.length || depth > MAX_FIND_CHILD_SPAN_DEPTH) {\n    return childSpans\n  }\n\n  Array.from(element.children).forEach(child => {\n    if (child.tagName === 'SPAN') {\n      childSpans.push(child as HTMLElement)\n    } else if (child.children.length) {\n      childSpans.push(...findChildSpans(child as HTMLElement, depth + 1))\n    }\n  })\n\n  return childSpans\n}\n\nconst mergeNestedSpanStyles = (element: HTMLElement) => {\n  if (!element.children.length) {\n    return\n  }\n\n  const childSpans = findChildSpans(element)\n\n  if (!childSpans) {\n    return\n  }\n\n  childSpans.forEach(childSpan => {\n    const childStyle = childSpan.getAttribute('style')\n    const closestParentSpanStyleOfChild = childSpan.parentElement?.closest('span')?.getAttribute('style')\n\n    childSpan.setAttribute('style', `${closestParentSpanStyleOfChild};${childStyle}`)\n  })\n}\n\n/**\n * This extension allows you to create text styles. It is required by default\n * for the `text-color` and `font-family` extensions.\n * @see https://www.tiptap.dev/api/marks/text-style\n */\nexport const TextStyle = Mark.create<TextStyleOptions>({\n  name: 'textStyle',\n\n  priority: 101,\n\n  addOptions() {\n    return {\n      HTMLAttributes: {},\n      mergeNestedSpanStyles: true,\n    }\n  },\n\n  parseHTML() {\n    return [\n      {\n        tag: 'span',\n        consuming: false,\n        getAttrs: element => {\n          const hasStyles = (element as HTMLElement).hasAttribute('style')\n\n          if (!hasStyles) {\n            return false\n          }\n\n          if (this.options.mergeNestedSpanStyles) {\n            mergeNestedSpanStyles(element)\n          }\n\n          return {}\n        },\n      },\n    ]\n  },\n\n  renderHTML({ HTMLAttributes }) {\n    return ['span', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]\n  },\n\n  addCommands() {\n    return {\n      toggleTextStyle:\n        attributes =>\n        ({ commands }) => {\n          return commands.toggleMark(this.name, attributes)\n        },\n      removeEmptyTextStyle:\n        () =>\n        ({ tr }) => {\n          const { selection } = tr\n\n          // Gather all of the nodes within the selection range.\n          // We would need to go through each node individually\n          // to check if it has any inline style attributes.\n          // Otherwise, calling commands.unsetMark(this.name)\n          // removes everything from all the nodes\n          // within the selection range.\n          tr.doc.nodesBetween(selection.from, selection.to, (node, pos) => {\n            // Check if it's a paragraph element, if so, skip this node as we apply\n            // the text style to inline text nodes only (span).\n            if (node.isTextblock) {\n              return true\n            }\n\n            // Check if the node has no inline style attributes.\n            // Filter out non-`textStyle` marks.\n            if (\n              !node.marks\n                .filter(mark => mark.type === this.type)\n                .some(mark => Object.values(mark.attrs).some(value => !!value))\n            ) {\n              // Proceed with the removal of the `textStyle` mark for this node only\n              tr.removeMark(pos, pos + node.nodeSize, this.type)\n            }\n          })\n\n          return true\n        },\n    }\n  },\n})\n","import '../text-style/index.js'\n\nimport { Extension } from '@tiptap/core'\n\nexport type ColorOptions = {\n  /**\n   * The types where the color can be applied\n   * @default ['textStyle']\n   * @example ['heading', 'paragraph']\n   */\n  types: string[]\n}\n\ndeclare module '@tiptap/core' {\n  interface Commands<ReturnType> {\n    color: {\n      /**\n       * Set the text color\n       * @param color The color to set\n       * @example editor.commands.setColor('red')\n       */\n      setColor: (color: string) => ReturnType\n\n      /**\n       * Unset the text color\n       * @example editor.commands.unsetColor()\n       */\n      unsetColor: () => ReturnType\n    }\n  }\n}\n\n// @ts-ignore because the module is not found during dts build\ndeclare module '@tiptap/extension-text-style' {\n  interface TextStyleAttributes {\n    color?: string | null\n  }\n}\n\n/**\n * This extension allows you to color your text.\n * @see https://tiptap.dev/api/extensions/color\n */\nexport const Color = Extension.create<ColorOptions>({\n  name: 'color',\n\n  addOptions() {\n    return {\n      types: ['textStyle'],\n    }\n  },\n\n  addGlobalAttributes() {\n    return [\n      {\n        types: this.options.types,\n        attributes: {\n          color: {\n            default: null,\n            parseHTML: element => {\n              // Prefer the raw inline `style` attribute so we preserve\n              // the original format (e.g. `#rrggbb`) instead of the\n              // computed `rgb(...)` value returned by `element.style.color`.\n              // When nested spans are merged the style attribute may contain\n              // multiple `color:` declarations (parent;child). We should pick\n              // the last declaration so the child's color takes priority.\n              const styleAttr = element.getAttribute('style')\n              if (styleAttr) {\n                const decls = styleAttr\n                  .split(';')\n                  .map(s => s.trim())\n                  .filter(Boolean)\n                for (let i = decls.length - 1; i >= 0; i -= 1) {\n                  const parts = decls[i].split(':')\n                  if (parts.length >= 2) {\n                    const prop = parts[0].trim().toLowerCase()\n                    const val = parts.slice(1).join(':').trim()\n                    if (prop === 'color') {\n                      return val.replace(/['\"]+/g, '')\n                    }\n                  }\n                }\n              }\n\n              return element.style.color?.replace(/['\"]+/g, '')\n            },\n            renderHTML: attributes => {\n              if (!attributes.color) {\n                return {}\n              }\n\n              return {\n                style: `color: ${attributes.color}`,\n              }\n            },\n          },\n        },\n      },\n    ]\n  },\n\n  addCommands() {\n    return {\n      setColor:\n        color =>\n        ({ chain }) => {\n          return chain().setMark('textStyle', { color }).run()\n        },\n      unsetColor:\n        () =>\n        ({ chain }) => {\n          return chain().setMark('textStyle', { color: null }).removeEmptyTextStyle().run()\n        },\n    }\n  },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAsC;AAuCtC,IAAM,4BAA4B;AAMlC,IAAM,iBAAiB,CAAC,SAAsB,QAAQ,MAAqB;AACzE,QAAM,aAA4B,CAAC;AAEnC,MAAI,CAAC,QAAQ,SAAS,UAAU,QAAQ,2BAA2B;AACjE,WAAO;AAAA,EACT;AAEA,QAAM,KAAK,QAAQ,QAAQ,EAAE,QAAQ,WAAS;AAC5C,QAAI,MAAM,YAAY,QAAQ;AAC5B,iBAAW,KAAK,KAAoB;AAAA,IACtC,WAAW,MAAM,SAAS,QAAQ;AAChC,iBAAW,KAAK,GAAG,eAAe,OAAsB,QAAQ,CAAC,CAAC;AAAA,IACpE;AAAA,EACF,CAAC;AAED,SAAO;AACT;AAEA,IAAM,wBAAwB,CAAC,YAAyB;AACtD,MAAI,CAAC,QAAQ,SAAS,QAAQ;AAC5B;AAAA,EACF;AAEA,QAAM,aAAa,eAAe,OAAO;AAEzC,MAAI,CAAC,YAAY;AACf;AAAA,EACF;AAEA,aAAW,QAAQ,eAAa;AA1ElC;AA2EI,UAAM,aAAa,UAAU,aAAa,OAAO;AACjD,UAAM,iCAAgC,qBAAU,kBAAV,mBAAyB,QAAQ,YAAjC,mBAA0C,aAAa;AAE7F,cAAU,aAAa,SAAS,GAAG,6BAA6B,IAAI,UAAU,EAAE;AAAA,EAClF,CAAC;AACH;AAOO,IAAM,YAAY,iBAAK,OAAyB;AAAA,EACrD,MAAM;AAAA,EAEN,UAAU;AA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