@uibot/document-editor 0.3.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{en as Z,zh as _}from"@blocknote/core/locales";import{BlockNoteView as ee}from"@blocknote/mantine";import{useCreateBlockNote as te}from"@blocknote/react";import{forwardRef as ne,useCallback as L,useEffect as oe,useImperativeHandle as re,useRef as j}from"react";import{jsx as ie}from"react/jsx-runtime";var P=(e)=>JSON.stringify(e),N=ne(function(n,u){let{blocks:d,markdown:E,onChange:h,readOnly:f,locale:p="zh-CN",uploadFile:r}=n,c=j(""),v=j(h);v.current=h;let i=te({dictionary:p==="en"?Z:_,...d&&d.length>0?{initialContent:d}:{},...r?{uploadFile:r}:{}},[p]),t=L(()=>({blocks:i.document,markdown:i.blocksToMarkdownLossy()}),[i]),s=L((a,k)=>{if(a&&a.length>0)i.replaceBlocks(i.document,a);else if(k)i.replaceBlocks(i.document,i.tryParseMarkdownToBlocks(k));c.current=P(i.document)},[i]);return oe(()=>{if(d&&d.length>0)c.current=P(i.document);else s(null,E)},[i]),re(u,()=>({getValue:t,setContent:(a)=>s(a.blocks,a.markdown)}),[t,s]),ie(ee,{className:n.className,editor:i,editable:!f,theme:n.theme??"light",onChange:()=>{let a=P(i.document);if(a===c.current)return;c.current=a,v.current?.(t())}})});import{useCallback as J,useEffect as D,useReducer as ae,useRef as g,useState as se}from"react";function O(e){return{phase:"clean",revision:e,message:"",remote:null,editedWhileSaving:!1}}function S(e){return e.phase==="dirty"||e.phase==="error"||e.phase==="unknown"}function M(e){return e.phase!=="clean"}function K(e,n){switch(n.type){case"edit":if(e.phase==="saving")return{...e,editedWhileSaving:!0};if(e.phase==="conflict")return e;return{...e,phase:"dirty",message:e.phase==="dirty"?e.message:""};case"save-start":if(!S(e))return e;return{...e,phase:"saving",message:"",editedWhileSaving:!1};case"saved":return{phase:e.editedWhileSaving?"dirty":"clean",revision:n.revision,message:"",remote:null,editedWhileSaving:!1};case"conflict":return{...e,phase:"conflict",message:n.message,remote:n.remote};case"failed":return{...e,phase:n.unknown?"unknown":"error",message:n.message,editedWhileSaving:!1};case"adopt-revision":return e.phase==="conflict"?e:{...e,revision:n.revision};case"keep-mine":return{phase:"dirty",revision:n.revision,message:"",remote:null,editedWhileSaving:!1};case"use-remote":return{phase:"clean",revision:n.revision,message:"",remote:null,editedWhileSaving:!1}}}function W(e,n,u){return e&&e.fingerprint===n?e:{fingerprint:n,key:u()}}import{jsx as m,jsxs as w,Fragment as me}from"react/jsx-runtime";var ce={"zh-CN":{title:"标题",titlePlaceholder:"无标题",clean:"已保存",dirty:"未保存的更改",saving:"保存中…",error:"保存失败,草稿已保留。",unknown:"保存结果未知,草稿已保留。",retry:"重试保存",conflict:"文档已在别处被修改",keepMine:"保留我的修改并覆盖",useRemote:"载入最新版本",readOnly:"只读"},en:{title:"Title",titlePlaceholder:"Untitled",clean:"Saved",dirty:"Unsaved changes",saving:"Saving…",error:"Saving failed. Your draft has been kept.",unknown:"The save result is unknown. Your draft has been kept.",retry:"Retry save",conflict:"This document was changed elsewhere",keepMine:"Keep my changes and overwrite",useRemote:"Load latest version",readOnly:"Read only"}},le=(e,n)=>`${JSON.stringify(e)}\x00${n}`,ue=()=>typeof crypto<"u"&&("randomUUID"in crypto)?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).slice(2)}`;function de(e){let{document:n,client:u,locale:d="zh-CN",hideTitle:E,onDirtyChange:h,onBusyChange:f}=e,p=e.autoSaveMs??1500,r=ce[d],c=n.readOnly===!0,v=u.uploadFile?.bind(u),i=g(e.onSaved);i.current=e.onSaved;let[t,s]=ae(K,n.revision,O),a=g(t);a.current=t;let[k,I]=se(n.title),C=g(n.title),x=g(null),y=g(null),B=g(!1),R=e.revision;D(()=>{if(R!==void 0)s({type:"adopt-revision",revision:R})},[R]);let V=J(()=>{if(!c)s({type:"edit"})},[c]),b=J(async()=>{let o=x.current;if(!o||c||B.current||!S(a.current))return;B.current=!0;let{blocks:H,markdown:A}=o.getValue(),U=C.current,G=le(H,U);y.current=W(y.current,G,ue);let Q={id:n.id,title:U,blocks:H,markdown:A,expectedRevision:a.current.revision,idempotencyKey:y.current.key};s({type:"save-start"});try{let l=await u.save(Q);if(l.ok)y.current=null,s({type:"saved",revision:l.document.revision}),i.current?.(l.document);else if(l.error.code==="conflict")s({type:"conflict",message:l.error.message,remote:l.error.remote??null});else s({type:"failed",message:l.error.message,unknown:l.error.code==="unknown"})}catch(l){s({type:"failed",message:l instanceof Error?l.message:r.unknown,unknown:!0})}finally{B.current=!1}},[u,n.id,c,r.unknown]);D(()=>{if(t.phase!=="dirty"||p<=0)return;let o=setTimeout(()=>void b(),p);return()=>clearTimeout(o)},[t.phase,p,b]);let z=M(t),F=t.phase==="saving";D(()=>h?.(z),[z,h]),D(()=>f?.(F),[F,f]);let Y=(o)=>{x.current?.setContent({blocks:o.blocks,markdown:o.markdown}),C.current=o.title,I(o.title),y.current=null,s({type:"use-remote",revision:o.revision})},T={message:t.message,remote:t.remote,keepMine:()=>t.remote&&s({type:"keep-mine",revision:t.remote.revision}),useRemote:()=>t.remote&&Y(t.remote)},X=(o)=>{if((o.metaKey||o.ctrlKey)&&o.key.toLowerCase()==="s")o.preventDefault(),b()},q=t.phase==="clean"?c?r.readOnly:r.clean:t.phase==="saving"?r.saving:t.phase==="error"?`${r.error}${t.message?` ${t.message}`:""}`:t.phase==="unknown"?r.unknown:r.dirty;return w("div",{"data-uibot":"document-editor",className:e.className,style:e.style,onKeyDown:X,children:[E?null:m("input",{"aria-label":r.title,placeholder:r.titlePlaceholder,value:k,readOnly:c,onChange:(o)=>{C.current=o.currentTarget.value,I(o.currentTarget.value),V()},style:{width:"100%",border:0,outline:"none",background:"transparent",color:"inherit",font:"inherit",fontSize:30,fontWeight:700,padding:"0 0 12px 54px",boxSizing:"border-box"}}),t.phase==="conflict"?e.renderConflict?e.renderConflict(T):w("div",{role:"alert","data-uibot":"document-conflict",style:{padding:"8px 0",display:"flex",gap:8,flexWrap:"wrap",alignItems:"center"},children:[m("strong",{children:r.conflict}),m("span",{children:t.message}),t.remote?w(me,{children:[m("button",{type:"button",onClick:T.keepMine,children:r.keepMine}),m("button",{type:"button",onClick:T.useRemote,children:r.useRemote})]}):null]}):null,m(N,{ref:x,blocks:n.blocks,markdown:n.markdown,readOnly:c||t.phase==="conflict",locale:d,...e.theme!==void 0?{theme:e.theme}:{},...v?{uploadFile:v}:{},onChange:V}),w("div",{"data-uibot":"document-status",role:t.phase==="error"||t.phase==="unknown"?"alert":"status",style:{fontSize:12,opacity:0.7,padding:"8px 0 0 54px",display:"flex",gap:8,alignItems:"center"},children:[m("span",{children:q}),t.phase==="error"||t.phase==="unknown"?m("button",{type:"button",onClick:()=>void b(),children:r.retry}):null]})]})}export{N as BlockEditor,de as DocumentEditor,S as canSaveDocument,K as documentSaveReducer,O as initialDocumentSaveState,M as isDocumentDirty,W as nextIdempotencyKey};
1
+ import{en as Z,zh as _}from"@blocknote/core/locales";import{BlockNoteView as ee}from"@blocknote/mantine";import{useCreateBlockNote as te}from"@blocknote/react";import{forwardRef as ne,useCallback as j,useEffect as oe,useImperativeHandle as re,useRef as N}from"react";import{jsx as ie}from"react/jsx-runtime";var O=(e)=>JSON.stringify(e),M=ne(function(n,l){let{blocks:u,markdown:C,onChange:f,readOnly:b,locale:p="zh-CN",uploadFile:r}=n,i=N(""),y=N(!1),h=N(f);h.current=f;let t=te({dictionary:p==="en"?Z:_,...u&&u.length>0?{initialContent:u}:{},...r?{uploadFile:r}:{}},[p]),a=j(()=>({blocks:t.document,markdown:t.blocksToMarkdownLossy()}),[t]),m=j((s,v)=>{y.current=!0;try{if(s&&s.length>0)t.replaceBlocks(t.document,s);else if(v)t.replaceBlocks(t.document,t.tryParseMarkdownToBlocks(v))}finally{i.current=O(t.document),y.current=!1}},[t]);return oe(()=>{if(u&&u.length>0)i.current=O(t.document);else m(null,C)},[t]),re(l,()=>({getValue:a,setContent:(s)=>m(s.blocks,s.markdown)}),[a,m]),ie(ee,{className:n.className,editor:t,editable:!b,theme:n.theme??"light",onChange:()=>{if(y.current||!i.current)return;let s=O(t.document);if(s===i.current)return;i.current=s,h.current?.(a())}})});import{useCallback as J,useEffect as w,useReducer as ae,useRef as g,useState as se}from"react";function K(e){return{phase:"clean",revision:e,message:"",remote:null,editedWhileSaving:!1}}function D(e){return e.phase==="dirty"||e.phase==="error"||e.phase==="unknown"}function W(e){return e.phase!=="clean"}function I(e,n){switch(n.type){case"edit":if(e.phase==="saving")return{...e,editedWhileSaving:!0};if(e.phase==="conflict")return e;return{...e,phase:"dirty",message:e.phase==="dirty"?e.message:""};case"save-start":if(!D(e))return e;return{...e,phase:"saving",message:"",editedWhileSaving:!1};case"saved":return{phase:e.editedWhileSaving?"dirty":"clean",revision:n.revision,message:"",remote:null,editedWhileSaving:!1};case"conflict":return{...e,phase:"conflict",message:n.message,remote:n.remote};case"failed":return{...e,phase:n.unknown?"unknown":"error",message:n.message,editedWhileSaving:!1};case"adopt-revision":return e.phase==="conflict"?e:{...e,revision:n.revision};case"keep-mine":return{phase:"dirty",revision:n.revision,message:"",remote:null,editedWhileSaving:!1};case"use-remote":return{phase:"clean",revision:n.revision,message:"",remote:null,editedWhileSaving:!1}}}function V(e,n,l){return e&&e.fingerprint===n?e:{fingerprint:n,key:l()}}import{jsx as d,jsxs as E,Fragment as me}from"react/jsx-runtime";var ce={"zh-CN":{title:"标题",titlePlaceholder:"无标题",clean:"已保存",dirty:"未保存的更改",saving:"保存中…",error:"保存失败,草稿已保留。",unknown:"保存结果未知,草稿已保留。",retry:"重试保存",conflict:"文档已在别处被修改",keepMine:"保留我的修改并覆盖",useRemote:"载入最新版本",readOnly:"只读"},en:{title:"Title",titlePlaceholder:"Untitled",clean:"Saved",dirty:"Unsaved changes",saving:"Saving…",error:"Saving failed. Your draft has been kept.",unknown:"The save result is unknown. Your draft has been kept.",retry:"Retry save",conflict:"This document was changed elsewhere",keepMine:"Keep my changes and overwrite",useRemote:"Load latest version",readOnly:"Read only"}},le=(e,n)=>`${JSON.stringify(e)}\x00${n}`,ue=()=>typeof crypto<"u"&&("randomUUID"in crypto)?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).slice(2)}`;function de(e){let{document:n,client:l,locale:u="zh-CN",hideTitle:C,onDirtyChange:f,onBusyChange:b}=e,p=e.autoSaveMs??1500,r=ce[u],i=n.readOnly===!0,y=l.uploadFile?.bind(l),h=g(e.onSaved);h.current=e.onSaved;let[t,a]=ae(I,n.revision,K),m=g(t);m.current=t;let[s,v]=se(n.title),x=g(n.title),B=g(null),k=g(null),R=g(!1),T=e.revision;w(()=>{if(T!==void 0)a({type:"adopt-revision",revision:T})},[T]);let z=J(()=>{if(!i)a({type:"edit"})},[i]),S=J(async()=>{let o=B.current;if(!o||i||R.current||!D(m.current))return;R.current=!0;let{blocks:U,markdown:A}=o.getValue(),L=x.current,G=le(U,L);k.current=V(k.current,G,ue);let Q={id:n.id,title:L,blocks:U,markdown:A,expectedRevision:m.current.revision,idempotencyKey:k.current.key};a({type:"save-start"});try{let c=await l.save(Q);if(c.ok)k.current=null,a({type:"saved",revision:c.document.revision}),h.current?.(c.document);else if(c.error.code==="conflict")a({type:"conflict",message:c.error.message,remote:c.error.remote??null});else a({type:"failed",message:c.error.message,unknown:c.error.code==="unknown"})}catch(c){a({type:"failed",message:c instanceof Error?c.message:r.unknown,unknown:!0})}finally{R.current=!1}},[l,n.id,i,r.unknown]);w(()=>{if(t.phase!=="dirty"||p<=0)return;let o=setTimeout(()=>void S(),p);return()=>clearTimeout(o)},[t.phase,p,S]);let F=W(t),H=t.phase==="saving";w(()=>f?.(F),[F,f]),w(()=>b?.(H),[H,b]);let Y=(o)=>{B.current?.setContent({blocks:o.blocks,markdown:o.markdown}),x.current=o.title,v(o.title),k.current=null,a({type:"use-remote",revision:o.revision})},P={message:t.message,remote:t.remote,keepMine:()=>t.remote&&a({type:"keep-mine",revision:t.remote.revision}),useRemote:()=>t.remote&&Y(t.remote)},X=(o)=>{if((o.metaKey||o.ctrlKey)&&o.key.toLowerCase()==="s")o.preventDefault(),S()},q=t.phase==="clean"?i?r.readOnly:r.clean:t.phase==="saving"?r.saving:t.phase==="error"?`${r.error}${t.message?` ${t.message}`:""}`:t.phase==="unknown"?r.unknown:r.dirty;return E("div",{"data-uibot":"document-editor",className:e.className,style:e.style,onKeyDown:X,children:[C?null:d("input",{"aria-label":r.title,placeholder:r.titlePlaceholder,value:s,readOnly:i,onChange:(o)=>{x.current=o.currentTarget.value,v(o.currentTarget.value),z()},style:{width:"100%",border:0,outline:"none",background:"transparent",color:"inherit",font:"inherit",fontSize:30,fontWeight:700,padding:"0 0 12px 54px",boxSizing:"border-box"}}),t.phase==="conflict"?e.renderConflict?e.renderConflict(P):E("div",{role:"alert","data-uibot":"document-conflict",style:{padding:"8px 0",display:"flex",gap:8,flexWrap:"wrap",alignItems:"center"},children:[d("strong",{children:r.conflict}),d("span",{children:t.message}),t.remote?E(me,{children:[d("button",{type:"button",onClick:P.keepMine,children:r.keepMine}),d("button",{type:"button",onClick:P.useRemote,children:r.useRemote})]}):null]}):null,d(M,{ref:B,blocks:n.blocks,markdown:n.markdown,readOnly:i||t.phase==="conflict",locale:u,...e.theme!==void 0?{theme:e.theme}:{},...y?{uploadFile:y}:{},onChange:z}),E("div",{"data-uibot":"document-status",role:t.phase==="error"||t.phase==="unknown"?"alert":"status",style:{fontSize:12,opacity:0.7,padding:"8px 0 0 54px",display:"flex",gap:8,alignItems:"center"},children:[d("span",{children:q}),t.phase==="error"||t.phase==="unknown"?d("button",{type:"button",onClick:()=>void S(),children:r.retry}):null]})]})}export{M as BlockEditor,de as DocumentEditor,D as canSaveDocument,I as documentSaveReducer,K as initialDocumentSaveState,W as isDocumentDirty,V as nextIdempotencyKey};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uibot/document-editor",
3
- "version": "0.3.0",
3
+ "version": "0.6.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -34,7 +34,7 @@
34
34
  "@blocknote/core": "0.56.0",
35
35
  "@blocknote/mantine": "0.56.0",
36
36
  "@blocknote/react": "0.56.0",
37
- "@uibot/contracts": "^0.3.0"
37
+ "@uibot/contracts": "^0.6.0"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "rm -rf dist && bun build --minify ./src/index.ts --outdir ./dist --format esm --target browser --packages external && bun x tsc -p tsconfig.json",