Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embed resize #201

Merged
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
10 changes: 7 additions & 3 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,10 @@ class App extends React.Component<AppProps, AppState> {
this.state.activeEmbeddable?.element === el &&
this.state.activeEmbeddable?.state === "hover";

// Modify the scale based on el.scale property
const [xScale, yScale] = el.scale;
const scaledTransform = `scale(${scale * xScale}, ${scale * yScale})`;

return (
<div
key={el.id}
Expand All @@ -878,7 +882,7 @@ class App extends React.Component<AppProps, AppState> {
transform: isVisible
? `translate(${x - this.state.offsetLeft}px, ${
y - this.state.offsetTop
}px) scale(${scale})`
}px) ${scaledTransform}`
: "none",
display: isVisible ? "block" : "none",
opacity: el.opacity / 100,
Expand Down Expand Up @@ -907,8 +911,8 @@ class App extends React.Component<AppProps, AppState> {
}}*/
className="excalidraw__embeddable-container__inner"
style={{
width: isVisible ? `${el.width}px` : 0,
height: isVisible ? `${el.height}px` : 0,
width: isVisible ? `${el.width / xScale}px` : 0,
height: isVisible ? `${el.height / yScale}px` : 0,
transform: isVisible ? `rotate(${el.angle}rad)` : "none",
pointerEvents: isActive
? POINTER_EVENTS.enabled
Expand Down
1 change: 1 addition & 0 deletions src/data/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ const restoreElement = (
case "embeddable":
return restoreElementWithProperties(element, {
validated: null,
scale: element.scale ?? [1, 1],
});
case "frame":
return restoreElementWithProperties(element, {
Expand Down
2 changes: 1 addition & 1 deletion src/element/embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type EmbeddedLink =
const embeddedLinkCache = new Map<string, EmbeddedLink>();

const RE_YOUTUBE =
/^(?:http(?:s)?:\/\/)?(?:www\.)?youtu(?:be\.com|\.be)\/(embed\/|watch\?v=|shorts\/|playlist\?list=|embed\/videoseries\?list=)?([a-zA-Z0-9_-]+)(?:\?t=|&t=|\?start=|&start=)?([a-zA-Z0-9_-]+)?[^\s]*$/;
/^(?:http(?:s)?:\/\/)?(?:www\.)?youtu(?:be\.com|\.be)\/(embed\/|watch\?v=|shorts\/|playlist\?list=|embed\/videoseries\?list=)?([a-zA-Z0-9_-]+)(?:\?t=|.*&t=|\?start=|.*&start=)?([a-zA-Z0-9_-]+)?[^\s]*$/;

const RE_VIMEO =
/^(?:http(?:s)?:\/\/)?(?:(?:w){3}.)?(?:player\.)?vimeo\.com\/(?:video\/)?([^?\s]+)(?:\?.*)?$/;
Expand Down
1 change: 1 addition & 0 deletions src/element/newElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export const newEmbeddableElement = (
return {
..._newElementBase<ExcalidrawEmbeddableElement>("embeddable", opts),
validated: opts.validated,
scale: [1, 1],
};
};

Expand Down
35 changes: 26 additions & 9 deletions src/element/resizeElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import {
isArrowElement,
isBoundToContainer,
isEmbeddableElement,
isFrameElement,
isFreeDrawElement,
isImageElement,
Expand Down Expand Up @@ -586,15 +587,31 @@ export const resizeSingleElement = (
};

if ("scale" in element && "scale" in stateAtResizeStart) {
mutateElement(element, {
scale: [
// defaulting because scaleX/Y can be 0/-0
(Math.sign(newBoundsX2 - stateAtResizeStart.x) ||
stateAtResizeStart.scale[0]) * stateAtResizeStart.scale[0],
(Math.sign(newBoundsY2 - stateAtResizeStart.y) ||
stateAtResizeStart.scale[1]) * stateAtResizeStart.scale[1],
],
});
if (isEmbeddableElement(element)) {
if (shouldMaintainAspectRatio) {
const scale: [number, number] = [
Math.abs(
eleNewWidth /
(stateAtResizeStart.width / stateAtResizeStart.scale[0]),
),
Math.abs(
eleNewHeight /
(stateAtResizeStart.height / stateAtResizeStart.scale[1]),
),
];
mutateElement(element, { scale });
}
} else {
mutateElement(element, {
scale: [
// defaulting because scaleX/Y can be 0/-0
(Math.sign(newBoundsX2 - stateAtResizeStart.x) ||
stateAtResizeStart.scale[0]) * stateAtResizeStart.scale[0],
(Math.sign(newBoundsY2 - stateAtResizeStart.y) ||
stateAtResizeStart.scale[1]) * stateAtResizeStart.scale[1],
],
});
}
}

if (
Expand Down
1 change: 1 addition & 0 deletions src/element/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type ExcalidrawEmbeddableElement = _ExcalidrawElementBase &
* may not have access to host-app supplied url validator during restore.
*/
validated: boolean | null;
scale: [number, number];
}>;

export type ExcalidrawImageElement = _ExcalidrawElementBase &
Expand Down
8 changes: 5 additions & 3 deletions src/renderer/renderElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,8 @@ export const renderElementToSvg = (
renderConfig,
);

const scaleX = element.scale?.[0] || 1;
const scaleY = element.scale?.[1] || 1;
// render embeddable element + iframe
const embeddableNode = roughSVGDrawWithPrecision(
rsvg,
Expand All @@ -996,7 +998,7 @@ export const renderElementToSvg = (
"transform",
`translate(${offsetX || 0} ${
offsetY || 0
}) rotate(${degree} ${cx} ${cy})`,
}) rotate(${degree} ${cx} ${cy}) scale(${scaleX}, ${scaleY})`,
);
while (embeddableNode.firstChild) {
embeddableNode.removeChild(embeddableNode.firstChild);
Expand Down Expand Up @@ -1027,8 +1029,8 @@ export const renderElementToSvg = (
SVG_NS,
"foreignObject",
);
foreignObject.style.width = `${element.width}px`;
foreignObject.style.height = `${element.height}px`;
foreignObject.style.width = `${element.width / scaleX}px`;
foreignObject.style.height = `${element.height / scaleY}px`;
foreignObject.style.border = "none";
const div = foreignObject.ownerDocument!.createElementNS(SVG_NS, "div");
div.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
Expand Down
1 change: 1 addition & 0 deletions src/tests/fixtures/elementFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const embeddableFixture: ExcalidrawElement = {
...elementBase,
type: "embeddable",
validated: null,
scale: [1, 1],
};
export const ellipseFixture: ExcalidrawElement = {
...elementBase,
Expand Down
Loading