@betteroffice/pptx-react 0.0.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/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # @betteroffice/pptx-react
2
+
3
+ React chrome for the BetterOffice PPTX editor — wraps
4
+ [`@betteroffice/pptx`](https://www.npmjs.com/package/@betteroffice/pptx) in a
5
+ slide canvas, slide strip, formatting toolbar, and keyboard editing surface.
6
+
7
+ > **Experimental (`0.0.x`).** The API is unstable and may change in any release.
8
+
9
+ ```bash
10
+ bun add @betteroffice/pptx-react @betteroffice/pptx react react-dom
11
+ ```
12
+
13
+ `react` and `react-dom` (18 or 19) are peer dependencies.
14
+
15
+ ## Usage
16
+
17
+ ```tsx
18
+ import { PptxEditor } from '@betteroffice/pptx-react';
19
+
20
+ export function Presentation({ file, fontBytes }: {
21
+ file: Uint8Array;
22
+ fontBytes: Uint8Array;
23
+ }) {
24
+ return (
25
+ <div style={{ height: 720 }}>
26
+ <PptxEditor
27
+ file={file}
28
+ fonts={[{ family: 'My Sans', bytes: fontBytes }]}
29
+ onChange={(deck) => console.log(deck.slides.length)}
30
+ />
31
+ </div>
32
+ );
33
+ }
34
+ ```
35
+
36
+ Click text to place the Rust-computed caret, type to edit the yrs story and
37
+ trigger Rust reflow, or use the toolbar for bold, italic, size, color, slides,
38
+ and text boxes. `onReady` exposes the core handle and a `refresh` callback for
39
+ host-driven edits.
40
+
41
+ ## Collaboration
42
+
43
+ Pass a `collaboration` prop to co-edit a deck live. `onReplica` hands you the
44
+ session; drive it with a `CollaborationProvider` over any transport. Every peer
45
+ must boot from the same `initialUpdate` seed.
46
+
47
+ ```tsx
48
+ import { CollaborationProvider } from '@betteroffice/pptx/collaboration';
49
+
50
+ <PptxEditor
51
+ file={file}
52
+ collaboration={{
53
+ clientId,
54
+ initialUpdate: sharedSeed,
55
+ onReplica: (replica) => {
56
+ if (!replica) return;
57
+ const provider = new CollaborationProvider(replica, transport);
58
+ provider.connect();
59
+ },
60
+ }}
61
+ />;
62
+ ```
63
+
64
+ Docs: https://betteroffice.dev · Apache-2.0.
@@ -0,0 +1,45 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { PptxFontFace, PresentationHandle, DeckSnapshot } from '@betteroffice/pptx';
4
+ import { CollaborationReplica } from '@betteroffice/pptx/collaboration';
5
+ import { Translations, TFunction } from '@betteroffice/pptx-i18n';
6
+
7
+ interface PptxTextSelection {
8
+ shapeId: string;
9
+ storyId: string;
10
+ anchor: number;
11
+ focus: number;
12
+ }
13
+ interface PptxEditorApi {
14
+ handle: PresentationHandle;
15
+ refresh: () => void;
16
+ }
17
+ interface PptxEditorCollaborationOptions {
18
+ clientId: number;
19
+ initialUpdate?: Uint8Array;
20
+ onReplica?: (replica: CollaborationReplica | null) => void;
21
+ }
22
+ interface PptxEditorProps {
23
+ file?: Uint8Array;
24
+ /** Font faces; equivalent inline arrays do not reopen the presentation. */
25
+ fonts: ReadonlyArray<PptxFontFace>;
26
+ clientId?: number;
27
+ collaboration?: PptxEditorCollaborationOptions;
28
+ i18n?: Translations;
29
+ className?: string;
30
+ onReady?: (api: PptxEditorApi) => void;
31
+ onChange?: (snapshot: DeckSnapshot) => void;
32
+ onError?: (error: Error) => void;
33
+ }
34
+ declare function PptxEditor({ i18n, ...props }: PptxEditorProps): react.JSX.Element;
35
+
36
+ interface LocaleProviderProps {
37
+ i18n?: Translations;
38
+ children: ReactNode;
39
+ }
40
+ declare function LocaleProvider({ i18n, children }: LocaleProviderProps): react.JSX.Element;
41
+ declare function useTranslation(): {
42
+ t: TFunction;
43
+ };
44
+
45
+ export { LocaleProvider, type LocaleProviderProps, PptxEditor, type PptxEditorApi, type PptxEditorCollaborationOptions, type PptxEditorProps, type PptxTextSelection, useTranslation };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import {initWasm,openPresentation,sizeCanvasForSlide,paintSlide}from'@betteroffice/pptx';import {createContext,useMemo,useContext,useRef,useState,useCallback,useEffect}from'react';import {en,deepMerge,createT}from'@betteroffice/pptx-i18n';import {jsx,jsxs,Fragment}from'react/jsx-runtime';var Ne=createContext(en),Xe=createContext("en");function ne({i18n:t,children:r}){let o=typeof t?._lang=="string"?t._lang:"en",d=useMemo(()=>deepMerge(en,t),[t]);return jsx(Xe.Provider,{value:o,children:jsx(Ne.Provider,{value:d,children:r})})}function re(){let t=useContext(Ne),r=useContext(Xe);return {t:useMemo(()=>createT(t,r),[t,r])}}function oe(t,r,o,d){if(!Number.isFinite(t.width)||!Number.isFinite(t.height)||t.width<=0||t.height<=0)return null;let h=(o-t.left)*r.width/t.width,f=(d-t.top)*r.height/t.height;return Number.isFinite(h)&&Number.isFinite(f)?{x:h,y:f}:null}function X(t,r){for(let o of t){if(o.id===r)return o;let d=X(o.children,r);if(d)return d}return null}function Ye(t,r){for(let o of t.shapes)if(o.id===r||X(o.children,r))return o;return null}function pe(t){return t.width>0&&t.height>0}function Ge(t,r,o){let d=new Set;qe(o,d);let h=r.primitives.filter(m=>m.shapeId&&d.has(m.shapeId));if(h.length===0)return o.width<=0||o.height<=0||t.widthEmu<=0||t.heightEmu<=0?null:{x:o.x*r.width/t.widthEmu,y:o.y*r.height/t.heightEmu,width:o.width*r.width/t.widthEmu,height:o.height*r.height/t.heightEmu};let f=h.map(m=>{let A=(m.transform?.rotationDeg??0)*Math.PI/180,U=Math.abs(m.w*Math.cos(A))+Math.abs(m.h*Math.sin(A)),g=Math.abs(m.w*Math.sin(A))+Math.abs(m.h*Math.cos(A));return {x:m.x+(m.w-U)/2,y:m.y+(m.h-g)/2,width:U,height:g}}),S=Math.min(...f.map(m=>m.x)),z=Math.min(...f.map(m=>m.y)),l=Math.max(...f.map(m=>m.x+m.width)),I=Math.max(...f.map(m=>m.y+m.height));return {x:S,y:z,width:l-S,height:I-z}}function Oe(t,r,o,d){let h=t.primitives.find(l=>l.kind==="textBox"&&l.shapeId===r&&l.storyId===o);if(!h||h.lines.length===0)return null;let f=h.lines.reduce((l,I)=>Ke(I.y,I.height,d.y)<Ke(l.y,l.height,d.y)?I:l),S=f.caretStops[0];return S?f.caretStops.reduce((l,I)=>Math.abs(I.x-d.x)<Math.abs(l.x-d.x)?I:l,S).position:f.start}function je(t,r,o,d){return {x:o.x+Math.round(d.x*t.widthEmu/r.width),y:o.y+Math.round(d.y*t.heightEmu/r.height)}}function We(t,r,o,d,h=4){return Math.hypot(o-t,d-r)>=h}function qe(t,r){r.add(t.id);for(let o of t.children)qe(o,r);}function Ke(t,r,o){return o<t?t-o:o>t+r?o-t-r:0}var mt={bold:false,italic:false,fontSizePt:24,color:"#111827"};function $({name:t}){return jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round","aria-hidden":"true",focusable:"false",style:{flex:"0 0 auto"},children:[t==="undo"&&jsx("path",{d:"m9 7-5 5 5 5M5 12h9a6 6 0 0 1 6 6"}),t==="redo"&&jsx("path",{d:"m15 7 5 5-5 5m4-5h-9a6 6 0 0 0-6 6"}),t==="addSlide"&&jsxs(Fragment,{children:[jsx("rect",{x:"3",y:"5",width:"14",height:"14",rx:"2"}),jsx("path",{d:"M7 9h6M7 13h4M20 10v6m-3-3h6"})]}),t==="deleteSlide"&&jsxs(Fragment,{children:[jsx("path",{d:"M5 7h14M9 7V4h6v3m2 0-1 13H8L7 7"}),jsx("path",{d:"M10 11v5m4-5v5"})]}),t==="textBox"&&jsxs(Fragment,{children:[jsx("rect",{x:"3",y:"4",width:"18",height:"16",rx:"2"}),jsx("path",{d:"M8 8h8m-4 0v8m-3 0h6"})]})]})}function bt({i18n:t,...r}){return jsx(ne,{i18n:t,children:jsx(xt,{...r})})}function xt({file:t,fonts:r,clientId:o,collaboration:d,className:h,onReady:f,onChange:S,onError:z}){let{t:l}=re(),I=l("errors.decodeSlideImage"),m=d?.clientId??o,A=d?.initialUpdate,U=d?.onReplica,g=useRef(null),F=useRef(null),ge=useRef(f),Se=useRef(S),Ie=useRef(z),V=useRef(null),Pe=useRef(null),ve=useRef(null),we=useRef(null),P=useRef(null),v=useRef(null),ie=useRef(new Map),ae=St(r),[y,Ce]=useState(null),[c,C]=useState(null),[J,T]=useState(null),[le,k]=useState(null),[D,Ze]=useState(mt),[Q,$e]=useState({width:0,height:0}),[Me,se]=useState(null),[et,ee]=useState(false),[de,Te]=useState(null);ge.current=f,Se.current=S,Ie.current=z,F.current=y;let w=useCallback(e=>{let n=e instanceof Error?e:new Error(String(e));se(n.message),Ie.current?.(n);},[]),L=useCallback((e,n=false,i=false)=>{let s=g.current;if(!s)return null;try{let a=s.snapshot(),p=gt(e??F.current?.slideIndex??0,a.slides.length),b=new Map;for(let M=0;M<a.slides.length;M+=1){let ce=a.slides[M],De=F.current?.thumbnails.get(ce.id);M!==p&&De&&!i?b.set(ce.id,De):M!==p&&b.set(ce.id,s.layoutSlide(M));}let H=a.slides.length>0?s.layoutSlide(p):null;H&&b.set(a.slides[p].id,H);let W={snapshot:a,slideIndex:p,frame:H,thumbnails:b},Y=a.slides[p];C(M=>M&&Y&&X(Y.shapes,M.shapeId)?M:null),T(M=>M&&Y?.id===M.slideId&&X(Y.shapes,M.shapeId)?M:null);let ue=P.current;return ue&&(!Y||Y.id!==ue.slideId||!X(Y.shapes,ue.shapeId))&&(P.current=null,v.current=null,k(null)),F.current=W,Ce(W),se(null),n&&Se.current?.(a),W}catch(a){return w(a),null}},[w]),ke=useCallback(()=>{L(void 0,false,true);},[L]);useEffect(()=>{let e=false,n=null,i=[],s=()=>{};if(g.current?.dispose(),g.current=null,Te(null),F.current=null,Ce(null),C(null),T(null),k(null),P.current=null,v.current=null,se(null),ie.current.clear(),!!t)return ee(true),Promise.all([initWasm(),Mt(ae)]).then(([,a])=>{if(i=a,e){_e(i);return}try{n=openPresentation(t,{clientId:m,fonts:ae,initialUpdate:A}),g.current=n,s=n.onUpdate((p,b)=>{b==="remote"&&L(void 0,!0,!0);}),L(0),ee(!1),Te(n),ge.current?.({handle:n,refresh:ke});}catch(p){ee(false),w(p);}},a=>{e||(ee(false),w(a));}),()=>{e=true,s(),n?.dispose(),g.current===n&&(g.current=null),_e(i);}},[m,A,t,ae,ke,L,w]),useEffect(()=>{if(!(!U||!de))return U(de),()=>U(null)},[U,de]),useEffect(()=>{let e=Pe.current;if(!e)return;let n=()=>$e({width:e.clientWidth,height:e.clientHeight});n();let i=new ResizeObserver(n);return i.observe(e),()=>i.disconnect()},[]);let B=useMemo(()=>!y?.frame||Q.width<=0||Q.height<=0?1:Math.min((Q.width-40)/y.frame.width,(Q.height-40)/y.frame.height,1),[y?.frame,Q]);useEffect(()=>{let e=ve.current,n=y?.frame;if(!e||!n)return;let i=e.getContext("2d");if(!i)return;let s=window.devicePixelRatio||1;sizeCanvasForSlide(e,n,s,B);let a=false;return paintSlide(i,n,s,B,{resolveImage:p=>Ve(p,g,ie,I)}).catch(p=>{a||w(p);}),()=>{a=true;}},[I,y?.frame,w,B]),useEffect(()=>{let e=we.current,n=y?.frame;if(!e||!n)return;let i=e.getContext("2d");if(!i)return;let s=window.devicePixelRatio||1;sizeCanvasForSlide(e,n,s,B),kt(i,n,c,s,B);},[y?.frame,B,c]);let K=useMemo(()=>{if(!y?.frame||!J)return null;let e=y.snapshot.slides[y.slideIndex];return !e||e.id!==J.slideId?null:X(e.shapes,J.shapeId)},[y,J]),Z=useMemo(()=>y?.frame&&K?Ge(y.snapshot,y.frame,K):null,[y,K]),tt=e=>{C(null),T(null),k(null),P.current=null,v.current=null,L(e),V.current?.focus();},nt=e=>{if(!e.isPrimary||e.button!==0)return;let n=g.current,i=F.current;if(!n||!i?.frame)return;let s=oe(e.currentTarget.getBoundingClientRect(),i.frame,e.clientX,e.clientY);if(s)try{n.layoutSlide(i.slideIndex);let a=n.hitTest(s.x,s.y),p=i.snapshot.slides[i.slideIndex];if(p&&c&&a?.kind==="text"&&a.shapeId===c.shapeId&&a.storyId===c.storyId){n.story(a.storyId);let b=e.shiftKey?c.anchor:a.position;C({shapeId:a.shapeId,storyId:a.storyId,anchor:b,focus:a.position}),T(null),k(null),v.current=null,P.current={kind:"text",pointerId:e.pointerId,slideId:p.id,shapeId:a.shapeId,storyId:a.storyId,anchor:b},e.currentTarget.setPointerCapture(e.pointerId);}else if(p&&a){let b=Ye(p,a.shapeId);if(b){C(null),T({slideId:p.id,shapeId:b.id}),k(null);let H=v.current,W=H?.slideId===p.id&&H.shapeId===b.id&&e.timeStamp-H.timeStamp<=600&&Math.hypot(e.clientX-H.clientX,e.clientY-H.clientY)<=6;v.current=null,pe(b)?(P.current={kind:"shape",pointerId:e.pointerId,slideId:p.id,shapeId:b.id,startClientX:e.clientX,startClientY:e.clientY,start:s,last:s,dragThreshold:W?8:4,repeatedClick:W,dragging:!1},e.currentTarget.setPointerCapture(e.pointerId)):P.current=null;}else C(null),T(null),v.current=null,P.current=null;}else C(null),T(null),k(null),v.current=null,P.current=null;V.current?.focus(),e.preventDefault();}catch(a){w(a);}},rt=e=>{if(e.button!==0)return;let n=g.current,i=F.current;if(!n||!i?.frame)return;let s=oe(e.currentTarget.getBoundingClientRect(),i.frame,e.clientX,e.clientY);if(s)try{n.layoutSlide(i.slideIndex);let a=n.hitTest(s.x,s.y);a?.kind==="text"&&(n.story(a.storyId),C({shapeId:a.shapeId,storyId:a.storyId,anchor:a.position,focus:a.position}),T(null),k(null),P.current=null,v.current=null,V.current?.focus(),e.preventDefault());}catch(a){w(a);}},Ee=e=>{let n=P.current,i=F.current;if(!n||n.pointerId!==e.pointerId||!i?.frame)return false;let s=oe(e.currentTarget.getBoundingClientRect(),i.frame,e.clientX,e.clientY);if(!s||i.snapshot.slides[i.slideIndex]?.id!==n.slideId)return false;if(n.kind==="text"){let a=Oe(i.frame,n.shapeId,n.storyId,s);a!==null&&C({shapeId:n.shapeId,storyId:n.storyId,anchor:n.anchor,focus:a});}else n.last=s,!n.dragging&&We(n.startClientX,n.startClientY,e.clientX,e.clientY,n.dragThreshold)&&(n.dragging=true),n.dragging&&k({shapeId:n.shapeId,delta:{x:s.x-n.start.x,y:s.y-n.start.y}});return true},ot=e=>{Ee(e)&&e.preventDefault();},it=e=>{Ee(e);let n=P.current;if(!n||n.pointerId!==e.pointerId||(P.current=null,k(null),e.currentTarget.hasPointerCapture(e.pointerId)&&e.currentTarget.releasePointerCapture(e.pointerId),n.kind!=="shape"))return;if(!n.dragging){v.current=n.repeatedClick?null:{slideId:n.slideId,shapeId:n.shapeId,clientX:e.clientX,clientY:e.clientY,timeStamp:e.timeStamp};return}v.current=null;let i=g.current,s=F.current,a=s?.snapshot.slides[s.slideIndex];if(!i||!s?.frame||a?.id!==n.slideId)return;let p=X(a.shapes,n.shapeId);if(p)try{let b=je(s.snapshot,s.frame,p,{x:n.last.x-n.start.x,y:n.last.y-n.start.y});(b.x!==p.x||b.y!==p.y)&&(i.moveShape(a.id,p.id,b.x,b.y),L(void 0,!0)),T({slideId:a.id,shapeId:p.id}),e.preventDefault();}catch(b){w(b);}},Le=e=>{P.current?.pointerId===e.pointerId&&(P.current=null,k(null),v.current=null);},G=e=>{C(e),T(null),v.current=null,L(void 0,true);},at=e=>{let n=g.current;if(!n||!c)return;if((e.metaKey||e.ctrlKey)&&(e.key==="b"||e.key==="B")){e.preventDefault(),O({bold:!D.bold});return}if((e.metaKey||e.ctrlKey)&&(e.key==="i"||e.key==="I")){e.preventDefault(),O({italic:!D.italic});return}let i=Math.min(c.anchor,c.focus),s=Math.max(c.anchor,c.focus);try{if(e.key==="ArrowLeft"||e.key==="ArrowRight"){e.preventDefault();let a=n.story(c.storyId),p=e.key==="ArrowLeft"?-1:1,b=Math.max(0,Math.min(a.length,c.focus+p));C({...c,anchor:e.shiftKey?c.anchor:b,focus:b});return}if(e.key==="Backspace"){if(e.preventDefault(),i!==s){n.deleteText(c.storyId,i,s),G({...c,anchor:i,focus:i});return}let a=wt(n.story(c.storyId),i);a<i&&(n.deleteText(c.storyId,a,i),G({...c,anchor:a,focus:a}));return}if(e.key==="Delete"){if(e.preventDefault(),i!==s){n.deleteText(c.storyId,i,s),G({...c,anchor:i,focus:i});return}let a=Ct(n.story(c.storyId),s);a>s&&(n.deleteText(c.storyId,s,a),G({...c,anchor:s,focus:s}));return}if(e.key==="Enter"){e.preventDefault(),i!==s&&n.deleteText(c.storyId,i,s),n.insertParagraphBreak(c.storyId,i),G({...c,anchor:i+1,focus:i+1});return}if(e.key.length>0&&!e.metaKey&&!e.ctrlKey&&!e.altKey&&Array.from(e.key).length===1){e.preventDefault(),i!==s&&n.deleteText(c.storyId,i,s),n.insertText(c.storyId,i,e.key,D);let a=i+e.key.length;G({...c,anchor:a,focus:a});}}catch(a){w(a);}},O=e=>{Ze(i=>({...i,...e}));let n=g.current;if(!(!n||!c||c.anchor===c.focus))try{n.formatText(c.storyId,Math.min(c.anchor,c.focus),Math.max(c.anchor,c.focus),e),L(void 0,!0);}catch(i){w(i);}},lt=()=>{let e=g.current,n=F.current;if(!(!e||!n))try{let i=n.slideIndex+1,s=n.snapshot.slides[n.slideIndex]?.layoutPartPath??void 0;e.insertSlide(i,s),C(null),T(null),k(null),P.current=null,v.current=null,L(i,!0,!0);}catch(i){w(i);}},st=()=>{let e=g.current,n=F.current;if(!(!e||!n||n.snapshot.slides.length<=1))try{e.deleteSlide(n.snapshot.slides[n.slideIndex].id),C(null),T(null),k(null),P.current=null,v.current=null,L(Math.max(0,n.slideIndex-1),!0,!0);}catch(i){w(i);}},dt=()=>{let e=g.current,n=F.current;if(!e||!n)return;let i=n.snapshot.slides[n.slideIndex];if(i)try{let s=e.addTextBox(i.id,{name:l("objects.defaultTextBoxName"),rect:{x:Math.round(n.snapshot.widthEmu*.15),y:Math.round(n.snapshot.heightEmu*.25),width:Math.round(n.snapshot.widthEmu*.7),height:Math.round(n.snapshot.heightEmu*.3)},text:"",style:D}),a=L(void 0,!0);T(null),k(null),P.current=null,v.current=null;let p=a?.snapshot.slides[a.slideIndex]?.shapes.find(H=>H.id===s.shapeId),b=p?.textStories[0];b&&(C({shapeId:p.id,storyId:b.id,anchor:0,focus:0}),V.current?.focus());}catch(s){w(s);}},Re=e=>{let n=g.current;if(n)try{e==="undo"?n.undo():n.redo(),C(null),T(null),k(null),P.current=null,v.current=null,L(void 0,!0);}catch(i){w(i);}},j=y?.snapshot.slides.length??0,te=y?.slideIndex??0,Fe=le&&le.shapeId===J?.shapeId?le.delta:null,ut=K?pe(K):false;return jsxs("div",{className:h,style:x.root,children:[jsx("div",{style:x.toolbarShell,children:jsxs("div",{style:x.toolbar,role:"group","aria-label":l("toolbar.label"),children:[jsxs("div",{style:x.toolbarGroup,role:"group","aria-label":l("toolbar.historyLabel"),children:[jsx("button",{type:"button",style:_(g.current?.canUndo()??false),disabled:!g.current?.canUndo(),"aria-label":l("toolbar.undo"),title:l("toolbar.undo"),onClick:()=>Re("undo"),children:jsx($,{name:"undo"})}),jsx("button",{type:"button",style:_(g.current?.canRedo()??false),disabled:!g.current?.canRedo(),"aria-label":l("toolbar.redo"),title:l("toolbar.redo"),onClick:()=>Re("redo"),children:jsx($,{name:"redo"})})]}),jsx("span",{style:x.divider,"aria-hidden":"true"}),jsxs("div",{style:x.toolbarGroup,role:"group","aria-label":l("toolbar.textFormattingLabel"),children:[jsx("button",{type:"button","aria-pressed":D.bold,"aria-label":l("toolbar.bold"),title:l("toolbar.bold"),style:Je(D.bold),onClick:()=>O({bold:!D.bold}),children:"B"}),jsx("button",{type:"button","aria-pressed":D.italic,"aria-label":l("toolbar.italic"),title:l("toolbar.italic"),style:{...Je(D.italic),fontStyle:"italic"},onClick:()=>O({italic:!D.italic}),children:"I"}),jsx("input",{"aria-label":l("toolbar.fontSize"),title:l("toolbar.fontSize"),type:"number",min:6,max:400,value:D.fontSizePt,style:x.numberInput,onChange:e=>{let n=Number(e.target.value);Number.isFinite(n)&&O({fontSizePt:n});}}),jsx("input",{"aria-label":l("toolbar.textColor"),title:l("toolbar.textColor"),type:"color",value:D.color,style:x.colorInput,onChange:e=>O({color:e.target.value})})]}),jsx("span",{style:x.divider,"aria-hidden":"true"}),jsxs("div",{style:x.toolbarGroup,role:"group","aria-label":l("toolbar.slidesLabel"),children:[jsx("button",{type:"button",style:_(true),"aria-label":l("toolbar.addSlide"),title:l("toolbar.addSlide"),onClick:lt,children:jsx($,{name:"addSlide"})}),jsx("button",{type:"button",style:_(j>1),disabled:j<=1,"aria-label":l("toolbar.deleteSlide"),title:l("toolbar.deleteSlide"),onClick:st,children:jsx($,{name:"deleteSlide"})})]}),jsx("span",{style:x.divider,"aria-hidden":"true"}),jsx("div",{style:x.toolbarGroup,role:"group","aria-label":l("toolbar.objectsLabel"),children:jsx("button",{type:"button",style:_(j>0),disabled:j===0,"aria-label":l("toolbar.addTextBox"),title:l("toolbar.addTextBox"),onClick:dt,children:jsx($,{name:"textBox"})})})]})}),jsxs("div",{style:x.workspace,children:[jsx("aside",{style:x.slideStrip,"aria-label":l("slides.panelLabel"),children:y?.snapshot.slides.map((e,n)=>jsxs("button",{type:"button","aria-current":n===te?"page":void 0,style:Lt(n===te),onClick:()=>tt(n),children:[jsx("span",{style:x.slideNumber,children:n+1}),jsx("span",{style:x.slidePreview,children:y.thumbnails.get(e.id)?jsx(yt,{frame:y.thumbnails.get(e.id),resolveImage:i=>Ve(i,g,ie,I)}):jsx("span",{style:x.slideTitle,children:Et(e.shapes)||l("slides.fallbackTitle",{number:n+1})})})]},e.id))}),jsxs("div",{ref:V,style:x.stage,tabIndex:0,role:"application","aria-label":l("editor.appLabel"),onKeyDown:at,children:[jsx("div",{ref:Pe,style:x.canvasHost,children:y?.frame?jsxs("div",{style:{...x.canvasFrame,width:y.frame.width*B,height:y.frame.height*B},children:[jsx("canvas",{ref:ve,style:{...x.canvas,cursor:c?"text":ut?"move":"default"},onPointerDown:nt,onPointerMove:ot,onPointerUp:it,onPointerCancel:Le,onLostPointerCapture:Le,onDoubleClick:rt,"aria-label":K?l("slides.canvasLabelWithSelection",{current:te+1,total:j,name:K.name}):l("slides.canvasLabel",{current:te+1,total:j})}),jsx("canvas",{ref:we,style:x.canvasOverlay,"aria-hidden":"true"}),Z?jsx("span",{style:{...x.shapeSelection,left:(Z.x+(Fe?.x??0))*B,top:(Z.y+(Fe?.y??0))*B,width:Math.max(1,Z.width*B),height:Math.max(1,Z.height*B)},"aria-hidden":"true"}):null]}):jsx("div",{style:x.empty,children:l(et?"editor.opening":t?"editor.noSlides":"editor.openPrompt")})}),Me?jsx("div",{style:x.error,children:Me}):null]})]})]})}function yt({frame:t,resolveImage:r}){let o=useRef(null);return useEffect(()=>{let d=o.current;if(!d)return;let h=d.getContext("2d");if(!h)return;let f=128/t.width,S=window.devicePixelRatio||1;sizeCanvasForSlide(d,t,S,f),paintSlide(h,t,S,f,{resolveImage:r}).catch(()=>{});},[t,r]),jsx("canvas",{ref:o,style:x.thumbnailCanvas,"aria-hidden":"true"})}function gt(t,r){return r===0?0:Math.max(0,Math.min(r-1,t))}function St(t){let r=useRef(t);return It(r.current,t)||(r.current=t),r.current}function It(t,r){return t===r?true:t.length!==r.length?false:t.every((o,d)=>Pt(o,r[d]))}function Pt(t,r){return t.family===r.family&&(t.bold??false)===(r.bold??false)&&(t.italic??false)===(r.italic??false)&&vt(t.bytes,r.bytes)}function vt(t,r){return t===r?true:t.byteLength!==r.byteLength?false:t.every((o,d)=>o===r[d])}function ye(t){return t.paragraphs.map(r=>r.runs.map(o=>o.text).join("")).join(`
2
+ `)}function wt(t,r){let o=ye(t).slice(0,r),d=Array.from(o),h=d[d.length-1];return h?r-h.length:r}function Ct(t,r){let o=Array.from(ye(t).slice(r))[0];return o?r+o.length:r}async function Mt(t){return typeof FontFace>"u"||typeof document>"u"?[]:await Promise.all(t.map(async o=>{let d=o.bytes.slice().buffer,f=await new FontFace(o.family,d,{style:o.italic?"italic":"normal",weight:o.bold?"700":"400"}).load();return document.fonts.add(f),f}))}function _e(t){if(!(typeof document>"u"))for(let r of t)document.fonts.delete(r);}function Ve(t,r,o,d){let h=o.current.get(t);if(h)return h;let f=Tt(r.current?.mediaBytes(t),d);return o.current.set(t,f),f}async function Tt(t,r){if(!t)return null;let o=new Blob([t.slice()]);if(typeof createImageBitmap=="function")return createImageBitmap(o);let d=URL.createObjectURL(o);try{return await new Promise((h,f)=>{let S=new Image;S.onload=()=>h(S),S.onerror=()=>f(new Error(r)),S.src=d;})}finally{URL.revokeObjectURL(d);}}function kt(t,r,o,d,h){if(!o)return;let f=r.primitives.find(l=>l.kind==="textBox"&&l.storyId===o.storyId&&l.shapeId===o.shapeId);if(!f)return;let S=Math.min(o.anchor,o.focus),z=Math.max(o.anchor,o.focus);if(t.save(),t.setTransform(d*h,0,0,d*h,0,0),S!==z){t.fillStyle="rgba(59, 130, 246, 0.26)";for(let l of f.lines){let I=Math.max(S,l.start),m=Math.min(z,l.end);if(I>=m)continue;let A=me(l,I),U=me(l,m);t.fillRect(Math.min(A,U),l.y,Math.max(1,Math.abs(U-A)),l.height);}}else {let l=f.lines.find(I=>S>=I.start&&S<=I.end)??f.lines[f.lines.length-1];if(l){let I=me(l,S);t.fillStyle="#1d4ed8",t.fillRect(I,l.y,1.5,l.height);}}t.restore();}function me(t,r){let o=t.caretStops[0];return o?t.caretStops.reduce((d,h)=>Math.abs(h.position-r)<Math.abs(d.position-r)?h:d,o).x:t.x}function Et(t){for(let r of t)for(let o of r.textStories){let d=ye(o).trim();if(d)return d.slice(0,40)}return ""}var x={root:{display:"flex",flexDirection:"column",width:"100%",height:"100%",minHeight:480,overflow:"hidden",color:"#172033",background:"#f3f5f8",fontFamily:"ui-sans-serif, system-ui, sans-serif"},toolbarShell:{flex:"0 0 auto",padding:"4px 0 5px",background:"#ffffff",borderBottom:"1px solid #e2e8f0"},toolbar:{display:"flex",alignItems:"center",gap:0,minHeight:36,margin:"0 8px",padding:"4px 8px",background:"#f1f5f9",borderRadius:999,boxSizing:"border-box",overflowX:"auto",overflowY:"hidden"},toolbarGroup:{display:"inline-flex",alignItems:"center",gap:1,flex:"0 0 auto"},divider:{width:1,height:24,margin:"0 6px",background:"#e2e8f0",flex:"0 0 auto"},numberInput:{appearance:"textfield",width:40,height:28,marginLeft:2,boxSizing:"border-box",border:"1px solid #e2e8f0",borderRadius:6,padding:"0 4px",background:"#f8fafc",color:"#0f172a",font:"12px ui-sans-serif, system-ui, sans-serif",textAlign:"center",outlineColor:"#2563eb"},colorInput:{width:28,height:28,marginLeft:2,padding:2,border:"1px solid #e2e8f0",borderRadius:6,background:"#f8fafc",cursor:"pointer",outlineColor:"#2563eb"},workspace:{display:"flex",flex:1,minHeight:0},slideStrip:{width:184,padding:"14px 10px",overflowY:"auto",background:"#eef1f5",borderRight:"1px solid #d8dee9",boxSizing:"border-box"},slideNumber:{width:18,flex:"0 0 auto",paddingTop:3,fontSize:11,color:"#647087",textAlign:"right"},slidePreview:{display:"flex",alignItems:"center",justifyContent:"center",width:132,aspectRatio:"16 / 9",padding:8,overflow:"hidden",background:"#ffffff",boxSizing:"border-box",boxShadow:"0 1px 4px rgba(20, 31, 50, 0.16)"},slideTitle:{fontSize:9,lineHeight:1.25,color:"#39445a",textAlign:"center"},thumbnailCanvas:{display:"block",maxWidth:"100%",height:"auto"},stage:{position:"relative",flex:1,minWidth:0,outline:"none",overflow:"hidden"},canvasHost:{display:"flex",alignItems:"center",justifyContent:"center",width:"100%",height:"100%",overflow:"auto"},canvasFrame:{position:"relative",flex:"0 0 auto"},canvas:{display:"block",flex:"0 0 auto",background:"#fff",boxShadow:"0 8px 32px rgba(27, 39, 61, 0.2)",touchAction:"none"},canvasOverlay:{position:"absolute",inset:0,display:"block",pointerEvents:"none"},shapeSelection:{position:"absolute",border:"2px solid #2563eb",boxSizing:"border-box",boxShadow:"0 0 0 1px rgba(255, 255, 255, 0.9)",pointerEvents:"none"},empty:{margin:"auto",color:"#6b7587",fontSize:14},error:{position:"absolute",left:16,right:16,bottom:14,padding:"9px 12px",color:"#8b1e2d",background:"#fff0f2",border:"1px solid #efb8c0",borderRadius:6,fontSize:12}};function _(t){return {appearance:"none",display:"inline-flex",alignItems:"center",justifyContent:"center",width:28,height:28,padding:0,border:0,borderRadius:6,color:t?"#64748b":"#94a3b8",background:"transparent",cursor:t?"pointer":"default",opacity:t?1:.32}}function Je(t){return {..._(true),fontWeight:700,color:t?"#ffffff":"#64748b",background:t?"#0f172a":"transparent"}}function Lt(t){return {display:"flex",alignItems:"flex-start",gap:7,width:"100%",marginBottom:12,padding:4,border:t?"2px solid #325ee6":"2px solid transparent",borderRadius:5,background:"transparent",cursor:"pointer"}}export{ne as LocaleProvider,bt as PptxEditor,re as useTranslation};
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@betteroffice/pptx-react",
3
+ "version": "0.0.1",
4
+ "description": "PowerPoint-faithful React PPTX editor and viewer — full OOXML fidelity, real-time human + AI agent collaboration.",
5
+ "keywords": [
6
+ "pptx",
7
+ "powerpoint",
8
+ "presentation",
9
+ "slides",
10
+ "ooxml",
11
+ "editor",
12
+ "viewer",
13
+ "react",
14
+ "collaborative",
15
+ "real-time",
16
+ "ai-agent"
17
+ ],
18
+ "type": "module",
19
+ "sideEffects": false,
20
+ "main": "./dist/index.js",
21
+ "module": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "scripts": {
33
+ "build": "tsup",
34
+ "typecheck": "tsc --noEmit -p ."
35
+ },
36
+ "dependencies": {
37
+ "@betteroffice/pptx": "0.0.1",
38
+ "@betteroffice/pptx-i18n": "0.0.1"
39
+ },
40
+ "peerDependencies": {
41
+ "react": "^18.0.0 || ^19.0.0",
42
+ "react-dom": "^18.0.0 || ^19.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "@types/react": "^19.2.17",
46
+ "@types/react-dom": "^19.2.3",
47
+ "react": "^19.2.7",
48
+ "react-dom": "^19.2.7",
49
+ "tsup": "^8.5.1",
50
+ "typescript": "^6.0.3"
51
+ },
52
+ "license": "Apache-2.0",
53
+ "homepage": "https://betteroffice.dev/",
54
+ "repository": {
55
+ "type": "git",
56
+ "url": "https://github.com/openooxml/betteroffice.git",
57
+ "directory": "packages/pptx-react"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public"
61
+ }
62
+ }