From 97407cffc0a2cd8f583317ee95873d6277ae9e0d Mon Sep 17 00:00:00 2001 From: Mohamadzadeh <7796733@gmail.com> Date: Mon, 19 Oct 2026 13:30:49 +0330 Subject: [PATCH] update utils --- README.md | 10 +++- ...ion-DjllHYU8.js => Pagination-AcPxxOEh.js} | 2 +- dist/chunks/numbers-CnO9mrbW.js | 18 ++++++ dist/index.js | 10 +++- dist/pagination.js | 46 +++++++-------- dist/table.js | 56 +++++++++---------- dist/utils.d.ts | 21 +++++++ dist/utils.js | 45 ++++++++++----- src/utils/images.js | 8 +++ src/utils/index.d.ts | 21 +++++++ src/utils/index.js | 3 + src/utils/scrollbar.js | 21 +++++++ src/utils/text.js | 4 ++ 13 files changed, 194 insertions(+), 71 deletions(-) rename dist/chunks/{Pagination-DjllHYU8.js => Pagination-AcPxxOEh.js} (98%) create mode 100644 dist/chunks/numbers-CnO9mrbW.js create mode 100644 src/utils/images.js create mode 100644 src/utils/scrollbar.js create mode 100644 src/utils/text.js diff --git a/README.md b/README.md index ccd46c6..5d50b71 100644 --- a/README.md +++ b/README.md @@ -61,11 +61,19 @@ yarn upgrade uikit # utils ```js -import { toFaDigits, toEnDigits, toFaNumber } from "uikit/utils"; +import { toFaDigits, toEnDigits, toFaNumber, processImage, truncateText, thinScrollbar } from "uikit/utils"; toFaDigits(123456); toEnDigits("۱۲۳۴۵۶"); toFaNumber(123456); + +processImage("https://example.com/image.jpg"); +processImage("https://example.com/image.jpg", { w: 300, h: 200, q: 70, webp: true, g: "sm" }); + +truncateText("این یک متن طولانی برای تست است", 12); // "این یک متن ط..." + +// در Chakra UI، بدون dependency اضافه: +// ... ``` or diff --git a/dist/chunks/Pagination-DjllHYU8.js b/dist/chunks/Pagination-AcPxxOEh.js similarity index 98% rename from dist/chunks/Pagination-DjllHYU8.js rename to dist/chunks/Pagination-AcPxxOEh.js index 380cb14..9eeb163 100644 --- a/dist/chunks/Pagination-DjllHYU8.js +++ b/dist/chunks/Pagination-AcPxxOEh.js @@ -1,7 +1,7 @@ import { jsxs as a, jsx as n } from "react/jsx-runtime"; import { forwardRef as D, useState as k, useRef as L, useEffect as y } from "react"; import { HStack as v, Text as u, IconButton as B, Button as S, Input as W } from "@chakra-ui/react"; -import { toFaDigits as t, toEnDigits as C } from "../utils.js"; +import { t, a as C } from "./numbers-CnO9mrbW.js"; const A = D( ({ size: i = 24, title: o, children: h, ...l }, r) => /* @__PURE__ */ a( "svg", diff --git a/dist/chunks/numbers-CnO9mrbW.js b/dist/chunks/numbers-CnO9mrbW.js new file mode 100644 index 0000000..d4b89fd --- /dev/null +++ b/dist/chunks/numbers-CnO9mrbW.js @@ -0,0 +1,18 @@ +const t = (n) => { + if (!(n == null || n === "")) + return String(n).replace(/\d/g, (r) => "۰۱۲۳۴۵۶۷۸۹"[Number(r)]); +}, e = (n) => { + if (!(n == null || n === "")) + return String(n).replace( + /[۰-۹]/g, + (r) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(r)) + ); +}, i = (n) => { + if (!(n == null || n === "")) + return new Intl.NumberFormat("fa-IR").format(Number(n)); +}; +export { + e as a, + i as b, + t +}; diff --git a/dist/index.js b/dist/index.js index 24e5819..2f4c15d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,6 +1,10 @@ -import { toEnDigits as i, toFaDigits as r, toFaNumber as a } from "./utils.js"; +import { a as o, t as r, b as e } from "./chunks/numbers-CnO9mrbW.js"; +import { processImage as i, thinScrollbar as m, truncateText as b } from "./utils.js"; export { - i as toEnDigits, + i as processImage, + m as thinScrollbar, + o as toEnDigits, r as toFaDigits, - a as toFaNumber + e as toFaNumber, + b as truncateText }; diff --git a/dist/pagination.js b/dist/pagination.js index 2f9bc84..9f60236 100644 --- a/dist/pagination.js +++ b/dist/pagination.js @@ -1,14 +1,14 @@ import { jsxs as A, jsx as s } from "react/jsx-runtime"; import { useState as B, useRef as g, useEffect as v, useCallback as E } from "react"; import { HStack as y, IconButton as I, Button as N, Input as j, Text as M } from "@chakra-ui/react"; -import { toFaDigits as f, toEnDigits as D } from "./utils.js"; -import { C as z, a as S } from "./chunks/Pagination-DjllHYU8.js"; -import { P as J } from "./chunks/Pagination-DjllHYU8.js"; +import { t as f, a as D } from "./chunks/numbers-CnO9mrbW.js"; +import { C as z, a as S } from "./chunks/Pagination-AcPxxOEh.js"; +import { P as J } from "./chunks/Pagination-AcPxxOEh.js"; const K = ({ currentPage: e, totalPages: t, totalCount: k, - onPageChange: a + onPageChange: p }) => { const [r, n] = B(f(e)), [d, c] = B(!1), [b, o] = B(null), x = g(null), h = [.../* @__PURE__ */ new Set([1, e, t])].filter( (i) => i >= 1 && i <= t @@ -26,7 +26,7 @@ const K = ({ const m = setTimeout(() => { var C; const u = Math.min(Math.max(i, 1), t); - u !== e ? (o(u), n(f(u)), a(u)) : (c(!1), n(f(e))), (C = x.current) == null || C.blur(); + u !== e ? (o(u), n(f(u)), p(u)) : (c(!1), n(f(e))), (C = x.current) == null || C.blur(); }, 1e3); return () => clearTimeout(m); }, [ @@ -35,7 +35,7 @@ const K = ({ b, t, e, - a + p ]); const w = () => { var u, C; @@ -45,7 +45,7 @@ const K = ({ return; } const m = Math.min(Math.max(i, 1), t); - m !== e ? (o(m), n(f(m)), a(m)) : (n(f(e)), c(!1), o(null)), (C = x.current) == null || C.blur(); + m !== e ? (o(m), n(f(m)), p(m)) : (n(f(e)), c(!1), o(null)), (C = x.current) == null || C.blur(); }; return t === 0 || !k ? null : /* @__PURE__ */ A( y, @@ -67,7 +67,7 @@ const K = ({ bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, - onClick: () => a(e - 1), + onClick: () => p(e - 1), isDisabled: e === 1, icon: /* @__PURE__ */ s(z, {}), "aria-label": "صفحه قبلی" @@ -81,7 +81,7 @@ const K = ({ bg: e === i ? "#4B49AC" : "#e6e6f1", color: e === i ? "#e6e6f1" : "#4B49AC", _hover: { bg: e === i ? "#4B49AC" : "#cbcbe8" }, - onClick: () => a(i), + onClick: () => p(i), variant: "soft", children: f(i) }, @@ -94,7 +94,7 @@ const K = ({ bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, - onClick: () => a(e + 1), + onClick: () => p(e + 1), isDisabled: e === t, icon: /* @__PURE__ */ s(S, {}), "aria-label": "صفحه بعدی" @@ -141,14 +141,14 @@ const K = ({ currentPage: e, totalPages: t, totalCount: k, - onPageChange: a + onPageChange: p }) => { const [r, n] = B(""), [d, c] = B(!1), [b, o] = B(null), x = g(null), h = b ?? e, w = E( (l) => { - const p = Math.min(Math.max(l, 1), t); - c(!1), n(""), o(null), p !== e && a(p); + const a = Math.min(Math.max(l, 1), t); + c(!1), n(""), o(null), a !== e && p(a); }, - [e, t, a] + [e, t, p] ), i = E(() => { if (!r) { c(!1), n(""), o(null); @@ -159,9 +159,9 @@ const K = ({ c(!1), n(""), o(null); return; } - const p = Math.min(Math.max(l, 1), t); - c(!1), n(""), p !== e ? (o(p), a(p)) : o(null); - }, [r, e, t, a]); + const a = Math.min(Math.max(l, 1), t); + c(!1), n(""), a !== e ? (o(a), p(a)) : o(null); + }, [r, e, t, p]); v(() => { var l; d && ((l = x.current) == null || l.focus()); @@ -176,14 +176,14 @@ const K = ({ }, [r, d, i]); const m = () => { o(null), n(""), c(!0); - }, u = (l, p = !1) => /* @__PURE__ */ s( + }, u = (l, a = !1) => /* @__PURE__ */ s( N, { size: "md", - bg: p ? "#4B49AC" : "#e6e6f1", - color: p ? "#e6e6f1" : "#4B49AC", - _hover: { bg: p ? "#4B49AC" : "#cbcbe8" }, - onClick: p ? m : () => w(l), + bg: a ? "#4B49AC" : "#e6e6f1", + color: a ? "#e6e6f1" : "#4B49AC", + _hover: { bg: a ? "#4B49AC" : "#cbcbe8" }, + onClick: a ? m : () => w(l), variant: "soft", children: f(l) }, @@ -227,7 +227,7 @@ const K = ({ w: "48px", value: r, onChange: (l) => { - const p = l.target.value, V = D(p).replace(/\D/g, ""); + const a = l.target.value, V = D(a).replace(/\D/g, ""); n(f(V)); }, onBlur: () => { diff --git a/dist/table.js b/dist/table.js index 3dd98bb..c936278 100644 --- a/dist/table.js +++ b/dist/table.js @@ -1,8 +1,8 @@ import { jsx as r, jsxs as X, Fragment as ne } from "react/jsx-runtime"; import { VStack as oe, Box as _, Tbody as re, Tr as H, Td as F, Text as R, Thead as le, Th as ce, Table as se, Checkbox as W, HStack as Y, Spacer as ie } from "@chakra-ui/react"; -import { P as ue } from "./chunks/Pagination-DjllHYU8.js"; -import { toFaDigits as q } from "./utils.js"; -import { useReactTable as ae, getCoreRowModel as de, flexRender as J } from "@tanstack/react-table"; +import { P as ae } from "./chunks/Pagination-AcPxxOEh.js"; +import { t as q } from "./chunks/numbers-CnO9mrbW.js"; +import { useReactTable as ue, getCoreRowModel as de, flexRender as J } from "@tanstack/react-table"; import { useMemo as P, useCallback as p, useState as me, useRef as fe, useEffect as ge, createContext as he, useContext as be } from "react"; const K = he(null); function C() { @@ -18,7 +18,7 @@ function xe({ page: s = 1, size: c = 10, pagination: l, - onPageChange: a, + onPageChange: u, getRowId: g = (d) => String(d == null ? void 0 : d.id), enableSelection: b = !1, showIndex: S = !0, @@ -31,15 +31,15 @@ function xe({ pageSize: h }), [d, h]), Q = p( (n) => { - const u = (typeof n == "function" ? n(z) : n).pageIndex + 1; - u !== d && (a == null || a(u)); + const a = (typeof n == "function" ? n(z) : n).pageIndex + 1; + a !== d && (u == null || u(a)); }, - [z, d, a] + [z, d, u] ), [A, U] = me(L), m = p( (n) => { U((i) => { - const u = typeof n == "function" ? n(i) : n; - return k == null || k(u), u; + const a = typeof n == "function" ? n(i) : n; + return k == null || k(a), a; }); }, [k] @@ -60,7 +60,7 @@ function xe({ m([]); }, [m]), B = p( (n) => { - m((i) => f.includes(n) ? i.includes(n) ? i.filter((u) => u !== n) : [...i, n] : i); + m((i) => f.includes(n) ? i.includes(n) ? i.filter((a) => a !== n) : [...i, n] : i); }, [f, m] ), Z = B, I = p(() => { @@ -81,20 +81,20 @@ function xe({ return b && n.push({ id: "__selection", header: ({ table: i }) => { - const u = i.options.meta; + const a = i.options.meta; return /* @__PURE__ */ r( W, { - isChecked: u.isAllSelected, - isIndeterminate: u.isIndeterminate, - onChange: (x) => u.toggleSelectAll(x.target.checked), + isChecked: a.isAllSelected, + isIndeterminate: a.isIndeterminate, + onChange: (x) => a.toggleSelectAll(x.target.checked), colorScheme: "purple", size: "md" } ); }, - cell: ({ row: i, table: u }) => { - const x = u.options.meta, D = x.getRowId(i.original); + cell: ({ row: i, table: a }) => { + const x = a.options.meta, D = x.getRowId(i.original); return /* @__PURE__ */ r( W, { @@ -109,14 +109,14 @@ function xe({ }), S && n.push({ id: "__index", header: "ردیف", - cell: ({ row: i, table: u }) => { - const { pageIndex: x, pageSize: D } = u.getState().pagination, te = x * D + i.index + 1; + cell: ({ row: i, table: a }) => { + const { pageIndex: x, pageSize: D } = a.getState().pagination, te = x * D + i.index + 1; return /* @__PURE__ */ r(R, { fontWeight: "bold", sx: { fontVariantNumeric: "normal" }, children: q(te) }); }, meta: { isCentered: !0 } }), [...n, ...o]; }, [o, b, S]), ee = { - table: ae({ + table: ue({ data: t, columns: E, getCoreRowModel: de(), @@ -149,7 +149,7 @@ function xe({ page: d, size: h, pagination: l, - onPageChange: a, + onPageChange: u, paginationState: z, totalCount: y, totalPages: v, @@ -193,14 +193,14 @@ function ke({ children: e = "انتخاب همه", ...t }) { enableSelection: s, isAllSelected: c, isIndeterminate: l, - toggleSelectAll: a + toggleSelectAll: u } = C(); return !s || o.length === 0 ? null : /* @__PURE__ */ r( W, { isChecked: c, isIndeterminate: l, - onChange: (g) => a(g.target.checked), + onChange: (g) => u(g.target.checked), colorScheme: "purple", size: "lg", ...t, @@ -322,7 +322,7 @@ function De({ emptyText: e = "موردی وجود ندارد" }) { _hover: { bg: "blue.50" }, transition: "0.2s", children: c.getVisibleCells().map((l) => { - var a; + var u; return /* @__PURE__ */ r( F, { @@ -331,7 +331,7 @@ function De({ emptyText: e = "موردی وجود ندارد" }) { color: "gray.700", borderRightWidth: "1px", borderRightColor: "gray.200", - textAlign: (a = l.column.columnDef.meta) != null && a.isCentered ? "center" : "start", + textAlign: (u = l.column.columnDef.meta) != null && u.isCentered ? "center" : "start", verticalAlign: "middle", children: l.column.columnDef.cell ? J(l.column.columnDef.cell, l.getContext()) : l.getValue() }, @@ -346,20 +346,20 @@ function Pe({ pagination: e, onPageChange: t }) { - const o = C(), { table: s } = o, c = e ?? o.pagination, l = (c == null ? void 0 : c.total) ?? (c == null ? void 0 : c.totalCount) ?? o.totalCount ?? 0, a = (c == null ? void 0 : c.total_pages) ?? (c == null ? void 0 : c.totalPages) ?? o.totalPages ?? s.getPageCount() ?? 0, g = s.getState().pagination.pageIndex + 1, b = (S) => { + const o = C(), { table: s } = o, c = e ?? o.pagination, l = (c == null ? void 0 : c.total) ?? (c == null ? void 0 : c.totalCount) ?? o.totalCount ?? 0, u = (c == null ? void 0 : c.total_pages) ?? (c == null ? void 0 : c.totalPages) ?? o.totalPages ?? s.getPageCount() ?? 0, g = s.getState().pagination.pageIndex + 1, b = (S) => { if (t) { t(S); return; } s.setPageIndex(S - 1); }; - return !o.onPageChange && !t || a <= 0 ? null : /* @__PURE__ */ r(_, { mt: 1, flexShrink: 0, children: /* @__PURE__ */ r( - ue, + return !o.onPageChange && !t || u <= 0 ? null : /* @__PURE__ */ r(_, { mt: 1, flexShrink: 0, children: /* @__PURE__ */ r( + ae, { currentPage: g, onPageChange: b, totalCount: l, - totalPages: a + totalPages: u } ) }); } diff --git a/dist/utils.d.ts b/dist/utils.d.ts index 6965668..9ca7cb8 100644 --- a/dist/utils.d.ts +++ b/dist/utils.d.ts @@ -1,3 +1,24 @@ export function toFaNumber(value: string | number): string; export function toFaDigits(value: string | number): string; export function toEnDigits(value: string | number): string; + +export interface ProcessImageOptions { + w?: number; + h?: number; + q?: number; + webp?: boolean; + g?: string; +} + +export function processImage(url?: string | null, options?: ProcessImageOptions): string; + +export function truncateText(str: string | null | undefined, length: number): string; + +export const thinScrollbar: { + scrollbarWidth: "thin"; + scrollbarColor: string; + "&::-webkit-scrollbar": { width: string; height: string }; + "&::-webkit-scrollbar-track": { background: string }; + "&::-webkit-scrollbar-thumb": { backgroundColor: string; borderRadius: string }; + "&::-webkit-scrollbar-button": { display: "none"; width: number; height: number }; +}; diff --git a/dist/utils.js b/dist/utils.js index 0fd5e21..507ac7c 100644 --- a/dist/utils.js +++ b/dist/utils.js @@ -1,18 +1,33 @@ -const t = (n) => { - if (!(n == null || n === "")) - return String(n).replace(/\d/g, (r) => "۰۱۲۳۴۵۶۷۸۹"[Number(r)]); -}, e = (n) => { - if (!(n == null || n === "")) - return String(n).replace( - /[۰-۹]/g, - (r) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(r)) - ); -}, i = (n) => { - if (!(n == null || n === "")) - return new Intl.NumberFormat("fa-IR").format(Number(n)); +import { a as u, t as g, b as h } from "./chunks/numbers-CnO9mrbW.js"; +const b = (r, { w: t, h: a, q: e = 70, webp: i = !0, g: o = "sm" } = {}) => r ? `https://img.d.aiengines.ir/preset:sharp${t || a ? `/rs:auto:${t || 0}:${a || 0}:0` : ""}/q:${e}/g:${o}/plain/${r}${i ? "@webp" : ""}` : ""; +function s(r, t) { + return (r == null ? void 0 : r.length) < t ? r : (r == null ? void 0 : r.slice(0, t)) + "..."; +} +const l = { + scrollbarWidth: "thin", + scrollbarColor: "rgba(0,0,0,0.22) transparent", + "&::-webkit-scrollbar": { + width: "4px", + height: "4px" + }, + "&::-webkit-scrollbar-track": { + background: "transparent" + }, + "&::-webkit-scrollbar-thumb": { + backgroundColor: "rgba(0,0,0,0.22)", + borderRadius: "999px" + }, + "&::-webkit-scrollbar-button": { + display: "none", + width: 0, + height: 0 + } }; export { - e as toEnDigits, - t as toFaDigits, - i as toFaNumber + b as processImage, + l as thinScrollbar, + u as toEnDigits, + g as toFaDigits, + h as toFaNumber, + s as truncateText }; diff --git a/src/utils/images.js b/src/utils/images.js new file mode 100644 index 0000000..0736c5b --- /dev/null +++ b/src/utils/images.js @@ -0,0 +1,8 @@ +export const processImage = ( + url, + { w, h, q = 70, webp = true, g = "sm" } = {}, +) => { + if (!url) return ""; + const resizePart = w || h ? `/rs:auto:${w || 0}:${h || 0}:0` : ""; + return `https://img.d.aiengines.ir/preset:sharp${resizePart}/q:${q}/g:${g}/plain/${url}${webp ? "@webp" : ""}`; +}; diff --git a/src/utils/index.d.ts b/src/utils/index.d.ts index 6965668..9ca7cb8 100644 --- a/src/utils/index.d.ts +++ b/src/utils/index.d.ts @@ -1,3 +1,24 @@ export function toFaNumber(value: string | number): string; export function toFaDigits(value: string | number): string; export function toEnDigits(value: string | number): string; + +export interface ProcessImageOptions { + w?: number; + h?: number; + q?: number; + webp?: boolean; + g?: string; +} + +export function processImage(url?: string | null, options?: ProcessImageOptions): string; + +export function truncateText(str: string | null | undefined, length: number): string; + +export const thinScrollbar: { + scrollbarWidth: "thin"; + scrollbarColor: string; + "&::-webkit-scrollbar": { width: string; height: string }; + "&::-webkit-scrollbar-track": { background: string }; + "&::-webkit-scrollbar-thumb": { backgroundColor: string; borderRadius: string }; + "&::-webkit-scrollbar-button": { display: "none"; width: number; height: number }; +}; diff --git a/src/utils/index.js b/src/utils/index.js index f98a470..e20ca21 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1 +1,4 @@ export { toFaNumber, toFaDigits, toEnDigits } from "./numbers.js"; +export { processImage } from "./images.js"; +export { default as truncateText } from "./text.js"; +export { thinScrollbar } from "./scrollbar.js"; diff --git a/src/utils/scrollbar.js b/src/utils/scrollbar.js new file mode 100644 index 0000000..8651510 --- /dev/null +++ b/src/utils/scrollbar.js @@ -0,0 +1,21 @@ +export const thinScrollbar = { + scrollbarWidth: "thin", + scrollbarColor: "rgba(0,0,0,0.22) transparent", + + "&::-webkit-scrollbar": { + width: "4px", + height: "4px", + }, + "&::-webkit-scrollbar-track": { + background: "transparent", + }, + "&::-webkit-scrollbar-thumb": { + backgroundColor: "rgba(0,0,0,0.22)", + borderRadius: "999px", + }, + "&::-webkit-scrollbar-button": { + display: "none", + width: 0, + height: 0, + }, +}; diff --git a/src/utils/text.js b/src/utils/text.js new file mode 100644 index 0000000..5ec42b2 --- /dev/null +++ b/src/utils/text.js @@ -0,0 +1,4 @@ +export default function truncateText(str, length) { + if (str?.length < length) return str; + return str?.slice(0, length) + "..."; +}