@superdoc-dev/react 1.2.0-next.8 → 1.2.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/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`react`);c=s(c);let l=require(`react/jsx-runtime`);function u(){let e=c.useRef(null);return e.current===null&&(e.current=`-${Date.now()}-${Math.random().toString(36).slice(2,9)}`),e.current}const d=typeof c.useId==`function`?c.useId:u;function f(e,t){let[n,r]=(0,c.useState)(!1),{id:i,renderLoading:a,hideToolbar:o=!1,contained:s=!1,className:u,style:f,onReady:p,onEditorCreate:m,onEditorDestroy:h,onEditorUpdate:g,onContentError:_,onException:v,document:y,user:b,users:x,modules:S,...C}=e,w=e.documentMode??`editing`,T=e.role??`editor`,E=(0,c.useRef)(null),D=(0,c.useRef)(null),O=d(),k=i??`superdoc${O}`,A=k,j=`${k}-toolbar`,[M,N]=(0,c.useState)(!0),P=(0,c.useRef)({onReady:p,onEditorCreate:m,onEditorDestroy:h,onEditorUpdate:g,onContentError:_,onException:v});(0,c.useEffect)(()=>{P.current={onReady:p,onEditorCreate:m,onEditorDestroy:h,onEditorUpdate:g,onContentError:_,onException:v}},[p,m,h,g,_,v]);let F=(0,c.useRef)(null),I=(0,c.useRef)(!1),L=(0,c.useRef)(w);(0,c.useEffect)(()=>{L.current!==w&&(E.current?E.current.setDocumentMode(w):I.current&&(F.current=w)),L.current=w},[w]),(0,c.useImperativeHandle)(t,()=>({getInstance:()=>E.current}),[]),(0,c.useEffect)(()=>{N(!0),r(!1),I.current=!0;let e=!1,t=null;return(async()=>{try{let n=(await import(`superdoc`)).SuperDoc;if(e)return;t=new n({...C,selector:`#${CSS.escape(A)}`,...!o&&D.current?{toolbar:`#${CSS.escape(j)}`}:{},documentMode:w,role:T,contained:s,...y==null?{}:{document:y},...b?{user:b}:{},...x?{users:x}:{},...S?{modules:S}:{},onReady:t=>{e||(N(!1),I.current=!1,F.current&&F.current!==w&&(t.superdoc.setDocumentMode(F.current),F.current=null),P.current.onReady?.(t))},onEditorCreate:t=>{e||P.current.onEditorCreate?.(t)},onEditorDestroy:()=>{e||P.current.onEditorDestroy?.()},onEditorUpdate:t=>{e||P.current.onEditorUpdate?.(t)},onContentError:t=>{e||P.current.onContentError?.(t)},onException:t=>{e||P.current.onException?.(t)}}),E.current=t}catch(t){e||(I.current=!1,N(!1),r(!0),console.error(`[SuperDocEditor] Failed to initialize SuperDoc:`,t),P.current.onException?.({error:t}))}})(),()=>{I.current=!1,F.current=null,t&&(t.destroy(),E.current=null),e=!0}},[y,b,x,S,T,o,s,A,j]);let R=[`superdoc-wrapper`,u].filter(Boolean).join(` `),z=M?{display:`none`}:void 0;return(0,l.jsxs)(`div`,{className:R,style:{...f,...s&&{display:`flex`,flexDirection:`column`}},children:[!o&&(0,l.jsx)(`div`,{ref:D,id:j,className:`superdoc-toolbar-container`,style:z}),(0,l.jsx)(`div`,{id:A,className:`superdoc-editor-container`,style:{...z,...s&&{flex:1,minHeight:0}}}),M&&!n&&a&&(0,l.jsx)(`div`,{className:`superdoc-loading-container`,children:a()}),n&&(0,l.jsx)(`div`,{className:`superdoc-error-container`,children:`Failed to load editor. Check console for details.`})]})}const p=(0,c.forwardRef)(f);p.displayName=`SuperDocEditor`;var m=p;exports.SuperDocEditor=p,exports.default=m;
1
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`react`);c=s(c);let l=require(`react/jsx-runtime`);function u(){let e=c.useRef(null);return e.current===null&&(e.current=`-${Date.now()}-${Math.random().toString(36).slice(2,9)}`),e.current}const d=typeof c.useId==`function`?c.useId:u;function f(e){let t=c.useRef(e),n=c.useRef(e);return t.current!==e&&(p(n.current,e)||(n.current=e),t.current=e),n.current}function p(e,t){if(e===t)return!0;if(e==null||t==null)return e===t;try{return JSON.stringify(e)===JSON.stringify(t)}catch{return!1}}function m(e,t){let[n,r]=(0,c.useState)(!1),{id:i,renderLoading:a,hideToolbar:o=!1,contained:s=!1,className:u,style:p,onReady:m,onEditorCreate:h,onEditorDestroy:g,onEditorUpdate:_,onTransaction:v,onContentError:y,onException:b,document:x,user:S,users:C,modules:w,...T}=e,E=e.documentMode??`editing`,D=e.role??`editor`,O=f(S),k=f(C),A=(0,c.useRef)(null),j=(0,c.useRef)(null),M=d(),N=i??`superdoc${M}`,P=N,F=`${N}-toolbar`,[I,L]=(0,c.useState)(!0),R=(0,c.useRef)({onReady:m,onEditorCreate:h,onEditorDestroy:g,onEditorUpdate:_,onTransaction:v,onContentError:y,onException:b});(0,c.useEffect)(()=>{R.current={onReady:m,onEditorCreate:h,onEditorDestroy:g,onEditorUpdate:_,onTransaction:v,onContentError:y,onException:b}},[m,h,g,_,v,y,b]);let z=(0,c.useRef)(null),B=(0,c.useRef)(!1),V=(0,c.useRef)(E);(0,c.useEffect)(()=>{V.current!==E&&(A.current?A.current.setDocumentMode(E):B.current&&(z.current=E)),V.current=E},[E]),(0,c.useImperativeHandle)(t,()=>({getInstance:()=>A.current}),[]),(0,c.useEffect)(()=>{L(!0),r(!1),B.current=!0;let e=!1,t=null;return(async()=>{try{let n=(await import(`superdoc`)).SuperDoc;if(e)return;t=new n({...T,selector:`#${CSS.escape(P)}`,...!o&&j.current?{toolbar:`#${CSS.escape(F)}`}:{},documentMode:E,role:D,contained:s,...x==null?{}:{document:x},...O?{user:O}:{},...k?{users:k}:{},...w?{modules:w}:{},onReady:t=>{e||(L(!1),B.current=!1,z.current&&z.current!==E&&(t.superdoc.setDocumentMode(z.current),z.current=null),R.current.onReady?.(t))},onEditorCreate:t=>{e||R.current.onEditorCreate?.(t)},onEditorDestroy:()=>{e||R.current.onEditorDestroy?.()},onEditorUpdate:t=>{e||R.current.onEditorUpdate?.(t)},onTransaction:t=>{e||R.current.onTransaction?.(t)},onContentError:t=>{e||R.current.onContentError?.(t)},onException:t=>{e||R.current.onException?.(t)}}),A.current=t}catch(t){e||(B.current=!1,L(!1),r(!0),console.error(`[SuperDocEditor] Failed to initialize SuperDoc:`,t),R.current.onException?.({error:t}))}})(),()=>{B.current=!1,z.current=null,t&&(t.destroy(),A.current=null),e=!0}},[x,O,k,w,D,o,s,P,F]);let H=[`superdoc-wrapper`,u].filter(Boolean).join(` `),U=I?{display:`none`}:void 0;return(0,l.jsxs)(`div`,{className:H,style:{...p,...s&&{display:`flex`,flexDirection:`column`}},children:[!o&&(0,l.jsx)(`div`,{ref:j,id:F,className:`superdoc-toolbar-container`,style:U}),(0,l.jsx)(`div`,{id:P,className:`superdoc-editor-container`,style:{...U,...s&&{flex:1,minHeight:0}}}),I&&!n&&a&&(0,l.jsx)(`div`,{className:`superdoc-loading-container`,children:a()}),n&&(0,l.jsx)(`div`,{className:`superdoc-error-container`,children:`Failed to load editor. Check console for details.`})]})}const h=(0,c.forwardRef)(m);h.displayName=`SuperDocEditor`;var g=h;exports.SuperDocEditor=h,exports.default=g;
package/dist/index.d.ts CHANGED
@@ -18,6 +18,8 @@ export declare interface CallbackProps {
18
18
  onEditorDestroy?: () => void;
19
19
  /** Callback when document content is updated */
20
20
  onEditorUpdate?: (event: SuperDocEditorUpdateEvent) => void;
21
+ /** Callback when a transaction is emitted */
22
+ onTransaction?: (event: SuperDocTransactionEvent) => void;
21
23
  /** Callback when there is a content parsing error */
22
24
  onContentError?: (event: SuperDocContentErrorEvent) => void;
23
25
  /** Callback when an exception is thrown */
@@ -29,11 +31,14 @@ export declare type DocumentMode = NonNullable<SuperDocConstructorConfig['docume
29
31
 
30
32
  export { Editor }
31
33
 
34
+ /** Surface where an editor event originated. */
35
+ export declare type EditorSurface = 'body' | 'header' | 'footer';
36
+
32
37
  /**
33
38
  * Callback props that are explicitly typed in CallbackProps.
34
39
  * These are excluded from SuperDocConfig to avoid type conflicts.
35
40
  */
36
- declare type ExplicitCallbackProps = 'onReady' | 'onEditorCreate' | 'onEditorDestroy' | 'onEditorUpdate' | 'onContentError' | 'onException';
41
+ declare type ExplicitCallbackProps = 'onReady' | 'onEditorCreate' | 'onEditorDestroy' | 'onEditorUpdate' | 'onTransaction' | 'onContentError' | 'onException';
37
42
 
38
43
  /**
39
44
  * Props managed internally by the React component (not exposed to users).
@@ -109,8 +114,19 @@ export declare interface SuperDocEditorCreateEvent {
109
114
  export declare interface SuperDocEditorProps extends Omit<SuperDocConfig, InternalProps | OptionalInReact | ExplicitCallbackProps>, Partial<Pick<SuperDocConfig, OptionalInReact>>, CallbackProps, ReactProps {
110
115
  }
111
116
 
112
- /** Event passed to onEditorUpdate callback */
113
- export declare type SuperDocEditorUpdateEvent = Parameters<NonNullable<SuperDocConstructorConfig['onEditorUpdate']>>[0];
117
+ /** Event passed to onEditorUpdate callback. Mirrors superdoc's EditorUpdateEvent. */
118
+ export declare interface SuperDocEditorUpdateEvent {
119
+ /** The primary editor associated with the update. For header/footer edits, this is the main body editor. */
120
+ editor: Editor;
121
+ /** The editor instance that emitted the update. For body edits, this matches `editor`. */
122
+ sourceEditor: Editor;
123
+ /** The surface where the edit originated. */
124
+ surface: EditorSurface;
125
+ /** Relationship ID for header/footer edits. */
126
+ headerId?: string | null;
127
+ /** Header/footer variant (`default`, `first`, `even`, `odd`) when available. */
128
+ sectionType?: string | null;
129
+ }
114
130
 
115
131
  /** Event passed to onException callback */
116
132
  export declare interface SuperDocExceptionEvent {
@@ -138,8 +154,23 @@ export declare interface SuperDocRef {
138
154
  getInstance(): SuperDocInstance | null;
139
155
  }
140
156
 
141
- /** Event passed to onTransaction callback */
142
- export declare type SuperDocTransactionEvent = Parameters<NonNullable<SuperDocConstructorConfig['onTransaction']>>[0];
157
+ /** Event passed to onTransaction callback. Mirrors superdoc's EditorTransactionEvent. */
158
+ export declare interface SuperDocTransactionEvent {
159
+ /** The primary editor associated with the transaction. For header/footer edits, this is the main body editor. */
160
+ editor: Editor;
161
+ /** The editor instance that emitted the transaction. For body edits, this matches `editor`. */
162
+ sourceEditor: Editor;
163
+ /** The ProseMirror transaction or transaction-like payload emitted by the source editor. */
164
+ transaction: any;
165
+ /** Time spent applying the transaction, in milliseconds. */
166
+ duration?: number;
167
+ /** The surface where the transaction originated. */
168
+ surface: EditorSurface;
169
+ /** Relationship ID for header/footer edits. */
170
+ headerId?: string | null;
171
+ /** Header/footer variant (`default`, `first`, `even`, `odd`) when available. */
172
+ sectionType?: string | null;
173
+ }
143
174
 
144
175
  /** User object - extracted from Config.user */
145
176
  export declare type SuperDocUser = NonNullable<SuperDocConstructorConfig['user']>;
package/dist/index.js CHANGED
@@ -6,122 +6,141 @@ function useIdPolyfill() {
6
6
  return s.current === null && (s.current = `-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`), s.current;
7
7
  }
8
8
  const useStableId = typeof React.useId == "function" ? React.useId : useIdPolyfill;
9
+ function useMemoByValue(s) {
10
+ let c = React.useRef(s), l = React.useRef(s);
11
+ return c.current !== s && (shallowJsonEqual(l.current, s) || (l.current = s), c.current = s), l.current;
12
+ }
13
+ function shallowJsonEqual(e, s) {
14
+ if (e === s) return !0;
15
+ if (e == null || s == null) return e === s;
16
+ try {
17
+ return JSON.stringify(e) === JSON.stringify(s);
18
+ } catch {
19
+ return !1;
20
+ }
21
+ }
9
22
  function SuperDocEditorInner(e, s) {
10
- let [l, u] = useState(!1), { id: d, renderLoading: f, hideToolbar: p = !1, contained: m = !1, className: h, style: g, onReady: _, onEditorCreate: v, onEditorDestroy: y, onEditorUpdate: b, onContentError: x, onException: S, document: C, user: w, users: T, modules: E, ...D } = e, O = e.documentMode ?? "editing", k = e.role ?? "editor", A = useRef(null), j = useRef(null), M = useStableId(), N = d ?? `superdoc${M}`, P = N, F = `${N}-toolbar`, [I, L] = useState(!0), R = useRef({
11
- onReady: _,
12
- onEditorCreate: v,
13
- onEditorDestroy: y,
14
- onEditorUpdate: b,
15
- onContentError: x,
16
- onException: S
23
+ let [u, d] = useState(!1), { id: f, renderLoading: p, hideToolbar: m = !1, contained: h = !1, className: g, style: _, onReady: v, onEditorCreate: y, onEditorDestroy: b, onEditorUpdate: x, onTransaction: S, onContentError: C, onException: w, document: T, user: E, users: D, modules: O, ...k } = e, A = e.documentMode ?? "editing", j = e.role ?? "editor", M = useMemoByValue(E), N = useMemoByValue(D), P = useRef(null), F = useRef(null), I = useStableId(), L = f ?? `superdoc${I}`, R = L, z = `${L}-toolbar`, [B, V] = useState(!0), H = useRef({
24
+ onReady: v,
25
+ onEditorCreate: y,
26
+ onEditorDestroy: b,
27
+ onEditorUpdate: x,
28
+ onTransaction: S,
29
+ onContentError: C,
30
+ onException: w
17
31
  });
18
32
  useEffect(() => {
19
- R.current = {
20
- onReady: _,
21
- onEditorCreate: v,
22
- onEditorDestroy: y,
23
- onEditorUpdate: b,
24
- onContentError: x,
25
- onException: S
33
+ H.current = {
34
+ onReady: v,
35
+ onEditorCreate: y,
36
+ onEditorDestroy: b,
37
+ onEditorUpdate: x,
38
+ onTransaction: S,
39
+ onContentError: C,
40
+ onException: w
26
41
  };
27
42
  }, [
28
- _,
29
43
  v,
30
44
  y,
31
45
  b,
32
46
  x,
33
- S
47
+ S,
48
+ C,
49
+ w
34
50
  ]);
35
- let z = useRef(null), B = useRef(!1), V = useRef(O);
51
+ let U = useRef(null), W = useRef(!1), G = useRef(A);
36
52
  useEffect(() => {
37
- V.current !== O && (A.current ? A.current.setDocumentMode(O) : B.current && (z.current = O)), V.current = O;
38
- }, [O]), useImperativeHandle(s, () => ({ getInstance: () => A.current }), []), useEffect(() => {
39
- L(!0), u(!1), B.current = !0;
53
+ G.current !== A && (P.current ? P.current.setDocumentMode(A) : W.current && (U.current = A)), G.current = A;
54
+ }, [A]), useImperativeHandle(s, () => ({ getInstance: () => P.current }), []), useEffect(() => {
55
+ V(!0), d(!1), W.current = !0;
40
56
  let e = !1, s = null;
41
57
  return (async () => {
42
58
  try {
43
59
  let c = (await import("superdoc")).SuperDoc;
44
60
  if (e) return;
45
61
  s = new c({
46
- ...D,
47
- selector: `#${CSS.escape(P)}`,
48
- ...!p && j.current ? { toolbar: `#${CSS.escape(F)}` } : {},
49
- documentMode: O,
50
- role: k,
51
- contained: m,
52
- ...C == null ? {} : { document: C },
53
- ...w ? { user: w } : {},
54
- ...T ? { users: T } : {},
55
- ...E ? { modules: E } : {},
62
+ ...k,
63
+ selector: `#${CSS.escape(R)}`,
64
+ ...!m && F.current ? { toolbar: `#${CSS.escape(z)}` } : {},
65
+ documentMode: A,
66
+ role: j,
67
+ contained: h,
68
+ ...T == null ? {} : { document: T },
69
+ ...M ? { user: M } : {},
70
+ ...N ? { users: N } : {},
71
+ ...O ? { modules: O } : {},
56
72
  onReady: (s) => {
57
- e || (L(!1), B.current = !1, z.current && z.current !== O && (s.superdoc.setDocumentMode(z.current), z.current = null), R.current.onReady?.(s));
73
+ e || (V(!1), W.current = !1, U.current && U.current !== A && (s.superdoc.setDocumentMode(U.current), U.current = null), H.current.onReady?.(s));
58
74
  },
59
75
  onEditorCreate: (s) => {
60
- e || R.current.onEditorCreate?.(s);
76
+ e || H.current.onEditorCreate?.(s);
61
77
  },
62
78
  onEditorDestroy: () => {
63
- e || R.current.onEditorDestroy?.();
79
+ e || H.current.onEditorDestroy?.();
64
80
  },
65
81
  onEditorUpdate: (s) => {
66
- e || R.current.onEditorUpdate?.(s);
82
+ e || H.current.onEditorUpdate?.(s);
83
+ },
84
+ onTransaction: (s) => {
85
+ e || H.current.onTransaction?.(s);
67
86
  },
68
87
  onContentError: (s) => {
69
- e || R.current.onContentError?.(s);
88
+ e || H.current.onContentError?.(s);
70
89
  },
71
90
  onException: (s) => {
72
- e || R.current.onException?.(s);
91
+ e || H.current.onException?.(s);
73
92
  }
74
- }), A.current = s;
93
+ }), P.current = s;
75
94
  } catch (s) {
76
- e || (B.current = !1, L(!1), u(!0), console.error("[SuperDocEditor] Failed to initialize SuperDoc:", s), R.current.onException?.({ error: s }));
95
+ e || (W.current = !1, V(!1), d(!0), console.error("[SuperDocEditor] Failed to initialize SuperDoc:", s), H.current.onException?.({ error: s }));
77
96
  }
78
97
  })(), () => {
79
- B.current = !1, z.current = null, s && (s.destroy(), A.current = null), e = !0;
98
+ W.current = !1, U.current = null, s && (s.destroy(), P.current = null), e = !0;
80
99
  };
81
100
  }, [
82
- C,
83
- w,
84
101
  T,
85
- E,
86
- k,
87
- p,
102
+ M,
103
+ N,
104
+ O,
105
+ j,
88
106
  m,
89
- P,
90
- F
107
+ h,
108
+ R,
109
+ z
91
110
  ]);
92
- let H = ["superdoc-wrapper", h].filter(Boolean).join(" "), U = I ? { display: "none" } : void 0;
111
+ let K = ["superdoc-wrapper", g].filter(Boolean).join(" "), q = B ? { display: "none" } : void 0;
93
112
  return /* @__PURE__ */ jsxs("div", {
94
- className: H,
113
+ className: K,
95
114
  style: {
96
- ...g,
97
- ...m && {
115
+ ..._,
116
+ ...h && {
98
117
  display: "flex",
99
118
  flexDirection: "column"
100
119
  }
101
120
  },
102
121
  children: [
103
- !p && /* @__PURE__ */ jsx("div", {
104
- ref: j,
105
- id: F,
122
+ !m && /* @__PURE__ */ jsx("div", {
123
+ ref: F,
124
+ id: z,
106
125
  className: "superdoc-toolbar-container",
107
- style: U
126
+ style: q
108
127
  }),
109
128
  /* @__PURE__ */ jsx("div", {
110
- id: P,
129
+ id: R,
111
130
  className: "superdoc-editor-container",
112
131
  style: {
113
- ...U,
114
- ...m && {
132
+ ...q,
133
+ ...h && {
115
134
  flex: 1,
116
135
  minHeight: 0
117
136
  }
118
137
  }
119
138
  }),
120
- I && !l && f && /* @__PURE__ */ jsx("div", {
139
+ B && !u && p && /* @__PURE__ */ jsx("div", {
121
140
  className: "superdoc-loading-container",
122
- children: f()
141
+ children: p()
123
142
  }),
124
- l && /* @__PURE__ */ jsx("div", {
143
+ u && /* @__PURE__ */ jsx("div", {
125
144
  className: "superdoc-error-container",
126
145
  children: "Failed to load editor. Check console for details."
127
146
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdoc-dev/react",
3
- "version": "1.2.0-next.8",
3
+ "version": "1.2.0",
4
4
  "description": "Official React wrapper for the SuperDoc document editor",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",