FIX[Editor]: More simplifications and fixes

This commit is contained in:
Nicolás Hatcher
2024-10-14 21:00:40 +02:00
committed by Nicolás Hatcher Andrés
parent 9805d0c518
commit 730a815729
5 changed files with 16 additions and 61 deletions

View File

@@ -1,5 +1,6 @@
import type { Model } from "@ironcalc/wasm"; import type { Model } from "@ironcalc/wasm";
import { columnNameFromNumber } from "@ironcalc/wasm"; import { columnNameFromNumber } from "@ironcalc/wasm";
import { getColor } from "../editor/util";
import type { Cell } from "../types"; import type { Cell } from "../types";
import type { WorkbookState } from "../workbookState"; import type { WorkbookState } from "../workbookState";
import { import {
@@ -17,7 +18,6 @@ import {
headerTextColor, headerTextColor,
outlineColor, outlineColor,
} from "./constants"; } from "./constants";
import { getColor } from "../editor/util";
export interface CanvasSettings { export interface CanvasSettings {
model: Model; model: Model;

View File

@@ -77,9 +77,6 @@ const Editor = (options: EditorOptions) => {
options; options;
const [text, setText] = useState(originalText); const [text, setText] = useState(originalText);
const [styledFormula, setStyledFormula] = useState(
getFormulaHTML(model, text, "").html,
);
const formulaRef = useRef<HTMLDivElement>(null); const formulaRef = useRef<HTMLDivElement>(null);
const maskRef = useRef<HTMLDivElement>(null); const maskRef = useRef<HTMLDivElement>(null);
@@ -87,11 +84,10 @@ const Editor = (options: EditorOptions) => {
useEffect(() => { useEffect(() => {
setText(originalText); setText(originalText);
setStyledFormula(getFormulaHTML(model, originalText, "").html);
if (textareaRef.current) { if (textareaRef.current) {
textareaRef.current.value = originalText; textareaRef.current.value = originalText;
} }
}, [originalText, model]); }, [originalText]);
const { onKeyDown } = useKeyDown({ const { onKeyDown } = useKeyDown({
model, model,
@@ -99,20 +95,10 @@ const Editor = (options: EditorOptions) => {
onTextUpdated, onTextUpdated,
workbookState, workbookState,
textareaRef, textareaRef,
setStyledFormula,
}); });
useEffect(() => {
if (text.length === 0) {
// noop
}
}, [text]);
useEffect(() => { useEffect(() => {
const cell = workbookState.getEditingCell(); const cell = workbookState.getEditingCell();
if (text.length === 0) {
// noop, just to keep the linter happy
}
if (!cell) { if (!cell) {
return; return;
} }
@@ -127,7 +113,10 @@ const Editor = (options: EditorOptions) => {
cell.editorHeight = scrollHeight; cell.editorHeight = scrollHeight;
} }
} }
}, [text, workbookState]); if (type === cell.focus) {
textareaRef.current?.focus();
}
});
const onChange = useCallback(() => { const onChange = useCallback(() => {
const textarea = textareaRef.current; const textarea = textareaRef.current;
@@ -140,7 +129,7 @@ const Editor = (options: EditorOptions) => {
cell.referencedRange = null; cell.referencedRange = null;
cell.cursorStart = textarea.selectionStart; cell.cursorStart = textarea.selectionStart;
cell.cursorEnd = textarea.selectionEnd; cell.cursorEnd = textarea.selectionEnd;
const styledFormula = getFormulaHTML(model, cell.text, ""); const styledFormula = getFormulaHTML(model, value);
if (value === "" && type === "cell") { if (value === "" && type === "cell") {
// When we delete the content of a cell we jump to accept mode // When we delete the content of a cell we jump to accept mode
cell.mode = "accept"; cell.mode = "accept";
@@ -149,7 +138,6 @@ const Editor = (options: EditorOptions) => {
workbookState.setActiveRanges(styledFormula.activeRanges); workbookState.setActiveRanges(styledFormula.activeRanges);
setText(cell.text); setText(cell.text);
setStyledFormula(styledFormula.html);
onTextUpdated(); onTextUpdated();
@@ -167,7 +155,6 @@ const Editor = (options: EditorOptions) => {
} }
if (textareaRef.current) { if (textareaRef.current) {
textareaRef.current.value = ""; textareaRef.current.value = "";
setStyledFormula(getFormulaHTML(model, "", "").html);
} }
// This happens if the blur hasn't been taken care before by // This happens if the blur hasn't been taken care before by
@@ -187,14 +174,9 @@ const Editor = (options: EditorOptions) => {
const cell = workbookState.getEditingCell(); const cell = workbookState.getEditingCell();
// If we are the focus, the get it
if (cell) {
if (type === cell.focus) {
textareaRef.current?.focus();
}
}
const showEditor = cell !== null || type === "formula-bar" ? "block" : "none"; const showEditor = cell !== null || type === "formula-bar" ? "block" : "none";
const mtext = cell ? workbookState.getEditingText() : originalText;
const styledFormula = getFormulaHTML(model, mtext).html;
return ( return (
<div <div

View File

@@ -2,7 +2,7 @@ import type { Model } from "@ironcalc/wasm";
import { type KeyboardEvent, type RefObject, useCallback } from "react"; import { type KeyboardEvent, type RefObject, useCallback } from "react";
import { rangeToStr } from "../util"; import { rangeToStr } from "../util";
import type { WorkbookState } from "../workbookState"; import type { WorkbookState } from "../workbookState";
import getFormulaHTML, { isInReferenceMode } from "./util"; import { isInReferenceMode } from "./util";
interface Options { interface Options {
model: Model; model: Model;
@@ -10,20 +10,13 @@ interface Options {
onTextUpdated: () => void; onTextUpdated: () => void;
workbookState: WorkbookState; workbookState: WorkbookState;
textareaRef: RefObject<HTMLTextAreaElement>; textareaRef: RefObject<HTMLTextAreaElement>;
setStyledFormula: (html: JSX.Element[]) => void;
} }
export const useKeyDown = ( export const useKeyDown = (
options: Options, options: Options,
): { onKeyDown: (event: KeyboardEvent) => void } => { ): { onKeyDown: (event: KeyboardEvent) => void } => {
const { const { model, onEditEnd, onTextUpdated, workbookState, textareaRef } =
model, options;
onEditEnd,
onTextUpdated,
workbookState,
textareaRef,
setStyledFormula,
} = options;
const onKeyDown = useCallback( const onKeyDown = useCallback(
(event: KeyboardEvent) => { (event: KeyboardEvent) => {
const { key, shiftKey, altKey } = event; const { key, shiftKey, altKey } = event;
@@ -80,7 +73,6 @@ export const useKeyDown = (
model.setSelectedCell(cell.row, cell.column + sign); model.setSelectedCell(cell.row, cell.column + sign);
if (textareaRef.current) { if (textareaRef.current) {
textareaRef.current.value = ""; textareaRef.current.value = "";
setStyledFormula(getFormulaHTML(model, "", "").html);
} }
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
@@ -164,7 +156,6 @@ export const useKeyDown = (
} }
if (textareaRef.current) { if (textareaRef.current) {
textareaRef.current.value = ""; textareaRef.current.value = "";
setStyledFormula(getFormulaHTML(model, "", "").html);
} }
onEditEnd(); onEditEnd();
@@ -233,7 +224,6 @@ export const useKeyDown = (
} }
if (textareaRef.current) { if (textareaRef.current) {
textareaRef.current.value = ""; textareaRef.current.value = "";
setStyledFormula(getFormulaHTML(model, "", "").html);
} }
onEditEnd(); onEditEnd();
@@ -307,7 +297,6 @@ export const useKeyDown = (
} }
if (textareaRef.current) { if (textareaRef.current) {
textareaRef.current.value = ""; textareaRef.current.value = "";
setStyledFormula(getFormulaHTML(model, "", "").html);
} }
onEditEnd(); onEditEnd();
@@ -377,7 +366,6 @@ export const useKeyDown = (
} }
if (textareaRef.current) { if (textareaRef.current) {
textareaRef.current.value = ""; textareaRef.current.value = "";
setStyledFormula(getFormulaHTML(model, "", "").html);
} }
onEditEnd(); onEditEnd();
@@ -405,14 +393,7 @@ export const useKeyDown = (
} }
} }
}, },
[ [model, onEditEnd, onTextUpdated, workbookState, textareaRef.current],
model,
setStyledFormula,
onEditEnd,
onTextUpdated,
workbookState,
textareaRef.current,
],
); );
return { onKeyDown }; return { onKeyDown };
}; };

View File

@@ -102,7 +102,6 @@ export function getColor(index: number, alpha = 1): string {
function getFormulaHTML( function getFormulaHTML(
model: Model, model: Model,
text: string, text: string,
referenceRange: string,
): { html: JSX.Element[]; activeRanges: ActiveRange[] } { ): { html: JSX.Element[]; activeRanges: ActiveRange[] } {
let html: JSX.Element[] = []; let html: JSX.Element[] = [];
const activeRanges: ActiveRange[] = []; const activeRanges: ActiveRange[] = [];
@@ -180,10 +179,6 @@ function getFormulaHTML(
html.push(<span key={index}>{formula.slice(start, end)}</span>); html.push(<span key={index}>{formula.slice(start, end)}</span>);
} }
} }
// If there is a reference range add it at the end
if (referenceRange !== "") {
html.push(<span key="reference">{referenceRange}</span>);
}
html = [<span key="equals">=</span>].concat(html); html = [<span key="equals">=</span>].concat(html);
} else { } else {
html = [<span key="single">{text}</span>]; html = [<span key="single">{text}</span>];

View File

@@ -54,8 +54,6 @@ function Worksheet(props: {
const ignoreScrollEventRef = useRef(false); const ignoreScrollEventRef = useRef(false);
const [originalText, setOriginalText] = useState("");
const { model, workbookState, refresh } = props; const { model, workbookState, refresh } = props;
const [clientWidth, clientHeight] = useWindowSize(); const [clientWidth, clientHeight] = useWindowSize();
@@ -330,7 +328,7 @@ function Worksheet(props: {
onDoubleClick={(event) => { onDoubleClick={(event) => {
// Starts editing cell // Starts editing cell
const { sheet, row, column } = model.getSelectedView(); const { sheet, row, column } = model.getSelectedView();
const text = model.getCellContent(sheet, row, column) || ""; const text = model.getCellContent(sheet, row, column);
const editorWidth = const editorWidth =
model.getColumnWidth(sheet, column) * COLUMN_WIDTH_SCALE; model.getColumnWidth(sheet, column) * COLUMN_WIDTH_SCALE;
const editorHeight = model.getRowHeight(sheet, row) * ROW_HEIGH_SCALE; const editorHeight = model.getRowHeight(sheet, row) * ROW_HEIGH_SCALE;
@@ -348,9 +346,8 @@ function Worksheet(props: {
editorWidth, editorWidth,
editorHeight, editorHeight,
}); });
setOriginalText(text);
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); // event.preventDefault();
props.refresh(); props.refresh();
}} }}
> >
@@ -358,7 +355,7 @@ function Worksheet(props: {
<CellOutline ref={cellOutline} /> <CellOutline ref={cellOutline} />
<EditorWrapper ref={editorElement}> <EditorWrapper ref={editorElement}>
<Editor <Editor
originalText={workbookState.getEditingText() || originalText} originalText={workbookState.getEditingText()}
onEditEnd={(): void => { onEditEnd={(): void => {
props.refresh(); props.refresh();
}} }}