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