refactor: 横屏提示与缩放改为组件标签(OrientationGuard)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
zhongyidianandClaude Opus 4.8 committed 2026-08-18 17:24:43 +08:00
1 parent a98b1d383e
commit ce9567d1da
12 files changed
+50 -70

No files matched your search

+6 -12
View File
@@ -1,17 +1,13 @@
import { useEffect } from "react"; import { useEffect } from "react";
import { Outlet } from "react-router-dom"; import { Outlet } from "react-router-dom";
import { appConfig } from "@/app.config";
import { ToastProvider, ToastViewport } from "@/components/ui/toast"; import { ToastProvider, ToastViewport } from "@/components/ui/toast";
import { CourseCanvas, useCourseCanvas } from "@/course-runtime/canvas"; import { CourseCanvas, useCourseCanvas } from "@/course-runtime/canvas";
import { OrientationGuard } from "@/course-runtime/orientation";
import { Navigation } from "./components/business"; import { Navigation } from "./components/business";
import { LandscapePrompt } from "./components/common";
import { registerAppPortalRoot } from "./components/common/canvasPortal"; import { registerAppPortalRoot } from "./components/common/canvasPortal";
import { useUserStore } from "./stores"; import { useUserStore } from "./stores";
import { import { isVideoAjaxAvailable } from "./utils";
DESIGN_HEIGHT,
DESIGN_WIDTH,
isVideoAjaxAvailable,
useOrientation,
} from "./utils";
function CourseShell() { function CourseShell() {
const { portalContainer } = useCourseCanvas(); const { portalContainer } = useCourseCanvas();
@@ -30,8 +26,6 @@ function CourseShell() {
} }
function App() { function App() {
const isPortrait = useOrientation();
useEffect(() => { useEffect(() => {
const params = new URLSearchParams(window.location.search); const params = new URLSearchParams(window.location.search);
const courseId = params.get("courseId") ?? params.get("courseid"); const courseId = params.get("courseId") ?? params.get("courseid");
@@ -69,10 +63,10 @@ function App() {
return ( return (
<> <>
{isPortrait && <LandscapePrompt />} <OrientationGuard />
<CourseCanvas <CourseCanvas
width={DESIGN_WIDTH} width={appConfig.design.width}
height={DESIGN_HEIGHT} height={appConfig.design.height}
className="bg-gray-900" className="bg-gray-900"
canvasClassName="flex flex-col overflow-hidden bg-white" canvasClassName="flex flex-col overflow-hidden bg-white"
> >
@@ -1 +0,0 @@
export { LandscapePrompt } from "./LandscapePrompt";
-1
View File
@@ -31,5 +31,4 @@ export type {
SpotlightController, SpotlightController,
} from "./GuideSpotlight"; } from "./GuideSpotlight";
export { defineSpotlight, GuideSpotlight } from "./GuideSpotlight"; export { defineSpotlight, GuideSpotlight } from "./GuideSpotlight";
export { LandscapePrompt } from "./LandscapePrompt";
export { RouteError, RouteFallback } from "./RouteFallback"; export { RouteError, RouteFallback } from "./RouteFallback";
+17 -5
View File
@@ -6,17 +6,25 @@ interface CanvasScaleOptions {
maxScale: number; maxScale: number;
} }
function getViewportSize() {
if (typeof window === "undefined") {
return null;
}
return {
width: window.visualViewport?.width ?? window.innerWidth,
height: window.visualViewport?.height ?? window.innerHeight,
};
}
function calculateScale({ function calculateScale({
width, width,
height, height,
maxScale, maxScale,
}: CanvasScaleOptions): number { }: CanvasScaleOptions): number {
if (typeof window === "undefined") return maxScale; if (typeof window === "undefined") return maxScale;
return Math.min( const viewport = getViewportSize();
window.innerWidth / width, if (!viewport) return maxScale;
window.innerHeight / height, return Math.min(viewport.width / width, viewport.height / height, maxScale);
maxScale,
);
} }
export function useCanvasScale({ export function useCanvasScale({
@@ -33,11 +41,15 @@ export function useCanvasScale({
setScale(calculateScale({ width, height, maxScale })); setScale(calculateScale({ width, height, maxScale }));
updateScale(); updateScale();
window.addEventListener("resize", updateScale); window.addEventListener("resize", updateScale);
window.addEventListener("orientationchange", updateScale);
window.visualViewport?.addEventListener("resize", updateScale); window.visualViewport?.addEventListener("resize", updateScale);
window.visualViewport?.addEventListener("scroll", updateScale);
return () => { return () => {
window.removeEventListener("resize", updateScale); window.removeEventListener("resize", updateScale);
window.removeEventListener("orientationchange", updateScale);
window.visualViewport?.removeEventListener("resize", updateScale); window.visualViewport?.removeEventListener("resize", updateScale);
window.visualViewport?.removeEventListener("scroll", updateScale);
}; };
}, [height, maxScale, width]); }, [height, maxScale, width]);
@@ -1,4 +1,7 @@
export function LandscapePrompt() { import type { OrientationGuardProps } from "./OrientationGuard.types";
import { useOrientation } from "./useOrientation";
function DefaultLandscapePrompt() {
return ( return (
<div className="fixed inset-0 z-2000 flex items-center justify-center bg-gray-950/80 px-8 text-white"> <div className="fixed inset-0 z-2000 flex items-center justify-center bg-gray-950/80 px-8 text-white">
<div className="text-center"> <div className="text-center">
@@ -17,9 +20,18 @@ export function LandscapePrompt() {
</div> </div>
<h1 className="text-2xl font-semibold">请旋转屏幕</h1> <h1 className="text-2xl font-semibold">请旋转屏幕</h1>
<p className="mt-3 text-base text-gray-400"> <p className="mt-3 text-base text-gray-400">
为获得最佳体验,请横屏浏览 为获得最佳体验,请横屏浏览
</p> </p>
</div> </div>
</div> </div>
); );
} }
export function OrientationGuard({ renderPrompt }: OrientationGuardProps) {
const isPortrait = useOrientation();
if (!isPortrait) return null;
if (renderPrompt) return <>{renderPrompt()}</>;
return <DefaultLandscapePrompt />;
}
@@ -0,0 +1,6 @@
import type { ReactNode } from "react";
export interface OrientationGuardProps {
/** 竖屏时渲染的自定义提示;不传则用内置的横屏提示 */
renderPrompt?: () => ReactNode;
}
+3
View File
@@ -0,0 +1,3 @@
export { OrientationGuard } from "./OrientationGuard";
export type { OrientationGuardProps } from "./OrientationGuard.types";
export { useOrientation } from "./useOrientation";
File renamed without changes.
+2 -2
View File
@@ -6,8 +6,8 @@ import {
Droppable, Droppable,
} from "@/components/course-interactions/drag-drop"; } from "@/components/course-interactions/drag-drop";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useCourseCanvas } from "@/course-runtime/canvas";
import { cn } from "@/lib/cn"; import { cn } from "@/lib/cn";
import { useScale } from "@/utils";
import type { import type {
DragDemoItem, DragDemoItem,
DragDemoZone, DragDemoZone,
@@ -153,7 +153,7 @@ const renderPlacedItem = (item: DragDemoItem) => (
); );
export function DragDropDemo() { export function DragDropDemo() {
const scale = useScale(); const { scale } = useCourseCanvas();
const [dropResult, setDropResult] = useState<DragDropDemoResult>(null); const [dropResult, setDropResult] = useState<DragDropDemoResult>(null);
const [placedItemIds, setPlacedItemIds] = useState<Record<string, string[]>>( const [placedItemIds, setPlacedItemIds] = useState<Record<string, string[]>>(
{}, {},
+2 -2
View File
@@ -5,8 +5,8 @@ import { Button } from "@/components/ui/button";
import { Overlay, type OverlayProps } from "@/components/ui/overlay"; import { Overlay, type OverlayProps } from "@/components/ui/overlay";
import { Portal } from "@/components/ui/portal"; import { Portal } from "@/components/ui/portal";
import { useToast } from "@/components/ui/toast"; import { useToast } from "@/components/ui/toast";
import { useCourseCanvas } from "@/course-runtime/canvas";
import { useSpotlightStore } from "@/stores"; import { useSpotlightStore } from "@/stores";
import { useScale } from "@/utils";
import { DragDropDemo } from "./DragDropDemo"; import { DragDropDemo } from "./DragDropDemo";
type OverlayDemoKey = type OverlayDemoKey =
@@ -113,7 +113,7 @@ export default function Components() {
? customRootRef ? customRootRef
: getAppPortalRoot; : getAppPortalRoot;
const toast = useToast(); const toast = useToast();
const scale = useScale(); const { scale } = useCourseCanvas();
const startGuideSpotlight = () => { const startGuideSpotlight = () => {
useSpotlightStore.getState().startSpotlight({ useSpotlightStore.getState().startSpotlight({
-2
View File
@@ -1,6 +1,4 @@
export * from "./assets"; export * from "./assets";
export * from "./lazyRoute"; export * from "./lazyRoute";
export * from "./tailwind"; export * from "./tailwind";
export * from "./useOrientation";
export * from "./useScale";
export * from "./yiban"; export * from "./yiban";
-43
View File
@@ -1,43 +0,0 @@
import { useCallback, useEffect, useState } from "react";
import { appConfig } from "@/app.config";
export const DESIGN_WIDTH = appConfig.design.width;
export const DESIGN_HEIGHT = appConfig.design.height;
function getViewportSize() {
if (typeof window === "undefined") {
return { width: DESIGN_WIDTH, height: DESIGN_HEIGHT };
}
return {
width: window.visualViewport?.width ?? window.innerWidth,
height: window.visualViewport?.height ?? window.innerHeight,
};
}
export function useScale() {
const calculateScale = useCallback(() => {
const viewport = getViewportSize();
return Math.min(
viewport.width / DESIGN_WIDTH,
viewport.height / DESIGN_HEIGHT,
appConfig.scale.max,
);
}, []);
const [scale, setScale] = useState(calculateScale);
useEffect(() => {
const updateScale = () => setScale(calculateScale());
window.addEventListener("resize", updateScale);
window.addEventListener("orientationchange", updateScale);
window.visualViewport?.addEventListener("resize", updateScale);
window.visualViewport?.addEventListener("scroll", updateScale);
return () => {
window.removeEventListener("resize", updateScale);
window.removeEventListener("orientationchange", updateScale);
window.visualViewport?.removeEventListener("resize", updateScale);
window.visualViewport?.removeEventListener("scroll", updateScale);
};
}, [calculateScale]);
return scale;
}