-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add format and quality parameters toDataURL function #2431
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
base: main
Are you sure you want to change the base?
Changes from 35 commits
9cc26da
b3c411a
20b35ce
b5c6cd7
57be53a
f93ea7a
b8aa9cd
f731f28
3b4f12b
78e31ad
6e762b9
f525454
471c2db
ead5351
5d07869
624f9f1
6720611
683dcce
88d412f
5fb2f9f
5421aaf
26b15e2
85ce71c
2b268ad
632d8d3
b6ea5b2
1b722b9
cf32be3
f58e649
33c70db
3edce70
992d003
ea3f8ac
a91999c
89963ce
9d7fef7
d0f44b8
a7a77d1
d84b048
1ae4bdf
887eb51
13d38ec
c2e2389
f5c6464
ea9564f
517a18e
21c5315
211f7e9
3cdc9a0
595d5d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
bohdanprog marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,39 @@ | |
title?: string; | ||
} | ||
|
||
export type DataUrlOptions = JpegOptions | PngOptions; | ||
|
||
interface JpegOptions { | ||
format: 'jpeg'; | ||
quality?: number; | ||
size?: Size; | ||
} | ||
|
||
interface PngOptions { | ||
format: 'png'; | ||
size?: Size; | ||
} | ||
|
||
interface Size { | ||
width: number; | ||
height: number; | ||
} | ||
|
||
function validateOptions(options?: DataUrlOptions) { | ||
if (options && options?.format === 'jpeg') { | ||
if (!validateJpegQualityParameter(options)) { | ||
throw new Error('toDataURL: Invalid quality parameter for JPEG format.'); | ||
} | ||
bohdanprog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
function validateJpegQualityParameter(options: JpegOptions): boolean { | ||
if (options.quality && (options.quality < 0 || options.quality > 1)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
bohdanprog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export default class Svg extends Shape<SvgProps> { | ||
static displayName = 'Svg'; | ||
|
||
|
@@ -81,16 +114,30 @@ | |
root && root.setNativeProps(props); | ||
}; | ||
|
||
toDataURL = (callback: (base64: string) => void, options?: object) => { | ||
/** | ||
* @deprecated use size property instead | ||
* @param callback | ||
* @param options as { size: { width: number, height: number }, format: 'jpeg' | 'png', quality?:1 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://jsdoc.app/tags-callback https://jsdoc.app/tags-type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated JSdoc, let me know if it's good enough for us. |
||
*/ | ||
toDataURL(callback: (base64: string) => void, options: Size): void; | ||
toDataURL(callback: (base64: string) => void): void; | ||
bohdanprog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
toDataURL(callback: (base64: string) => void, options: DataUrlOptions): void; | ||
bohdanprog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
toDataURL( | ||
callback: (base64: string) => void, | ||
options?: DataUrlOptions | Size | ||
): void { | ||
if (!callback) { | ||
return; | ||
} | ||
if (options && 'format' in options) { | ||
validateOptions(options); | ||
} | ||
const handle = findNodeHandle(this.root as Component); | ||
const RNSVGSvgViewModule: Spec = | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
require('../fabric/NativeSvgViewModule').default; | ||
RNSVGSvgViewModule.toDataURL(handle, options, callback); | ||
}; | ||
} | ||
|
||
render() { | ||
const { | ||
|
@@ -185,7 +232,7 @@ | |
props.transform = gStyle.transform; | ||
gStyle.transform = undefined; | ||
} | ||
props.transform = extractTransformSvgView(props as any); | ||
} | ||
|
||
const RNSVGSvg = Platform.OS === 'android' ? RNSVGSvgAndroid : RNSVGSvgIOS; | ||
|
Uh oh!
There was an error while loading. Please reload this page.