{"version":3,"sources":["../../src/background-color/plugin/with-background-color.ts","../../src/background-color/plugin/background-color-editor.ts","../../src/background-color/options.ts"],"sourcesContent":["import { colord } from 'colord'\nimport { Editor } from '@editablejs/models'\nimport { Editable, RenderLeafProps, Hotkey } from '@editablejs/editor'\nimport { BACKGROUND_COLOR_KEY } from '../constants'\nimport { BackgroundColorEditor } from './background-color-editor'\nimport { BackgroundColor } from '../interfaces/background-color'\nimport { BackgroundColorHotkey, BackgroundColorOptions, setOptions } from '../options'\n\nconst defaultHotkeys: BackgroundColorHotkey = {}\n\nexport const withBackgroundColor = <T extends Editable>(\n  editor: T,\n  options: BackgroundColorOptions = {},\n) => {\n  const newEditor = editor as T & BackgroundColorEditor\n\n  setOptions(newEditor, options)\n\n  newEditor.toggleBackgroundColor = (color: string) => {\n    editor.normalizeSelection(selection => {\n      if (editor.selection !== selection) editor.selection = selection\n      const { defaultColor } = BackgroundColorEditor.getOptions(editor)\n      if (defaultColor && colord(color).toRgbString() === colord(defaultColor).toRgbString()) {\n        Editor.removeMark(editor, BACKGROUND_COLOR_KEY)\n      } else {\n        Editor.addMark(editor, BACKGROUND_COLOR_KEY, color)\n      }\n    })\n  }\n\n  const { renderLeaf } = newEditor\n\n  newEditor.renderLeaf = ({ attributes, children, text }: RenderLeafProps) => {\n    const style: typeof attributes.style = attributes.style ?? {}\n    if (BackgroundColor.isBackgroundColor(text)) {\n      style.backgroundColor = text.backgroundColor\n    }\n    return renderLeaf({ attributes: Object.assign({}, attributes, { style }), children, text })\n  }\n\n  const { onKeydown } = newEditor\n\n  const hotkeys: BackgroundColorHotkey = Object.assign({}, defaultHotkeys, options.hotkey)\n  newEditor.onKeydown = (e: KeyboardEvent) => {\n    const value = Hotkey.match(hotkeys, e)\n    if (value) {\n      e.preventDefault()\n      newEditor.toggleBackgroundColor(value)\n      return\n    }\n    onKeydown(e)\n  }\n  return newEditor\n}\n","import { Editor } from '@editablejs/models'\nimport { BACKGROUND_COLOR_KEY } from '../constants'\nimport { BackgroundColor } from '../interfaces/background-color'\nimport { getOptions } from '../options'\n\nexport interface BackgroundColorEditor extends Editor {\n  toggleBackgroundColor: (color: string) => void\n}\n\nexport const BackgroundColorEditor = {\n  isBackgroundColorEditor: (editor: Editor): editor is BackgroundColorEditor => {\n    return !!(editor as BackgroundColorEditor).toggleBackgroundColor\n  },\n\n  isBackgroundColor: (editor: Editor, value: any): value is BackgroundColor => {\n    return BackgroundColor.isBackgroundColor(value)\n  },\n\n  queryActive: (editor: Editor) => {\n    const marks = Editor.marks(editor) as Partial<BackgroundColor>\n    return marks[BACKGROUND_COLOR_KEY] ?? null\n  },\n\n  toggle: (editor: Editor, size: string) => {\n    if (BackgroundColorEditor.isBackgroundColorEditor(editor)) editor.toggleBackgroundColor(size)\n  },\n\n  getOptions,\n}\n","import { Editor } from '@editablejs/models'\n\nexport type BackgroundColorHotkey = Record<string, string | ((e: KeyboardEvent) => boolean)>\n\nexport interface BackgroundColorOptions {\n  hotkey?: BackgroundColorHotkey\n  defaultColor?: string\n}\n\nconst BACKGROUNDCOLOR_OPTIONS = new WeakMap<Editor, BackgroundColorOptions>()\n\nexport const getOptions = (editor: Editor): BackgroundColorOptions => {\n  return BACKGROUNDCOLOR_OPTIONS.get(editor) ?? {}\n}\n\nexport const setOptions = (editor: Editor, options: BackgroundColorOptions) => {\n  BACKGROUNDCOLOR_OPTIONS.set(editor, options)\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,cAAc;AACvB,SAAS,UAAAA,eAAc;AACvB,SAAoC,cAAc;;;ACFlD,SAAS,cAAc;;;ACSvB,IAAM,0BAA0B,oBAAI,QAAwC;AAErE,IAAM,aAAa,CAAC,WAA2C;AACpE,SAAO,wBAAwB,IAAI,MAAM,KAAK,CAAC;AACjD;AAEO,IAAM,aAAa,CAAC,QAAgB,YAAoC;AAC7E,0BAAwB,IAAI,QAAQ,OAAO;AAC7C;;;ADRO,IAAM,wBAAwB;AAAA,EACnC,yBAAyB,CAAC,WAAoD;AAC5E,WAA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