{"version":3,"file":"odt-exporter.cjs","names":[],"sources":["../src/odt-exporter/index.ts"],"sourcesContent":["import type {\n  BlockConfig,\n  BlockFromConfigNoChildren,\n  Exporter,\n} from \"@blocknote/core\";\nimport { plainContentToString } from \"@blocknote/core\";\nimport { ODTExporter } from \"@blocknote/xl-odt-exporter\";\nimport { createElement } from \"react\";\n\nimport { latexToMathML } from \"../exporterHelpers/latexToMathML.js\";\nimport { getMathExporterDictionary } from \"../i18n/dictionary.js\";\n\n// The ODT elements are created with `createElement` string tags rather than\n// JSX: the ODT exporter's namespaced tags (`text:p`, `draw:frame`, ...) need\n// JSX runtime module augmentation plus a transform that allows namespaces,\n// neither of which this package sets up for its React sources.\n\ntype MathBlock = BlockFromConfigNoChildren<\n  BlockConfig<\"mathBlock\", {}, \"plain\">,\n  any,\n  any\n>;\n\ntype InlineMath = { type: \"math\"; content: string };\n\n// A formula object, anchored as a character so it can sit inline among text.\n// The MathML goes into an object sub-document (rather than inline into the\n// frame) and the frame gets no explicit size, with a graphic style derived\n// from the built-in \"Formula\" style - this exact combination makes\n// LibreOffice load the formula as a real formula object and compute its\n// natural size (sized frames get the formula scaled-to-fit instead, and\n// inline MathML renders at zero size).\nfunction formulaFrame(exporter: ODTExporter<any, any, any>, mathML: string) {\n  const objectPath = exporter.registerObject(\n    '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\\n' + mathML,\n  );\n  const styleName = exporter.registerStyle((name) =>\n    createElement(\n      \"style:style\",\n      {\n        \"style:family\": \"graphic\",\n        \"style:name\": name,\n        \"style:parent-style-name\": \"Formula\",\n      },\n      createElement(\"style:graphic-properties\", {\n        \"style:vertical-pos\": \"middle\",\n        \"style:vertical-rel\": \"text\",\n      }),\n    ),\n  );\n\n  return createElement(\n    \"draw:frame\",\n    { \"draw:style-name\": styleName, \"text:anchor-type\": \"as-char\" },\n    createElement(\"draw:object\", {\n      \"xlink:href\": objectPath,\n      \"xlink:type\": \"simple\",\n      \"xlink:show\": \"embed\",\n      \"xlink:actuate\": \"onLoad\",\n    }),\n  );\n}\n\n// Mirrors the editor, which shows the error state in the preview\n// placeholder, identifying the formula by its source. The parser's message\n// is deliberately NOT rendered: it's authoring detail (and untranslated\n// English) - the editor is where the author sees and fixes it. Styled muted like the other\n// exporters' placeholders.\nfunction errorText(source: string, exporter: ODTExporter<any, any, any>) {\n  const styleName = exporter.registerStyle((name) =>\n    createElement(\n      \"style:style\",\n      { \"style:family\": \"text\", \"style:name\": name },\n      createElement(\"style:text-properties\", {\n        \"fo:font-style\": \"italic\",\n        \"fo:color\": \"#999999\",\n      }),\n    ),\n  );\n\n  return createElement(\n    \"text:span\",\n    { \"text:style-name\": styleName },\n    getMathExporterDictionary(exporter).invalid_formula(source),\n  );\n}\n\n/**\n * ODT block mapping for `@blocknote/math-block` that renders math blocks as\n * native (editable) formula objects. Invalid LaTeX renders an error\n * placeholder (mirroring the editor):\n *\n * ```ts\n * import { mathBlockMapping } from \"@blocknote/math-block/odt-exporter\";\n *\n * new ODTExporter(schema, {\n *   ...odtDefaultSchemaMappings,\n *   blockMapping: {\n *     ...odtDefaultSchemaMappings.blockMapping,\n *     mathBlock: mathBlockMapping,\n *   },\n * });\n * ```\n */\nexport function mathBlockMapping(\n  block: MathBlock,\n  exporter: Exporter<any, any, any, any, any, any, any>,\n) {\n  // Only the ODTExporter invokes ODT mappings, but mapping signatures are\n  // contravariant in the exporter parameter, so requiring the subclass here\n  // wouldn't satisfy the mapping type - hence the base type + cast.\n  const odtExporter = exporter as ODTExporter<any, any, any>;\n  const source = plainContentToString(block.content);\n  if (!source.trim()) {\n    return createElement(\"text:p\");\n  }\n\n  const mathML = latexToMathML(source, false);\n  if (mathML.error !== undefined) {\n    return createElement(\"text:p\", null, errorText(source, odtExporter));\n  }\n\n  const styleName = odtExporter.registerStyle((name) =>\n    createElement(\n      \"style:style\",\n      {\n        \"style:family\": \"paragraph\",\n        \"style:name\": name,\n        \"style:parent-style-name\": \"Standard\",\n      },\n      createElement(\"style:paragraph-properties\", {\n        \"fo:text-align\": \"center\",\n      }),\n    ),\n  );\n\n  return createElement(\n    \"text:p\",\n    { \"text:style-name\": styleName },\n    formulaFrame(odtExporter, mathML.mathML),\n  );\n}\n\n/**\n * ODT inline content mapping for `@blocknote/math-block` that renders inline\n * math as native (editable) formula objects. Invalid LaTeX renders an error\n * placeholder (mirroring the editor):\n *\n * ```ts\n * import { inlineMathMapping } from \"@blocknote/math-block/odt-exporter\";\n *\n * new ODTExporter(schema, {\n *   ...odtDefaultSchemaMappings,\n *   inlineContentMapping: {\n *     ...odtDefaultSchemaMappings.inlineContentMapping,\n *     math: inlineMathMapping,\n *   },\n * });\n * ```\n */\nexport function inlineMathMapping(\n  inlineContent: InlineMath,\n  exporter: Exporter<any, any, any, any, any, any, any>,\n) {\n  // Only the ODTExporter invokes ODT mappings, but mapping signatures are\n  // contravariant in the exporter parameter, so requiring the subclass here\n  // wouldn't satisfy the mapping type - hence the base type + cast.\n  const odtExporter = exporter as ODTExporter<any, any, any>;\n  const source = inlineContent.content;\n  if (!source.trim()) {\n    return createElement(\"text:span\");\n  }\n\n  const mathML = latexToMathML(source, true);\n  if (mathML.error !== undefined) {\n    return errorText(source, odtExporter);\n  }\n\n  return formulaFrame(odtExporter, mathML.mathML);\n}\n"],"mappings":"8MAgCA,SAAS,EAAa,EAAsC,EAAgB,CAC1E,IAAM,EAAa,EAAS,eAC1B;EAA6C,CAC/C,EACM,EAAY,EAAS,cAAe,IAAA,EACxC,EAAA,cAAA,CACE,cACA,CACE,eAAgB,UAChB,aAAc,EACd,0BAA2B,SAC7B,GAAA,EACA,EAAA,cAAA,CAAc,2BAA4B,CACxC,qBAAsB,SACtB,qBAAsB,MACxB,CAAC,CACH,CACF,EAEA,OAAA,EAAO,EAAA,cAAA,CACL,aACA,CAAE,kBAAmB,EAAW,mBAAoB,SAAU,GAAA,EAC9D,EAAA,cAAA,CAAc,cAAe,CAC3B,aAAc,EACd,aAAc,SACd,aAAc,QACd,gBAAiB,QACnB,CAAC,C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