Skip to content

Commit 482198f

Browse files
committed
feat: replace positionOptions to postions
1 parent 6d3aa94 commit 482198f

File tree

4 files changed

+65
-30
lines changed

4 files changed

+65
-30
lines changed

android/src/main/java/com/jimmydaddy/imagemarker/base/TextOptions.kt

+11-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ data class TextOptions(val options: ReadableMap) {
3131
throw MarkerError(ErrorCode.PARAMS_REQUIRED, "mark text is required")
3232
}
3333
val positionOptions =
34-
if (null != options.getMap("positionOptions")) options.getMap("positionOptions") else null
34+
if (null != options.getMap("position")) options.getMap("position") else null
3535
x = if (positionOptions!!.hasKey("X")) Utils.handleDynamicToString(positionOptions.getDynamic("X")) else null
3636
y = if (positionOptions.hasKey("Y")) Utils.handleDynamicToString(positionOptions.getDynamic("Y")) else null
3737
positionEnum =
@@ -69,11 +69,12 @@ data class TextOptions(val options: ReadableMap) {
6969
Typeface.DEFAULT
7070
}
7171
}
72-
val textSize = TypedValue.applyDimension(
73-
TypedValue.COMPLEX_UNIT_SP,
74-
style.fontSize,
75-
context.resources.displayMetrics
76-
)
72+
// val textSize = TypedValue.applyDimension(
73+
// TypedValue.COMPLEX_UNIT_SP,
74+
// style.fontSize,
75+
// context.resources.displayMetrics
76+
// )
77+
val textSize = style.fontSize
7778
textPaint.isAntiAlias = true
7879
textPaint.textSize = textSize
7980
Log.i(Constants.IMAGE_MARKER_TAG, "textSize: " + textSize + " fontSize: " + style.fontSize + " displayMetrics: " + context.resources.displayMetrics)
@@ -121,7 +122,7 @@ data class TextOptions(val options: ReadableMap) {
121122
).toInt()
122123
}
123124
val margin = DEFAULT_MARGIN
124-
var position = Position(margin.toFloat(), margin.toFloat())
125+
var position = Position(margin, margin)
125126
if (positionEnum != null) {
126127
position = Position.getTextPosition(
127128
positionEnum,
@@ -168,9 +169,9 @@ data class TextOptions(val options: ReadableMap) {
168169
if (style.textBackgroundStyle!!.cornerRadius != null) {
169170
val path = Path()
170171

171-
path.addRoundRect(bgRect, style.textBackgroundStyle!!.cornerRadius!!.radii(bgRect), Path.Direction.CW);
172+
path.addRoundRect(bgRect, style.textBackgroundStyle!!.cornerRadius!!.radii(bgRect), Path.Direction.CW)
172173

173-
canvas.drawPath(path, paint);
174+
canvas.drawPath(path, paint)
174175
} else {
175176
canvas.drawRect(bgRect, paint)
176177
}
@@ -179,6 +180,7 @@ data class TextOptions(val options: ReadableMap) {
179180
Paint.Align.RIGHT -> x + textWidth
180181
Paint.Align.CENTER -> x + textWidth / 2
181182
Paint.Align.LEFT -> x
183+
else -> x
182184
}
183185
canvas.translate(textX, y)
184186
textLayout.draw(canvas)

example/src/App.tsx

+33-19
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,11 @@ function useViewModel() {
291291
useModal: true,
292292
},
293293
(buttonIndex) => {
294-
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
295-
else setBackgroundFormat(options[buttonIndex] as any);
294+
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
295+
return;
296+
} else {
297+
setBackgroundFormat(options[buttonIndex] as any);
298+
}
296299
}
297300
);
298301
}
@@ -309,8 +312,11 @@ function useViewModel() {
309312
useModal: true,
310313
},
311314
(buttonIndex) => {
312-
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
313-
else setWaterMarkType(options[buttonIndex] as any);
315+
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
316+
return;
317+
} else {
318+
setWaterMarkType(options[buttonIndex] as any);
319+
}
314320
}
315321
);
316322
}
@@ -332,8 +338,11 @@ function useViewModel() {
332338
useModal: true,
333339
},
334340
(buttonIndex) => {
335-
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
336-
else setSaveFormat(options[buttonIndex] as any);
341+
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
342+
return;
343+
} else {
344+
setSaveFormat(options[buttonIndex] as any);
345+
}
337346
}
338347
);
339348
}
@@ -359,8 +368,9 @@ function useViewModel() {
359368
useModal: true,
360369
},
361370
(buttonIndex) => {
362-
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
363-
else {
371+
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
372+
return;
373+
} else {
364374
setPosition(options[buttonIndex] as any);
365375
}
366376
}
@@ -384,8 +394,9 @@ function useViewModel() {
384394
useModal: true,
385395
},
386396
(buttonIndex) => {
387-
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
388-
else {
397+
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
398+
return;
399+
} else {
389400
setTextBgStretch(options[buttonIndex] as any);
390401
}
391402
}
@@ -404,8 +415,9 @@ function useViewModel() {
404415
useModal: true,
405416
},
406417
(buttonIndex) => {
407-
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
408-
else {
418+
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
419+
return;
420+
} else {
409421
setTextAlign(options[buttonIndex] as any);
410422
}
411423
}
@@ -514,7 +526,7 @@ function useViewModel() {
514526
},
515527
},
516528
{
517-
text: `text marker normal`,
529+
text: 'text marker normal',
518530
positionOptions: {
519531
position: Position.center,
520532
},
@@ -560,13 +572,14 @@ function useViewModel() {
560572
saveFormat: saveFormat,
561573
});
562574
}
563-
setUri(
575+
const resUri =
564576
saveFormat === ImageFormat.base64
565577
? path
566578
: Platform.OS === 'android'
567579
? 'file:' + path
568-
: path
569-
);
580+
: path;
581+
setUri(resUri);
582+
console.log(resUri);
570583
setLoading(false);
571584
setShow(true);
572585
const stat = await RNBlobUtil.fs.stat(path);
@@ -698,13 +711,14 @@ function useViewModel() {
698711
saveFormat: saveFormat,
699712
});
700713
}
701-
setUri(
714+
const resUri =
702715
saveFormat === ImageFormat.base64
703716
? path
704717
: Platform.OS === 'android'
705718
? 'file:' + path
706-
: path
707-
);
719+
: path;
720+
setUri(resUri);
721+
console.log(resUri);
708722
setShow(true);
709723
setLoading(false);
710724
const stat = await RNBlobUtil.fs.stat(path);

