Skip to content

Commit

Permalink
feat: replace positionOptions to postions
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyDaddy committed Jan 24, 2024
1 parent 6e4bd2e commit 8077617
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data class TextOptions(val options: ReadableMap) {
throw MarkerError(ErrorCode.PARAMS_REQUIRED, "mark text is required")
}
val positionOptions =
if (null != options.getMap("positionOptions")) options.getMap("positionOptions") else null
if (null != options.getMap("position")) options.getMap("position") else null
x = if (positionOptions!!.hasKey("X")) Utils.handleDynamicToString(positionOptions.getDynamic("X")) else null
y = if (positionOptions.hasKey("Y")) Utils.handleDynamicToString(positionOptions.getDynamic("Y")) else null
positionEnum =
Expand Down Expand Up @@ -69,11 +69,12 @@ data class TextOptions(val options: ReadableMap) {
Typeface.DEFAULT
}
}
val textSize = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
style.fontSize,
context.resources.displayMetrics
)
// val textSize = TypedValue.applyDimension(
// TypedValue.COMPLEX_UNIT_SP,
// style.fontSize,
// context.resources.displayMetrics
// )
val textSize = style.fontSize
textPaint.isAntiAlias = true
textPaint.textSize = textSize
Log.i(Constants.IMAGE_MARKER_TAG, "textSize: " + textSize + " fontSize: " + style.fontSize + " displayMetrics: " + context.resources.displayMetrics)
Expand Down Expand Up @@ -121,7 +122,7 @@ data class TextOptions(val options: ReadableMap) {
).toInt()
}
val margin = DEFAULT_MARGIN
var position = Position(margin.toFloat(), margin.toFloat())
var position = Position(margin, margin)
if (positionEnum != null) {
position = Position.getTextPosition(
positionEnum,
Expand Down Expand Up @@ -168,9 +169,9 @@ data class TextOptions(val options: ReadableMap) {
if (style.textBackgroundStyle!!.cornerRadius != null) {
val path = Path()

path.addRoundRect(bgRect, style.textBackgroundStyle!!.cornerRadius!!.radii(bgRect), Path.Direction.CW);
path.addRoundRect(bgRect, style.textBackgroundStyle!!.cornerRadius!!.radii(bgRect), Path.Direction.CW)

canvas.drawPath(path, paint);
canvas.drawPath(path, paint)
} else {
canvas.drawRect(bgRect, paint)
}
Expand All @@ -179,6 +180,7 @@ data class TextOptions(val options: ReadableMap) {
Paint.Align.RIGHT -> x + textWidth
Paint.Align.CENTER -> x + textWidth / 2
Paint.Align.LEFT -> x
else -> x
}
canvas.translate(textX, y)
textLayout.draw(canvas)
Expand Down
52 changes: 33 additions & 19 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,11 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else setBackgroundFormat(options[buttonIndex] as any);
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setBackgroundFormat(options[buttonIndex] as any);
}
}
);
}
Expand All @@ -309,8 +312,11 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else setWaterMarkType(options[buttonIndex] as any);
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setWaterMarkType(options[buttonIndex] as any);
}
}
);
}
Expand All @@ -332,8 +338,11 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else setSaveFormat(options[buttonIndex] as any);
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setSaveFormat(options[buttonIndex] as any);
}
}
);
}
Expand All @@ -359,8 +368,9 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setPosition(options[buttonIndex] as any);
}
}
Expand All @@ -384,8 +394,9 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setTextBgStretch(options[buttonIndex] as any);
}
}
Expand All @@ -404,8 +415,9 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setTextAlign(options[buttonIndex] as any);
}
}
Expand Down Expand Up @@ -514,7 +526,7 @@ function useViewModel() {
},
},
{
text: `text marker normal`,
text: 'text marker normal',
positionOptions: {
position: Position.center,
},
Expand Down Expand Up @@ -560,13 +572,14 @@ function useViewModel() {
saveFormat: saveFormat,
});
}
setUri(
const resUri =
saveFormat === ImageFormat.base64
? path
: Platform.OS === 'android'
? 'file:' + path
: path
);
: path;
setUri(resUri);
console.log(resUri);
setLoading(false);
setShow(true);
const stat = await RNBlobUtil.fs.stat(path);
Expand Down Expand Up @@ -698,13 +711,14 @@ function useViewModel() {
saveFormat: saveFormat,
});
}
setUri(
const resUri =
saveFormat === ImageFormat.base64
? path
: Platform.OS === 'android'
? 'file:' + path
: path
);
: path;
setUri(resUri);
console.log(resUri);
setShow(true);
setLoading(false);
const stat = await RNBlobUtil.fs.stat(path);
Expand Down
2 changes: 1 addition & 1 deletion ios/RCTImageMarker/TextOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TextOptions: NSObject {
throw NSError(domain: ErrorDomainEnum.PARAMS_REQUIRED.rawValue, code: 0, userInfo: [NSLocalizedDescriptionKey: "text is required"])
}

if let positionOpts = opts["positionOptions"] as? [AnyHashable: Any] {
if let positionOpts = opts["position"] as? [AnyHashable: Any] {
self.X = Utils.handleDynamicToString(v: positionOpts["X"])
self.Y = Utils.handleDynamicToString(v: positionOpts["Y"])
self.position = positionOpts["position"] != nil ? RCTConvert.MarkerPosition(positionOpts["position"]) : .none
Expand Down
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export interface TextBackgroundStyle extends Padding {
* @example
* type: TextBackgroundType.stretchX
**/
type: TextBackgroundType | null;
type?: TextBackgroundType | null;
/**
* @description background color
* @example
Expand Down Expand Up @@ -452,6 +452,7 @@ export interface TextOptions {
**/
text: string;
/**
* @deprecated since 1.2.4 use position instead
* @description text position options
* @example
* positionOptions: {
Expand All @@ -462,6 +463,19 @@ export interface TextOptions {
* }
*/
positionOptions?: PositionOptions;

/**
* @description text position options
* @example
* positionOptions: {
* X: 10,
* Y: 10,
* // or
* // position: Position.center
* }
*/
position?: PositionOptions;

/**
* @description text style
* @example
Expand Down Expand Up @@ -922,6 +936,11 @@ class Marker {
};
}

options.watermarkTexts.forEach((item) => {
item.position = item.position || item.positionOptions;
delete item.positionOptions;
});

options.backgroundImage.src = srcObj;
// let mShadowStyle = shadowStyle || {};
// let mTextBackgroundStyle = textBackgroundStyle || {};
Expand Down

0 comments on commit 8077617

Please sign in to comment.