@blocknote/xl-docx-exporter
Version:
A "Notion-style" block-based extensible text editor built on top of Prosemirror and Tiptap.
3 lines • 8.4 kB
JavaScript
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@blocknote/core`),t=require(`docx`);async function n(e){if(typeof window<`u`){let t=await createImageBitmap(e),{width:n,height:r}=t;return t.close(),{width:n,height:r}}else{let t=(await import(`image-meta`)).imageMeta,n=t(new Uint8Array(await e.arrayBuffer()));if(!n.width||!n.height)throw Error(`Image dimensions not found`);return{width:n.width,height:n.height}}}var r=(n,r)=>{let i=Array(n.headerRows??0).fill(!0),a=Array(n.headerCols??0).fill(!0);return new t.Table({layout:`autofit`,columnWidths:n.columnWidths.map(e=>(e??120)*.75*20),rows:n.rows.map((o,s)=>{let c=i[s];return new t.TableRow({tableHeader:c,children:o.cells.map((i,o)=>{let s=n.columnWidths?.[o],l=(0,e.mapTableCell)(i),u=a[o];return new t.TableCell({width:s?{size:s*.75*20,type:`dxa`}:void 0,columnSpan:l.props.colspan,rowSpan:l.props.rowspan,shading:l.props.backgroundColor===`default`||!l.props.backgroundColor?void 0:{type:t.ShadingType.SOLID,color:(()=>{let e=r.options.colors[l.props.backgroundColor]?.background;if(e)return e.slice(1)})()},children:[new t.Paragraph({children:r.transformInlineContent(l.content),alignment:!l.props.textAlignment||l.props.textAlignment===`left`?void 0:l.props.textAlignment===`center`?`center`:l.props.textAlignment===`right`?`right`:l.props.textAlignment===`justify`?`distribute`:(()=>{throw new e.UnreachableCaseError(l.props.textAlignment)})(),run:{bold:c||u,color:l.props.textColor===`default`||!l.props.textColor?void 0:(()=>{let e=r.options.colors[l.props.textColor]?.text;if(e)return e.slice(1)})()}})]})})})})})};function i(n,r){return{shading:n.backgroundColor===`default`||!n.backgroundColor?void 0:{type:t.ShadingType.CLEAR,fill:(()=>{let e=r[n.backgroundColor]?.background;if(e)return e.slice(1)})()},run:n.textColor===`default`||!n.textColor?void 0:{color:(()=>{let e=r[n.textColor]?.text;if(e)return e.slice(1)})()},alignment:!n.textAlignment||n.textAlignment===`left`?void 0:n.textAlignment===`center`?`center`:n.textAlignment===`right`?`right`:n.textAlignment===`justify`?`distribute`:(()=>{throw new e.UnreachableCaseError(n.textAlignment)})()}}var a={paragraph:(e,n)=>new t.Paragraph({...i(e.props,n.options.colors),children:n.transformInlineContent(e.content)}),toggleListItem:(e,n)=>new t.Paragraph({...i(e.props,n.options.colors),children:[new t.TextRun({children:[`> `]}),...n.transformInlineContent(e.content)]}),numberedListItem:(e,n,r)=>new t.Paragraph({...i(e.props,n.options.colors),children:n.transformInlineContent(e.content),numbering:{reference:`blocknote-numbered-list`,level:r}}),bulletListItem:(e,n,r)=>new t.Paragraph({...i(e.props,n.options.colors),children:n.transformInlineContent(e.content),numbering:{reference:`blocknote-bullet-list`,level:r}}),checkListItem:(e,n)=>new t.Paragraph({...i(e.props,n.options.colors),children:[new t.CheckBox({checked:e.props.checked}),new t.TextRun({children:[` `]}),...n.transformInlineContent(e.content)]}),heading:(e,n)=>new t.Paragraph({...i(e.props,n.options.colors),children:n.transformInlineContent(e.content),heading:`Heading${e.props.level}`}),quote:(e,n)=>new t.Paragraph({style:`BlockQuote`,...i(e.props,n.options.colors),children:n.transformInlineContent(e.content)}),audio:(e,t)=>[o(e.props,`Open audio`,t),...s(e.props,t)],video:(e,t)=>[o(e.props,`Open video`,t),...s(e.props,t)],file:(e,t)=>[o(e.props,`Open file`,t),...s(e.props,t)],codeBlock:e=>new t.Paragraph({style:`SourceCode`,children:[...(e.content[0]?.text||``).split(`
`).map((e,n)=>new t.TextRun({text:e,break:+(n>0)}))]}),pageBreak:()=>new t.Paragraph({children:[new t.PageBreak]}),divider:()=>new t.Paragraph({border:{top:{color:`auto`,space:1,style:`single`,size:1}}}),column:(e,n,r,i,a)=>new t.TableCell({width:{size:`${e.props.width*100}%`,type:`pct`},children:(a||[]).flatMap(e=>Array.isArray(e)?e:[e])}),columnList:(e,n,r,i,a)=>new t.Table({layout:`autofit`,borders:{bottom:{style:`nil`},top:{style:`nil`},left:{style:`nil`},right:{style:`nil`},insideHorizontal:{style:`nil`},insideVertical:{style:`nil`}},rows:[new t.TableRow({children:a.map((e,n,r)=>new t.TableCell({width:{size:`${parseFloat(`${e.options.width?.size||`100%`}`)/(r.length*100)*100}%`,type:`pct`},children:e.options.children}))})]}),image:async(e,r)=>{let a=await r.resolveFile(e.props.url),{width:o,height:c}=await n(a);return[new t.Paragraph({...i(e.props,r.options.colors),children:[new t.ImageRun({data:await a.arrayBuffer(),type:`gif`,altText:e.props.caption?{description:e.props.caption,name:e.props.caption,title:e.props.caption}:void 0,transformation:{width:e.props.previewWidth||o,height:(e.props.previewWidth||o)/o*c}})]}),...s(e.props,r)]},table:(e,t)=>r(e.content,t)};function o(e,n,r){return new t.Paragraph({...i(e,r.options.colors),children:[new t.ExternalHyperlink({children:[new t.TextRun({text:e.name||n,style:`Hyperlink`})],link:e.url})]})}function s(e,n){return e.caption?[new t.Paragraph({...i(e,n.options.colors),children:[new t.TextRun({text:e.caption})],style:`Caption`})]:[]}var c={blockMapping:a,inlineContentMapping:{link:(e,n)=>new t.ExternalHyperlink({children:e.content.map(e=>n.transformStyledText(e,!0)),link:e.href}),text:(e,t)=>t.transformStyledText(e)},styleMapping:{bold:e=>e?{bold:e}:{},italic:e=>e?{italics:e}:{},underline:e=>e?{underline:{type:`single`}}:{},strike:e=>e?{strike:e}:{},backgroundColor:(e,n)=>{if(!e)return{};let r=n.options.colors[e]?.background;return r?{shading:{type:t.ShadingType.CLEAR,fill:r.slice(1)}}:{}},textColor:(e,t)=>{if(!e)return{};let n=t.options.colors[e]?.text;return n?{color:n.slice(1)}:{}},code:e=>e?{style:`VerbatimChar`}:{}}};async function l(e){return`https://corsproxy.api.blocknotejs.org/corsproxy/?url=`+encodeURIComponent(e)}async function u(e){{let t=e.default;return await(await fetch(t)).arrayBuffer()}}var d=16*.75*1.5*20,f=class extends e.Exporter{constructor(t,n,r){let i={colors:e.COLORS_DEFAULT,resolveFileUrl:l,...r};super(t,n,i),this.schema=t,this.mappings=n}transformStyledText(e,n){let r=this.mapStyles(e.styles),i=Object.assign({},...r);return new t.TextRun({...i,style:n?`Hyperlink`:i.style,text:e.text})}async transformBlocks(e,n=0){let r=[];for(let i of e){let e=await this.transformBlocks(i.children,n+1);[`columnList`,`column`].includes(i.type)||(e=e.map((e,n)=>(e instanceof t.Paragraph&&!e.properties.numberingReferences.length&&e.addRunToFront(new t.TextRun({children:[new t.Tab]})),e)));let a=await this.mapBlock(i,n,0,e);[`columnList`,`column`].includes(i.type)?r.push(a):Array.isArray(a)?r.push(...a,...e):r.push(a,...e)}return r}async getFonts(){let e=await u(await Promise.resolve().then(()=>require(`./Inter_18pt-Regular-PkBfh_8C.cjs`))),t=await u(await Promise.resolve().then(()=>require(`./GeistMono-Regular-CHVKamKz.cjs`)));if(e instanceof ArrayBuffer||t instanceof ArrayBuffer){let n=(await import(`buffer/`)).Buffer;e instanceof ArrayBuffer&&(e=n.from(e)),t instanceof ArrayBuffer&&(t=n.from(t))}return[{name:`Inter`,data:e},{name:`GeistMono`,data:t}]}async createDefaultDocumentOptions(e){let n=(await Promise.resolve().then(()=>require(`./styles-Cz94-HtD.cjs`))).default,r=e?.trim();n=r?n.replace(/(<w:lang\b[^>]*\bw:val=")([^"]+)("[^>]*\/>)/g,(e,t,n,i)=>`${t}${r}${i}`):n.replace(/\s*<w:lang\b[^>]*\/>/g,``);let i=[`•`];return{numbering:{config:[{reference:`blocknote-numbered-list`,levels:Array.from({length:9},(e,n)=>({start:1,level:n,format:t.LevelFormat.DECIMAL,text:`%${n+1}.`,alignment:t.AlignmentType.LEFT,style:{paragraph:{indent:{left:d*(n+1),hanging:d}}}}))},{reference:`blocknote-bullet-list`,levels:Array.from({length:9},(e,n)=>({start:1,level:n,format:t.LevelFormat.BULLET,text:i[n%i.length],alignment:t.AlignmentType.LEFT,style:{paragraph:{indent:{left:d*(n+1),hanging:d}}}}))}]},fonts:await this.getFonts(),defaultTabStop:200,externalStyles:n}}async toBlob(e,n={sectionOptions:{},documentOptions:{}}){let r=await this.toDocxJsDocument(e,n),i=globalThis.Buffer;try{return globalThis.Buffer||(globalThis.Buffer=(await import(`buffer`)).default.Buffer),t.Packer.toBlob(r)}finally{globalThis.Buffer=i}}async toDocxJsDocument(e,n={sectionOptions:{},documentOptions:{}}){return new t.Document({...await this.createDefaultDocumentOptions(n.locale),...n.documentOptions,sections:[{children:await this.transformBlocks(e),...n.sectionOptions}]})}};exports.DOCXExporter=f,exports.docxDefaultSchemaMappings=c;
//# sourceMappingURL=blocknote-xl-docx-exporter.cjs.map