@syncfusion/ej2-spreadsheet
Version:
Feature-rich JavaScript Spreadsheet (Excel) control with built-in support for selection, editing, formatting, importing and exporting to Excel
97 lines (76 loc) • 7.8 kB
Markdown
# Formula Bar Module data flow
## What It Does
The Formula Bar module manages the name box, formula input area, and Insert Function dialog in the Spreadsheet UI. It synchronizes the active cell's display/formula with the editor, manages edit lifecycle (start/refresh/submit), and integrates function insertion and defined-name navigation with workbook operations.
Primary implementation file: `src/spreadsheet/integrations/formula-bar.ts`.
## Entry Points (UI)
**Class:** `FormulaBar`
- Listens / handles: `formulaBar`, `click`, `keyDown`, `keyUp`, `renderInsertDlg`, `selectionComplete`, `mouseUpAfterSelection`, `mouseDown`, `formulaBarOperation`, `enableFormulaInput`, `isFormulaBarEdit`.
- Key handlers:
- `createFormulaBar()` — build name box (ComboBox), insert-function button, and formula `textarea` UI; attach event handlers.
- `formulaBarUpdateHandler()` — update formula input when selection changes or on mousemove/mouseout; fetch cell via `getData` and compute display text.
- `startFormulaBarEdit()` — validate protection/readonly and begin edit via `editOperation` notifications.
- `renderInsertDlg()` / `selectFormula()` — show insert-function dialog, get `workbookFormulaOperation` data, and insert selected function into editor.
- `keyDownHandler()` / `keyUpHandler()` — manage editor refresh, multiline entry, and live refresh notifications for edit engine.
- Name-box handlers: `nameBoxBeforeOpen()`, `nameBoxBlur()`, `nameBoxSelect()` — manage defined-name navigation and selection.
- `editOperationHandler()` — react to `formulaBarOperation`/`editOperation` messages to refresh the bar or return the textarea element.
- `destroy()` / `removeEventListener()` — cleanup components and listeners.
## Entry Points (Core Interactions)
- Interacts with workbook services via notifications/events: `editOperation`, `formulaOperation`, `workbookFormulaOperation`, `formulaBarOperation`, `getData`, `selectionComplete`, `addressHandle`, `editValue`, `readonlyAlert`, `editAlert`.
- Uses `getData()` to fetch `CellModel` for current address and `getFormatFromType` / `isCustomDateTime` to format display text.
- Insert-function dialog queries formula metadata via `workbookFormulaOperation` and uses `Dialog` service for UI.
## ASCII Core Logic Flow
User changes selection or activates cell → `selectionComplete` → `formulaBarUpdateHandler()`
↓
If single-cell: call `getData()` → `getFormulaBarValue(cell)` → format value or show formula → update textarea and name box
↓
User types in formula bar → `keyDownHandler`/`keyUpHandler` → `parent.notify(editOperation, { refreshEditor/refreshEditorElem })` (live editor sync)
↓
User commits or inserts function → `editOperation` / `formulaBarOperation` notifications (`startEdit`/`refreshFormulabar`) → workbook applies edit (undo/redo recorded by edit pipeline)
↓
User opens Insert Function → `renderInsertDlg()` → query `workbookFormulaOperation` → dialog populated → `selectFormula()` inserts function via `editOperation` and closes dialog
## Operations (functions & responsibilities)
- `createFormulaBar(args)`
- Render UI: name box (`ComboBox`), insert function button (`.e-insert-function`), expand toggle, and formula `textarea` (`id: {id}_formula_input`).
- Apply localization for titles and ARIA labels.
- `formulaBarUpdateHandler(e)`
- For pointermove: show range size label; for pointerup/selectionComplete: retrieve active cell value via `getData`, compute display via `getFormulaBarValue()`, and update formula input and name box.
- `getFormulaBarValue(cell)`
- Prefer `cell.formula` if present; otherwise produce display text respecting custom date/time format, percent formatting, hyperlink, and locale-specific decimal separators.
- `startFormulaBarEdit(e)`
- Validate sheet protection and cell locking (`isReadOnly`, `isLocked`), notify `readonlyAlert` or `editAlert` as needed, and call `editOperation` to start or refresh edit mode.
- Insert Function dialog (`renderInsertDlg`, `dialogOpen`, `selectFormula`, `dialogClose`, `dialogBeforeClose`)
- Build category dropdown (`DropDownList`) and `ListView` of formulas using `workbookFormulaOperation` queries; present dialog via `Dialog` service; on select, notify `editOperation` to insert function text and `formulaBarOperation` to refresh.
- Name box (defined names)
- `nameBoxBeforeOpen` / `nameBoxBlur` / `nameBoxSelect`: manage editing state, populate defined names via `formulaOperation` notifications, and navigate to selected range using `addressHandle`/`updateSelectedRange`.
- Keyboard handlers
- `keyDownHandler` throttles editor refresh; `keyUpHandler` supports Alt+Enter newline insertion and syncs edited value with `editOperation`.
- Lifecycle
- `addEventListener()` / `removeEventListener()` register/unregister global notifications; `destroy()` disposes UI components (`ComboBox`, dialog) and detaches DOM.
## Validation & Safety
- Protection & readonly: `startFormulaBarEdit()` checks `sheet.isProtected`, `isLocked` and uses `readonlyAlert`/`editAlert` to block edits.
- Edit state: prevents starting a new name-box open when name editing in-progress (`e-name-editing` class).
- Localization & accessibility: titles and ARIA labels obtained from `L10n`; dialog Ok button ARIA labels set; focus management returns focus to spreadsheet after operations.
- Dialog cancel gating: `dialogBeforeOpen` allows external cancellation via `dialogBeforeOpen` event.
- Resource cleanup: `destroy()` removes listeners and destroys child components to avoid leaks.
## Desired Outputs
User-facing
- Name box (`ComboBox`) showing defined names or active cell reference; id pattern: `{id}_name_box` and class `e-name-box`.
- Insert Function button (`.e-insert-function`) and Insert Function dialog (`.e-spreadsheet-function-dlg`) with category dropdown (`.e-ss-formula-category`) and formula list (`.e-formula-list`).
- Formula `textarea` with id `{id}_formula_input`, expand toggle (`.e-drop-icon`) and submit button `.e-formula-submit` shown while editing.
- Multiline editing (Alt+Enter), live editor refresh while typing, and proper focus/keyboard behavior.
System-level
- Events & notifications used:
- Incoming/UI: `formulaBar`, `click`, `renderInsertDlg`, `selectionComplete`, `mouseUpAfterSelection`, `mouseDown`, `keyDown`, `keyUp`, `formulaBarOperation`, `editOperation`, `formulaOperation`, `workbookFormulaOperation`.
- Data access: `getData(sheetName!A1)` to fetch `CellModel` for display and formula extraction.
- Actions: `editOperation` notifications (e.g., `startEdit`, `refreshEditor`) integrate with the edit action pipeline and undo/redo.
- Model artifacts & DOM hooks:
- IDs: `{id}_formula_input`, `{id}_name_box`, `{id}_category_content`, `{id}_formula_category`, `{id}_formula_list`, `{id}_description_content`.
- CSS/classes: `e-formula-bar-panel`, `e-formula-bar`, `e-insert-function`, `e-formula-description`, `e-formula-list`, `e-spreadsheet-function-dlg`, `e-name-box`, `e-drop-icon`, `e-formula-submit`.
- Undo/Redo: edits initiated via `editOperation` are expected to participate in the workbook action pipeline so commits can be undone/redone.
## Implementation Notes (short)
- Keep formula bar UI isolated: it should only notify `editOperation`/`formulaBarOperation` and let core handle persistence and undo/redo.
- Use `getData()` for async retrieval of `CellModel`; guard against missing `parent` during async callbacks.
- Respect sheet protection and cell-level locking before starting edits.
- Localize all user-visible strings via `L10n` and ensure ARIA labels are updated when values change.
- Ensure `destroy()` always runs on teardown to release `ComboBox`, `Dialog`, and event handlers.
---