sales-frontend-components 0.0.48 → 0.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -984,5 +984,229 @@ function useCamera({
984
984
  };
985
985
  }
986
986
 
987
- export { Attachment, DataTable, FormDatePicker, FormSegmentGroup, FormSignature, FormTextField, StepIndicator, resize, useCamera, useFormSignature, useTable };
987
+ const HISTORY_SIZE = 100;
988
+ const DEFAULT_DOWNLOAD_PROPS = {
989
+ fileExtendsion: "png",
990
+ fileName: "image",
991
+ transparent: true,
992
+ backgroundColor: "white",
993
+ width: 500,
994
+ height: 500
995
+ };
996
+ function usePaint(paintProps = {}) {
997
+ const { pen = { strokeWeight: 5, strokeColor: "black" }, onChange, onStart, onEnd } = paintProps;
998
+ const canvasRef = useRef(null);
999
+ const contextRef = useRef(null);
1000
+ const [isPainting, setIsPainting] = useState(false);
1001
+ const [history, setHistory] = useState([]);
1002
+ const [historyIndex, setHistoryIndex] = useState(-1);
1003
+ const saveState = useCallback(() => {
1004
+ const canvas = canvasRef.current;
1005
+ if (!canvas) {
1006
+ return;
1007
+ }
1008
+ const dataUrl = canvas.toDataURL();
1009
+ const newHistory = history.slice(0, historyIndex + 1);
1010
+ if (newHistory.length >= HISTORY_SIZE) {
1011
+ newHistory.shift();
1012
+ }
1013
+ newHistory.push(dataUrl);
1014
+ setHistory(newHistory);
1015
+ setHistoryIndex(newHistory.length - 1);
1016
+ onChange && onChange();
1017
+ }, [history, historyIndex, onChange]);
1018
+ const restoreState = useCallback(
1019
+ (index) => {
1020
+ const canvas = canvasRef.current;
1021
+ const context = contextRef.current;
1022
+ if (!canvas || !context || !history[index]) {
1023
+ return;
1024
+ }
1025
+ const dataUrl = history[index];
1026
+ const img = new Image();
1027
+ img.onload = () => {
1028
+ context.clearRect(0, 0, canvas.width, canvas.height);
1029
+ context.drawImage(img, 0, 0);
1030
+ };
1031
+ img.src = dataUrl;
1032
+ onChange && onChange();
1033
+ },
1034
+ [history, onChange]
1035
+ );
1036
+ useEffect(() => {
1037
+ const canvas = canvasRef.current;
1038
+ if (!canvas) {
1039
+ return;
1040
+ }
1041
+ const context = canvas.getContext("2d", { willReadFrequently: true });
1042
+ if (!context) {
1043
+ return;
1044
+ }
1045
+ contextRef.current = context;
1046
+ const initialDataUrl = canvas.toDataURL();
1047
+ setHistory([initialDataUrl]);
1048
+ setHistoryIndex(0);
1049
+ }, []);
1050
+ useEffect(() => {
1051
+ const context = contextRef.current;
1052
+ if (context) {
1053
+ context.lineWidth = pen.strokeWeight;
1054
+ context.strokeStyle = pen.strokeColor;
1055
+ }
1056
+ }, [pen]);
1057
+ const startPainting = useCallback(
1058
+ (event) => {
1059
+ const context = contextRef.current;
1060
+ if (!context) {
1061
+ return;
1062
+ }
1063
+ onStart && onStart();
1064
+ const { offsetX, offsetY } = event;
1065
+ context.beginPath();
1066
+ context.moveTo(offsetX, offsetY);
1067
+ setIsPainting(true);
1068
+ },
1069
+ [onStart]
1070
+ );
1071
+ const stopPainting = useCallback(() => {
1072
+ const context = contextRef.current;
1073
+ if (!context || !isPainting) {
1074
+ return;
1075
+ }
1076
+ context.closePath();
1077
+ setIsPainting(false);
1078
+ onEnd && onEnd();
1079
+ saveState();
1080
+ }, [isPainting, onEnd, saveState]);
1081
+ const paint = useCallback(
1082
+ (event) => {
1083
+ if (!isPainting || !contextRef.current) {
1084
+ return;
1085
+ }
1086
+ const { offsetX, offsetY } = event;
1087
+ contextRef.current.lineTo(offsetX, offsetY);
1088
+ contextRef.current.stroke();
1089
+ },
1090
+ [isPainting]
1091
+ );
1092
+ const handleMouseEnter = useCallback(
1093
+ (event) => {
1094
+ if (event.buttons === 1 && !isPainting) {
1095
+ startPainting(event);
1096
+ }
1097
+ },
1098
+ [isPainting, startPainting]
1099
+ );
1100
+ useEffect(() => {
1101
+ const canvas = canvasRef.current;
1102
+ if (!canvas) {
1103
+ return;
1104
+ }
1105
+ canvas.addEventListener("mousedown", startPainting);
1106
+ canvas.addEventListener("mouseup", stopPainting);
1107
+ canvas.addEventListener("mousemove", paint);
1108
+ canvas.addEventListener("mouseleave", stopPainting);
1109
+ canvas.addEventListener("mouseenter", handleMouseEnter);
1110
+ return () => {
1111
+ canvas.removeEventListener("mousedown", startPainting);
1112
+ canvas.removeEventListener("mouseup", stopPainting);
1113
+ canvas.removeEventListener("mousemove", paint);
1114
+ canvas.removeEventListener("mouseleave", stopPainting);
1115
+ canvas.removeEventListener("mouseenter", handleMouseEnter);
1116
+ };
1117
+ }, [startPainting, stopPainting, paint, handleMouseEnter]);
1118
+ const clear = () => {
1119
+ const canvas = canvasRef.current;
1120
+ const context = contextRef.current;
1121
+ if (canvas && context) {
1122
+ context.clearRect(0, 0, canvas.width, canvas.height);
1123
+ const initialDataUrl = canvas.toDataURL();
1124
+ setHistory([initialDataUrl]);
1125
+ setHistoryIndex(0);
1126
+ onChange && onChange();
1127
+ }
1128
+ };
1129
+ const undo = () => {
1130
+ if (historyIndex > 0) {
1131
+ const newIndex = historyIndex - 1;
1132
+ setHistoryIndex(newIndex);
1133
+ restoreState(newIndex);
1134
+ }
1135
+ };
1136
+ const redo = () => {
1137
+ if (historyIndex < history.length - 1) {
1138
+ const newIndex = historyIndex + 1;
1139
+ setHistoryIndex(newIndex);
1140
+ restoreState(newIndex);
1141
+ }
1142
+ };
1143
+ const loadImage = (base64String) => {
1144
+ const canvas = canvasRef.current;
1145
+ const context = contextRef.current;
1146
+ if (!canvas || !context) {
1147
+ return;
1148
+ }
1149
+ const img = new Image();
1150
+ img.onload = () => {
1151
+ context.clearRect(0, 0, canvas.width, canvas.height);
1152
+ context.drawImage(img, 0, 0, canvas.width, canvas.height);
1153
+ saveState();
1154
+ };
1155
+ img.src = base64String;
1156
+ };
1157
+ const getBase64String = (imageProps) => {
1158
+ const props = { ...DEFAULT_DOWNLOAD_PROPS, ...imageProps };
1159
+ const { fileExtendsion, transparent, backgroundColor, width, height } = props;
1160
+ const originalCanvas = canvasRef.current;
1161
+ if (!originalCanvas) {
1162
+ return;
1163
+ }
1164
+ const tempCanvas = document.createElement("canvas");
1165
+ tempCanvas.width = width;
1166
+ tempCanvas.height = height;
1167
+ const tempCtx = tempCanvas.getContext("2d");
1168
+ if (tempCtx) {
1169
+ if (!transparent) {
1170
+ tempCtx.fillStyle = backgroundColor || "white";
1171
+ tempCtx.fillRect(0, 0, width, height);
1172
+ }
1173
+ tempCtx.drawImage(originalCanvas, 0, 0, width, height);
1174
+ return tempCanvas.toDataURL(`image/${fileExtendsion}`);
1175
+ }
1176
+ };
1177
+ const download = (downloadProps) => {
1178
+ const props = { ...DEFAULT_DOWNLOAD_PROPS, ...downloadProps };
1179
+ const { fileName, fileExtendsion, transparent, backgroundColor, width, height } = props;
1180
+ const originalCanvas = canvasRef.current;
1181
+ if (!originalCanvas) {
1182
+ return;
1183
+ }
1184
+ const tempCanvas = document.createElement("canvas");
1185
+ tempCanvas.width = width;
1186
+ tempCanvas.height = height;
1187
+ const tempCtx = tempCanvas.getContext("2d");
1188
+ if (tempCtx) {
1189
+ if (!transparent) {
1190
+ tempCtx.fillStyle = backgroundColor || "white";
1191
+ tempCtx.fillRect(0, 0, width, height);
1192
+ }
1193
+ tempCtx.drawImage(originalCanvas, 0, 0, width, height);
1194
+ const link = document.createElement("a");
1195
+ link.download = `${fileName}.${fileExtendsion}`;
1196
+ link.href = tempCanvas.toDataURL(`image/${fileExtendsion}`);
1197
+ link.click();
1198
+ }
1199
+ };
1200
+ return {
1201
+ clear,
1202
+ undo,
1203
+ redo,
1204
+ loadImage,
1205
+ download,
1206
+ canvasRef,
1207
+ getBase64String
1208
+ };
1209
+ }
1210
+
1211
+ export { Attachment, DataTable, FormDatePicker, FormSegmentGroup, FormSignature, FormTextField, StepIndicator, resize, useCamera, useFormSignature, usePaint, useTable };
988
1212
  //# sourceMappingURL=index.esm.js.map