FIX: Move model out of te nameManager
This commit is contained in:
committed by
Nicolás Hatcher Andrés
parent
cbb413f100
commit
ea194ee730
@@ -1,4 +1,4 @@
|
|||||||
import type { Model } from "@ironcalc/wasm";
|
import type { DefinedName, WorksheetProperties } from "@ironcalc/wasm";
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
Button,
|
Button,
|
||||||
@@ -13,58 +13,48 @@ import {
|
|||||||
import { t } from "i18next";
|
import { t } from "i18next";
|
||||||
import { BookOpen, Plus, X } from "lucide-react";
|
import { BookOpen, Plus, X } from "lucide-react";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { getFullRangeToString } from "../util";
|
|
||||||
import NamedRangeActive from "./NamedRangeActive";
|
import NamedRangeActive from "./NamedRangeActive";
|
||||||
import NamedRangeInactive from "./NamedRangeInactive";
|
import NamedRangeInactive from "./NamedRangeInactive";
|
||||||
|
|
||||||
interface NameManagerDialogProperties {
|
export interface NameManagerProperties {
|
||||||
open: boolean;
|
newDefinedName: (
|
||||||
model: Model;
|
|
||||||
onClose: () => void;
|
|
||||||
onNamesChanged: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|
||||||
const { open, model, onClose, onNamesChanged } = properties;
|
|
||||||
// If editingNameIndex is -1, then we are adding a new name
|
|
||||||
// If editingNameIndex is -2, then we are not editing any name
|
|
||||||
// If editingNameIndex is a positive number, then we are editing that index
|
|
||||||
const [editingNameIndex, setEditingNameIndex] = useState(-2);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (open) {
|
|
||||||
setEditingNameIndex(-2);
|
|
||||||
}
|
|
||||||
}, [open]);
|
|
||||||
|
|
||||||
// Model stuff:
|
|
||||||
const worksheets = model.getWorksheetsProperties();
|
|
||||||
const definedNameList = model.getDefinedNameList();
|
|
||||||
const formatFormula = (): string => {
|
|
||||||
const worksheetNames = worksheets.map((s) => s.name);
|
|
||||||
const selectedView = model.getSelectedView();
|
|
||||||
|
|
||||||
return getFullRangeToString(selectedView, worksheetNames);
|
|
||||||
};
|
|
||||||
const newDefinedName = (
|
|
||||||
name: string,
|
name: string,
|
||||||
scope: number | undefined,
|
scope: number | undefined,
|
||||||
formula: string
|
formula: string,
|
||||||
) => {
|
) => void;
|
||||||
model.newDefinedName(name, scope, formula);
|
updateDefinedName: (
|
||||||
};
|
|
||||||
const updateDefinedName = (
|
|
||||||
name: string,
|
name: string,
|
||||||
scope: number | undefined,
|
scope: number | undefined,
|
||||||
newName: string,
|
newName: string,
|
||||||
newScope: number | undefined,
|
newScope: number | undefined,
|
||||||
newFormula: string
|
newFormula: string,
|
||||||
) => {
|
) => void;
|
||||||
model.updateDefinedName(name, scope, newName, newScope, newFormula);
|
deleteDefinedName: (name: string, scope: number | undefined) => void;
|
||||||
};
|
selectedArea: () => string;
|
||||||
const deleteDefinedName = (name: string, scope: number | undefined) => {
|
worksheets: WorksheetProperties[];
|
||||||
model.deleteDefinedName(name, scope);
|
definedNameList: DefinedName[];
|
||||||
};
|
}
|
||||||
|
|
||||||
|
interface NameManagerDialogProperties {
|
||||||
|
open: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
model: NameManagerProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
function NameManagerDialog(properties: NameManagerDialogProperties) {
|
||||||
|
const { open, model, onClose } = properties;
|
||||||
|
const {
|
||||||
|
newDefinedName,
|
||||||
|
updateDefinedName,
|
||||||
|
deleteDefinedName,
|
||||||
|
selectedArea,
|
||||||
|
worksheets,
|
||||||
|
definedNameList,
|
||||||
|
} = model;
|
||||||
|
// If editingNameIndex is -1, then we are adding a new name
|
||||||
|
// If editingNameIndex is -2, then we are not editing any name
|
||||||
|
// If editingNameIndex is a positive number, then we are editing that index
|
||||||
|
const [editingNameIndex, setEditingNameIndex] = useState(-2);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (open) {
|
if (open) {
|
||||||
@@ -102,10 +92,10 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
onSave={(
|
onSave={(
|
||||||
newName,
|
newName,
|
||||||
newScope,
|
newScope,
|
||||||
newFormula
|
newFormula,
|
||||||
): string | undefined => {
|
): string | undefined => {
|
||||||
const scope_index = worksheets.findIndex(
|
const scope_index = worksheets.findIndex(
|
||||||
(s) => s.name === newScope
|
(s) => s.name === newScope,
|
||||||
);
|
);
|
||||||
const scope = scope_index > 0 ? scope_index : undefined;
|
const scope = scope_index > 0 ? scope_index : undefined;
|
||||||
try {
|
try {
|
||||||
@@ -114,10 +104,9 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
definedName.scope,
|
definedName.scope,
|
||||||
newName,
|
newName,
|
||||||
scope,
|
scope,
|
||||||
newFormula
|
newFormula,
|
||||||
);
|
);
|
||||||
setEditingNameIndex(-2);
|
setEditingNameIndex(-2);
|
||||||
onNamesChanged();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return `${e}`;
|
return `${e}`;
|
||||||
}
|
}
|
||||||
@@ -136,7 +125,6 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
onEdit={() => setEditingNameIndex(index)}
|
onEdit={() => setEditingNameIndex(index)}
|
||||||
onDelete={() => {
|
onDelete={() => {
|
||||||
deleteDefinedName(definedName.name, definedName.scope);
|
deleteDefinedName(definedName.name, definedName.scope);
|
||||||
onNamesChanged();
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
@@ -146,7 +134,7 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
<NamedRangeActive
|
<NamedRangeActive
|
||||||
worksheets={worksheets}
|
worksheets={worksheets}
|
||||||
name={""}
|
name={""}
|
||||||
formula={formatFormula()}
|
formula={selectedArea()}
|
||||||
scope={"[global]"}
|
scope={"[global]"}
|
||||||
onSave={(name, scope, formula): string | undefined => {
|
onSave={(name, scope, formula): string | undefined => {
|
||||||
const scope_index = worksheets.findIndex((s) => s.name === scope);
|
const scope_index = worksheets.findIndex((s) => s.name === scope);
|
||||||
@@ -154,7 +142,6 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
try {
|
try {
|
||||||
newDefinedName(name, scope_value, formula);
|
newDefinedName(name, scope_value, formula);
|
||||||
setEditingNameIndex(-2);
|
setEditingNameIndex(-2);
|
||||||
onNamesChanged();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return `${e}`;
|
return `${e}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import type {
|
import type {
|
||||||
BorderOptions,
|
BorderOptions,
|
||||||
HorizontalAlignment,
|
HorizontalAlignment,
|
||||||
Model,
|
|
||||||
VerticalAlignment,
|
VerticalAlignment,
|
||||||
} from "@ironcalc/wasm";
|
} from "@ironcalc/wasm";
|
||||||
import { styled } from "@mui/material/styles";
|
import { styled } from "@mui/material/styles";
|
||||||
@@ -37,6 +36,7 @@ import {
|
|||||||
} from "../icons";
|
} from "../icons";
|
||||||
import { theme } from "../theme";
|
import { theme } from "../theme";
|
||||||
import NameManagerDialog from "./NameManagerDialog";
|
import NameManagerDialog from "./NameManagerDialog";
|
||||||
|
import type { NameManagerProperties } from "./NameManagerDialog/NameManagerDialog";
|
||||||
import BorderPicker from "./borderPicker";
|
import BorderPicker from "./borderPicker";
|
||||||
import ColorPicker from "./colorPicker";
|
import ColorPicker from "./colorPicker";
|
||||||
import { TOOLBAR_HEIGHT } from "./constants";
|
import { TOOLBAR_HEIGHT } from "./constants";
|
||||||
@@ -63,7 +63,6 @@ type ToolbarProperties = {
|
|||||||
onFillColorPicked: (hex: string) => void;
|
onFillColorPicked: (hex: string) => void;
|
||||||
onNumberFormatPicked: (numberFmt: string) => void;
|
onNumberFormatPicked: (numberFmt: string) => void;
|
||||||
onBorderChanged: (border: BorderOptions) => void;
|
onBorderChanged: (border: BorderOptions) => void;
|
||||||
onNamesChanged: () => void;
|
|
||||||
fillColor: string;
|
fillColor: string;
|
||||||
fontColor: string;
|
fontColor: string;
|
||||||
bold: boolean;
|
bold: boolean;
|
||||||
@@ -76,7 +75,7 @@ type ToolbarProperties = {
|
|||||||
numFmt: string;
|
numFmt: string;
|
||||||
showGridLines: boolean;
|
showGridLines: boolean;
|
||||||
onToggleShowGridLines: (show: boolean) => void;
|
onToggleShowGridLines: (show: boolean) => void;
|
||||||
model: Model;
|
nameManagerProperties: NameManagerProperties;
|
||||||
};
|
};
|
||||||
|
|
||||||
function Toolbar(properties: ToolbarProperties) {
|
function Toolbar(properties: ToolbarProperties) {
|
||||||
@@ -398,8 +397,7 @@ function Toolbar(properties: ToolbarProperties) {
|
|||||||
onClose={() => {
|
onClose={() => {
|
||||||
setNameManagerDialogOpen(false);
|
setNameManagerDialogOpen(false);
|
||||||
}}
|
}}
|
||||||
onNamesChanged={properties.onNamesChanged}
|
model={properties.nameManagerProperties}
|
||||||
model={properties.model}
|
|
||||||
/>
|
/>
|
||||||
</ToolbarContainer>
|
</ToolbarContainer>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -21,7 +21,11 @@ import {
|
|||||||
import FormulaBar from "./formulabar";
|
import FormulaBar from "./formulabar";
|
||||||
import Toolbar from "./toolbar";
|
import Toolbar from "./toolbar";
|
||||||
import useKeyboardNavigation from "./useKeyboardNavigation";
|
import useKeyboardNavigation from "./useKeyboardNavigation";
|
||||||
import { type NavigationKey, getCellAddress } from "./util";
|
import {
|
||||||
|
type NavigationKey,
|
||||||
|
getCellAddress,
|
||||||
|
getFullRangeToString,
|
||||||
|
} from "./util";
|
||||||
import type { WorkbookState } from "./workbookState";
|
import type { WorkbookState } from "./workbookState";
|
||||||
import Worksheet from "./worksheet";
|
import Worksheet from "./worksheet";
|
||||||
|
|
||||||
@@ -32,11 +36,13 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
|
|||||||
// Calling `setRedrawId((id) => id + 1);` forces a redraw
|
// Calling `setRedrawId((id) => id + 1);` forces a redraw
|
||||||
// This is needed because `model` or `workbookState` can change without React being aware of it
|
// This is needed because `model` or `workbookState` can change without React being aware of it
|
||||||
const setRedrawId = useState(0)[1];
|
const setRedrawId = useState(0)[1];
|
||||||
const info = model
|
|
||||||
.getWorksheetsProperties()
|
const worksheets = model.getWorksheetsProperties();
|
||||||
.map(({ name, color, sheet_id, state }: WorksheetProperties) => {
|
const info = worksheets.map(
|
||||||
|
({ name, color, sheet_id, state }: WorksheetProperties) => {
|
||||||
return { name, color: color ? color : "#FFF", sheetId: sheet_id, state };
|
return { name, color: color ? color : "#FFF", sheetId: sheet_id, state };
|
||||||
});
|
},
|
||||||
|
);
|
||||||
const focusWorkbook = useCallback(() => {
|
const focusWorkbook = useCallback(() => {
|
||||||
if (rootRef.current) {
|
if (rootRef.current) {
|
||||||
rootRef.current.focus();
|
rootRef.current.focus();
|
||||||
@@ -569,8 +575,38 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
|
|||||||
model.setShowGridLines(sheet, show);
|
model.setShowGridLines(sheet, show);
|
||||||
setRedrawId((id) => id + 1);
|
setRedrawId((id) => id + 1);
|
||||||
}}
|
}}
|
||||||
onNamesChanged={() => setRedrawId((id) => id + 1)}
|
nameManagerProperties={{
|
||||||
model={model}
|
newDefinedName: (
|
||||||
|
name: string,
|
||||||
|
scope: number | undefined,
|
||||||
|
formula: string,
|
||||||
|
) => {
|
||||||
|
model.newDefinedName(name, scope, formula);
|
||||||
|
setRedrawId((id) => id + 1);
|
||||||
|
},
|
||||||
|
updateDefinedName: (
|
||||||
|
name: string,
|
||||||
|
scope: number | undefined,
|
||||||
|
newName: string,
|
||||||
|
newScope: number | undefined,
|
||||||
|
newFormula: string,
|
||||||
|
) => {
|
||||||
|
model.updateDefinedName(name, scope, newName, newScope, newFormula);
|
||||||
|
setRedrawId((id) => id + 1);
|
||||||
|
},
|
||||||
|
deleteDefinedName: (name: string, scope: number | undefined) => {
|
||||||
|
model.deleteDefinedName(name, scope);
|
||||||
|
setRedrawId((id) => id + 1);
|
||||||
|
},
|
||||||
|
selectedArea: () => {
|
||||||
|
const worksheetNames = worksheets.map((s) => s.name);
|
||||||
|
const selectedView = model.getSelectedView();
|
||||||
|
|
||||||
|
return getFullRangeToString(selectedView, worksheetNames);
|
||||||
|
},
|
||||||
|
worksheets,
|
||||||
|
definedNameList: model.getDefinedNameList(),
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<FormulaBar
|
<FormulaBar
|
||||||
cellAddress={cellAddress()}
|
cellAddress={cellAddress()}
|
||||||
|
|||||||
Reference in New Issue
Block a user