-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feature/#569-add-filename-to-footer
- Loading branch information
Showing
12 changed files
with
130 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...nts/mock-components/front-text-components/front-text-hooks/resize-fontsize-change.hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { calcTextDimensions } from '@/common/utils/calc-text-dimensions'; | ||
import { useCanvasContext } from '@/core/providers'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export const useResizeOnFontSizeChange = ( | ||
id: string, | ||
coords: { x: number; y: number }, | ||
text: string, | ||
fontSize: number, | ||
fontVariant: string, | ||
multiline: boolean = false | ||
) => { | ||
const previousFontSize = useRef(fontSize); | ||
const { updateShapeSizeAndPosition, stageRef } = useCanvasContext(); | ||
const konvaLayer = stageRef.current?.getLayers()[0]; | ||
|
||
useEffect(() => { | ||
if (previousFontSize.current !== fontSize) { | ||
previousFontSize.current = fontSize; | ||
|
||
const paragraphLines = _extractParagraphLines(text); | ||
const longestLine = _findLongestString(paragraphLines); | ||
|
||
const { width, height } = calcTextDimensions( | ||
multiline ? longestLine : text, | ||
fontSize, | ||
fontVariant, | ||
konvaLayer | ||
); | ||
|
||
updateShapeSizeAndPosition( | ||
id, | ||
coords, | ||
{ | ||
width: width * 1.15, | ||
height: multiline | ||
? _calcParagraphTotalHeight(height, 0.8, paragraphLines.length) | ||
: height, | ||
}, | ||
true | ||
); | ||
} | ||
}, [fontSize]); | ||
}; | ||
|
||
/* Hook Helper functions */ | ||
function _extractParagraphLines(multilineText: string) { | ||
return multilineText.split(/\r?\n/).filter(line => line.trim() !== ''); | ||
} | ||
|
||
function _findLongestString(stringsArray: string[]): string { | ||
return stringsArray.reduce((longest, current) => | ||
current.length > longest.length ? current : longest | ||
); | ||
} | ||
|
||
function _calcParagraphTotalHeight( | ||
heightPerLine: number, | ||
lineHeight: number = 1.3, | ||
linesQty: number | ||
) { | ||
return heightPerLine * lineHeight * linesQty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters