Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/review/app/src/CodePeek.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export function CodePeekCard({
xstyle,
)}
role="status"
data-code-peek-key={key}
>
{outcome === "failed" ? "Code unavailable" : "Loading code…"}
</section>
Expand All @@ -215,6 +216,7 @@ export function CodePeekCard({
xstyle,
)}
data-code-rendering="inline-editor"
data-code-peek-key={key}
>
<DocumentCodeView
path={source.file}
Expand Down
20 changes: 15 additions & 5 deletions packages/review/app/src/animation/animation-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
anchorSelection,
animationCode,
animationCodeId,
selectionKey,
} from "@dev.fast/whiteboard-document";
import { STAGE_KIT_CSS, STAGE_THEME } from "@review/animation-stage-kit";
import * as stylex from "@stylexjs/stylex";
Expand Down Expand Up @@ -627,7 +628,7 @@ function StageBlock({

useEffect(() => onPhase(), [motion]);

const openTour = () => {
const openTour = (revealCode?: string) => {
if (!tour || !frameAnchor) return;

if (!tourOpen) {
Expand All @@ -639,7 +640,11 @@ function StageBlock({
setExpanded(false);
panelStore
.getState()
.openOverlayTour({ tourId: node.id, kind: "animation" }, frameAnchor);
.openOverlayTour(
{ tourId: node.id, kind: "animation" },
frameAnchor,
revealCode,
);
};

// A drawn element's code opens beside the board with the tour, at the
Expand All @@ -660,9 +665,12 @@ function StageBlock({
content: { kind: "source", source: anchor.peek },
});

if (tourOpen) return moveOverlayTour(tourStop.anchor.id, { reveal: true });
const code = selectionKey(anchor.peek);

if (tourOpen)
return moveOverlayTour(tourStop.anchor.id, { reveal: true, code });

if (expanded || diagramFullscreenOnClick) return openTour();
if (expanded || diagramFullscreenOnClick) return openTour(code);

openPeek({
kind: "peek",
Expand All @@ -671,6 +679,7 @@ function StageBlock({
tour: {
definition: tour,
revealRequest: (active?.tour?.revealRequest ?? 0) + 1,
revealCode: code,
},
});
};
Expand Down Expand Up @@ -1391,7 +1400,7 @@ function StageBlock({
<TourButton
buttonRef={openRef}
stops={tour.stops.length}
onClick={openTour}
onClick={() => openTour()}
/>
) : (
<Button
Expand Down Expand Up @@ -1566,6 +1575,7 @@ function StageBlock({
tour={tour}
activeAnchor={overlay.anchor}
revealRequest={overlay.revealRequest}
revealCode={overlay.revealCode}
onActiveAnchorChange={moveOverlayTour}
onClose={closeOverlayTour}
>
Expand Down
3 changes: 3 additions & 0 deletions packages/review/app/src/diagram-tour.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function DiagramTourOverlay({
tour,
activeAnchor,
revealRequest,
revealCode,
onActiveAnchorChange,
onClose,
children,
Expand All @@ -36,6 +37,7 @@ export function DiagramTourOverlay({
tour: GuidedTour;
activeAnchor: string;
revealRequest: number;
revealCode?: string;
onActiveAnchorChange: (anchor: string, options: { reveal: boolean }) => void;
onClose: () => void;
children: ReactNode;
Expand Down Expand Up @@ -111,6 +113,7 @@ export function DiagramTourOverlay({
tour={tour}
activeAnchor={activeAnchor}
revealRequest={revealRequest}
revealCode={revealCode}
onActiveAnchorChange={onActiveAnchorChange}
onClose={onClose}
docked
Expand Down
19 changes: 17 additions & 2 deletions packages/review/app/src/review-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ export function ReviewPanelHost() {
tour={activePanel.tour.definition}
activeAnchor={activePanel.anchor.id}
revealRequest={activePanel.tour.revealRequest}
revealCode={activePanel.tour.revealCode}
onActiveAnchorChange={moveInlineTour}
onClose={close}
/>
Expand Down Expand Up @@ -974,13 +975,16 @@ export function GuidedTourPanel({
tour,
activeAnchor,
revealRequest,
revealCode,
onActiveAnchorChange,
onClose,
docked = false,
}: {
tour: GuidedTour;
activeAnchor: string;
revealRequest: number;
/** The `selectionKey` of the code card to reveal in the requested stop. */
revealCode?: string;
onActiveAnchorChange: (anchor: string, options: { reveal: boolean }) => void;
onClose: () => void;
/** Keeps the panel in its column at every width (a diagram tour's pane). */
Expand Down Expand Up @@ -1115,6 +1119,17 @@ export function GuidedTourPanel({

if (!section || !scroller) return;

// A stop showing several pieces of code reveals the one asked for. The
// first sits under the heading, so the heading still leads to it; code
// the stop does not show leaves the heading as the target too.
const cards = section.querySelectorAll<HTMLElement>("[data-code-peek-key]");

const card = [...cards].find(
(item) => item.dataset.codePeekKey === revealCode,
);

const target = card && card !== cards[0] ? card : section;

// Source peeks above the requested stop resize as their native editors
// load. Keep the heading at the reading line until the reader takes over.
revealingRef.current = true;
Expand All @@ -1127,7 +1142,7 @@ export function GuidedTourPanel({
if (disposed) return;

const offset =
section.getBoundingClientRect().top -
target.getBoundingClientRect().top -
scroller.getBoundingClientRect().top -
TOUR_ACTIVE_TOP_SLACK_PX;

Expand Down Expand Up @@ -1175,7 +1190,7 @@ export function GuidedTourPanel({
reveal();

return dispose;
}, [activeAnchor, revealRequest]);
}, [activeAnchor, revealRequest, revealCode]);

const displayIndex = Math.max(0, activeIndex);
const lastIndex = tour.stops.length - 1;
Expand Down
7 changes: 6 additions & 1 deletion packages/review/app/src/review-panel-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export interface GuidedTour {

export interface PeekPanel {
kind: "peek";
tour?: { definition: GuidedTour; revealRequest: number };
tour?: {
definition: GuidedTour;
revealRequest: number;
/** The `selectionKey` of the stop's code to reveal, not its heading. */
revealCode?: string;
};
anchor?: PeekAnchor;
content: ReviewPeekContent;
}
Expand Down
19 changes: 14 additions & 5 deletions packages/review/app/src/review-panel-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export interface OverlayTour {
kind?: OverlayTourKind;
anchor: string;
revealRequest: number;
/** The `selectionKey` of the stop's code to reveal, not its heading. */
revealCode?: string;
}

/** Which canvas view is showing and what it is scoped to. */
Expand Down Expand Up @@ -161,8 +163,12 @@ export interface ReviewNavigationActions {
openOverlayTour: (
tour: { tourId: string; kind: OverlayTourKind },
anchor: string,
revealCode?: string,
) => void;
moveOverlayTour: (
anchor: string,
options: { reveal: boolean; code?: string },
) => void;
moveOverlayTour: (anchor: string, options: { reveal: boolean }) => void;
closeOverlayTour: () => void;
}

Expand Down Expand Up @@ -332,7 +338,7 @@ export function createReviewPanelStore({
anchor: stop.anchor,
content: stop.content,
tour: {
...tour,
definition: tour.definition,
revealRequest: tour.revealRequest + Number(reveal),
},
},
Expand Down Expand Up @@ -442,23 +448,26 @@ export function createReviewPanelStore({
? { mapFocus: { ...state.mapFocus, pending: false } }
: state,
),
openOverlayTour: (tour, anchor) =>
openOverlayTour: (tour, anchor, revealCode) =>
set((state) => ({
overlayTour: {
...tour,
anchor,
...(revealCode && { revealCode }),
// Counts on across tours: a use-case switch keeps the panel mounted.
revealRequest: (state.overlayTour?.revealRequest ?? 0) + 1,
},
})),
moveOverlayTour: (anchor, { reveal }) =>
moveOverlayTour: (anchor, { reveal, code }) =>
set((state) =>
state.overlayTour
? {
overlayTour: {
...state.overlayTour,
tourId: state.overlayTour.tourId,
...(state.overlayTour.kind && { kind: state.overlayTour.kind }),
anchor,
revealRequest: state.overlayTour.revealRequest + Number(reveal),
...(code && { revealCode: code }),
},
}
: state,
Expand Down
79 changes: 79 additions & 0 deletions packages/review/app/src/review-panel.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { REVIEW_CANVAS_RESUME_EVENT } from "@dev.fast/review-protocol";
import {
type DiffSelection,
selectionKey,
} from "@dev.fast/whiteboard-document";
import { type ReactNode, act, useEffect, useRef, useState } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
Expand Down Expand Up @@ -373,6 +377,81 @@ describe("Guided tour panel", () => {
).toBe("first");
},
);

it("reveals the requested code card in a stop that shows several", async () => {
const container = document.createElement("div");
document.body.append(container);
root = createRoot(container);

const sources: DiffSelection[] = [1, 40].map((line) => ({
file: "order.ts",
start: { side: "head", line },
end: { side: "head", line: line + 4 },
}));

const anchor = { id: "code", title: "Code" } as PeekAnchor;

const tour: GuidedTour = {
id: "tour",
stops: [
{
anchor,
label: "Code",
content: { kind: "source", source: sources[0]!, more: [sources[1]!] },
},
],
};

const renderTour = (revealRequest: number, revealCode?: string) =>
renderWithSession(
<ReviewDebugSettingsProvider>
<ReviewProvider>
<ReviewPanelProvider>
<GuidedTourPanel
tour={tour}
activeAnchor="code"
revealRequest={revealRequest}
revealCode={revealCode}
onActiveAnchorChange={() => {}}
onClose={() => {}}
/>
</ReviewPanelProvider>
</ReviewProvider>
</ReviewDebugSettingsProvider>,
);

await act(async () => renderTour(0));

const body = container.querySelector<HTMLElement>(panelBodySelector)!;

const stop = container.querySelector<HTMLElement>(
"section[data-review-anchor-id]",
)!;

const [, secondCard] = stop.querySelectorAll<HTMLElement>(
"[data-code-peek-key]",
);

vi.spyOn(body, "getBoundingClientRect").mockReturnValue(
domRect({ top: 100, bottom: 600, height: 500 }),
);
vi.spyOn(stop, "getBoundingClientRect").mockReturnValue(
domRect({ top: 100, height: 1400 }),
);
vi.spyOn(secondCard!, "getBoundingClientRect").mockReturnValue(
domRect({ top: 900, height: 300 }),
);

const scrollTo = vi.fn<(options: ScrollToOptions) => void>();
Object.defineProperty(body, "scrollTo", { value: scrollTo });

// The heading leads to the stop's first code.
await act(async () => renderTour(1, selectionKey(sources[0]!)));
expect(scrollTo).toHaveBeenLastCalledWith({ top: -18 });

await act(async () => renderTour(2, selectionKey(sources[1]!)));
expect(scrollTo).toHaveBeenLastCalledWith({ top: 782 });
});
});

describe("Side panel scrollbar", () => {
Expand Down
Loading