Skip to content

Commit df0586a

Browse files
author
farfromrefug
committed
fix(svg): src can be a function/promise/string
1 parent 9501dc8 commit df0586a

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/ui-svg/index.android.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,10 @@ class MySVGView extends android.view.View {
319319
if (!svg) {
320320
return;
321321
}
322-
if (this._blendingMode !== undefined) {
323-
const picture = svg.renderToPicture(this.renderOptions);
324-
}
322+
this.renderOptions.viewPort(0, 0, canvas.getWidth(), canvas.getHeight());
323+
// if (this._blendingMode !== undefined) {
324+
// const picture = svg.renderToPicture(this.renderOptions);
325+
// }
325326
svg.renderToCanvas(canvas, this.renderOptions);
326327
}
327328
_blendingMode: android.graphics.PorterDuff.Mode;
@@ -399,10 +400,24 @@ export class SVGView extends SVGViewBase {
399400
}
400401
return view;
401402
}
403+
async handleSrc(src) {
404+
if (src instanceof Promise) {
405+
this.handleSrc(await src);
406+
return;
407+
} else if (typeof src === 'function') {
408+
const newSrc = src();
409+
if (newSrc instanceof Promise) {
410+
await newSrc;
411+
}
412+
this.handleSrc(newSrc);
413+
return;
414+
}
415+
this.nativeViewProtected.setSvg(getSVG(src));
416+
}
402417

403418
[srcProperty.setNative](value) {
419+
this.handleSrc(value);
404420
// this.nativeViewProtected.image = getSVGKImage(value);
405-
this.nativeViewProtected.setSvg(getSVG(value));
406421
// if (this._imageSourceAffectsLayout) {
407422
// this._imageSourceAffectsLayout = false;
408423
// this.requestLayout();

src/ui-svg/index.common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ export const stretchProperty = new Property<SVGView, Stretch>({ name: 'stretch'
6161
// export const blendingModeProperty = new Property<SVGView, string>({ name: 'blendingMode' });
6262

6363
@CSSType('SVGView')
64-
export class SVGView extends View {}
64+
export class SVGView extends View {
65+
src: string;
66+
}
6567
srcProperty.register(SVGView);
6668
stretchProperty.register(SVGView);
6769
// blendingModeProperty.register(SVGView);

src/ui-svg/index.ios.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function getSVGKImage(src: string | ImageAsset | File) {
5555
}
5656
if (Utils.isFileOrResourcePath(imagePath)) {
5757
if (imagePath.indexOf(Utils.RESOURCE_PREFIX) === 0) {
58-
const resName = imagePath.substr(Utils.RESOURCE_PREFIX.length);
58+
const resName = imagePath.slice(Utils.RESOURCE_PREFIX.length);
5959
return SVGKImage.imageNamed(resName);
6060
} else if (imagePath.indexOf('~/') === 0) {
6161
const strPath = path.join(knownFolders.currentApp().path, imagePath.replace('~/', ''));
@@ -320,14 +320,29 @@ export class SVGView extends SVGViewBase {
320320
}
321321
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
322322
}
323-
[srcProperty.setNative](value) {
324-
this.nativeViewProtected.image = getSVGKImage(value);
323+
async handleSrc(src) {
324+
if (src instanceof Promise) {
325+
this.handleSrc(await src);
326+
return;
327+
} else if (typeof src === 'function') {
328+
const newSrc = src();
329+
if (newSrc instanceof Promise) {
330+
await newSrc;
331+
}
332+
this.handleSrc(newSrc);
333+
return;
334+
}
335+
this.nativeViewProtected.image = getSVGKImage(src);
325336
// this.nativeViewProtected.renderer = getRenderer(value);
326337
if (this._imageSourceAffectsLayout) {
327338
this._imageSourceAffectsLayout = false;
328339
this.requestLayout();
329340
}
330341
}
342+
343+
[srcProperty.setNative](value) {
344+
this.handleSrc(value);
345+
}
331346
[stretchProperty.setNative](value: 'none' | 'aspectFill' | 'aspectFit' | 'fill') {
332347
this.nativeViewProtected.contentMode = getUIImageScaleType(value);
333348
// switch (value) {

0 commit comments

Comments
 (0)