UNPKG

@blocknote/server-util

Version:

A "Notion-style" block-based extensible text editor built on top of Prosemirror and Tiptap.

3 lines (2 loc) 3.38 kB
"use strict";var T=Object.defineProperty;var p=(t,o,e)=>o in t?T(t,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[o]=e;var i=(t,o,e)=>p(t,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("@blocknote/core"),y=require("@blocknote/react"),w=require("jsdom"),d=require("react"),u=require("react-dom"),c=require("y-prosemirror"),_=t=>t&&typeof t=="object"&&"default"in t?t:{default:t};function k(t){if(t&&typeof t=="object"&&"default"in t)return t;const o=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const e in t)if(e!=="default"){const r=Object.getOwnPropertyDescriptor(t,e);Object.defineProperty(o,e,r.get?r:{enumerable:!0,get:()=>t[e]})}}return o.default=t,Object.freeze(o)}const S=k(w),b=_(u);var a,n=b.default;if(process.env.NODE_ENV==="production")a=n.createRoot,n.hydrateRoot;else{var m=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;a=function(t,o){m.usingClientEntryPoint=!0;try{return n.createRoot(t,o)}finally{m.usingClientEntryPoint=!1}}}class l{constructor(o){i(this,"editor");i(this,"jsdom",new S.JSDOM);this.editor=s.BlockNoteEditor.create({...o,_headless:!0})}async _withJSDOM(o){const e=globalThis.window,r=globalThis.document;globalThis.document=this.jsdom.window.document,globalThis.window=this.jsdom.window,globalThis.window.__TEST_OPTIONS=e==null?void 0:e.__TEST_OPTIONS;try{return await o()}finally{globalThis.document=r,globalThis.window=e}}static create(o={}){return new l(o)}_prosemirrorNodeToBlocks(o){const e=[];return o.firstChild.descendants(r=>(e.push(s.nodeToBlock(r,this.editor.pmSchema)),!1)),e}_prosemirrorJSONToBlocks(o){const e=this.editor.pmSchema.nodeFromJSON(o);return this._prosemirrorNodeToBlocks(e)}_blocksToProsemirrorNode(o){const e=this.editor.pmSchema,r=o.map(h=>s.blockToNode(h,e));return e.topNodeType.create(null,e.nodes.blockGroup.create(null,r))}yXmlFragmentToBlocks(o){const e=c.yXmlFragmentToProseMirrorRootNode(o,this.editor.pmSchema);return this._prosemirrorNodeToBlocks(e)}blocksToYXmlFragment(o,e){return c.prosemirrorToYXmlFragment(this._blocksToProsemirrorNode(o),e)}yDocToBlocks(o,e="prosemirror"){return this.yXmlFragmentToBlocks(o.getXmlFragment(e))}blocksToYDoc(o,e="prosemirror"){return c.prosemirrorToYDoc(this._blocksToProsemirrorNode(o),e)}async blocksToHTMLLossy(o){return this._withJSDOM(async()=>s.createExternalHTMLExporter(this.editor.pmSchema,this.editor).exportBlocks(o,{document:this.jsdom.window.document}))}async blocksToFullHTML(o){return this._withJSDOM(async()=>s.createInternalHTMLSerializer(this.editor.pmSchema,this.editor).serializeBlocks(o,{document:this.jsdom.window.document}))}async tryParseHTMLToBlocks(o){return this._withJSDOM(()=>this.editor.tryParseHTMLToBlocks(o))}async blocksToMarkdownLossy(o){return this._withJSDOM(async()=>s.blocksToMarkdown(o,this.editor.pmSchema,this.editor,{document:this.jsdom.window.document}))}async tryParseMarkdownToBlocks(o){return this._withJSDOM(()=>this.editor.tryParseMarkdownToBlocks(o))}async withReactContext(o,e){return this._withJSDOM(async()=>{const r=a(this.jsdom.window.document.createElement("div"));u.flushSync(()=>{r.render(d.createElement(o,{},d.createElement(y.BlockNoteViewRaw,{editor:this.editor})))});try{return await e()}finally{r.unmount()}})}}exports.ServerBlockNoteEditor=l; //# sourceMappingURL=blocknote-server-util.cjs.map