Skip to content

Commit 09c2c0a

Browse files
svg: fit arg (#1413)
1 parent 0e679ac commit 09c2c0a

File tree

10 files changed

+442
-299
lines changed

10 files changed

+442
-299
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: SVG `fit` option

znai-reactjs/src/doc-elements/images/AnnotatedImage.tsx

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { CSSProperties} from "react";
19-
import React, { useState } from "react";
18+
import React, { CSSProperties, useState } from "react";
2019

2120
import { imageAdditionalPreviewUrlParam } from "./imagePreviewAdditionalUrlParam";
2221

23-
import { cssVarPixelValue } from "../../utils/cssVars";
22+
import { calcFitScale } from "./fitUtils";
2423

2524
import Annotations from "./annotations/Annotations";
2625
import { zoom } from "../zoom/Zoom";
@@ -31,12 +30,10 @@ import { TooltipPlacement } from "../../components/Tooltip";
3130

3231
import { WithElementsLibrary } from "../default-elements/DocElement";
3332

34-
import { ContainerTitleCommonProps} from "../container/ContainerTitle";
35-
import { ContainerTitle, useIsUserDrivenCollapsed } from "../container/ContainerTitle";
33+
import { ContainerTitle, ContainerTitleCommonProps, useIsUserDrivenCollapsed } from "../container/ContainerTitle";
3634

3735
import { useIsMobile } from "../../theme/ViewPortContext";
38-
import { ContainerCommonProps } from "../container/Container";
39-
import { Container } from "../container/Container";
36+
import { Container, ContainerCommonProps } from "../container/Container";
4037

4138
import "./AnnotatedImage.css";
4239

@@ -87,7 +84,7 @@ export function AnnotatedImage(props: AnnotatedImageProps) {
8784
const isMobile = useIsMobile();
8885
const { userDrivenCollapsed, collapseToggle } = useIsUserDrivenCollapsed(collapsed);
8986

90-
const scaleToUse = calcScale();
87+
const scaleToUse = calcFitScale(fit, width, scale, isMobile);
9188

9289
const [mousePosition, setMousePosition] = useState<{ x: number; y: number }>({ x: -1, y: -1 });
9390

@@ -243,15 +240,6 @@ export function AnnotatedImage(props: AnnotatedImageProps) {
243240
}
244241
}
245242

246-
function calcScale() {
247-
if (!fit) {
248-
return scale || 1.0;
249-
}
250-
251-
const singleColumnWidth = isMobile ? window.innerWidth : cssVarPixelValue("znai-single-column-full-width");
252-
return Math.min(1.0, singleColumnWidth / width);
253-
}
254-
255243
function renderTitle() {
256244
const renderedTitle = title ? (
257245
<ContainerTitle
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2025 znai maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { cssVarPixelValue } from "../../utils/cssVars";
18+
19+
export function calcFitScale(
20+
fit: boolean | undefined,
21+
width: number,
22+
scale: number | undefined,
23+
isMobile: boolean
24+
): number {
25+
if (!fit) {
26+
return scale || 1.0;
27+
}
28+
29+
const singleColumnWidth = isMobile
30+
? window.innerWidth
31+
: cssVarPixelValue("znai-single-column-full-width");
32+
return Math.min(1.0, singleColumnWidth / width);
33+
}

znai-reactjs/src/doc-elements/presentation/PresentationSlideContainer.tsx

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import React, { useLayoutEffect, useRef, useState } from 'react';
17+
import React, { useLayoutEffect, useRef, useState } from "react";
1818

19-
import { PresentationDimension, SlideAreaDimension } from './PresentationDimensions';
19+
import { PresentationDimension, SlideAreaDimension } from "./PresentationDimensions";
2020

21-
import './PresentationSlideContainer.css';
21+
import "./PresentationSlideContainer.css";
2222

2323
interface Props {
2424
slideIdx: number;
@@ -32,75 +32,108 @@ interface Props {
3232
}
3333

3434
export function PresentationSlideContainer({
35-
slideIdx,
36-
maxScaleRatio,
37-
isPadded,
38-
isScaled,
39-
isCentered,
40-
presentationArea,
41-
slideArea,
42-
render
43-
}: Props) {
35+
slideIdx,
36+
maxScaleRatio,
37+
isPadded,
38+
isScaled,
39+
isCentered,
40+
presentationArea,
41+
slideArea,
42+
render,
43+
}: Props) {
4444
const [scaleRatio, setScaleRatio] = useState(1);
4545
const [slideAppeared, setSlideAppeared] = useState(false);
4646

4747
const contentNode = useRef<HTMLDivElement>(null);
4848

4949
useLayoutEffect(() => {
50-
setScaleRatio(isScaled ?
51-
calcScale(presentationArea, slideArea, contentNode.current!, isPadded, maxScaleRatio) :
52-
1.0);
53-
54-
setSlideAppeared(true);
55-
}, [isPadded, maxScaleRatio, isScaled, slideIdx, presentationArea, slideArea])
56-
57-
const className = "znai-presentation-slide-container " +
50+
if (!isScaled) {
51+
setScaleRatio(1.0);
52+
setSlideAppeared(true);
53+
return;
54+
}
55+
56+
const node = contentNode.current!;
57+
58+
const tryCalcScale = () => {
59+
if (!hasContentLoaded(node)) {
60+
return false;
61+
}
62+
setScaleRatio(calcScale(presentationArea, slideArea, node, isPadded, maxScaleRatio));
63+
setSlideAppeared(true);
64+
return true;
65+
};
66+
67+
if (tryCalcScale()) {
68+
return;
69+
}
70+
71+
const resizeObserver = new ResizeObserver(() => {
72+
if (tryCalcScale()) {
73+
resizeObserver.disconnect();
74+
}
75+
});
76+
resizeObserver.observe(node);
77+
78+
return () => {
79+
resizeObserver.disconnect();
80+
};
81+
}, [isPadded, maxScaleRatio, isScaled, slideIdx, presentationArea, slideArea]);
82+
83+
const className =
84+
"znai-presentation-slide-container " +
5885
(isCentered ? " centered" : "") +
5986
(isPadded ? " padded" : "") +
6087
(slideAppeared ? " znai-presentation-slide-appeared" : "");
6188

6289
const rendered = render();
6390

6491
const wrappedChildren = isScaled ? (
65-
<div ref={contentNode} style={{transform: "scale(" + scaleRatio + ")"}}>
92+
<div ref={contentNode} style={{ transform: "scale(" + scaleRatio + ")" }}>
6693
{rendered}
6794
</div>
68-
) : rendered;
95+
) : (
96+
rendered
97+
);
6998

70-
const maxWidth = presentationArea.width * slideArea.widthPercentage / 100.0
71-
const maxHeight = presentationArea.height * slideArea.heightPercentage / 100.0
99+
const maxWidth = (presentationArea.width * slideArea.widthPercentage) / 100.0;
100+
const maxHeight = (presentationArea.height * slideArea.heightPercentage) / 100.0;
72101

73102
return (
74-
<div className={className} style={{width: maxWidth, height: maxHeight}}>
103+
<div className={className} style={{ width: maxWidth, height: maxHeight }}>
75104
{wrappedChildren}
76105
</div>
77-
)
106+
);
78107
}
79108

