@blocknote/xl-docx-exporter
Version:
A "Notion-style" block-based extensible text editor built on top of Prosemirror and Tiptap.
4 lines (3 loc) • 8.81 kB
JavaScript
;var A=Object.create;var x=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,I=Object.prototype.hasOwnProperty;var L=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of D(t))!I.call(e,i)&&i!==n&&x(e,i,{get:()=>t[i],enumerable:!(o=B(t,i))||o.enumerable});return e};var b=(e,t,n)=>(n=e!=null?A(v(e)):{},L(t||!e||!e.__esModule?x(n,"default",{value:e,enumerable:!0}):n,e));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@blocknote/core"),r=require("docx");async function S(e){if(typeof window<"u"){const t=await createImageBitmap(e),{width:n,height:o}=t;return t.close(),{width:n,height:o}}else{const t=(await import("image-meta")).imageMeta,n=new Uint8Array(await e.arrayBuffer()),o=t(n);if(!o.width||!o.height)throw new Error("Image dimensions not found");return{width:o.width,height:o.height}}}const O=(e,t)=>{const o=new Array(e.headerRows??0).fill(!0),i=new Array(e.headerCols??0).fill(!0);return new r.Table({layout:"autofit",columnWidths:e.columnWidths.map(a=>(a??120)*.75*20),rows:e.rows.map((a,u)=>{const c=o[u];return new r.TableRow({tableHeader:c,children:a.cells.map((p,m)=>{var y;const w=(y=e.columnWidths)==null?void 0:y[m],s=d.mapTableCell(p),T=i[m];return new r.TableCell({width:w?{size:`${w*.75}pt`,type:"dxa"}:void 0,columnSpan:s.props.colspan,rowSpan:s.props.rowspan,shading:s.props.backgroundColor==="default"||!s.props.backgroundColor?void 0:{type:r.ShadingType.SOLID,color:t.options.colors[s.props.backgroundColor].background.slice(1)},children:[new r.Paragraph({children:t.transformInlineContent(s.content),alignment:!s.props.textAlignment||s.props.textAlignment==="left"?void 0:s.props.textAlignment==="center"?"center":s.props.textAlignment==="right"?"right":s.props.textAlignment==="justify"?"distribute":(()=>{throw new d.UnreachableCaseError(s.props.textAlignment)})(),run:{bold:c||T,color:s.props.textColor==="default"||!s.props.textColor?void 0:t.options.colors[s.props.textColor].text.slice(1)}})]})})})})})};function l(e,t){return{shading:e.backgroundColor==="default"||!e.backgroundColor?void 0:{type:r.ShadingType.SOLID,color:t[e.backgroundColor].background.slice(1)},run:e.textColor==="default"||!e.textColor?void 0:{color:t[e.textColor].text.slice(1)},alignment:!e.textAlignment||e.textAlignment==="left"?void 0:e.textAlignment==="center"?"center":e.textAlignment==="right"?"right":e.textAlignment==="justify"?"distribute":(()=>{throw new d.UnreachableCaseError(e.textAlignment)})()}}const P={paragraph:(e,t)=>new r.Paragraph({...l(e.props,t.options.colors),children:t.transformInlineContent(e.content),style:"Normal",run:{font:"Inter"}}),toggleListItem:(e,t)=>new r.Paragraph({...l(e.props,t.options.colors),children:[new r.TextRun({children:["> "]}),...t.transformInlineContent(e.content)]}),numberedListItem:(e,t,n)=>new r.Paragraph({...l(e.props,t.options.colors),children:t.transformInlineContent(e.content),numbering:{reference:"blocknote-numbered-list",level:n}}),bulletListItem:(e,t,n)=>new r.Paragraph({...l(e.props,t.options.colors),children:t.transformInlineContent(e.content),numbering:{reference:"blocknote-bullet-list",level:n}}),checkListItem:(e,t)=>new r.Paragraph({...l(e.props,t.options.colors),children:[new r.CheckBox({checked:e.props.checked}),new r.TextRun({children:[" "]}),...t.transformInlineContent(e.content)]}),heading:(e,t)=>new r.Paragraph({...l(e.props,t.options.colors),children:t.transformInlineContent(e.content),heading:`Heading${e.props.level}`}),quote:(e,t)=>new r.Paragraph({shading:{color:"#7D797A"},border:{left:{color:"#7D797A",space:100,style:"single",size:8}},...l(e.props,t.options.colors),children:t.transformInlineContent(e.content)}),audio:(e,t)=>[g(e.props,"Open audio",t),...f(e.props,t)],video:(e,t)=>[g(e.props,"Open video",t),...f(e.props,t)],file:(e,t)=>[g(e.props,"Open file",t),...f(e.props,t)],codeBlock:e=>{var n;const t=((n=e.content[0])==null?void 0:n.text)||"";return new r.Paragraph({style:"Codeblock",shading:{type:r.ShadingType.SOLID,fill:"161616",color:"161616"},children:[...t.split(`
`).map((o,i)=>new r.TextRun({text:o,break:i>0?1:0}))]})},pageBreak:()=>new r.Paragraph({children:[new r.PageBreak]}),column:(e,t,n,o,i)=>new r.TableCell({width:{size:`${e.props.width*100}%`,type:"pct"},children:(i||[]).flatMap(a=>Array.isArray(a)?a:[a])}),columnList:(e,t,n,o,i)=>new r.Table({layout:"autofit",borders:{bottom:{style:"nil"},top:{style:"nil"},left:{style:"nil"},right:{style:"nil"},insideHorizontal:{style:"nil"},insideVertical:{style:"nil"}},rows:[new r.TableRow({children:i.map((a,u,c)=>{var p;return new r.TableCell({width:{size:`${parseFloat(`${((p=a.options.width)==null?void 0:p.size)||"100%"}`)/(c.length*100)*100}%`,type:"pct"},children:a.options.children})})})]}),image:async(e,t)=>{const n=await t.resolveFile(e.props.url),{width:o,height:i}=await S(n);return[new r.Paragraph({...l(e.props,t.options.colors),children:[new r.ImageRun({data:await n.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*i}})]}),...f(e.props,t)]},table:(e,t)=>O(e.content,t)};function g(e,t,n){return new r.Paragraph({...l(e,n.options.colors),children:[new r.ExternalHyperlink({children:[new r.TextRun({text:e.name||t,style:"Hyperlink"})],link:e.url})]})}function f(e,t){return e.caption?[new r.Paragraph({...l(e,t.options.colors),children:[new r.TextRun({text:e.caption})],style:"Caption"})]:[]}const k={link:(e,t)=>new r.ExternalHyperlink({children:e.content.map(n=>t.transformStyledText(n,!0)),link:e.href}),text:(e,t)=>t.transformStyledText(e)},F={bold:e=>e?{bold:e}:{},italic:e=>e?{italics:e}:{},underline:e=>e?{underline:{type:"single"}}:{},strike:e=>e?{strike:e}:{},backgroundColor:(e,t)=>e?{shading:{fill:t.options.colors[e].background.slice(1)}}:{},textColor:(e,t)=>e?{color:t.options.colors[e].text.slice(1)}:{},code:e=>e?{font:"GeistMono"}:{}},M={blockMapping:P,inlineContentMapping:k,styleMapping:F};async function R(e){return"https://corsproxy.api.blocknotejs.org/corsproxy/?url="+encodeURIComponent(e)}async function C(e){{const t=e.default;return await(await fetch(t)).arrayBuffer()}}const h=16*.75*1.5*20;class _ extends d.Exporter{constructor(t,n,o){const a={...{colors:d.COLORS_DEFAULT,resolveFileUrl:R},...o};super(t,n,a),this.schema=t,this.mappings=n}transformStyledText(t,n){const o=this.mapStyles(t.styles),i=Object.assign({},...o);return new r.TextRun({...i,style:n?"Hyperlink":void 0,text:t.text})}async transformBlocks(t,n=0){const o=[];for(const i of t){let a=await this.transformBlocks(i.children,n+1);["columnList","column"].includes(i.type)||(a=a.map((c,p)=>(c instanceof r.Paragraph&&!c.properties.numberingReferences.length&&c.addRunToFront(new r.TextRun({children:[new r.Tab]})),c)));const u=await this.mapBlock(i,n,0,a);["columnList","column"].includes(i.type)?o.push(u):Array.isArray(u)?o.push(...u,...a):o.push(u,...a)}return o}async getFonts(){let t=await C(await Promise.resolve().then(()=>require("./Inter_18pt-Regular-CCMUw8TC.cjs"))),n=await C(await Promise.resolve().then(()=>require("./GeistMono-Regular--NrcstcO.cjs")));if(t instanceof ArrayBuffer||n instanceof ArrayBuffer){const o=(await Promise.resolve().then(()=>require("./index-B-FmPo2r.cjs")).then(i=>i.index)).Buffer;t instanceof ArrayBuffer&&(t=o.from(t)),n instanceof ArrayBuffer&&(n=o.from(n))}return[{name:"Inter",data:t},{name:"GeistMono",data:n}]}async createDefaultDocumentOptions(){const t=(await Promise.resolve().then(()=>require("./styles-C7c5RlKz.cjs"))).default,n=["•"];return{numbering:{config:[{reference:"blocknote-numbered-list",levels:Array.from({length:9},(o,i)=>({start:1,level:i,format:r.LevelFormat.DECIMAL,text:`%${i+1}.`,alignment:r.AlignmentType.LEFT,style:{paragraph:{indent:{left:h*(i+1),hanging:h}}}}))},{reference:"blocknote-bullet-list",levels:Array.from({length:9},(o,i)=>({start:1,level:i,format:r.LevelFormat.BULLET,text:n[i%n.length],alignment:r.AlignmentType.LEFT,style:{paragraph:{indent:{left:h*(i+1),hanging:h}}}}))}]},fonts:await this.getFonts(),defaultTabStop:200,externalStyles:t}}async toBlob(t,n={sectionOptions:{},documentOptions:{}}){const o=await this.toDocxJsDocument(t,n),i=globalThis.Buffer;try{return globalThis.Buffer||(globalThis.Buffer=(await import("buffer")).default.Buffer),r.Packer.toBlob(o)}finally{globalThis.Buffer=i}}async toDocxJsDocument(t,n={sectionOptions:{},documentOptions:{}}){return new r.Document({...await this.createDefaultDocumentOptions(),...n.documentOptions,sections:[{children:await this.transformBlocks(t),...n.sectionOptions}]})}}exports.DOCXExporter=_;exports.docxDefaultSchemaMappings=M;
//# sourceMappingURL=blocknote-xl-docx-exporter.cjs.map