@uibot/document-editor 0.10.0 → 0.10.1
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.
- package/dist/index.js +13 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1 +1,13 @@
|
|
|
1
|
-
import{en as
|
|
1
|
+
import{en as ee,zh as te}from"@blocknote/core/locales";import{BlockNoteView as ne}from"@blocknote/mantine";import{useCreateBlockNote as oe}from"@blocknote/react";import{forwardRef as re,useCallback as A,useEffect as ie,useImperativeHandle as ae,useRef as O}from"react";import{jsx as Y,jsxs as se,Fragment as ce}from"react/jsx-runtime";var M=(e)=>JSON.stringify(e),i="uibot-block-editor",le=`
|
|
2
|
+
.${i} .bn-block-outer{line-height:var(--uibot-doc-line-height,1.75)}
|
|
3
|
+
.${i} [data-content-type=heading]{line-height:1.4;padding-top:var(--uibot-doc-heading-gap,14px);--level:var(--uibot-doc-h1,1.625em)}
|
|
4
|
+
.${i} [data-content-type=heading][data-level="2"]{--level:var(--uibot-doc-h2,1.375em)}
|
|
5
|
+
.${i} [data-content-type=heading][data-level="3"]{--level:var(--uibot-doc-h3,1.25em)}
|
|
6
|
+
.${i} [data-content-type=heading][data-level="4"]{--level:var(--uibot-doc-h4,1.125em)}
|
|
7
|
+
.${i} [data-content-type=heading][data-level="5"],.${i} [data-content-type=heading][data-level="6"]{--level:1em}
|
|
8
|
+
.${i} [data-prev-level="1"]{--prev-level:var(--uibot-doc-h1,1.625em)}
|
|
9
|
+
.${i} [data-prev-level="2"]{--prev-level:var(--uibot-doc-h2,1.375em)}
|
|
10
|
+
.${i} [data-prev-level="3"]{--prev-level:var(--uibot-doc-h3,1.25em)}
|
|
11
|
+
.${i} [data-prev-level="4"]{--prev-level:var(--uibot-doc-h4,1.125em)}
|
|
12
|
+
.${i} [data-prev-level="5"],.${i} [data-prev-level="6"]{--prev-level:1em}
|
|
13
|
+
`,K=re(function(n,d){let{blocks:u,markdown:x,onChange:y,readOnly:S,locale:v="zh-CN",uploadFile:r}=n,a=O(""),h=O(!1),f=O(y);f.current=y;let t=oe({dictionary:v==="en"?ee:te,...u&&u.length>0?{initialContent:u}:{},...r?{uploadFile:r}:{}},[v]),l=A(()=>({blocks:t.document,markdown:t.blocksToMarkdownLossy()}),[t]),p=A((c,k)=>{h.current=!0;try{if(c&&c.length>0)t.replaceBlocks(t.document,c);else if(k)t.replaceBlocks(t.document,t.tryParseMarkdownToBlocks(k))}finally{a.current=M(t.document),h.current=!1}},[t]);return ie(()=>{if(u&&u.length>0)a.current=M(t.document);else p(null,x)},[t]),ae(d,()=>({getValue:l,setContent:(c)=>p(c.blocks,c.markdown)}),[l,p]),se(ce,{children:[Y("style",{"data-uibot":"document-type-scale",children:le}),Y(ne,{className:[i,n.className].filter(Boolean).join(" "),editor:t,editable:!S,theme:n.theme??"light",onChange:()=>{if(h.current||!a.current)return;let c=M(t.document);if(c===a.current)return;a.current=c,f.current?.(l())}})]})});import{useCallback as J,useEffect as C,useReducer as de,useRef as g,useState as ue}from"react";function W(e){return{phase:"clean",revision:e,message:"",remote:null,editedWhileSaving:!1}}function E(e){return e.phase==="dirty"||e.phase==="error"||e.phase==="unknown"}function I(e){return e.phase!=="clean"}function V(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(!E(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 z(e,n,d){return e&&e.fingerprint===n?e:{fingerprint:n,key:d()}}import{jsx as m,jsxs as w,Fragment as ge}from"react/jsx-runtime";var me={"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"}},pe=(e,n)=>`${JSON.stringify(e)}\x00${n}`,ve=()=>typeof crypto<"u"&&("randomUUID"in crypto)?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).slice(2)}`;function he(e){let{document:n,client:d,locale:u="zh-CN",hideTitle:x,onDirtyChange:y,onBusyChange:S}=e,v=e.autoSaveMs??1500,r=me[u],a=n.readOnly===!0,h=d.uploadFile?.bind(d),f=g(e.onSaved);f.current=e.onSaved;let[t,l]=de(V,n.revision,W),p=g(t);p.current=t;let[c,k]=ue(n.title),B=g(n.title),T=g(null),b=g(null),R=g(!1),P=e.revision;C(()=>{if(P!==void 0)l({type:"adopt-revision",revision:P})},[P]);let F=J(()=>{if(!a)l({type:"edit"})},[a]),D=J(async()=>{let o=T.current;if(!o||a||R.current||!E(p.current))return;R.current=!0;let{blocks:U,markdown:G}=o.getValue(),j=B.current,Q=pe(U,j);b.current=z(b.current,Q,ve);let Z={id:n.id,title:j,blocks:U,markdown:G,expectedRevision:p.current.revision,idempotencyKey:b.current.key};l({type:"save-start"});try{let s=await d.save(Z);if(s.ok)b.current=null,l({type:"saved",revision:s.document.revision}),f.current?.(s.document);else if(s.error.code==="conflict")l({type:"conflict",message:s.error.message,remote:s.error.remote??null});else l({type:"failed",message:s.error.message,unknown:s.error.code==="unknown"})}catch(s){l({type:"failed",message:s instanceof Error?s.message:r.unknown,unknown:!0})}finally{R.current=!1}},[d,n.id,a,r.unknown]);C(()=>{if(t.phase!=="dirty"||v<=0)return;let o=setTimeout(()=>void D(),v);return()=>clearTimeout(o)},[t.phase,v,D]);let H=I(t),L=t.phase==="saving";C(()=>y?.(H),[H,y]),C(()=>S?.(L),[L,S]);let _=(o)=>{T.current?.setContent({blocks:o.blocks,markdown:o.markdown}),B.current=o.title,k(o.title),b.current=null,l({type:"use-remote",revision:o.revision})},N={message:t.message,remote:t.remote,keepMine:()=>t.remote&&l({type:"keep-mine",revision:t.remote.revision}),useRemote:()=>t.remote&&_(t.remote)},X=(o)=>{if((o.metaKey||o.ctrlKey)&&o.key.toLowerCase()==="s")o.preventDefault(),D()},q=t.phase==="clean"?a?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:[x?null:m("input",{"aria-label":r.title,placeholder:r.titlePlaceholder,value:c,readOnly:a,onChange:(o)=>{B.current=o.currentTarget.value,k(o.currentTarget.value),F()},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(N):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(ge,{children:[m("button",{type:"button",onClick:N.keepMine,children:r.keepMine}),m("button",{type:"button",onClick:N.useRemote,children:r.useRemote})]}):null]}):null,m(K,{ref:T,blocks:n.blocks,markdown:n.markdown,readOnly:a||t.phase==="conflict",locale:u,...e.theme!==void 0?{theme:e.theme}:{},...h?{uploadFile:h}:{},onChange:F}),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 D(),children:r.retry}):null]})]})}export{K as BlockEditor,he as DocumentEditor,E as canSaveDocument,V as documentSaveReducer,W as initialDocumentSaveState,I as isDocumentDirty,z as nextIdempotencyKey};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uibot/document-editor",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.1",
|
|
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.10.
|
|
37
|
+
"@uibot/contracts": "^0.10.1"
|
|
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",
|