parent
23eb08088e
commit
4e4802b19e
@ -1,6 +1,5 @@
|
||||
import {
|
||||
Spreadsheet,
|
||||
sortSpreadsheet,
|
||||
tryParseCells,
|
||||
tryParseNumber,
|
||||
VALID_SPREADSHEET,
|
||||
@ -119,29 +118,4 @@ describe("charts", () => {
|
||||
expect(values).toEqual([61, -60, 85, -67, 54, 95]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sortSpreadsheet", () => {
|
||||
it("sorts strictly numerical labels columns in ascending order", () => {
|
||||
const spreadsheet = [
|
||||
["x", "y"],
|
||||
["1°", "1"],
|
||||
["9°", "2"],
|
||||
["3°", "3"],
|
||||
["6°", "4"],
|
||||
];
|
||||
|
||||
const result = tryParseCells(spreadsheet);
|
||||
|
||||
expect(result.type).toBe(VALID_SPREADSHEET);
|
||||
|
||||
const { title, labels, values } = sortSpreadsheet(
|
||||
(result as { type: typeof VALID_SPREADSHEET; spreadsheet: Spreadsheet })
|
||||
.spreadsheet,
|
||||
);
|
||||
|
||||
expect(title).toEqual("y");
|
||||
expect(labels).toEqual(["1°", "3°", "6°", "9°"]);
|
||||
expect(values).toEqual([1, 3, 4, 2]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -36,8 +36,11 @@ type ParseSpreadsheetResult =
|
||||
| { type: typeof NOT_SPREADSHEET; reason: string }
|
||||
| { type: typeof VALID_SPREADSHEET; spreadsheet: Spreadsheet };
|
||||
|
||||
/**
|
||||
* @private exported for testing
|
||||
*/
|
||||
export const tryParseNumber = (s: string): number | null => {
|
||||
const match = /^([-+]?)[$€£¥₩]?([-+]?)([\d.,]+)[%°]?$/.exec(s);
|
||||
const match = /^([-+]?)[$€£¥₩]?([-+]?)([\d.,]+)[%]?$/.exec(s);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
@ -162,32 +165,6 @@ export const tryParseSpreadsheet = (text: string): ParseSpreadsheetResult => {
|
||||
return result;
|
||||
};
|
||||
|
||||
export const sortSpreadsheet = (spreadsheet: Spreadsheet) => {
|
||||
const rows = [] as { label: string; value: number }[];
|
||||
if (spreadsheet.labels == null || spreadsheet.values == null) {
|
||||
return spreadsheet;
|
||||
}
|
||||
if (spreadsheet.labels.every((val) => tryParseNumber(val))) {
|
||||
for (let i = 0; i < spreadsheet.labels.length; i++) {
|
||||
rows.push({
|
||||
label: spreadsheet.labels[i],
|
||||
value: spreadsheet.values[i],
|
||||
});
|
||||
}
|
||||
rows.sort((a, b) => {
|
||||
const aParsed = tryParseNumber(a.label)!;
|
||||
const bParsed = tryParseNumber(b.label)!;
|
||||
return aParsed - bParsed;
|
||||
});
|
||||
const newSpreadsheet = {} as Spreadsheet;
|
||||
newSpreadsheet.title = spreadsheet.title;
|
||||
newSpreadsheet.labels = rows.flatMap((row) => row.label);
|
||||
newSpreadsheet.values = rows.flatMap((row) => row.value);
|
||||
return newSpreadsheet;
|
||||
}
|
||||
return spreadsheet;
|
||||
};
|
||||
|
||||
const bgColors = getAllColorsSpecificShade(DEFAULT_CHART_COLOR_INDEX);
|
||||
|
||||
// Put all the common properties here so when the whole chart is selected
|
||||
|
@ -1,13 +1,7 @@
|
||||
import oc from "open-color";
|
||||
import React, { useLayoutEffect, useRef, useState } from "react";
|
||||
import { trackEvent } from "../analytics";
|
||||
import {
|
||||
ChartElements,
|
||||
renderSpreadsheet,
|
||||
sortSpreadsheet,
|
||||
Spreadsheet,
|
||||
tryParseNumber,
|
||||
} from "../charts";
|
||||
import { ChartElements, renderSpreadsheet, Spreadsheet } from "../charts";
|
||||
import { ChartType } from "../element/types";
|
||||
import { t } from "../i18n";
|
||||
import { exportToSvg } from "../scene/export";
|
||||
@ -22,7 +16,6 @@ import {
|
||||
getContainerElement,
|
||||
redrawTextBoundingBox,
|
||||
} from "../element/textElement";
|
||||
import { CheckboxItem } from "./CheckboxItem";
|
||||
|
||||
type OnInsertChart = (chartType: ChartType, elements: ChartElements) => void;
|
||||
|
||||
@ -31,7 +24,6 @@ const ChartPreviewBtn = (props: {
|
||||
chartType: ChartType;
|
||||
selected: boolean;
|
||||
onClick: OnInsertChart;
|
||||
sortChartLabels: boolean;
|
||||
}) => {
|
||||
const previewRef = useRef<HTMLDivElement | null>(null);
|
||||
const [chartElements, setChartElements] = useState<ChartElements | null>(
|
||||
@ -51,10 +43,12 @@ const ChartPreviewBtn = (props: {
|
||||
return;
|
||||
}
|
||||
|
||||
const spreadsheet = props.sortChartLabels
|
||||
? sortSpreadsheet(props.spreadsheet)
|
||||
: props.spreadsheet;
|
||||
elements = renderSpreadsheet(props.chartType, spreadsheet, 0, 0);
|
||||
elements = renderSpreadsheet(
|
||||
props.chartType,
|
||||
props.spreadsheet,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
elements.forEach(
|
||||
(el) =>
|
||||
isTextElement(el) &&
|
||||
@ -85,12 +79,7 @@ const ChartPreviewBtn = (props: {
|
||||
previewNode.replaceChildren();
|
||||
};
|
||||
})();
|
||||
}, [
|
||||
props.spreadsheet,
|
||||
props.chartType,
|
||||
props.selected,
|
||||
props.sortChartLabels,
|
||||
]);
|
||||
}, [props.spreadsheet, props.chartType, props.selected]);
|
||||
|
||||
return (
|
||||
<button
|
||||
@ -133,10 +122,6 @@ export const PasteChartDialog = ({
|
||||
},
|
||||
});
|
||||
};
|
||||
const showSortChartLabels = appState.pasteDialog.data?.labels?.every((val) =>
|
||||
tryParseNumber(val),
|
||||
);
|
||||
const [sortChartLabels, setSortChartLabels] = useState<boolean>(false);
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
@ -152,28 +137,14 @@ export const PasteChartDialog = ({
|
||||
spreadsheet={appState.pasteDialog.data}
|
||||
selected={appState.currentChartType === "bar"}
|
||||
onClick={handleChartClick}
|
||||
sortChartLabels={(showSortChartLabels && sortChartLabels) ?? false}
|
||||
/>
|
||||
<ChartPreviewBtn
|
||||
chartType="line"
|
||||
spreadsheet={appState.pasteDialog.data}
|
||||
selected={appState.currentChartType === "line"}
|
||||
onClick={handleChartClick}
|
||||
sortChartLabels={(showSortChartLabels && sortChartLabels) ?? false}
|
||||
/>
|
||||
</div>
|
||||
{showSortChartLabels && (
|
||||
<div className={"container"}>
|
||||
<CheckboxItem
|
||||
checked={sortChartLabels}
|
||||
onChange={(checked: boolean) => {
|
||||
setSortChartLabels(checked);
|
||||
}}
|
||||
>
|
||||
{t("labels.sortChartLabels")}
|
||||
</CheckboxItem>
|
||||
</div>
|
||||
)}
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
@ -46,15 +46,12 @@ class LocalFileManager extends FileManager {
|
||||
const saveDataStateToLocalStorage = (
|
||||
elements: readonly ExcalidrawElement[],
|
||||
appState: AppState,
|
||||
appStateOnly = false,
|
||||
) => {
|
||||
try {
|
||||
if (!appStateOnly) {
|
||||
localStorage.setItem(
|
||||
STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS,
|
||||
JSON.stringify(clearElementsForLocalStorage(elements)),
|
||||
);
|
||||
}
|
||||
localStorage.setItem(
|
||||
STORAGE_KEYS.LOCAL_STORAGE_APP_STATE,
|
||||
JSON.stringify(clearAppStateForLocalStorage(appState)),
|
||||
@ -75,12 +72,8 @@ export class LocalData {
|
||||
appState: AppState,
|
||||
files: BinaryFiles,
|
||||
onFilesSaved: () => void,
|
||||
appStateOnly = false,
|
||||
) => {
|
||||
saveDataStateToLocalStorage(elements, appState, appStateOnly);
|
||||
if (appStateOnly) {
|
||||
return;
|
||||
}
|
||||
saveDataStateToLocalStorage(elements, appState);
|
||||
|
||||
await this.fileStorage.saveFiles({
|
||||
elements,
|
||||
@ -104,14 +97,6 @@ export class LocalData {
|
||||
}
|
||||
};
|
||||
|
||||
/** Saves the AppState, only if saving is paused. */
|
||||
static saveAppState = (appState: AppState) => {
|
||||
// we need to make the `isSavePaused` check synchronously (undebounced)
|
||||
if (this.isSavePaused()) {
|
||||
this._save([], appState, {}, () => {}, true);
|
||||
}
|
||||
};
|
||||
|
||||
static flushSave = () => {
|
||||
this._save.flush();
|
||||
};
|
||||
|
@ -234,7 +234,7 @@ const initializeScene = async (opts: {
|
||||
...restoreAppState(
|
||||
{
|
||||
...scene?.appState,
|
||||
...localDataState?.appState,
|
||||
theme: localDataState?.appState?.theme || scene?.appState?.theme,
|
||||
},
|
||||
excalidrawAPI.getAppState(),
|
||||
),
|
||||
@ -590,8 +590,6 @@ const ExcalidrawWrapper = () => {
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
LocalData.saveAppState(appState);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
"paste": "Paste",
|
||||
"pasteAsPlaintext": "Paste as plaintext",
|
||||
"pasteCharts": "Paste charts",
|
||||
"sortChartLabels": "Sort numerical labels in ascending order",
|
||||
"selectAll": "Select all",
|
||||
"multiSelect": "Add element to selection",
|
||||
"moveCanvas": "Move canvas",
|
||||
|
Loading…
x
Reference in New Issue
Block a user