Skip to content

Commit b88158c

Browse files
committed
Add watermark components.
1 parent 5440edc commit b88158c

File tree

7 files changed

+240
-11
lines changed

7 files changed

+240
-11
lines changed

App.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
* @format
1010
* @flow
1111
*/
12+
import * as Sentry from "@sentry/react-native";
1213
import React, { useEffect } from "react";
14+
import { BackHandler, Platform, StatusBar } from "react-native";
15+
import { RootSiblingParent } from "react-native-root-siblings";
1316
import { Provider } from "react-redux";
14-
import { StatusBar, BackHandler, Platform } from "react-native";
1517
import { PersistGate } from "redux-persist/integration/react";
16-
import { RootSiblingParent } from "react-native-root-siblings";
17-
import { HomeStackScreen } from "~/routers/index";
18+
import I18n from "~/common/languages";
1819
import Loading from "~/common/loading";
19-
import configureStore from "~/redux/store/store";
2020
import Toast from "~/components/toast";
21-
import I18n from "~/common/languages";
22-
import * as Sentry from "@sentry/react-native";
21+
import Watermark from "~/components/Watermark";
22+
import configureStore from "~/redux/store/store";
23+
import { HomeStackScreen } from "~/routers/index";
2324
Sentry.init({
2425
dsn: "https://your@your.ingest.us.sentry.io/0000", // replace your sentry DSN.
2526
});
@@ -70,6 +71,7 @@ const APP = function () {
7071
<RootSiblingParent>
7172
<HomeStackScreen />
7273
</RootSiblingParent>
74+
<Watermark />
7375
<Loading />
7476
</PersistGate>
7577
</Provider>

src/components/Watermark.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from 'react';
2+
import { Dimensions, StyleSheet, Text, View } from 'react-native';
3+
import { useSelector } from 'react-redux';
4+
5+
interface WatermarkProps {
6+
text?: string;
7+
opacity?: number;
8+
rotate?: string;
9+
colors?: string[];
10+
}
11+
/**
12+
* A component used to add watermark effects to the application.
13+
* @param {WatermarkProps} props - Properties for the watermark component
14+
* @param {string} props.text
15+
* @param {number} props.opacity
16+
* @param {string} props.rotate
17+
*/
18+
const Watermark: React.FC<WatermarkProps> = (props) => {
19+
// 从Redux状态中获取水印设置
20+
const { text, opacity, rotate, colors } = useSelector(state => state.WatermarkReducer);
21+
const { width, height } = Dimensions.get('window');
22+
const rows = Math.ceil(height / 100);
23+
const cols = Math.ceil(width / 200);
24+
25+
return (
26+
<View style={styles.container}>
27+
{Array.from({ length: rows }).map((_, rowIndex) =>
28+
Array.from({ length: cols }).map((_, colIndex) => (
29+
<View
30+
key={`${rowIndex}-${colIndex}`}
31+
style={[
32+
styles.watermarkItem,
33+
{
34+
top: rowIndex * 100,
35+
left: colIndex * 200,
36+
opacity,
37+
transform: [{ rotate }],
38+
},
39+
]}>
40+
<Text>
41+
{text}
42+
</Text>
43+
</View>
44+
))
45+
)}
46+
</View>
47+
);
48+
};
49+
50+
const styles = StyleSheet.create({
51+
container: {
52+
position: 'absolute',
53+
top: 0,
54+
left: 0,
55+
right: 0,
56+
bottom: 0,
57+
pointerEvents: 'none',
58+
},
59+
watermarkItem: {
60+
position: 'absolute',
61+
justifyContent: 'center',
62+
alignItems: 'center',
63+
},
64+
gradient: {
65+
padding: 8,
66+
borderRadius: 4,
67+
fontSize: 16,
68+
},
69+
});
70+
71+
export default Watermark;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Created by Trae AI on 2023.
3+
* 水印相关的Redux action类型
4+
* Watermark related Redux action types
5+
*/
6+
7+
/**
8+
* 设置水印文本
9+
* Set watermark text
10+
*/
11+
export const SET_WATERMARK_TEXT = "SET_WATERMARK_TEXT";
12+
13+
/**
14+
* 设置水印透明度
15+
* Set watermark opacity
16+
*/
17+
export const SET_WATERMARK_OPACITY = "SET_WATERMARK_OPACITY";
18+
19+
/**
20+
* 设置水印旋转角度
21+
* Set watermark rotation
22+
*/
23+
export const SET_WATERMARK_ROTATE = "SET_WATERMARK_ROTATE";
24+
25+
/**
26+
* 设置水印颜色
27+
* Set watermark colors
28+
*/
29+
export const SET_WATERMARK_COLORS = "SET_WATERMARK_COLORS";
30+
31+
/**
32+
* 重置水印设置为默认值
33+
* Reset watermark settings to default
34+
*/
35+
export const RESET_WATERMARK = "RESET_WATERMARK";
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Created by Trae AI on 2023.
3+
* 水印相关的Redux actions
4+
* Watermark related Redux actions
5+
*/
6+
import {
7+
SET_WATERMARK_TEXT,
8+
SET_WATERMARK_OPACITY,
9+
SET_WATERMARK_ROTATE,
10+
SET_WATERMARK_COLORS,
11+
RESET_WATERMARK
12+
} from './watermarkActionTypes';
13+
14+
/**
15+
* 设置水印文本
16+
* @param text 水印文本
17+
*/
18+
export const setWatermarkText = (text: string) => ({
19+
type: SET_WATERMARK_TEXT,
20+
text
21+
});
22+
23+
/**
24+
* 设置水印透明度
25+
* @param opacity 透明度值 (0-1)
26+
*/
27+
export const setWatermarkOpacity = (opacity: number) => ({
28+
type: SET_WATERMARK_OPACITY,
29+
opacity
30+
});
31+
32+
/**
33+
* 设置水印旋转角度
34+
* @param rotate 旋转角度 (例如: '-45deg')
35+
*/
36+
export const setWatermarkRotate = (rotate: string) => ({
37+
type: SET_WATERMARK_ROTATE,
38+
rotate
39+
});
40+
41+
/**
42+
* 设置水印颜色
43+
* @param colors 颜色数组
44+
*/
45+
export const setWatermarkColors = (colors: string[]) => ({
46+
type: SET_WATERMARK_COLORS,
47+
colors
48+
});
49+
50+
/**
51+
* 重置水印设置为默认值
52+
*/
53+
export const resetWatermark = () => ({
54+
type: RESET_WATERMARK
55+
});

src/redux/reducer/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import UserReducer from "./userReducer";
33
import SettingReducer from "./settingReducer";
44
import HookReducer from "./hookReducer";
55
import InteractionReducer from "./interactionReducer";
6+
import WatermarkReducer from "./watermarkReducer";
67

78
/**
89
* Created by supervons on 2019/08/20.
@@ -14,6 +15,7 @@ const rootReducer = combineReducers({
1415
SettingReducer,
1516
HookReducer,
1617
InteractionReducer,
18+
WatermarkReducer,
1719
});
1820

1921
export default rootReducer;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Created by Trae AI on 2023.
3+
* 水印相关的Redux reducer
4+
* Watermark related Redux reducer
5+
*/
6+
import { handleActions } from "redux-actions";
7+
import {
8+
RESET_WATERMARK,
9+
SET_WATERMARK_COLORS,
10+
SET_WATERMARK_OPACITY,
11+
SET_WATERMARK_ROTATE,
12+
SET_WATERMARK_TEXT,
13+
} from "../action/watermarkActionTypes";
14+
15+
// 初始化水印状态
16+
// Initialize watermark state
17+
const initialState = {
18+
text: "探索 RN",
19+
opacity: 0.2,
20+
rotate: "-45deg",
21+
colors: [],
22+
};
23+
24+
const handler = {};
25+
26+
handler[SET_WATERMARK_TEXT] = (state, action) => {
27+
const { text } = action;
28+
return {
29+
...state,
30+
text,
31+
};
32+
};
33+
34+
handler[SET_WATERMARK_OPACITY] = (state, action) => {
35+
const { opacity } = action;
36+
return {
37+
...state,
38+
opacity,
39+
};
40+
};
41+
42+
handler[SET_WATERMARK_ROTATE] = (state, action) => {
43+
const { rotate } = action;
44+
return {
45+
...state,
46+
rotate,
47+
};
48+
};
49+
50+
handler[SET_WATERMARK_COLORS] = (state, action) => {
51+
const { colors } = action;
52+
return {
53+
...state,
54+
colors,
55+
};
56+
};
57+
58+
handler[RESET_WATERMARK] = () => {
59+
return {
60+
...initialState,
61+
};
62+
};
63+
64+
export default handleActions(handler, initialState);

src/redux/store/store.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
* 对 redux 进行配置,增加 redux-persist 对 whitelist 中的数据作缓存(以 reducer 为单位)
44
* Configure redux and add redux-persist to cache data in whitelist (in reducer)
55
*/
6-
import { createStore, applyMiddleware, compose } from "redux";
7-
import thunk from "redux-thunk";
8-
import rootReducer from "../reducer";
9-
import { persistStore, persistReducer } from "redux-persist";
106
import AsyncStorage from "@react-native-community/async-storage";
7+
import { applyMiddleware, compose, createStore } from "redux";
118
import logger from "redux-logger";
9+
import { persistReducer, persistStore } from "redux-persist";
10+
import thunk from "redux-thunk";
11+
import rootReducer from "../reducer";
1212
const middleWares = [thunk];
1313

1414
const persistConfig = {
1515
key: "root", // 对于数据 key 的定义
1616
storage: AsyncStorage, // 选择的存储引擎
17-
whitelist: ["UserReducer", "SettingReducer"], // 白名单,位于数组中的会被缓存
17+
whitelist: ["UserReducer", "SettingReducer", "WatermarkReducer"], // 白名单,位于数组中的会被缓存
1818
};
1919

2020
// 对 reducers 的封装处理

0 commit comments

Comments
 (0)