FIX: More fixes to the cell editor

* Font family is Inter, font size 13, line-width 22
* Correct vertical align for multiline text
* Entering multiline text sets the height of the row (!)
This commit is contained in:
Nicolás Hatcher
2024-10-12 23:57:48 +02:00
committed by Nicolás Hatcher Andrés
parent 585e594d8d
commit 9a46e5ccc7
4 changed files with 40 additions and 20 deletions

View File

@@ -345,13 +345,27 @@ impl UserModel {
self.evaluate_if_not_paused(); self.evaluate_if_not_paused();
let diff_list = vec![Diff::SetCellValue { let mut diff_list = vec![Diff::SetCellValue {
sheet, sheet,
row, row,
column, column,
new_value: value.to_string(), new_value: value.to_string(),
old_value: Box::new(old_value), old_value: Box::new(old_value),
}]; }];
let line_count = value.split("\n").count();
let row_height = self.model.get_row_height(sheet, row)?;
let cell_height = (line_count * 22) as f64;
if cell_height > row_height {
diff_list.push(Diff::SetRowHeight {
sheet,
row,
new_value: cell_height,
old_value: row_height,
});
self.model.set_row_height(sheet, row, cell_height)?;
}
self.push_diff_list(diff_list); self.push_diff_list(diff_list);
Ok(()) Ok(())
} }

View File

@@ -492,9 +492,11 @@ export default class WorksheetCanvas {
context.clip(); context.clip();
// Is there any better parameter? // Is there any better parameter?
const lineHeight = fontSize; const lineHeight = 22;
const lines = fullText.split("\n");
const lineCount = lines.length;
fullText.split("\n").forEach((text, line) => { lines.forEach((text, line) => {
const textWidth = context.measureText(text).width; const textWidth = context.measureText(text).width;
let textX: number; let textX: number;
let textY: number; let textY: number;
@@ -510,14 +512,18 @@ export default class WorksheetCanvas {
textX = padding + x + textWidth / 2; textX = padding + x + textWidth / 2;
} }
if (verticalAlign === "bottom") { if (verticalAlign === "bottom") {
textY = y + height - fontSize / 2 - verticalPadding; textY =
y +
height -
fontSize / 2 -
verticalPadding +
(line - lineCount + 1) * lineHeight;
} else if (verticalAlign === "center") { } else if (verticalAlign === "center") {
textY = y + height / 2; textY = y + height / 2 + (line + (1 - lineCount) / 2) * lineHeight;
} else { } else {
// aligned top // aligned top
textY = y + fontSize / 2 + verticalPadding; textY = y + fontSize / 2 + verticalPadding + line * lineHeight;
} }
textY += line * lineHeight;
context.fillText(text, textX, textY); context.fillText(text, textX, textY);
if (style.font) { if (style.font) {
if (style.font.u) { if (style.font.u) {
@@ -1102,8 +1108,7 @@ export default class WorksheetCanvas {
private drawCellEditor(): void { private drawCellEditor(): void {
const cell = this.workbookState.getEditingCell(); const cell = this.workbookState.getEditingCell();
const [selectedSheet, selectedRow, selectedColumn] = const selectedSheet = this.model.getSelectedSheet();
this.model.getSelectedCell();
const { editor } = this; const { editor } = this;
if (!cell || cell.sheet !== selectedSheet) { if (!cell || cell.sheet !== selectedSheet) {
// If the editing cell is not in the same sheet as the selected sheet // If the editing cell is not in the same sheet as the selected sheet
@@ -1112,7 +1117,7 @@ export default class WorksheetCanvas {
editor.style.top = "-9999px"; editor.style.top = "-9999px";
return; return;
} }
const { sheet, row, column } = cell; const { row, column } = cell;
// const style = this.model.getCellStyle( // const style = this.model.getCellStyle(
// selectedSheet, // selectedSheet,
// selectedRow, // selectedRow,

View File

@@ -95,13 +95,11 @@ const Editor = (options: EditorOptions) => {
const { onKeyDown } = useKeyDown({ const { onKeyDown } = useKeyDown({
model, model,
text,
onEditEnd, onEditEnd,
onTextUpdated, onTextUpdated,
workbookState, workbookState,
textareaRef, textareaRef,
setStyledFormula, setStyledFormula,
setText,
}); });
useEffect(() => { useEffect(() => {
@@ -124,6 +122,10 @@ const Editor = (options: EditorOptions) => {
if (scrollWidth > editorWidth - 5) { if (scrollWidth > editorWidth - 5) {
cell.editorWidth = scrollWidth + 10; cell.editorWidth = scrollWidth + 10;
} }
const scrollHeight = formulaRef.current.scrollHeight;
if (scrollHeight > editorHeight) {
cell.editorHeight = scrollHeight;
}
} }
}, [text, workbookState]); }, [text, workbookState]);
@@ -203,6 +205,8 @@ const Editor = (options: EditorOptions) => {
overflow: "hidden", overflow: "hidden",
display: showEditor, display: showEditor,
background: "#FFF", background: "#FFF",
fontFamily: "Inter",
fontSize: "13px"
}} }}
> >
<div <div

View File

@@ -6,12 +6,10 @@ import getFormulaHTML, { isInReferenceMode } from "./util";
interface Options { interface Options {
model: Model; model: Model;
text: string;
onEditEnd: () => void; onEditEnd: () => void;
onTextUpdated: () => void; onTextUpdated: () => void;
workbookState: WorkbookState; workbookState: WorkbookState;
textareaRef: RefObject<HTMLTextAreaElement>; textareaRef: RefObject<HTMLTextAreaElement>;
setText: (s: string) => void;
setStyledFormula: (html: JSX.Element[]) => void; setStyledFormula: (html: JSX.Element[]) => void;
} }
@@ -20,12 +18,10 @@ export const useKeyDown = (
): { onKeyDown: (event: KeyboardEvent) => void } => { ): { onKeyDown: (event: KeyboardEvent) => void } => {
const { const {
model, model,
text,
onEditEnd, onEditEnd,
onTextUpdated, onTextUpdated,
workbookState, workbookState,
textareaRef, textareaRef,
setText,
setStyledFormula, setStyledFormula,
} = options; } = options;
const onKeyDown = useCallback( const onKeyDown = useCallback(
@@ -42,13 +38,16 @@ export const useKeyDown = (
// new line // new line
const start = textarea.selectionStart; const start = textarea.selectionStart;
const end = textarea.selectionEnd; const end = textarea.selectionEnd;
const newText = `${text.slice(0, start)}\n${text.slice(end)}`; const value = textarea.value;
setText(newText); const newText = `${value.slice(0, start)}\n${value.slice(end)}`;
cell.text = newText;
workbookState.setEditingCell(cell);
setTimeout(() => { setTimeout(() => {
textarea.setSelectionRange(start + 1, start + 1); textarea.setSelectionRange(start + 1, start + 1);
}, 0); }, 0);
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
onTextUpdated();
return; return;
} }
event.stopPropagation(); event.stopPropagation();
@@ -408,8 +407,6 @@ export const useKeyDown = (
}, },
[ [
model, model,
text,
setText,
setStyledFormula, setStyledFormula,
onEditEnd, onEditEnd,
onTextUpdated, onTextUpdated,