80-
function calcScale(presentationArea: PresentationDimension,
81-
slideArea: SlideAreaDimension,
82-
contentNode: HTMLDivElement,
83-
isPadded: boolean,
84-
maxScaleRatio: number): number {
109+
function hasContentLoaded(contentNode: HTMLDivElement): boolean {
110+
return contentNode.offsetWidth > 0 && contentNode.offsetHeight > 0;
111+
}
112+
113+
function calcScale(
114+
presentationArea: PresentationDimension,
115+
slideArea: SlideAreaDimension,
116+
contentNode: HTMLDivElement,
117+
isPadded: boolean,
118+
maxScaleRatio: number
119+
): number {
85120
// code based padding to avoid margin collapse
86121
const hPad = isPadded ? 60 : 0;
87122
const vPad = isPadded ? 30 : 0;
88123

89-
const maxWidth = calcMaxWidth(presentationArea, slideArea)
90-
const maxHeight = calcMaxHeight(presentationArea, slideArea)
124+
const maxWidth = calcMaxWidth(presentationArea, slideArea);
125+
const maxHeight = calcMaxHeight(presentationArea, slideArea);
91126

92127
const widthRatio = (maxWidth - hPad) / contentNode.offsetWidth;
93128
const heightRatio = (maxHeight - vPad) / contentNode.offsetHeight;
94129

95130
return Math.min(widthRatio, heightRatio, maxScaleRatio);
96131
}
97132

98-
function calcMaxWidth(presentationArea: PresentationDimension,
99-
slideArea: SlideAreaDimension) {
100-
return presentationArea.width * slideArea.widthPercentage / 100.0
133+
function calcMaxWidth(presentationArea: PresentationDimension, slideArea: SlideAreaDimension) {
134+
return (presentationArea.width * slideArea.widthPercentage) / 100.0;
101135
}
102136

103-
function calcMaxHeight(presentationArea: PresentationDimension,
104-
slideArea: SlideAreaDimension) {
105-
return presentationArea.height * slideArea.heightPercentage / 100.0
106-
}
137+
function calcMaxHeight(presentationArea: PresentationDimension, slideArea: SlideAreaDimension) {
138+
return (presentationArea.height * slideArea.heightPercentage) / 100.0;
139+
}

znai-reactjs/src/doc-elements/svg/EmbeddedSvg.demo.jsx

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 znai maintainers
3+
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import React from "react";
19+
20+
import { Svg } from "./Svg";
21+
import { Registry, simulateState } from "react-component-viewer";
22+
import { svg } from "./svg.testdata";
23+
24+
const [getActualSize, setActualSize] = simulateState(false);
25+
26+
export function embeddedSvgDemo(registry: Registry) {
27+
registry
28+
.add("simple", () => <Svg svg={svg()} />)
29+
.add("partially revealed", () => <Svg svg={svg()} idsToReveal={["partC"]} />)
30+
.add("actual size", () => <Svg svg={svg()} actualSize={true} />)
31+
.add("partially revealed with actual size", () => <Svg svg={svg()} idsToReveal={["partC"]} actualSize={true} />)
32+
.add("actual size with scale", () => <Svg svg={svg()} actualSize={true} scale={0.5} />)
33+
.add("partially revealed with actual size and scale", () => (
34+
<Svg svg={svg()} idsToReveal={["partC"]} scale={0.5} actualSize={true} />
35+
))
36+
.add("flip actual size for preview mode", () => (
37+
<div>
38+
<button onClick={toggleActualSize}>toggle actual size</button>
39+
<Svg svg={svg(300, undefined)} idsToReveal={["partC"]} scale={0.5} actualSize={getActualSize()} />
40+
</div>
41+
))
42+
.add("wide svg without fit", () => <Svg svg={svg(1200, 400)} />)
43+
.add("wide svg with fit", () => <Svg svg={svg(1200, 400)} fit={true} />)
44+
.add("wide svg with fit and scale", () => <Svg svg={svg(1200, 400)} fit={true} scale={0.8} />);
45+
}
46+
47+
function toggleActualSize() {
48+
setActualSize(!getActualSize());
49+
}

0 commit comments

Comments
 (0)