{"version":3,"sources":["../src/lib/editor-to-text.ts"],"sourcesContent":["import { BaseEditor, Descendant, Element } from \"slate\";\nimport { HistoryEditor } from \"slate-history\";\nimport { ReactEditor } from \"slate-react\";\nimport { SuggestionAwareText } from \"../types/base/custom-editor\";\n\nfunction nodeChildrenToTextComponents(\n  editor: BaseEditor & ReactEditor & HistoryEditor,\n  nodes: Descendant[],\n): SuggestionAwareText[] {\n  // find inlineable elements\n  const indeciesOfInlineElements = new Set(\n    nodes\n      .map((node, index) => {\n        if (Element.isElement(node) && editor.isInline(node)) {\n          return index;\n        }\n        return -1;\n      })\n      .filter((index) => index !== -1),\n  );\n\n  // ignorable elements = inline elements,\n  // or neighbors of inline elements that are {text: \"\"}\n  const nonIgnorableItems = nodes.filter((node, index) => {\n    const isInline = indeciesOfInlineElements.has(index);\n    if (isInline) {\n      return false;\n    }\n\n    const isNeighbourOfInline =\n      indeciesOfInlineElements.has(index - 1) || indeciesOfInlineElements.has(index + 1);\n    if (isNeighbourOfInline) {\n      return (node as any).text !== \"\";\n    }\n\n    return true;\n  });\n\n  return nonIgnorableItems\n    .map((node) => {\n      if (Element.isElement(node)) {\n        switch (node.type) {\n          case \"paragraph\":\n            return nodeChildrenToTextComponents(editor, node.children);\n          case \"suggestion\":\n            return [];\n        }\n      } else {\n        return [node];\n      }\n    })\n    .reduce((acc, val) => acc.concat(val), []);\n}\n\nexport const editorToText = (editor: BaseEditor & ReactEditor & HistoryEditor) => {\n  const flattened = nodeChildrenToTextComponents(editor, editor.children);\n\n  const text = flattened.map((textComponent) => textComponent.text).join(\"\\n\");\n\n  return text;\n};\n"],"mappings":";AAAA,SAAiC,eAAe;AAKhD,SAAS,6BACP,QACA,OACuB;AAEvB,QAAM,2BAA2B,IAAI;AAAA,IACnC,MACG,IAAI,CAAC,MAAM,UAAU;AACpB,UAAI,QAAQ,UAAU,IAAI,KAAK,OAAO,SAAS,IAAI,GAAG;AACpD,eAAO;AAAA,MACT;AACA,aAAO;AAAA,IACT,CAAC,EACA,OAAO,CAAC,UAAU,UAAU,EAAE;AAAA,EACnC;AAIA,QAAM,oBAAoB,MAAM,OAAO,CAAC,MAAM,UAAU;AACtD,UAAM,WAAW,yBAAyB,IAAI,KAAK;AACnD,QAAI,UAAU;AACZ,aAAO;AAAA,IACT;AAEA,UAAM,sBACJ,yBAAyB,IAAI,QAAQ,CAAC,KAAK,yBAAyB,IAAI,QAAQ,CAAC;AACnF,QAAI,qBAAqB;AACvB,aAAQ,KAAa,SAAS;AAAA,IAChC;AAEA,WAAO;AAAA,EACT,CAAC;AAED,SAAO,kBACJ,IAAI,CAAC,SAAS;AACb,QAAI,QAAQ,UAAU,IAAI,GAAG;AAC3B,cAAQ,KAAK,MAAM;AAAA,QACjB,KAAK;AACH,iBAAO,6BAA6B,QAAQ,KAAK,QAAQ;AAAA,QAC3D,KAAK;AACH,iBAAO,CAAC;AAAA,MACZ;AAAA,IACF,OAAO;AACL,aAAO,CAAC,IAAI;AAAA,IACd;AAAA,EACF,CAAC,EACA,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,GAAG,GAAG,CAAC,CAAC;AAC7C;AAEO,IAAM,eAAe,CAAC,WAAqD;AAChF,QAAM,YAAY,6BAA6B,QAAQ,OAAO,QAAQ;AAEtE,QAAM,OAAO,UAAU,IAAI,CAAC,kBAAkB,cAAc,IAAI,EAAE,KAAK,IAAI;AAE3E,SAAO;AACT;","names":[]}