@jswork/ppt-slidebridge
v1.0.5
Published
Typed postMessage SDK for remote-controlling embedded PPT/slide iframes across origins.
Readme
ppt-slidebridge
Typed postMessage SDK for remote-controlling embedded PPT/slide iframes across origins.
installation
yarn add @jswork/ppt-slidebridgeusage
Host side (your app that embeds the player)
import { HostBridge } from "@jswork/ppt-slidebridge";
const host = new HostBridge(iframeEl, { targetOrigin: "https://player.example.com" });
// resolved when the player announces itself (auto-retried until handshake)
const { total } = await host.whenReady();
host.next();
host.prev();
host.goto(3);
host.onPageChange((index, total) => console.log(`slide ${index + 1}/${total}`));
const { index } = await host.getPage();Player side (inside the embedded PPT iframe)
import { PlayerBridge } from "@jswork/ppt-slidebridge";
const player = new PlayerBridge({
goto: (index) => showSlide(index),
next: () => showSlide(index + 1),
prev: () => showSlide(index - 1),
getPage: () => ({ index }),
});
player.emitReady(totalSlides);
player.emitPageChange(index, totalSlides);React hooks
import { useRef } from "react";
import { useHostBridge } from "@jswork/ppt-slidebridge";
function Demo() {
const iframeRef = useRef<HTMLIFrameElement>(null);
const { host, ready, index, total, canPrev, canNext } = useHostBridge(iframeRef, { targetOrigin: "*" });
return (
<>
<button disabled={!ready || !canPrev} onClick={() => host?.prev()}>Prev</button>
<button disabled={!ready || !canNext} onClick={() => host?.next()}>Next</button>
<iframe ref={iframeRef} src="/player.html" />
</>
);
}Also available: usePlayerBridge(commands) and usePageChange(host).
useHostBridgeexposes the same navigation surface asuseViewerControls:go/goto/next/prev/first/lastpluscanPrev/canNext. Slide index is 0-based (0 <= index < total); display{index + 1}/{total}in UI.go(direction)takes only1(next) or-1(prev); multi-page jumps usegoto(can* are false until ready).
useViewerControls
Headless pagination controls shared by viewer UIs (0-based page index):
const ctrl = useViewerControls({
total: pages.length,
keyboard: true, // optional: ArrowLeft/ArrowRight
player, // optional: PlayerBridge from usePlayerBridge; next/prev/goto/first/last are forwarded
});
// ctrl: { index, total, canPrev, canNext, go, goto, first, last }
// render `${ctrl.index}/${ctrl.total}` directly, no +1 neededControlled mode: pass index + onChange and keep state in your component. total === 0 disables all actions.
Vue 3 composables
<script setup>
import { ref } from "vue";
import { useHostBridgeVue } from "@jswork/ppt-slidebridge";
const iframeRef = ref(null);
const { host, ready, index, total, canPrev, canNext } = useHostBridgeVue(iframeRef, { targetOrigin: "*" });
</script>
<template>
<button :disabled="!ready || !canPrev" @click="host?.prev()">Prev</button>
<button :disabled="!ready || !canNext" @click="host?.next()">Next</button>
<iframe ref="iframeRef" src="/player.html" />
</template>Also available: usePlayerBridgeVue(commands).
useHostBridgeVueexposes the same navigation surface asuseViewerControls:go/goto/next/prev/first/lastpluscanPrev/canNext. Slide index is 0-based (0 <= index < total); display{index + 1}/{total}in UI.go(direction)takes only1(next) or-1(prev); multi-page jumps usegoto(can* are false until ready).
Runnable demos (host + player) live in the monorepo: apps/example-react and apps/example-vue — run pnpm dev inside either.
license
Code released under the MIT license.
