@uibot/document-editor 0.18.0 → 0.19.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.
- package/dist/document-editor.css +1 -0
- package/dist/document-outline.d.ts +6 -0
- package/dist/index.js +13 -13
- package/dist/solutions.d.ts +2 -0
- package/dist/solutions.js +12 -0
- package/dist/spec-catalog.d.ts +2 -0
- package/dist/spec-catalog.js +3 -0
- package/dist/spec.d.ts +28 -0
- package/dist/spec.js +19 -0
- package/package.json +18 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@import "@blocknote/mantine/style.css";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type RefObject } from "react";
|
|
2
|
+
/** Read the rendered editor, so imported blocks and live edits use the same outline. */
|
|
3
|
+
export declare function DocumentOutline({ root, locale, }: {
|
|
4
|
+
root: RefObject<HTMLElement | null>;
|
|
5
|
+
locale: "zh-CN" | "en";
|
|
6
|
+
}): import("react").JSX.Element[] | import("react").JSX.Element;
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
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),
|
|
2
|
-
.${
|
|
3
|
-
.${
|
|
4
|
-
.${
|
|
5
|
-
.${
|
|
6
|
-
.${
|
|
7
|
-
.${
|
|
8
|
-
.${
|
|
9
|
-
.${
|
|
10
|
-
.${
|
|
11
|
-
.${
|
|
12
|
-
.${
|
|
13
|
-
`,K=re(function(n,
|
|
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),a="uibot-block-editor",le=`
|
|
2
|
+
.${a} .bn-block-outer{line-height:var(--uibot-doc-line-height,1.75)}
|
|
3
|
+
.${a} [data-content-type=heading]{line-height:1.4;padding-top:var(--uibot-doc-heading-gap,14px);--level:var(--uibot-doc-h1,1.625em)}
|
|
4
|
+
.${a} [data-content-type=heading][data-level="2"]{--level:var(--uibot-doc-h2,1.375em)}
|
|
5
|
+
.${a} [data-content-type=heading][data-level="3"]{--level:var(--uibot-doc-h3,1.25em)}
|
|
6
|
+
.${a} [data-content-type=heading][data-level="4"]{--level:var(--uibot-doc-h4,1.125em)}
|
|
7
|
+
.${a} [data-content-type=heading][data-level="5"],.${a} [data-content-type=heading][data-level="6"]{--level:1em}
|
|
8
|
+
.${a} [data-prev-level="1"]{--prev-level:var(--uibot-doc-h1,1.625em)}
|
|
9
|
+
.${a} [data-prev-level="2"]{--prev-level:var(--uibot-doc-h2,1.375em)}
|
|
10
|
+
.${a} [data-prev-level="3"]{--prev-level:var(--uibot-doc-h3,1.25em)}
|
|
11
|
+
.${a} [data-prev-level="4"]{--prev-level:var(--uibot-doc-h4,1.125em)}
|
|
12
|
+
.${a} [data-prev-level="5"],.${a} [data-prev-level="6"]{--prev-level:1em}
|
|
13
|
+
`,K=re(function(n,u){let{blocks:m,markdown:x,onChange:y,readOnly:D,locale:p="zh-CN",uploadFile:o}=n,i=O(""),h=O(!1),f=O(y);f.current=y;let t=oe({dictionary:p==="en"?ee:te,...m&&m.length>0?{initialContent:m}:{},...o?{uploadFile:o}:{}},[p]),l=A(()=>({blocks:t.document,markdown:t.blocksToMarkdownLossy()}),[t]),v=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{i.current=M(t.document),h.current=!1}},[t]);return ie(()=>{if(m&&m.length>0)i.current=M(t.document);else v(null,x)},[t]),ae(u,()=>({getValue:l,setContent:(c)=>v(c.blocks,c.markdown)}),[l,v]),se(ce,{children:[Y("style",{"data-uibot":"document-type-scale",children:le}),Y(ne,{className:[a,n.className].filter(Boolean).join(" "),editor:t,editable:!D,theme:n.theme??"light",onChange:()=>{if(h.current||!i.current)return;let c=M(t.document);if(c===i.current)return;i.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,u){return e&&e.fingerprint===n?e:{fingerprint:n,key:u()}}import{jsx as d,jsxs as w,Fragment as ge}from"react/jsx-runtime";var me={"zh-CN":{title:"标题",titlePlaceholder:"无标题",clean:"已保存",dirty:"未保存的更改",saving:"保存中…",error:"保存失败,草稿已保留。",unknown:"保存结果未知,草稿已保留。",retry:"重试保存",save:"保存",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",save:"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:u,locale:m="zh-CN",hideTitle:x,onDirtyChange:y,onBusyChange:D}=e,p=e.autoSaveMs??1500,o=me[m],i=n.readOnly===!0,h=u.uploadFile?.bind(u),f=g(e.onSaved);f.current=e.onSaved;let[t,l]=de(V,n.revision,W),v=g(t);v.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(!i)l({type:"edit"})},[i]),S=J(async()=>{let r=T.current;if(!r||i||R.current||!E(v.current))return;R.current=!0;let{blocks:U,markdown:G}=r.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:v.current.revision,idempotencyKey:b.current.key};l({type:"save-start"});try{let s=await u.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:o.unknown,unknown:!0})}finally{R.current=!1}},[u,n.id,i,o.unknown]);C(()=>{if(t.phase!=="dirty"||p<=0)return;let r=setTimeout(()=>void S(),p);return()=>clearTimeout(r)},[t.phase,p,S]);let H=I(t),L=t.phase==="saving";C(()=>y?.(H),[H,y]),C(()=>D?.(L),[L,D]);let _=(r)=>{T.current?.setContent({blocks:r.blocks,markdown:r.markdown}),B.current=r.title,k(r.title),b.current=null,l({type:"use-remote",revision:r.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=(r)=>{if((r.metaKey||r.ctrlKey)&&r.key.toLowerCase()==="s")r.preventDefault(),S()},q=t.phase==="clean"?i?o.readOnly:o.clean:t.phase==="saving"?o.saving:t.phase==="error"?`${o.error}${t.message?` ${t.message}`:""}`:t.phase==="unknown"?o.unknown:o.dirty;return w("div",{"data-uibot":"document-editor",className:e.className,style:e.style,onKeyDown:X,children:[x?null:d("input",{"aria-label":o.title,placeholder:o.titlePlaceholder,value:c,readOnly:i,onChange:(r)=>{B.current=r.currentTarget.value,k(r.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:[d("strong",{children:o.conflict}),d("span",{children:t.message}),t.remote?w(ge,{children:[d("button",{type:"button",onClick:N.keepMine,children:o.keepMine}),d("button",{type:"button",onClick:N.useRemote,children:o.useRemote})]}):null]}):null,d(K,{ref:T,blocks:n.blocks,markdown:n.markdown,readOnly:i||t.phase==="conflict",locale:m,...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:[d("span",{children:q}),p<=0&&!i&&t.phase==="dirty"?d("button",{type:"button",onClick:()=>void S(),children:o.save}):null,t.phase==="error"||t.phase==="unknown"?d("button",{type:"button",onClick:()=>void S(),children:o.retry}):null]})]})}export{K as BlockEditor,he as DocumentEditor,E as canSaveDocument,V as documentSaveReducer,W as initialDocumentSaveState,I as isDocumentDirty,z as nextIdempotencyKey};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
var a=[["writing","专注写作","Focused writing"],["reading","目录阅读","Reading with an outline"],["library","文档列表与编辑","Document library"],["assistant","文档与 AI 双栏","Document with an assistant"]].map(([t,n,o])=>{let e=`document-${t}`,i={id:`${e}-editor`,type:"DocumentWorkspace",props:{bindingId:"document",title:o,markdown:`# Project notes
|
|
2
|
+
|
|
3
|
+
Capture the decisions behind your next idea.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Write, select text, and explore the formatting controls.
|
|
8
|
+
|
|
9
|
+
## Next steps
|
|
10
|
+
|
|
11
|
+
- Review the draft
|
|
12
|
+
- Connect your document host`,readOnly:t==="reading",autoSaveMs:1500,locale:"zh-CN",previewState:"ready",variant:t}};return{id:e,title:{zh:n,en:o},description:`${o}: shared rich editor with revision-aware host saves and protected drafts.`,solution:{functions:t==="assistant"?["documents","chat"]:["documents"],scenarios:[t],unit:"page",industries:["knowledge","education"],readiness:"interactive-demo",hostRequirements:["Authorized document and DocumentEditorClient","Host revision and idempotency handling",...t==="assistant"?["Authorized chat binding"]:[]],limitations:["Preview documents are stored in memory for this compilation only.","Sharing, collaboration, file upload and model editing require host services."]},spec:{version:"2.0",pages:[{id:e,path:`/${e}`,title:o,blocks:t==="assistant"?[{type:"Grid",props:{cols:2,gap:"md"},children:[i,{id:`${e}-chat`,type:"ChatWorkspace",props:{bindingId:"chat",variant:"copilot",title:"Writing assistant / 写作助手"}}]}]:[i]}]},controls:[...t!=="assistant"?[{id:"layout",label:{zh:"文档布局",en:"Document layout"},values:["writing","reading","library"],target:{pageId:e,blockId:`${e}-editor`,property:"variant"}}]:[],{id:"readonly",label:{zh:"只读",en:"Read only"},values:[!1,!0],target:{pageId:e,blockId:`${e}-editor`,property:"readOnly"}},{id:"autosave",label:{zh:"自动保存间隔",en:"Autosave delay"},values:[0,1500,5000],target:{pageId:e,blockId:`${e}-editor`,property:"autoSaveMs"}},{id:"state",label:{zh:"演示状态",en:"Demo state"},values:["ready","empty","loading","error","conflict","readonly"],target:{pageId:e,blockId:`${e}-editor`,property:"previewState"}}]}});export{a as DOCUMENT_SOLUTIONS};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var e={DocumentWorkspace:{name:"DocumentWorkspace",description:"Rich document editing using the shared editor and host-owned revision/CAS client.",category:"feature",level:"L2",props:{variant:{enum:["writing","reading","library","assistant"],default:"writing"},bindingId:{type:"string",default:"document"},title:{type:"string",default:"Untitled / 无标题"},markdown:{type:"string",default:`# Your next idea
|
|
2
|
+
|
|
3
|
+
Write something useful.`},readOnly:{type:"boolean",default:!1},autoSaveMs:{type:"number",default:1500},locale:{enum:["zh-CN","en"],default:"zh-CN"},previewState:{enum:["ready","empty","loading","error","conflict","readonly"],default:"ready"}},events:{},tags:["interactive","documents","editor","writing"],constraints:["Inject an authorized document/client binding. Local preview saves are not persistence receipts."]}};export{e as DOCUMENT_SPEC_CATALOG};
|
package/dist/spec.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { DocumentEditorClient, EditableDocument } from "@uibot/contracts";
|
|
2
|
+
export interface DocumentSpecBinding {
|
|
3
|
+
document: EditableDocument;
|
|
4
|
+
client: DocumentEditorClient;
|
|
5
|
+
}
|
|
6
|
+
export interface DocumentSpecOptions {
|
|
7
|
+
resolveHost?(bindingId: string): DocumentSpecBinding | undefined;
|
|
8
|
+
/** Trusted preview-only opt-in. Spec cannot enable it. */
|
|
9
|
+
demo?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export { DOCUMENT_SPEC_CATALOG } from "./spec-catalog.js";
|
|
12
|
+
export declare function DocumentWorkspace({ bindingId, title, markdown, readOnly, autoSaveMs, locale, previewState, variant, binding, demo, }: {
|
|
13
|
+
bindingId?: string;
|
|
14
|
+
title?: string | undefined;
|
|
15
|
+
markdown?: string | undefined;
|
|
16
|
+
readOnly?: boolean;
|
|
17
|
+
autoSaveMs?: number;
|
|
18
|
+
locale?: "zh-CN" | "en";
|
|
19
|
+
previewState?: string;
|
|
20
|
+
variant?: string;
|
|
21
|
+
binding?: DocumentSpecBinding | undefined;
|
|
22
|
+
demo?: boolean;
|
|
23
|
+
}): import("react").JSX.Element;
|
|
24
|
+
export declare function createDocumentSpecEntries(options?: DocumentSpecOptions): {
|
|
25
|
+
DocumentWorkspace: {
|
|
26
|
+
component: (props: Record<string, unknown>) => import("react").JSX.Element;
|
|
27
|
+
};
|
|
28
|
+
};
|
package/dist/spec.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import{useMemo as Ee,useRef as xe,useState as K}from"react";import{useCallback as X,useEffect as P,useReducer as fe,useRef as R,useState as ye}from"react";import{en as ae,zh as le}from"@blocknote/core/locales";import{BlockNoteView as se}from"@blocknote/mantine";import{useCreateBlockNote as ce}from"@blocknote/react";import{forwardRef as de,useCallback as F,useEffect as ue,useImperativeHandle as me,useRef as W}from"react";import{jsx as _,jsxs as pe,Fragment as ge}from"react/jsx-runtime";var z=(e)=>JSON.stringify(e),f="uibot-block-editor",ve=`
|
|
2
|
+
.${f} .bn-block-outer{line-height:var(--uibot-doc-line-height,1.75)}
|
|
3
|
+
.${f} [data-content-type=heading]{line-height:1.4;padding-top:var(--uibot-doc-heading-gap,14px);--level:var(--uibot-doc-h1,1.625em)}
|
|
4
|
+
.${f} [data-content-type=heading][data-level="2"]{--level:var(--uibot-doc-h2,1.375em)}
|
|
5
|
+
.${f} [data-content-type=heading][data-level="3"]{--level:var(--uibot-doc-h3,1.25em)}
|
|
6
|
+
.${f} [data-content-type=heading][data-level="4"]{--level:var(--uibot-doc-h4,1.125em)}
|
|
7
|
+
.${f} [data-content-type=heading][data-level="5"],.${f} [data-content-type=heading][data-level="6"]{--level:1em}
|
|
8
|
+
.${f} [data-prev-level="1"]{--prev-level:var(--uibot-doc-h1,1.625em)}
|
|
9
|
+
.${f} [data-prev-level="2"]{--prev-level:var(--uibot-doc-h2,1.375em)}
|
|
10
|
+
.${f} [data-prev-level="3"]{--prev-level:var(--uibot-doc-h3,1.25em)}
|
|
11
|
+
.${f} [data-prev-level="4"]{--prev-level:var(--uibot-doc-h4,1.125em)}
|
|
12
|
+
.${f} [data-prev-level="5"],.${f} [data-prev-level="6"]{--prev-level:1em}
|
|
13
|
+
`,Y=de(function(n,u){let{blocks:m,markdown:y,onChange:v,readOnly:c,locale:d="zh-CN",uploadFile:o}=n,i=W(""),r=W(!1),g=W(v);g.current=v;let t=ce({dictionary:d==="en"?ae:le,...m&&m.length>0?{initialContent:m}:{},...o?{uploadFile:o}:{}},[d]),l=F(()=>({blocks:t.document,markdown:t.blocksToMarkdownLossy()}),[t]),k=F((p,w)=>{r.current=!0;try{if(p&&p.length>0)t.replaceBlocks(t.document,p);else if(w)t.replaceBlocks(t.document,t.tryParseMarkdownToBlocks(w))}finally{i.current=z(t.document),r.current=!1}},[t]);return ue(()=>{if(m&&m.length>0)i.current=z(t.document);else k(null,y)},[t]),me(u,()=>({getValue:l,setContent:(p)=>k(p.blocks,p.markdown)}),[l,k]),pe(ge,{children:[_("style",{"data-uibot":"document-type-scale",children:ve}),_(se,{className:[f,n.className].filter(Boolean).join(" "),editor:t,editable:!c,theme:n.theme??"light",onChange:()=>{if(r.current||!i.current)return;let p=z(t.document);if(p===i.current)return;i.current=p,g.current?.(l())}})]})});function j(e){return{phase:"clean",revision:e,message:"",remote:null,editedWhileSaving:!1}}function L(e){return e.phase==="dirty"||e.phase==="error"||e.phase==="unknown"}function q(e){return e.phase!=="clean"}function G(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(!L(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 J(e,n,u){return e&&e.fingerprint===n?e:{fingerprint:n,key:u()}}import{jsx as D,jsxs as I,Fragment as Se}from"react/jsx-runtime";var he={"zh-CN":{title:"标题",titlePlaceholder:"无标题",clean:"已保存",dirty:"未保存的更改",saving:"保存中…",error:"保存失败,草稿已保留。",unknown:"保存结果未知,草稿已保留。",retry:"重试保存",save:"保存",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",save:"Save",conflict:"This document was changed elsewhere",keepMine:"Keep my changes and overwrite",useRemote:"Load latest version",readOnly:"Read only"}},be=(e,n)=>`${JSON.stringify(e)}\x00${n}`,ke=()=>typeof crypto<"u"&&("randomUUID"in crypto)?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).slice(2)}`;function Q(e){let{document:n,client:u,locale:m="zh-CN",hideTitle:y,onDirtyChange:v,onBusyChange:c}=e,d=e.autoSaveMs??1500,o=he[m],i=n.readOnly===!0,r=u.uploadFile?.bind(u),g=R(e.onSaved);g.current=e.onSaved;let[t,l]=fe(G,n.revision,j),k=R(t);k.current=t;let[p,w]=ye(n.title),T=R(n.title),C=R(null),h=R(null),a=R(!1),N=e.revision;P(()=>{if(N!==void 0)l({type:"adopt-revision",revision:N})},[N]);let B=X(()=>{if(!i)l({type:"edit"})},[i]),E=X(async()=>{let s=C.current;if(!s||i||a.current||!L(k.current))return;a.current=!0;let{blocks:U,markdown:oe}=s.getValue(),V=T.current,re=be(U,V);h.current=J(h.current,re,ke);let ie={id:n.id,title:V,blocks:U,markdown:oe,expectedRevision:k.current.revision,idempotencyKey:h.current.key};l({type:"save-start"});try{let b=await u.save(ie);if(b.ok)h.current=null,l({type:"saved",revision:b.document.revision}),g.current?.(b.document);else if(b.error.code==="conflict")l({type:"conflict",message:b.error.message,remote:b.error.remote??null});else l({type:"failed",message:b.error.message,unknown:b.error.code==="unknown"})}catch(b){l({type:"failed",message:b instanceof Error?b.message:o.unknown,unknown:!0})}finally{a.current=!1}},[u,n.id,i,o.unknown]);P(()=>{if(t.phase!=="dirty"||d<=0)return;let s=setTimeout(()=>void E(),d);return()=>clearTimeout(s)},[t.phase,d,E]);let S=q(t),M=t.phase==="saving";P(()=>v?.(S),[S,v]),P(()=>c?.(M),[M,c]);let O=(s)=>{C.current?.setContent({blocks:s.blocks,markdown:s.markdown}),T.current=s.title,w(s.title),h.current=null,l({type:"use-remote",revision:s.revision})},H={message:t.message,remote:t.remote,keepMine:()=>t.remote&&l({type:"keep-mine",revision:t.remote.revision}),useRemote:()=>t.remote&&O(t.remote)},te=(s)=>{if((s.metaKey||s.ctrlKey)&&s.key.toLowerCase()==="s")s.preventDefault(),E()},ne=t.phase==="clean"?i?o.readOnly:o.clean:t.phase==="saving"?o.saving:t.phase==="error"?`${o.error}${t.message?` ${t.message}`:""}`:t.phase==="unknown"?o.unknown:o.dirty;return I("div",{"data-uibot":"document-editor",className:e.className,style:e.style,onKeyDown:te,children:[y?null:D("input",{"aria-label":o.title,placeholder:o.titlePlaceholder,value:p,readOnly:i,onChange:(s)=>{T.current=s.currentTarget.value,w(s.currentTarget.value),B()},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(H):I("div",{role:"alert","data-uibot":"document-conflict",style:{padding:"8px 0",display:"flex",gap:8,flexWrap:"wrap",alignItems:"center"},children:[D("strong",{children:o.conflict}),D("span",{children:t.message}),t.remote?I(Se,{children:[D("button",{type:"button",onClick:H.keepMine,children:o.keepMine}),D("button",{type:"button",onClick:H.useRemote,children:o.useRemote})]}):null]}):null,D(Y,{ref:C,blocks:n.blocks,markdown:n.markdown,readOnly:i||t.phase==="conflict",locale:m,...e.theme!==void 0?{theme:e.theme}:{},...r?{uploadFile:r}:{},onChange:B}),I("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:ne}),d<=0&&!i&&t.phase==="dirty"?D("button",{type:"button",onClick:()=>void E(),children:o.save}):null,t.phase==="error"||t.phase==="unknown"?D("button",{type:"button",onClick:()=>void E(),children:o.retry}):null]})]})}import{useEffect as De,useState as we}from"react";import{jsx as Z}from"react/jsx-runtime";function ee({root:e,locale:n}){let[u,m]=we([]);return De(()=>{let y=e.current;if(!y)return;let v=new WeakMap,c=0,d=()=>{let i=Array.from(y.querySelectorAll(".bn-block-content[data-content-type=heading]")).flatMap((r)=>{let g=r.textContent?.replace(/\s+/g," ").trim(),t=v.get(r);if(t===void 0)t=c++,v.set(r,t);return g?[{id:t,node:r,text:g,level:Math.max(1,Math.min(6,Number(r.dataset.level)||1))}]:[]});m((r)=>r.length===i.length&&r.every((g,t)=>{let l=i[t];return l?.node===g.node&&l.text===g.text&&l.level===g.level})?r:i)};d();let o=new MutationObserver(d);return o.observe(y,{childList:!0,subtree:!0,characterData:!0,attributes:!0,attributeFilter:["data-content-type","data-level"]}),()=>o.disconnect()},[e]),u.length?u.map(({id:y,node:v,text:c,level:d})=>Z("button",{type:"button",className:"block text-left text-sm underline",style:{paddingInlineStart:`${(d-1)*0.75}rem`},onClick:()=>{if(e.current?.contains(v))v.scrollIntoView({block:"center",behavior:"smooth"})},children:c},y)):Z("p",{className:"text-sm text-muted-foreground",children:n==="en"?"No headings yet":"暂无标题"})}var Ce={DocumentWorkspace:{name:"DocumentWorkspace",description:"Rich document editing using the shared editor and host-owned revision/CAS client.",category:"feature",level:"L2",props:{variant:{enum:["writing","reading","library","assistant"],default:"writing"},bindingId:{type:"string",default:"document"},title:{type:"string",default:"Untitled / 无标题"},markdown:{type:"string",default:`# Your next idea
|
|
14
|
+
|
|
15
|
+
Write something useful.`},readOnly:{type:"boolean",default:!1},autoSaveMs:{type:"number",default:1500},locale:{enum:["zh-CN","en"],default:"zh-CN"},previewState:{enum:["ready","empty","loading","error","conflict","readonly"],default:"ready"}},events:{},tags:["interactive","documents","editor","writing"],constraints:["Inject an authorized document/client binding. Local preview saves are not persistence receipts."]}};import{jsx as x,jsxs as A}from"react/jsx-runtime";var Te={fontFamily:"var(--font-body, ui-sans-serif, system-ui, sans-serif)",colors:{editor:{text:"var(--foreground)",background:"var(--background)"},menu:{text:"var(--popover-foreground, var(--foreground))",background:"var(--popover, var(--background))"},tooltip:{text:"var(--popover-foreground, var(--foreground))",background:"var(--popover, var(--background))"},hovered:{text:"var(--accent-foreground)",background:"var(--accent)"},selected:{text:"var(--accent-foreground)",background:"var(--accent)"},disabled:{text:"var(--muted-foreground)",background:"var(--muted)"},border:"var(--border)",shadow:"var(--border)",sideMenu:"var(--muted-foreground)"}};function Ne({bindingId:e="document",title:n="Your next idea / 新文档",markdown:u=`# Your next idea
|
|
16
|
+
|
|
17
|
+
Write something useful.`,readOnly:m=!1,autoSaveMs:y=1500,locale:v="zh-CN",previewState:c="ready",variant:d="writing",binding:o,demo:i=!1}){let[r,g]=K("main"),[t,l]=K(!1),[k,p]=K(!1),w=xe(null),T=Ee(()=>({bindingId:e,title:n,markdown:u,readOnly:m,previewState:c,entries:new Map}),[e,n,u,m,c]),C=T.entries.get(r);if(!C)C=(()=>{let a={id:`${e}:${r}`,title:r==="main"?n:"Integration notes / 接入笔记",markdown:c==="empty"?"":r==="main"?u:`# Integration notes
|
|
18
|
+
|
|
19
|
+
The host owns authorization, revisions and storage.`,blocks:null,revision:1,readOnly:m||c==="readonly"},N=!1,B=new Map,E={document:a,client:{async save(S){let M=B.get(S.idempotencyKey);if(M)return M;if(c==="error")return{ok:!1,error:{code:"validation",message:"演示保存错误 / Demo save error"}};if(c==="conflict"&&!N||S.expectedRevision!==a.revision)return N=!0,a={...a,revision:Number(a.revision)+1},{ok:!1,error:{code:"conflict",message:"演示版本冲突 / Demo revision conflict",remote:{...a,revision:a.revision}}};a={...a,title:S.title,blocks:S.blocks,markdown:S.markdown,revision:Number(a.revision)+1},E.document=a;let O={ok:!0,document:a};return B.set(S.idempotencyKey,O),O}}};return E})(),T.entries.set(r,C);let h=o??(i?C:void 0);if(i&&!o&&c==="loading")return x("p",{role:"status",className:"p-6",children:"演示加载中 / Demo loading…"});if(!h)return x("p",{role:"status",className:"p-6",children:"文档尚未接入:宿主需提供文档与保存接口。Document host required."});return A("section",{ref:w,className:"min-h-80 rounded border bg-background text-foreground",children:[x("p",{className:"border-b px-4 py-2 text-xs text-muted-foreground",children:i&&!o?"本地内存演示 · 刷新后清空 · 不代表持久化保存":"宿主文档 / Host document"}),A("div",{className:"flex flex-col md:flex-row",children:[(d==="reading"||d==="library")&&A("nav",{"aria-label":"Document navigation / 文档导航",className:"shrink-0 space-y-3 border-b p-4 md:w-52 md:border-b-0 md:border-r",children:[d==="library"&&["main","integration"].map((a)=>x("button",{type:"button",disabled:k||!!o,"aria-pressed":r===a,className:"block text-left text-sm underline disabled:opacity-50",onClick:()=>{if(t&&!window.confirm("放弃当前文档的未保存更改?Discard unsaved changes?"))return;l(!1),g(a)},children:a==="main"?"项目笔记 / Project notes":"接入笔记 / Integration notes"},a)),d==="reading"&&x(ee,{root:w,locale:v})]}),x("div",{className:"min-w-0 flex-1",children:x(Q,{theme:Te,onDirtyChange:l,onBusyChange:p,document:{...h.document,readOnly:h.document.readOnly||m},client:h.client,autoSaveMs:Math.max(0,y),locale:v},`${h.document.id}:${o?"host":c}`)})]})]})}function qe(e={}){return{DocumentWorkspace:{component:(n)=>x(Ne,{variant:String(n.variant??"writing"),bindingId:String(n.bindingId??"document"),title:typeof n.title==="string"?n.title:void 0,markdown:typeof n.markdown==="string"?n.markdown:void 0,readOnly:n.readOnly===!0,autoSaveMs:typeof n.autoSaveMs==="number"?n.autoSaveMs:1500,locale:n.locale==="en"?"en":"zh-CN",previewState:String(n.previewState??"ready"),binding:e.resolveHost?.(String(n.bindingId??"document")),demo:e.demo===!0},String(n.bindingId??"document"))}}}export{Ce as DOCUMENT_SPEC_CATALOG,Ne as DocumentWorkspace,qe as createDocumentSpecEntries};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uibot/document-editor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,9 +12,24 @@
|
|
|
12
12
|
"module": "./dist/index.js",
|
|
13
13
|
"types": "./dist/index.d.ts",
|
|
14
14
|
"exports": {
|
|
15
|
+
"./style.css": {
|
|
16
|
+
"default": "./dist/document-editor.css"
|
|
17
|
+
},
|
|
15
18
|
".": {
|
|
16
19
|
"types": "./dist/index.d.ts",
|
|
17
20
|
"default": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./spec": {
|
|
23
|
+
"types": "./dist/spec.d.ts",
|
|
24
|
+
"default": "./dist/spec.js"
|
|
25
|
+
},
|
|
26
|
+
"./catalog": {
|
|
27
|
+
"types": "./dist/spec-catalog.d.ts",
|
|
28
|
+
"default": "./dist/spec-catalog.js"
|
|
29
|
+
},
|
|
30
|
+
"./solutions": {
|
|
31
|
+
"types": "./dist/solutions.d.ts",
|
|
32
|
+
"default": "./dist/solutions.js"
|
|
18
33
|
}
|
|
19
34
|
},
|
|
20
35
|
"files": [
|
|
@@ -34,10 +49,10 @@
|
|
|
34
49
|
"@blocknote/core": "0.56.0",
|
|
35
50
|
"@blocknote/mantine": "0.56.0",
|
|
36
51
|
"@blocknote/react": "0.56.0",
|
|
37
|
-
"@uibot/contracts": "^0.
|
|
52
|
+
"@uibot/contracts": "^0.19.0"
|
|
38
53
|
},
|
|
39
54
|
"scripts": {
|
|
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",
|
|
55
|
+
"build": "rm -rf dist && bun build --minify ./src/index.ts ./src/spec.tsx ./src/spec-catalog.ts ./src/solutions.ts --outdir ./dist --format esm --target browser --packages external && cp ./src/document-editor.css ./dist/ && bun x tsc -p tsconfig.json",
|
|
41
56
|
"typecheck": "bun x tsc -p tsconfig.json --noEmit",
|
|
42
57
|
"test": "bun test"
|
|
43
58
|
}
|