ios/RCTImageMarker/TextOptions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TextOptions: NSObject {
2121
throw NSError(domain: ErrorDomainEnum.PARAMS_REQUIRED.rawValue, code: 0, userInfo: [NSLocalizedDescriptionKey: "text is required"])
2222
}
2323

24-
if let positionOpts = opts["positionOptions"] as? [AnyHashable: Any] {
24+
if let positionOpts = opts["position"] as? [AnyHashable: Any] {
2525
self.X = Utils.handleDynamicToString(v: positionOpts["X"])
2626
self.Y = Utils.handleDynamicToString(v: positionOpts["Y"])
2727
self.position = positionOpts["position"] != nil ? RCTConvert.MarkerPosition(positionOpts["position"]) : .none

src/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export interface TextBackgroundStyle extends Padding {
376376
* @example
377377
* type: TextBackgroundType.stretchX
378378
**/
379-
type: TextBackgroundType | null;
379+
type?: TextBackgroundType | null;
380380
/**
381381
* @description background color
382382
* @example
@@ -452,6 +452,7 @@ export interface TextOptions {
452452
**/
453453
text: string;
454454
/**
455+
* @deprecated since 1.2.4 use position instead
455456
* @description text position options
456457
* @example
457458
* positionOptions: {
@@ -462,6 +463,19 @@ export interface TextOptions {
462463
* }
463464
*/
464465
positionOptions?: PositionOptions;
466+
467+
/**
468+
* @description text position options
469+
* @example
470+
* positionOptions: {
471+
* X: 10,
472+
* Y: 10,
473+
* // or
474+
* // position: Position.center
475+
* }
476+
*/
477+
position?: PositionOptions;
478+
465479
/**
466480
* @description text style
467481
* @example
@@ -921,6 +935,11 @@ class Marker {
921935
};
922936
}
923937

938+
options.watermarkTexts.forEach((item) => {
939+
item.position = item.position || item.positionOptions;
940+
delete item.positionOptions;
941+
});
942+
924943
options.backgroundImage.src = srcObj;
925944
// let mShadowStyle = shadowStyle || {};
926945
// let mTextBackgroundStyle = textBackgroundStyle || {};

0 commit comments

Comments
 (0)