From 2bf3f13959132f064c7ecf13ad0ab6f62b70c1cb Mon Sep 17 00:00:00 2001 From: Sanchit Agarwal Date: Sat, 31 Aug 2024 07:57:18 +0000 Subject: [PATCH] feat: add support for custom currency symbols --- packages/core/src/context.ts | 2 ++ packages/core/src/locale/en.ts | 14 ++++++++--- packages/core/src/locale/es.ts | 10 +++++--- packages/core/src/locale/hi.ts | 10 +++++--- packages/core/src/locale/zh.ts | 10 +++++--- packages/core/src/locale/zh_tw.ts | 10 +++++--- packages/core/src/modules/ssf.js | 2 +- packages/core/src/modules/toolbar.ts | 2 +- packages/core/src/settings.ts | 2 ++ packages/react/src/components/SVGDefines.tsx | 25 +++++++++++++------ .../react/src/components/Toolbar/index.tsx | 12 +++++---- .../react/src/components/Workbook/index.tsx | 3 ++- 12 files changed, 72 insertions(+), 30 deletions(-) diff --git a/packages/core/src/context.ts b/packages/core/src/context.ts index 23eb1e30..b9bc14d7 100644 --- a/packages/core/src/context.ts +++ b/packages/core/src/context.ts @@ -53,6 +53,7 @@ export type Context = { rangeDialog?: RangeDialogProps; // 坐标选区鼠标选择 // 提醒弹窗 warnDialog?: string; + currency: string; dataVerification?: { selectStatus: boolean; selectRange: []; @@ -236,6 +237,7 @@ export function defaultContext(refs: RefValues): Context { config: {}, // 提醒弹窗 warnDialog: undefined, + currency: "$", rangeDialog: { show: false, rangeTxt: "", diff --git a/packages/core/src/locale/en.ts b/packages/core/src/locale/en.ts index 1549e2dc..04b1923a 100644 --- a/packages/core/src/locale/en.ts +++ b/packages/core/src/locale/en.ts @@ -11226,7 +11226,7 @@ export default { { name: "Chilean Peso", pos: "before", value: "$" }, { name: "CFA Franc BEAC", pos: "before", value: "FCFA" }, ], - defaultFmt: [ + defaultFmt: (currency: string) => [ { text: "Automatic", value: "General", example: "" }, { text: "Plain text", value: "@", example: "" }, { text: "", value: "split", example: "" }, @@ -11234,9 +11234,17 @@ export default { { text: "Percent", value: "#0.00%", example: "12.21%" }, { text: "Scientific", value: "0.00E+00", example: "1.01E+5" }, { text: "", value: "split", example: "" }, - { text: "Accounting", value: "$(0.00)", example: "$(1200.09)" }, + { + text: "Accounting", + value: `${currency}(0.00)`, + example: `${currency}(1200.09)`, + }, - { text: "Currency", value: "$0.00", example: "$1200.09" }, + { + text: "Currency", + value: `${currency}0.00`, + example: `${currency}1200.09`, + }, { text: "", value: "split", example: "" }, { text: "Date", value: "yyyy-MM-dd", example: "2017-11-29" }, diff --git a/packages/core/src/locale/es.ts b/packages/core/src/locale/es.ts index 4811c906..3481cf24 100644 --- a/packages/core/src/locale/es.ts +++ b/packages/core/src/locale/es.ts @@ -11199,7 +11199,7 @@ export default { ChileanPeso: "Chilean Peso", CFAFrancBEAC: "CFA Franc BEAC", }, - defaultFmt: [ + defaultFmt: (currency: string) => [ { text: "Automático", value: "General", example: "" }, { text: "Texto", value: "@", example: "" }, { text: "", value: "split", example: "" }, @@ -11207,9 +11207,13 @@ export default { { text: "Porcentaje", value: "#0.00%", example: "12.21%" }, { text: "Científico", value: "0.00E+00", example: "1.01E+5" }, { text: "", value: "split", example: "" }, - { text: "Contabilidad", value: "¥(0.00)", example: "¥(1200.09)" }, + { + text: "Contabilidad", + value: `${currency}(0.00)`, + example: `${currency}(1200.09)`, + }, - { text: "Moneda", value: "¥0.00", example: "¥1200.09" }, + { text: "Moneda", value: `${currency}0.00`, example: `${currency}1200.09` }, { text: "", value: "split", example: "" }, { text: "Fecha", value: "yyyy-MM-dd", example: "2017-11-29" }, diff --git a/packages/core/src/locale/hi.ts b/packages/core/src/locale/hi.ts index 85254ee3..10fe1741 100644 --- a/packages/core/src/locale/hi.ts +++ b/packages/core/src/locale/hi.ts @@ -11232,7 +11232,7 @@ export default { { name: "चिली पेसो", pos: "before", value: "$" }, { name: "सीएफए फ्रैंक बीईएसी", pos: "before", value: "FCFA" }, ], - defaultFmt: [ + defaultFmt: (currency: string) => [ { text: "स्वचालित", value: "General", example: "" }, { text: "सादा टेक्स्ट", value: "@", example: "" }, { text: "", value: "split", example: "" }, @@ -11240,9 +11240,13 @@ export default { { text: "प्रतिशत", value: "#0.00%", example: "12.21%" }, { text: "वैज्ञानिक", value: "0.00E+00", example: "1.01E+5" }, { text: "", value: "split", example: "" }, - { text: "लेखांकन", value: "$(0.00)", example: "$(1200.09)" }, + { + text: "लेखांकन", + value: `${currency}(0.00)`, + example: `${currency}(1200.09)`, + }, - { text: "मुद्रा", value: "$0.00", example: "$1200.09" }, + { text: "मुद्रा", value: `${currency}0.00`, example: `${currency}1200.09` }, { text: "", value: "split", example: "" }, { text: "दिनांक", value: "yyyy-MM-dd", example: "2017-11-29" }, diff --git a/packages/core/src/locale/zh.ts b/packages/core/src/locale/zh.ts index 4017602f..c06f5298 100644 --- a/packages/core/src/locale/zh.ts +++ b/packages/core/src/locale/zh.ts @@ -11209,7 +11209,7 @@ export default { { name: "智利比索", pos: "before", value: "$" }, { name: "中非金融合作法郎", pos: "before", value: "FCFA" }, ], - defaultFmt: [ + defaultFmt: (currency: string) => [ { text: "自动", value: "General", example: "" }, { text: "纯文本", value: "@", example: "" }, { text: "", value: "split", example: "" }, @@ -11217,10 +11217,14 @@ export default { { text: "百分比", value: "#0.00%", example: "12.21%" }, { text: "科学计数", value: "0.00E+00", example: "1.01E+5" }, { text: "", value: "split", example: "" }, - { text: "会计", value: "¥(0.00)", example: "¥(1200.09)" }, + { + text: "会计", + value: `${currency}(0.00)`, + example: `${currency}(1200.09)`, + }, // { "text": "财务", "value": "(#.####)", "example": "(1200.09)" }, { text: "万元", value: "w", example: "1亿2000万2500" }, - { text: "货币", value: "¥0.00", example: "¥1200.09" }, + { text: "货币", value: `${currency}0.00`, example: `${currency}1200.09` }, // { "text": "货币整数", "value": "¥####", "example": "¥1200" }, { text: "万元2位小数", value: "w0.00", example: "2万2500.55" }, { text: "", value: "split", example: "" }, diff --git a/packages/core/src/locale/zh_tw.ts b/packages/core/src/locale/zh_tw.ts index d5512d09..d5f92c4a 100644 --- a/packages/core/src/locale/zh_tw.ts +++ b/packages/core/src/locale/zh_tw.ts @@ -11176,7 +11176,7 @@ export default { ChileanPeso: "智利比索", CFAFrancBEAC: "中非金融合作法郎", }, - defaultFmt: [ + defaultFmt: (currency: string) => [ { text: "自動", value: "General", example: "" }, { text: "純文字", value: "@", example: "" }, { text: "", value: "split", example: "" }, @@ -11184,10 +11184,14 @@ export default { { text: "百分比", value: "#0.00%", example: "12.21%" }, { text: "科學計數", value: "0.00E+00", example: "1.01E+5" }, { text: "", value: "split", example: "" }, - { text: "會計", value: "¥(0.00)", example: "¥(1200.09)" }, + { + text: "會計", + value: `${currency}(0.00)`, + example: `${currency}(1200.09)`, + }, // { "text": "財務", "value": "(#.####)", "example": "(1200.09)" }, { text: "萬元", value: "w", example: "1亿2000万2500" }, - { text: "貨幣", value: "¥0.00", example: "¥1200.09" }, + { text: "貨幣", value: `${currency}0.00`, example: `${currency}1200.09` }, // { "text": "貨幣整數", "value": "¥####", "example": "¥1200" }, { text: "萬元2位小數", value: "w0.00", example: "2万2500.55" }, { text: "", value: "split", example: "" }, diff --git a/packages/core/src/modules/ssf.js b/packages/core/src/modules/ssf.js index bfb26392..5b9f5a72 100644 --- a/packages/core/src/modules/ssf.js +++ b/packages/core/src/modules/ssf.js @@ -1369,7 +1369,7 @@ const make_ssf = function make_ssf(SSF) { default: // if ("¤฿BsBr₵₡₫ƒFtRs.₭kr£₤Lm₥₦₱PQRSkRp৲৳R$S/.〒₮₩¥NT¥zł₴₪៛руб€$,$-+/():!^&'~{}<>=€acfijklopqrtuvwxzP".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt); if ( - "¤฿BsBr₵₡₫ƒFtRs.₭kr£₤Lm₥₦₱PQRSkRp৲৳R$S/.〒₮₩¥NT¥zł₴₪៛руб€$,$-+/():!^&'~{}<>=€acfijklopqrtuvwxzP$¥LekdinAf$dhAflRial?£BirrKzMOPPGKRsGsB/R$ррlevkrKMzBsPNuFBuKPkrRD$NfkCFA?CVEGMDFrCDHTGNAfLFdjKGSFGGHSRielKCFknKshLSLL£LtRFRONArRfMWKRMMURsMROS/KMDLMTnRC$kr€GELCHFSLLSCRDbSZLSDGSOSSomFCFPTShT$VUVQUGXгрнsomWSTNT$FtDramRpZMWFCFA".indexOf( + "¤฿BsBr₵₡₫ƒFtRs.₭kr£₤Lm₥₦₱PQRSkRp৲৳R$S/.〒₮₩¥NT¥zł₴₪៛руб€₹$,$-+/():!^&'~{}<>=€acfijklopqrtuvwxzP$¥LekdinAf$dhAflRial?£BirrKzMOPPGKRsGsB/R$ррlevkrKMzBsPNuFBuKPkrRD$NfkCFA?CVEGMDFrCDHTGNAfLFdjKGSFGGHSRielKCFknKshLSLL£LtRFRONArRfMWKRMMURsMROS/KMDLMTnRC$kr€GELCHFSLLSCRDbSZLSDGSOSSomFCFPTShT$VUVQUGXгрнsomWSTNT$FtDramRpZMWFCFA".indexOf( c ) === -1 ) diff --git a/packages/core/src/modules/toolbar.ts b/packages/core/src/modules/toolbar.ts index acf54c3a..4753e4c8 100644 --- a/packages/core/src/modules/toolbar.ts +++ b/packages/core/src/modules/toolbar.ts @@ -935,7 +935,7 @@ export function handleCurrencyFormat(ctx: Context, cellInput: HTMLDivElement) { const flowdata = getFlowdata(ctx); if (!flowdata) return; - updateFormat(ctx, cellInput, flowdata, "ct", "¥ #.00"); + updateFormat(ctx, cellInput, flowdata, "ct", `${ctx.currency} #.00`); } export function handlePercentageFormat( diff --git a/packages/core/src/settings.ts b/packages/core/src/settings.ts index c0e3488a..c4a4bd39 100644 --- a/packages/core/src/settings.ts +++ b/packages/core/src/settings.ts @@ -155,6 +155,7 @@ export type Settings = { icon?: React.ReactNode; onClick?: (e: React.MouseEvent) => void; }[]; + currency: string; }; export const defaultSettings: Required = { @@ -286,4 +287,5 @@ export const defaultSettings: Required = { generateSheetId: () => uuidv4(), hooks: {}, customToolbarItems: [], + currency: "¥", }; diff --git a/packages/react/src/components/SVGDefines.tsx b/packages/react/src/components/SVGDefines.tsx index d90b6fed..317d98d5 100644 --- a/packages/react/src/components/SVGDefines.tsx +++ b/packages/react/src/components/SVGDefines.tsx @@ -1,7 +1,22 @@ /* eslint-disable react/no-unknown-property */ import React from "react"; -const SVGDefines: React.FC = () => ( +const getPath = (currency: string) => { + switch (currency) { + case "$": + return "M4 10.781c.148 1.667 1.513 2.85 3.591 3.003V15h1.043v-1.216c2.27-.179 3.678-1.438 3.678-3.3 0-1.59-.947-2.51-2.956-3.028l-.722-.187V3.467c1.122.11 1.879.714 2.07 1.616h1.47c-.166-1.6-1.54-2.748-3.54-2.875V1H7.591v1.233c-1.939.23-3.27 1.472-3.27 3.156 0 1.454.966 2.483 2.661 2.917l.61.162v4.031c-1.149-.17-1.94-.8-2.131-1.718H4zm3.391-3.836c-1.043-.263-1.6-.825-1.6-1.616 0-.944.704-1.641 1.8-1.828v3.495l-.2-.05zm1.591 1.872c1.287.323 1.852.859 1.852 1.769 0 1.097-.826 1.828-2.2 1.939V8.73l.348.086z"; + case "€": + return "M4 9.42h1.063C5.4 12.323 7.317 14 10.34 14c.622 0 1.167-.068 1.659-.185v-1.3c-.484.119-1.045.17-1.659.17-2.1 0-3.455-1.198-3.775-3.264h4.017v-.928H6.497v-.936c0-.11 0-.219.008-.329h4.078v-.927H6.618c.388-1.898 1.719-2.985 3.723-2.985.614 0 1.175.05 1.659.177V2.194A6.617 6.617 0 0010.341 2c-2.928 0-4.82 1.569-5.244 4.3H4v.928h1.01v1.265H4v.928z"; + case "£": + return "M4 8.585h1.969c.115.465.186.939.186 1.43 0 1.385-.736 2.496-2.075 2.771V14H12v-1.24H6.492v-.129c.825-.525 1.135-1.446 1.135-2.694 0-.465-.07-.913-.168-1.352h3.29v-.972H7.22c-.186-.723-.372-1.455-.372-2.247 0-1.274 1.047-2.066 2.58-2.066a5.32 5.32 0 012.103.465V2.456A5.629 5.629 0 009.348 2C6.865 2 5.322 3.291 5.322 5.366c0 .775.195 1.515.399 2.247H4v.972z"; + case "₹": + return "M4 3.06h2.726c1.22 0 2.12.575 2.325 1.724H4v1.051h5.051C8.855 7.001 8 7.558 6.788 7.558H4v1.317L8.437 14h2.11L6.095 8.884h.855c2.316-.018 3.465-1.476 3.688-3.049H12V4.784h-1.345c-.08-.778-.357-1.335-.793-1.732H12V2H4v1.06z"; + default: + return "M8.75 14v-2.629h2.446v-.967H8.75v-1.31h2.445v-.967H9.128L12.5 2h-1.699L8.047 7.327h-.086L5.207 2H3.5l3.363 6.127H4.778v.968H7.25v1.31H4.78v.966h2.47V14h1.502z"; + } +}; + +const SVGDefines: React.FC<{ currency: string }> = ({ currency }) => ( ( - - + + v.value === curr?.fa); + const format = _.find(defaultFormat, (v) => v.value === curr?.fa); if (curr?.fa != null) { if (format != null) { currentFmt = format.text; } else { - currentFmt = defaultFmt[defaultFmt.length - 1].text; + currentFmt = defaultFormat[defaultFormat.length - 1].text; } } } @@ -276,7 +278,7 @@ const Toolbar: React.FC<{ {(setOpen) => (
{mergedSettings.showToolbar && (