Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/powerpoint-addin/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState, useRef } from "react";
import { useMutation, useQuery } from "convex/react";
import type { Id } from "../../../convex/_generated/dataModel";
import { useAddinStore } from "./store/addinStore";
import {
initOfficeBridge,
Expand Down Expand Up @@ -73,7 +74,7 @@ export function App({ convexReady }: AppProps) {
try {
await setCurrentSlide({
token: store.presenterToken,
sessionId: store.sessionId,
sessionId: store.sessionId as Id<"presentationSessions">,
slideNumber,
totalSlides,
presentationTitle: title,
Expand Down
64 changes: 52 additions & 12 deletions apps/powerpoint-addin/src/lib/officeBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ let _lastReportedSlide: number | null = null;

/** Debounce timer for DocumentSelectionChanged */
let _debounceTimer: ReturnType<typeof setTimeout> | null = null;
let _pollTimer: ReturnType<typeof setInterval> | null = null;
const DEBOUNCE_MS = 250;
const POLL_MS = 1000;

/**
* Returns true if Office.js is available in this environment.
Expand Down Expand Up @@ -70,6 +72,8 @@ export function initOfficeBridge(callbacks: OfficeBridgeCallbacks): Promise<Sync
}

isOfficeInitialized = true;
void syncCurrentSlide();
startPolling();

try {
Office.context.document.addHandlerAsync(
Expand All @@ -78,14 +82,25 @@ export function initOfficeBridge(callbacks: OfficeBridgeCallbacks): Promise<Sync
(result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
callbacks.onError("Automatischer Sync nicht verfügbar. Manueller Modus aktiv.");
callbacks.onModeChange("hybrid");
resolve("hybrid");
} else {
callbacks.onModeChange("auto");
try {
Office.context.document.addHandlerAsync(
Office.EventType.ActiveViewChanged,
handleActiveViewChanged
);
} catch {
// Ignore optional view change registration failures.
}
resolve("auto");
}
}
);
} catch {
callbacks.onError("Slide-Erkennung nicht verfügbar. Manueller Modus aktiv.");
callbacks.onModeChange("hybrid");
resolve("hybrid");
}
});
Expand All @@ -101,21 +116,38 @@ export function initOfficeBridge(callbacks: OfficeBridgeCallbacks): Promise<Sync
function handleSelectionChanged() {
if (_debounceTimer) clearTimeout(_debounceTimer);
_debounceTimer = setTimeout(() => {
getCurrentSlideInfo()
.then((info) => {
if (!info || !_callbacks) return;
// Only fire if slide actually changed
if (info.slideNumber !== _lastReportedSlide) {
_lastReportedSlide = info.slideNumber;
_callbacks.onSlideChange(info);
}
})
.catch((e) => {
_callbacks?.onError(`Folien-Update fehlgeschlagen: ${e}`);
});
void syncCurrentSlide();
}, DEBOUNCE_MS);
}

function handleActiveViewChanged() {
handleSelectionChanged();
}

async function syncCurrentSlide() {
try {
const info = await getCurrentSlideInfo();
if (!info || !_callbacks) return;

if (info.slideNumber !== _lastReportedSlide) {
_lastReportedSlide = info.slideNumber;
_callbacks.onSlideChange(info);
}
} catch (e) {
_callbacks?.onError(`Folien-Update fehlgeschlagen: ${e}`);
}
}

function startPolling() {
if (_pollTimer) {
clearInterval(_pollTimer);
}

_pollTimer = setInterval(() => {
void syncCurrentSlide();
}, POLL_MS);
}

/**
* Get current slide info from Office.js.
* Uses getSlideCountAsync() for total slides (public API, not private property).
Expand Down Expand Up @@ -181,12 +213,20 @@ export function destroyOfficeBridge() {
clearTimeout(_debounceTimer);
_debounceTimer = null;
}
if (_pollTimer) {
clearInterval(_pollTimer);
_pollTimer = null;
}
if (!isOfficeInitialized || !isOfficeAvailable()) return;
try {
Office.context.document.removeHandlerAsync(
Office.EventType.DocumentSelectionChanged,
{ handler: handleSelectionChanged }
);
Office.context.document.removeHandlerAsync(
Office.EventType.ActiveViewChanged,
{ handler: handleActiveViewChanged }
);
} catch {
// ignore
}
Expand Down
2 changes: 1 addition & 1 deletion apps/powerpoint-addin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"types": ["office-js"]
"types": ["office-js", "vite/client"]
},
"include": ["src"]
}
2 changes: 1 addition & 1 deletion apps/web/src/lib/powerpoint/officeBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function getCurrentSlideInfo(): Promise<OfficeSlideInfo | null> {
return;
}

const slideNumber = slides[0].index;
const slideNumber = slides[0].index + 1;

(Office.context.document as any).getSlideCountAsync(
(countResult: { status: string; value: number }) => {
Expand Down