@ericchen1990/pano-view 3.1.2 → 3.1.3

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.mjs CHANGED
@@ -2311,6 +2311,9 @@ function isMediaActivationEvent(event) {
2311
2311
  return false;
2312
2312
  }
2313
2313
  }
2314
+ function isAutoplayPolicyError(error) {
2315
+ return typeof error === "object" && error !== null && "name" in error && error.name === "NotAllowedError";
2316
+ }
2314
2317
 
2315
2318
  // src/audio/howl.ts
2316
2319
  import_howler.Howler.autoUnlock = true;
@@ -3856,7 +3859,7 @@ function hasPanoMediaActivationIntent(host) {
3856
3859
  return host.intents.size > 0;
3857
3860
  }
3858
3861
  function needsMediaGesture() {
3859
- if (typeof navigator !== "undefined" && navigator.userActivation?.hasBeenActive) {
3862
+ if (typeof navigator !== "undefined" && navigator.userActivation?.isActive) {
3860
3863
  return false;
3861
3864
  }
3862
3865
  if (import_howler2.Howler.ctx?.state === "running") {
@@ -7189,10 +7192,6 @@ var PanoViewer = forwardRef2(
7189
7192
  if (!shouldActivateMedia) {
7190
7193
  return;
7191
7194
  }
7192
- if (!needsMediaGesture()) {
7193
- void activateMedia();
7194
- return;
7195
- }
7196
7195
  let cancelled = false;
7197
7196
  let activating = false;
7198
7197
  const removeListeners = () => {
@@ -7200,32 +7199,69 @@ var PanoViewer = forwardRef2(
7200
7199
  window.removeEventListener("pointerup", handleGesture, true);
7201
7200
  window.removeEventListener("keydown", handleGesture, true);
7202
7201
  };
7203
- const handleGesture = (event) => {
7204
- if (!isMediaActivationEvent(event) || activating || cancelled) {
7202
+ const completeIfUnlocked = (requestedVideo, videoOk) => {
7203
+ if (cancelled || !isPanoAudioUnlocked()) {
7204
+ return;
7205
+ }
7206
+ if (requestedVideo && videoHost.controller && !videoOk) {
7207
+ return;
7208
+ }
7209
+ removeListeners();
7210
+ setMediaActivated(true);
7211
+ };
7212
+ const tryActivate = () => {
7213
+ if (activating || cancelled) {
7205
7214
  return;
7206
7215
  }
7207
7216
  activating = true;
7217
+ let videoPlay;
7218
+ const controls2 = {
7219
+ resumeAudio: mediaActivationControls.resumeAudio,
7220
+ playVideo: (options) => {
7221
+ const result = mediaActivationControls.playVideo(options);
7222
+ videoPlay = result;
7223
+ return result;
7224
+ },
7225
+ playBackgroundAudio: mediaActivationControls.playBackgroundAudio
7226
+ };
7208
7227
  try {
7209
- const resume = runMediaActivation();
7210
- void resume.then(() => {
7211
- if (cancelled || !isPanoAudioUnlocked()) {
7212
- return;
7228
+ const resume = controls2.resumeAudio();
7229
+ mediaActivationOptions?.onActivate?.(controls2);
7230
+ const requestedVideo = videoPlay !== void 0;
7231
+ void Promise.all([resume, videoPlay ?? Promise.resolve(true)]).then(
7232
+ ([, videoOk]) => {
7233
+ completeIfUnlocked(requestedVideo, videoOk);
7234
+ },
7235
+ () => {
7236
+ completeIfUnlocked(requestedVideo, false);
7213
7237
  }
7214
- removeListeners();
7215
- setMediaActivated(true);
7216
- });
7238
+ );
7217
7239
  } finally {
7218
7240
  activating = false;
7219
7241
  }
7220
7242
  };
7243
+ const handleGesture = (event) => {
7244
+ if (!isMediaActivationEvent(event)) {
7245
+ return;
7246
+ }
7247
+ tryActivate();
7248
+ };
7221
7249
  window.addEventListener("pointerdown", handleGesture, { capture: true });
7222
7250
  window.addEventListener("pointerup", handleGesture, { capture: true });
7223
7251
  window.addEventListener("keydown", handleGesture, { capture: true });
7252
+ if (!needsMediaGesture()) {
7253
+ tryActivate();
7254
+ }
7224
7255
  return () => {
7225
7256
  cancelled = true;
7226
7257
  removeListeners();
7227
7258
  };
7228
- }, [activateMedia, runMediaActivation, shouldActivateMedia]);
7259
+ }, [
7260
+ mediaActivationControls,
7261
+ mediaActivationOptions,
7262
+ shouldActivateMedia,
7263
+ videoHost
7264
+ ]);
7229
7265
  const fallbackViewRef = useRef13(DEFAULT_VIEW);
7230
7266
  const normalizedMinFov = Math.max(1, Math.min(minFov, maxFov - 1));
7231
7267
  const normalizedMaxFov = Math.min(
@@ -10574,8 +10610,10 @@ function PanoVideo({
10574
10610
  try {
10575
10611
  await video.play();
10576
10612
  } catch (error) {
10577
- blockedRef.current = true;
10578
- onPlaybackStateChangeRef.current?.("blocked");
10613
+ if (isAutoplayPolicyError(error)) {
10614
+ blockedRef.current = true;
10615
+ onPlaybackStateChangeRef.current?.("blocked");
10616
+ }
10579
10617
  onErrorRef.current?.({ source: "video", error });
10580
10618
  emitSnapshot();
10581
10619
  }