Skip to content

Commit 9bd7c08

Browse files
authored
Modify - Decimal, Percentage, Currency (#358)
* Modify - Decimal * decimal not default number remains auto * manage digit limit beyong 15, even for formula output * add conversion after 15 to scientific for decimal and formula * percentage and currency * minor changes * minor changes * Hyperlink warning (#359) * hyperlink warning for formula cell * hyperlink warning for formula cell * minor changes
1 parent ed5be20 commit 9bd7c08

20 files changed

Lines changed: 1012 additions & 165 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@fileverse-dev/dsheet",
33
"private": false,
44
"description": "DSheet",
5-
"version": "2.0.7",
5+
"version": "2.0.8",
66
"main": "dist/index.es.js",
77
"module": "dist/index.es.js",
88
"exports": {
@@ -91,4 +91,4 @@
9191
"typescript": "^5.2.2",
9292
"vite": "^5.0.0"
9393
}
94-
}
94+
}

src/editor/constants/shared-constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const SERVICES_API_KEY: ServicesApiKeyType = RawServicesApiKey;
2121
export const DEFAULT_SHEET_DATA = [
2222
{
2323
id: '0',
24-
name: 'Sheet2',
24+
name: 'Sheet1',
2525
celldata: [],
2626
config: {},
2727
order: 0,

src/sheet-engine/core/events/keyboard.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,9 @@ export async function handleGlobalKeyDown(
949949
}
950950
if ((e.metaKey || e.ctrlKey) && e.code === "KeyK") {
951951
handleLink(ctx, cellInput, cache);
952+
e.preventDefault();
953+
e.stopPropagation();
954+
return;
952955
}
953956
if ((e.metaKey || e.ctrlKey) && !e.shiftKey && e.code === "Semicolon") {
954957
fillDate(ctx);
@@ -1306,6 +1309,15 @@ export async function handleGlobalKeyDown(
13061309
cellAt?.f != null && String(cellAt.f).trim() !== ""
13071310
? String(cellAt.f).replace(/[\r\n]/g, "")
13081311
: null;
1312+
const isPercentFormattedCell =
1313+
!!cellAt &&
1314+
typeof (cellAt as any)?.ct?.fa === "string" &&
1315+
String((cellAt as any).ct.fa).includes("%");
1316+
const isEmptyPercentFormattedCell =
1317+
isPercentFormattedCell &&
1318+
_.isNil((cellAt as any).v) &&
1319+
_.isNil((cellAt as any).m) &&
1320+
_.isNil((cellAt as any).f);
13091321

13101322
if (existingFormula != null) {
13111323
suppressFormulaRangeSelectionForInitialEdit(ctx);
@@ -1323,8 +1335,11 @@ export async function handleGlobalKeyDown(
13231335
cellInput.focus();
13241336
const initial = getTypeOverInitialContent(e);
13251337
if (initial !== undefined) {
1326-
cellInput.textContent = initial;
1327-
if (fxInput) fxInput.textContent = initial;
1338+
const seededText = isPercentFormattedCell
1339+
? `${initial}%`
1340+
: initial;
1341+
cellInput.textContent = seededText;
1342+
if (fxInput) fxInput.textContent = seededText;
13281343
handleFormulaInput(ctx, fxInput, cellInput, kcode);
13291344
e.preventDefault();
13301345
} else {
@@ -1337,6 +1352,22 @@ export async function handleGlobalKeyDown(
13371352
// Only adjust the in-cell editor: moveToEnd() calls focus() and would steal
13381353
// focus to the formula bar if applied to fxInput.
13391354
queueMicrotask(() => {
1355+
if (initial !== undefined && isPercentFormattedCell) {
1356+
const textNode = cellInput.firstChild;
1357+
const caretOffset = Math.max(
1358+
0,
1359+
(cellInput.textContent || "").length - 1
1360+
);
1361+
const sel = window.getSelection();
1362+
if (textNode && sel) {
1363+
const range = document.createRange();
1364+
range.setStart(textNode, Math.min(caretOffset, textNode.textContent?.length || 0));
1365+
range.collapse(true);
1366+
sel.removeAllRanges();
1367+
sel.addRange(range);
1368+
return;
1369+
}
1370+
}
13401371
moveToEnd(cellInput);
13411372
});
13421373

src/sheet-engine/core/events/paste.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,12 @@ const handleFormulaOnPaste = (ctx: Context, d: any) => {
394394
};
395395

396396
function pasteHandler(ctx: Context, data: any, borderInfo?: any) {
397+
const normalizeDecimalStringOnPaste = (input: string): string => {
398+
const trimmed = input.trim();
399+
if (!/^[+-]?\d+\.\d+$/.test(trimmed)) return input;
400+
return trimmed.replace(/(\.\d*?[1-9])0+$/, "$1").replace(/\.0+$/, "");
401+
};
402+
397403
if (ctx.luckysheet_selection_range) {
398404
ctx.luckysheet_selection_range = [];
399405
}
@@ -729,26 +735,31 @@ function pasteHandler(ctx: Context, data: any, borderInfo?: any) {
729735
const url = getUrlFromText(normalizedValueStr);
730736
const isUrl = url !== null;
731737

738+
const normalizedNumericValueStr =
739+
!isUrl && !isMultilineValue && isRealNum(value)
740+
? normalizeDecimalStringOnPaste(normalizedValueStr)
741+
: normalizedValueStr;
742+
732743
if (!isUrl && !isMultilineValue && isRealNum(value)) {
733744
// 如果单元格设置了纯文本格式,那么就不要转成数值类型了,防止数值过大自动转成科学计数法
734745
if (originCell && originCell.ct && originCell.ct.fa === "@") {
735-
value = String(value);
746+
value = normalizedNumericValueStr;
736747
} else if (!/^0x?[a-fA-F0-9]+$/.test(value)) {
737-
value = parseFloat(value);
748+
value = parseFloat(normalizedNumericValueStr);
738749
}
739750
}
740751

741752
if (originCell) {
742753
if (!isUrl && isMultilineValue) {
743754
applyMultilineTextToCell(originCell, normalizedValueStr);
744755
} else if (!isUrl) {
745-
const generated = genarate(originalValueStr);
756+
const generated = genarate(normalizedNumericValueStr);
746757
if (generated) {
747758
const [genM, genCt, genV] = generated;
748759
if (genCt?.t === "d") {
749760
// Pasted value is a date — always update ct so toolbar shows "Date"
750761
originCell.v = genV;
751-
originCell.m = genM ?? originalValueStr;
762+
originCell.m = genM ?? normalizedNumericValueStr;
752763
originCell.ct = genCt;
753764
} else {
754765
// Not a date: preserve destination format, just update value
@@ -831,9 +842,9 @@ function pasteHandler(ctx: Context, data: any, borderInfo?: any) {
831842
cell.m = value;
832843
cell.ct = { fa: "@", t: "s" };
833844
} else {
834-
const [m, ct, v] = genarate(originalValueStr) ?? [];
835-
cell.v = v ?? originalValueStr;
836-
cell.m = m != null ? String(m) : originalValueStr;
845+
const [m, ct, v] = genarate(normalizedNumericValueStr) ?? [];
846+
cell.v = v ?? normalizedNumericValueStr;
847+
cell.m = m != null ? String(m) : normalizedNumericValueStr;
837848
if (ct?.t === "n") {
838849
cell.ct = { fa: "General", t: "g" };
839850
} else {

src/sheet-engine/core/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export {
7979
update,
8080
is_date,
8181
valueShowEs,
82+
isTypedCurrencyDisplayFormat,
83+
isCurrencyLikeNumberFormat,
8284
// formula
8385
FormulaCache,
8486
groupValuesRefresh,
@@ -125,6 +127,7 @@ export {
125127
selectTitlesRange,
126128
normalizeSelection,
127129
syncPrimaryCellActiveFromSelection,
130+
selectionIsExactlyOneMergeBlock,
128131
setPrimaryCellActive,
129132
advancePrimaryCellInLastMultiSelection,
130133
snapSheetSelectionFocusToCellPreserveMultiRange,
@@ -152,12 +155,14 @@ export {
152155
handleTextBackground,
153156
handleBorder,
154157
handleMerge,
158+
mergeSelectionHasValues,
155159
handleSort,
156160
handleFreeze,
157161
handleTextSize,
158162
handleSum,
159163
handleLink,
160164
captureLinkEditorOpenSnapshot,
165+
isHyperlinkCreationBlocked,
161166
toolbarItemClickHandler,
162167
toolbarItemSelectedFunc,
163168
handleScreenShot,

0 commit comments

Comments
 (0)