Skip to content

Commit

Permalink
Merge pull request #590 from ruilisi/589-set-default-currency-symbol-…
Browse files Browse the repository at this point in the history
…in-settings

feat: add support for custom currency symbols
  • Loading branch information
sanchit3008 authored Aug 31, 2024
2 parents 3debb19 + 64ff1bd commit 202aa69
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 30 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type Context = {
rangeDialog?: RangeDialogProps; // 坐标选区鼠标选择
// 提醒弹窗
warnDialog?: string;
currency?: string;
dataVerification?: {
selectStatus: boolean;
selectRange: [];
Expand Down Expand Up @@ -236,6 +237,7 @@ export function defaultContext(refs: RefValues): Context {
config: {},
// 提醒弹窗
warnDialog: undefined,
currency: "¥",
rangeDialog: {
show: false,
rangeTxt: "",
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11226,17 +11226,25 @@ 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: "" },
{ text: "Number", value: "##0.00", example: "1000.12" },
{ 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" },
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/locale/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11199,17 +11199,21 @@ 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: "" },
{ text: "Número", value: "##0.00", example: "1000.12" },
{ 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" },
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/locale/hi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11232,17 +11232,21 @@ 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: "" },
{ text: "संख्या", value: "##0.00", example: "1000.12" },
{ 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" },
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/locale/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11209,18 +11209,22 @@ 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: "" },
{ text: "数字", value: "##0.00", example: "1000.12" },
{ 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: "" },
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/locale/zh_tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11176,18 +11176,22 @@ export default {
ChileanPeso: "智利比索",
CFAFrancBEAC: "中非金融合作法郎",
},
defaultFmt: [
defaultFmt: (currency: string) => [
{ text: "自動", value: "General", example: "" },
{ text: "純文字", value: "@", example: "" },
{ text: "", value: "split", example: "" },
{ text: "數位", value: "##0.00", example: "1000.12" },
{ 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: "" },
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/ssf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/modules/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,9 @@ export function handleCurrencyFormat(ctx: Context, cellInput: HTMLDivElement) {
const flowdata = getFlowdata(ctx);
if (!flowdata) return;

updateFormat(ctx, cellInput, flowdata, "ct", "¥ #.00");
const currency = ctx.currency || "¥";

updateFormat(ctx, cellInput, flowdata, "ct", `${currency} #.00`);
}

export function handlePercentageFormat(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export type Settings = {
icon?: React.ReactNode;
onClick?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
}[];
currency?: string;
};

export const defaultSettings: Required<Settings> = {
Expand Down Expand Up @@ -286,4 +287,5 @@ export const defaultSettings: Required<Settings> = {
generateSheetId: () => uuidv4(),
hooks: {},
customToolbarItems: [],
currency: "¥",
};
25 changes: 18 additions & 7 deletions packages/react/src/components/SVGDefines.tsx
Original file line number Diff line number Diff line change
@@ -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 }) => (
<svg
style={{ position: "absolute", width: 0, height: 0 }}
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -845,12 +860,8 @@ const SVGDefines: React.FC = () => (
<symbol viewBox="0 0 1024 1024" id="fx">
<path d="M841 370c3-3.3 2.7-8.3-0.6-11.3-1.5-1.3-3.4-2.1-5.3-2.1h-72.6c-2.4 0-4.6 1-6.1 2.8L633.5 504.6c-2.9 3.4-7.9 3.8-11.3 0.9-0.9-0.8-1.6-1.7-2.1-2.8l-63.5-141.3c-1.3-2.9-4.1-4.7-7.3-4.7H380.7l0.9-4.7 8-42.3c10.5-55.4 38-81.4 85.8-81.4 18.6 0 35.5 1.7 48.8 4.7l14.1-66.8c-22.6-4.7-35.2-6.1-54.9-6.1-103.3 0-156.4 44.3-175.9 147.3l-9.4 49.4h-97.6c-3.8 0-7.1 2.7-7.8 6.4L181.9 415c-0.9 4.3 1.9 8.6 6.2 9.5 0.5 0.1 1.1 0.2 1.6 0.2H284l-89 429.9c-0.9 4.3 1.9 8.6 6.2 9.5 0.5 0.1 1.1 0.2 1.6 0.2H269c3.8 0 7.1-2.7 7.8-6.4l89.7-433.1h135.8l68.2 139.1c1.4 2.9 1 6.4-1.2 8.8l-180.6 203c-2.9 3.3-2.6 8.4 0.7 11.3 1.5 1.3 3.4 2 5.3 2h72.7c2.4 0 4.6-1 6.1-2.8l123.7-146.7c2.8-3.4 7.9-3.8 11.3-1 0.9 0.8 1.6 1.7 2.1 2.8L676.4 784c1.3 2.8 4.1 4.7 7.3 4.7h64.6c4.4 0 8-3.6 8-8 0-1.2-0.3-2.4-0.8-3.5l-95.2-198.9c-1.4-2.9-0.9-6.4 1.3-8.8L841 370z" />
</symbol>
<symbol viewBox="0 0 1024 1024" id="currency-format">
<path
fill="#525C6F"
d="M675.861333 192l45.610667 44.906667-166.997333 169.6h237.525333v64H544v144.597333h248v64H544v176.597333h-64v-176.597333H232v-64H480v-144.597333H232v-64h237.482667l-166.954667-169.6L348.138667 192 512 358.464 675.861333 192z"
p-id="5490"
/>
<symbol viewBox="0 0 16 16" id="currency-format">
<path fill="#525C6F" d={getPath(currency)} p-id="5490" />
</symbol>
<symbol viewBox="0 0 1024 1024" id="percentage-format">
<path
Expand Down
12 changes: 7 additions & 5 deletions packages/react/src/components/Toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const Toolbar: React.FC<{
} = locale(context);
const toolbarFormat = locale(context).format;
const sheetWidth = context.luckysheetTableContentHW[0];
const { currency } = settings;
const defaultFormat = defaultFmt(currency);

const [customColor, setcustomColor] = useState("#000000");
const [customStyle, setcustomStyle] = useState("1");
Expand Down Expand Up @@ -260,23 +262,23 @@ const Toolbar: React.FC<{
);
}
if (name === "format") {
let currentFmt = defaultFmt[0].text;
let currentFmt = defaultFormat[0].text;
if (cell) {
const curr = normalizedCellAttr(cell, "ct");
const format = _.find(defaultFmt, (v) => 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;
}
}
}
return (
<Combo text={currentFmt} key={name} tooltip={tooltip}>
{(setOpen) => (
<Select>
{defaultFmt.map(({ text, value, example }, ii) => {
{defaultFormat.map(({ text, value, example }, ii) => {
if (value === "split") {
return <MenuDivider key={ii} />;
}
Expand Down Expand Up @@ -1416,7 +1418,7 @@ const Toolbar: React.FC<{
refs.cellInput,
refs.fxInput,
refs.globalCache,
defaultFmt,
defaultFormat,
align,
handleUndo,
handleRedo,
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/components/Workbook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ const Workbook = React.forwardRef<WorkbookInstance, Settings & AdditionalProps>(

draftCtx.config = _.isNil(sheet.config) ? {} : sheet.config;
draftCtx.insertedImgs = sheet.images;
draftCtx.currency = mergedSettings.currency || "¥";

draftCtx.zoomRatio = _.isNil(sheet.zoomRatio) ? 1 : sheet.zoomRatio;
draftCtx.rowHeaderWidth =
Expand Down Expand Up @@ -718,7 +719,7 @@ const Workbook = React.forwardRef<WorkbookInstance, Settings & AdditionalProps>(
ref={workbookContainer}
onKeyDown={onKeyDown}
>
<SVGDefines />
<SVGDefines currency={mergedSettings.currency} />
<div className="fortune-workarea">
{mergedSettings.showToolbar && (
<Toolbar
Expand Down

0 comments on commit 202aa69

Please sign in to comment.