Skip to content

Commit

Permalink
- Fixed artboard 0 bug
Browse files Browse the repository at this point in the history
- vulnerability fixes
  • Loading branch information
akram-r committed Jan 30, 2024
1 parent a85333c commit bd6e582
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 70 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,6 @@ function App() {
const artboardPosition = getArtboardPosition(canvasRef.current, activeArtboardId);
const artboardLeftAdjustment = artboardPosition.left;
const artboardTopAdjustment = artboardPosition.top;

if (artboardLeftAdjustment === undefined || artboardTopAdjustment === undefined) {
return;
}
const artboardDimensions = getArtboardDimensions(canvasRef.current, activeArtboardId);
// Now we need to create a new canvas and add the artboard to it
const offscreenCanvas = new fabric.Canvas('print', {
Expand Down
8 changes: 2 additions & 6 deletions src/modules/animate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,8 @@ const Animation: React.FC<AnimationProps> = ({ currentSelectedElements, saveArtb
if (!artboard) {
throw new Error('Artboard not found');
}
const artboardLeftAdjustment = artboard.left;
const artboardTopAdjustment = artboard.top;

if (!artboardLeftAdjustment || !artboardTopAdjustment) {
return;
}
const artboardLeftAdjustment = artboard.left!;
const artboardTopAdjustment = artboard.top!;

const width = artboard.width!;
const height = artboard.height!;
Expand Down
20 changes: 4 additions & 16 deletions src/modules/artboard/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@ export const getArtboardDimensions = (
artboardId: string,
): { width: number; height: number } => {
const artboard = getArtboardObject(canvas, artboardId);

const width = artboard.width;
const height = artboard.height;

if (width === undefined || height === undefined) {
throw new Error('Artboard dimensions not found');
}

const width = artboard.width!;
const height = artboard.height!;
return {
width,
height,
Expand All @@ -36,14 +30,8 @@ export const getArtboardPosition = (
artboardId: string,
): { left: number; top: number } => {
const artboard = getArtboardObject(canvas, artboardId);

const left = artboard.left;
const top = artboard.top;

if (left === undefined || top === undefined || left === null || top === null) {
throw new Error('Artboard position not found');
}

const left = artboard.left!;
const top = artboard.top!;
return {
left,
top,
Expand Down
12 changes: 4 additions & 8 deletions src/modules/image/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ export const getScaledPosition = (artboard: fabric.Rect): { left: number; top: n
throw new Error('Artboard not found');
}

const left = artboard.left;
const top = artboard.top;
const width = artboard.width;
const height = artboard.height;

if (left === undefined || top === undefined || width === undefined || height === undefined) {
throw new Error('Artboard dimensions not found');
}
const left = artboard.left!;
const top = artboard.top!;
const width = artboard.width!;
const height = artboard.height!;

// calculate the center of the artboard
const centerX = left + width / 2;
Expand Down
57 changes: 27 additions & 30 deletions src/modules/position/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,71 +23,68 @@ export const alignElementToRect = (
canvas: fabric.Canvas,
) => {
const targetRect = getArtboardObject(canvas, activeArtboardId);

const targetLeft = targetRect.left!;
const targetTop = targetRect.top!;
const targetWidth = targetRect.width!;
const targetHeight = targetRect.height!;
switch (position) {
case 'left':
currentSelectedElements.forEach(element => {
if (!targetRect.left || !element.left) throw new Error('Invalid target rect in left align');
const elementLeft = element.left!;
const { left: elementExtremeLeft } = getExtremePoints(element);
element.set({
left: targetRect.left + (element.left - getExtremePoints(element).left),
left: targetLeft + (elementLeft - elementExtremeLeft),
});
});
break;
case 'center':
currentSelectedElements.forEach(element => {
if (!targetRect.left || !element.left || !targetRect.width)
throw new Error('Invalid target rect in center align');
const artboardCenter = targetRect.left + (targetRect.width + targetRect.left - targetRect.left) / 2;
const elementCenter =
getExtremePoints(element).left +
(getExtremePoints(element).right - getExtremePoints(element).left) / 2;
const elementLeft = element.left!;
const artboardCenter = targetLeft + (targetWidth + targetLeft - targetLeft) / 2;
const { left: elementExtremeLeft, right: elementExtremeRight } = getExtremePoints(element);
const elementCenter = elementExtremeLeft + (elementExtremeRight - elementExtremeLeft) / 2;
element.set({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
left: element.left + (artboardCenter - elementCenter),
left: elementLeft + (artboardCenter - elementCenter),
});
});

break;
case 'right':
currentSelectedElements.forEach(element => {
const elementLeft = element.left!;
const { right: elementExtremeRight } = getExtremePoints(element);

element.set({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
left: element.left + (targetRect.left + targetRect.width) - getExtremePoints(element).right,
left: elementLeft + (targetLeft + targetWidth) - elementExtremeRight,
});
});
break;
case 'top':
currentSelectedElements.forEach(element => {
if (!targetRect.top || !element.top || !targetRect.width)
throw new Error('Invalid target rect in top align');
const elementTop = element.top!;
const { top: elementExtremeTop } = getExtremePoints(element);
element.set({
top: targetRect.top + (element.top - getExtremePoints(element).top),
top: targetTop + (elementTop - elementExtremeTop),
});
});
break;
case 'middle':
currentSelectedElements.forEach(element => {
if (!targetRect.top || !element.top || !targetRect.height)
throw new Error('Invalid target rect in middle align');
const artboardCenter = targetRect.top + (targetRect.height + targetRect.top - targetRect.top) / 2;
const elementCenter =
getExtremePoints(element).top +
(getExtremePoints(element).bottom - getExtremePoints(element).top) / 2;
const elementTop = element.top!;
const artboardCenter = targetTop + (targetHeight + targetTop - targetTop) / 2;
const { top: elementExtremeTop, bottom: elementExtremeBottom } = getExtremePoints(element);
const elementCenter = elementExtremeTop + (elementExtremeBottom - elementExtremeTop) / 2;
element.set({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
top: element.top + (artboardCenter - elementCenter),
top: elementTop + (artboardCenter - elementCenter),
});
});
break;
case 'bottom':
currentSelectedElements.forEach(element => {
const elementTop = element.top!;
const { bottom: elementExtremeBottom } = getExtremePoints(element);
element.set({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
top: element.top + (targetRect.top + targetRect.height) - getExtremePoints(element).bottom,
top: elementTop + (targetTop + targetHeight) - elementExtremeBottom,
});
});
break;
Expand Down

0 comments on commit bd6e582

Please sign in to comment.