Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Metrics explorer editor race fix (fixes a bunch of flaky tests) (#79128)

Egor Grushinece1b7cefb7f
diff --git a/e2e/test/scenarios/metrics/metrics-explorer.cy.spec.ts b/e2e/test/scenarios/metrics/metrics-explorer.cy.spec.ts
index da6b3592765..e6b4d6acc6b 100644
--- a/e2e/test/scenarios/metrics/metrics-explorer.cy.spec.ts
+++ b/e2e/test/scenarios/metrics/metrics-explorer.cy.spec.ts
@@ -164,30 +164,32 @@ const selectEntityPickerItem = (path: string | string[]) => {
const addMetricInputSequence = (
sequence: InputToken[],
- {
- runExpression = true,
- clearInput = false,
- skipRunCompletionWait = false,
- } = {},
+ { runExpression = true, clearInput = false } = {},
) => {
- H.MetricsViewer.searchInput().then(($input) => {
- if (clearInput) {
- cy.wrap($input).clear({ waitForAnimations: true });
- return;
- }
+ H.MetricsViewer.searchInput()
+ .should(($input) => {
+ expect(
+ $input.text().trim(),
+ "editor session is initialized",
+ ).not.to.equal("");
+ })
+ .then(($input) => {
+ if (clearInput) {
+ cy.wrap($input).clear({ waitForAnimations: true });
+ return;
+ }
- const currentText = $input.text().trim();
+ const currentText = $input.text().trim();
- if (
- currentText !== INPUT_PLACEHOLDER_TEXT &&
- currentText !== "" &&
- typeof sequence[0] !== "string"
- ) {
- cy.wrap($input).type("{end}, ", {
- waitForAnimations: true,
- });
- }
- });
+ if (
+ currentText !== INPUT_PLACEHOLDER_TEXT &&
+ typeof sequence[0] !== "string"
+ ) {
+ cy.wrap($input).type("{end}, ", {
+ waitForAnimations: true,
+ });
+ }
+ });
for (let i = 0; i < sequence.length; i++) {
const item = sequence[i];
@@ -203,12 +205,10 @@ const addMetricInputSequence = (
if (runExpression) {
runFormula();
- if (!skipRunCompletionWait) {
- // It is expected that the elements below do not exist after the expression ran successfully
- cy.findByTestId("metrics-viewer-search-input").should("not.exist");
- cy.findByTestId("run-expression-button").should("not.exist");
- cy.findByTestId("loading-indicator").should("not.exist");
- }
+ // It is expected that the elements below do not exist after the expression ran successfully
+ cy.findByTestId("metrics-viewer-search-input").should("not.exist");
+ cy.findByTestId("run-expression-button").should("not.exist");
+ cy.findByTestId("loading-indicator").should("not.exist");
}
};
@@ -217,29 +217,15 @@ const addMetricInputSequence = (
*/
const addMetric = (
nameOrPath: string | string[],
- {
- runExpression = true,
- clearInput = false,
- skipRunCompletionWait = false,
- } = {},
+ { runExpression = true, clearInput = false } = {},
) => {
addMetricInputSequence([{ nameOrPath }], {
runExpression,
clearInput,
- skipRunCompletionWait,
});
};
const runFormula = () => {
- cy.log("Make sure mini picker is closed before clicking Run");
- H.MetricsViewer.runButton().should("be.visible");
- cy.get("body").then(($body) => {
- if ($body.find('[data-testid="mini-picker"]').length > 0) {
- cy.realPress("Escape");
- cy.get('[data-testid="mini-picker"]').should("not.exist");
- }
- });
-
H.MetricsViewer.runButton().should("not.be.disabled").click();
};
@@ -2105,12 +2091,18 @@ describe("scenarios > metrics > explorer", () => {
beforeEach(() => {
H.MetricsViewer.goToViewer();
addMetricInputSequence([{ nameOrPath: "Count of orders" }]);
+ cy.wait("@dataset");
addMetricInputSequence([
",",
{ nameOrPath: "Count of orders" },
"+",
{ nameOrPath: testMeasurePath },
]);
+ // The second run re-renders the chart once the measure's dataset
+ // arrives; brushing during that re-render is swallowed because ECharts
+ // resets the brush cursor on setOption. Wait for the data first —
+ // ensureChartIsActive alone passes on the still-single-series chart.
+ cy.wait("@dataset");
});
it("should drill into more granular time dimensions on timeseries chart", () => {
diff --git a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/MetricSearchInput.tsx b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/MetricSearchInput.tsx
index eb35a0405e9..753816e8ccd 100644
--- a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/MetricSearchInput.tsx
+++ b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/MetricSearchInput.tsx
@@ -32,6 +32,7 @@ import {
MetricSearchDropdown,
type MetricSearchDropdownRef,
} from "../MetricSearchDropdown";
+import { buildFullTextWithIdentities } from "../utils";
import S from "./MetricSearchInput.module.css";
import { buildEditorExtensions } from "./editorExtensions";
@@ -88,7 +89,6 @@ export function MetricSearchInput({
});
const {
- editText,
isFocused,
isOpen,
setIsOpen,
@@ -98,6 +98,7 @@ export function MetricSearchInput({
isExpressionDirty,
pendingFocusRef,
handleInputFocus,
+ handleEditorCreate,
handleInputBlur,
handleEditExpression,
handleChange,
@@ -135,6 +136,14 @@ export function MetricSearchInput({
() => computeMetricSlots(formulaEntities),
[formulaEntities],
);
+ // The initial document for a fresh editor: the same text useFormulaEditor
+ // initializes every editing session with. Once the session is active the
+ // document is the source of truth — @uiw/react-codemirror's value-sync
+ // replacement is cancelled by the trustedDocChangesOnly transaction filter.
+ const sessionText = useMemo(
+ () => buildFullTextWithIdentities(formulaEntities, metricNames).text,
+ [formulaEntities, metricNames],
+ );
return (
<Flex
@@ -250,8 +259,9 @@ export function MetricSearchInput({
ref={editorRef}
basicSetup={false}
autoFocus
- value={editText}
+ value={sessionText}
onChange={handleChange}
+ onCreateEditor={handleEditorCreate}
extensions={editorExtensions}
data-testid="metrics-viewer-search-input"
/>
diff --git a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/editorExtensions.ts b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/editorExtensions.ts
index 50aa25a8f1d..9ac603bdde2 100644
--- a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/editorExtensions.ts
+++ b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/editorExtensions.ts
@@ -8,6 +8,7 @@ import type { MetricSearchDropdownRef } from "../MetricSearchDropdown";
import { errorHighlight } from "./errorHighlight";
import { metricTokenHighlight } from "./metricTokenHighlight";
import { operatorHighlight } from "./operatorHighlight";
+import { trustedDocChangesOnly } from "./trustedDocChangesOnly";
type EditorExtensionRefs = {
handleRunRef: { current: () => void };
@@ -19,6 +20,7 @@ export function buildEditorExtensions(
refs: EditorExtensionRefs,
): Extension[] {
return [
+ trustedDocChangesOnly,
history(),
operatorHighlight,
errorHighlight,
diff --git a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/metricTokenHighlight.ts b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/metricTokenHighlight.ts
index c4a9a77784d..2a0c7be3d63 100644
--- a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/metricTokenHighlight.ts
+++ b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/metricTokenHighlight.ts
@@ -244,6 +244,7 @@ const metricTokenKeymap = keymap.of([
}
view.dispatch({
changes: { from: match.from, to: match.to },
+ userEvent: "delete",
});
return true;
},
@@ -263,6 +264,7 @@ const metricTokenKeymap = keymap.of([
}
view.dispatch({
changes: { from: match.from, to: match.to },
+ userEvent: "delete",
});
return true;
},
diff --git a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/trustedDocChangesOnly.ts b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/trustedDocChangesOnly.ts
new file mode 100644
index 00000000000..610c456a83c
--- /dev/null
+++ b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/trustedDocChangesOnly.ts
@@ -0,0 +1,18 @@
+import { Annotation, EditorState, Transaction } from "@codemirror/state";
+
+export const programmaticFormulaUpdate = Annotation.define<boolean>();
+
+// The document may only be changed by user input or by our own annotated
+// dispatches. In particular this cancels @uiw/react-codemirror's value-sync
+// replacement, which would destroy the metric identity RangeSet.
+export const trustedDocChangesOnly = EditorState.transactionFilter.of((tr) => {
+ // Only document changes are guarded — selection moves, identity/decoration
+ // effects and reconfigure pass freely.
+ if (!tr.docChanged) {
+ return tr;
+ }
+ const isTrusted =
+ tr.annotation(programmaticFormulaUpdate) ||
+ tr.annotation(Transaction.userEvent) !== undefined;
+ return isTrusted ? tr : [];
+});
diff --git a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/useFormulaEditor.ts b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/useFormulaEditor.ts
index 436b69a17ff..aafde1ed46e 100644
--- a/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/useFormulaEditor.ts
+++ b/frontend/src/metabase/metrics-viewer/components/MetricSearch/MetricSearchInput/useFormulaEditor.ts
@@ -1,5 +1,5 @@
-import { isolateHistory } from "@codemirror/commands";
-import { EditorSelection } from "@codemirror/state";
+import { EditorSelection, Transaction } from "@codemirror/state";
+import type { EditorView } from "@codemirror/view";
import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import type { MutableRefObject } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
@@ -37,6 +37,7 @@ import {
readMetricIdentities,
setMetricIdentities,
} from "./metricTokenHighlight";
+import { programmaticFormulaUpdate } from "./trustedDocChangesOnly";
type UseFormulaEditorParams = {
formulaEntities: MetricsViewerFormulaEntity[];
@@ -58,7 +59,6 @@ type UseFormulaEditorParams = {
};
export type UseFormulaEditorResult = {
- editText: string;
isFocused: boolean;
isOpen: boolean;
setIsOpen: (open: boolean) => void;
@@ -68,6 +68,7 @@ export type UseFormulaEditorResult = {
isExpressionDirty: boolean;
pendingFocusRef: MutableRefObject<boolean>;
handleInputFocus: () => void;
+ handleEditorCreate: (view: EditorView) => void;
handleInputBlur: (event: React.FocusEvent) => void;
handleEditExpression: (entityIndex: number) => void;
handleChange: (newText: string) => void;
@@ -93,8 +94,6 @@ export function useFormulaEditor({
containerRef,
dropdownRef,
}: UseFormulaEditorParams): UseFormulaEditorResult {
- // editText is the full expression as plain text — only meaningful while focused
- const [editText, setEditText] = useState("");
// currentWord is the word under the cursor, used as the dropdown search query
const [currentWord, setCurrentWord] = useState("");
const [isOpen, setIsOpen] = useState(false);
@@ -114,8 +113,7 @@ export function useFormulaEditor({
// caret lands at the end of that expression instead of the full formula.
const pendingCaretPositionRef = useRef<number | null>(null);
// Refs for reading latest values in callbacks without stale closures
- const editTextRef = useRef(editText);
- editTextRef.current = editText;
+ const editTextRef = useRef("");
const formulaEntitiesRef = useRef(formulaEntities);
formulaEntitiesRef.current = formulaEntities;
const definitionsRef = useRef(definitions);
@@ -128,16 +126,10 @@ export function useFormulaEditor({
const handleRunRef = useRef<() => void>(() => {});
// Text captured at focus time — used to detect whether the user actually
// changed the expression and therefore needs to click "Run" to commit.
- const [textAtFocus, setTextAtFocus] = useState("");
- const textAtFocusRef = useRef(textAtFocus);
- textAtFocusRef.current = textAtFocus;
- const pendingMetricIdentitiesRef = useRef<MetricIdentityEntry[] | null>(null);
- const [editingSessionIdentities, setEditingSessionIdentities] = useState<
- MetricIdentityEntry[]
- >([]);
+ const textAtFocusRef = useRef("");
+ const isSyncingDocRef = useRef(false);
// Explicitly tracks whether the expression was modified during this editing
- // session (metric selected from dropdown, or text typed). Avoids timing
- // issues with comparing editText vs textAtFocus across async state updates.
+ // session (metric selected from dropdown, or text typed).
const [isExpressionDirty, setIsExpressionDirty] = useState(false);
// Clean up parens per expression entry (only when not actively editing)
@@ -171,64 +163,134 @@ export function useFormulaEditor({
}
}, [isFocused, editorRef]);
- const handleInputFocus = useCallback(() => {
- if (isCollapsingRef.current) {
- return;
- }
- // If an editing session is already active (e.g. focus returning from a
- // dropdown item click via view.focus()), do not reset the text or the
- // committed baseline.
- if (isEditingSessionActiveRef.current) {
- return;
- }
- isEditingSessionActiveRef.current = true;
- const { text: fullText, identities: initialIdentities } =
- buildFullTextWithIdentities(
- formulaEntitiesRef.current,
- metricNamesRef.current,
- );
- setEditingSessionIdentities(initialIdentities);
+ // Deferred via requestMeasure because coordsAtPos needs the freshly created
+ // editor to be laid out and measured first. A destroyed view (e.g. a
+ // StrictMode remount) cancels its pending measure, so no staleness guards
+ // are needed beyond the session-active check.
+ const scheduleDropdownAtCaret = useCallback(
+ (view: EditorView, caretPos: number, shouldOpenDropdown: boolean) => {
+ view.requestMeasure({
+ read: (measuredView) =>
+ measuredView.coordsAtPos(
+ Math.min(caretPos, measuredView.state.doc.length),
+ ),
+ write: (coords) => {
+ if (!isEditingSessionActiveRef.current) {
+ return;
+ }
+ if (coords) {
+ setAnchorRect({ left: coords.left, top: coords.bottom });
+ }
+ if (shouldOpenDropdown) {
+ setCurrentWord("");
+ setIsOpen(true);
+ }
+ },
+ });
+ },
+ [],
+ );
- const requestedCaret = pendingCaretPositionRef.current;
- const shouldOpenDropdown = requestedCaret == null;
+ /** One transaction that fixes the doc (only if it diverges), sets the caret, and installs identities atomically. */
+ const syncViewWithSession = useCallback(
+ (
+ view: EditorView,
+ fullText: string,
+ identities: MetricIdentityEntry[],
+ caretPos: number,
+ ) => {
+ const currentDoc = view.state.doc.toString();
+ // Replacing the doc with identical text still wipes identities
+ // (TrackDel maps positions through the deletion), so only dispatch
+ // changes when the text actually diverges.
+ const changes =
+ currentDoc !== fullText
+ ? { from: 0, to: currentDoc.length, insert: fullText }
+ : undefined;
+ isSyncingDocRef.current = true;
+ view.dispatch({
+ changes,
+ selection: EditorSelection.cursor(caretPos),
+ effects: setMetricIdentities.of(identitiesFromEntries(identities)),
+ annotations: [
+ Transaction.addToHistory.of(false),
+ programmaticFormulaUpdate.of(true),
+ ],
+ });
+ isSyncingDocRef.current = false;
+ },
+ [],
+ );
- setTextAtFocus(fullText);
- setIsFocused(true);
+ /** Session text, identities and caret derived from the committed entities and the requested caret. */
+ const planEditingSession = useCallback(() => {
+ const { text: fullText, identities } = buildFullTextWithIdentities(
+ formulaEntitiesRef.current,
+ metricNamesRef.current,
+ );
+ const requestedCaret = pendingCaretPositionRef.current;
+ return {
+ fullText,
+ identities,
+ caretPos: Math.min(requestedCaret ?? Infinity, fullText.length),
+ shouldOpenDropdown: requestedCaret === null,
+ };
+ }, [metricNamesRef]);
+
+ /** Resets the React side of a session: the committed baseline and the editing flags. */
+ const applySessionState = useCallback((fullText: string) => {
+ textAtFocusRef.current = fullText;
editTextRef.current = fullText;
- setEditText(fullText);
+ setIsFocused(true);
setValidationError(null);
setIsExpressionDirty(false);
- // After CodeMirror renders the initial text, position the caret and
- // create an undo boundary. The @uiw/react-codemirror value sync adds
- // to the undo history, so without isolateHistory("before"), a quick
- // Cmd+Z after deleting a metric token would undo both the deletion
- // AND the initial text insertion (they'd be grouped together).
- setTimeout(() => {
- const view = editorRef.current?.view;
- if (view) {
- const docLen = view.state.doc.length;
- pendingCaretPositionRef.current = null;
- const caretPos =
- requestedCaret != null
- ? Math.min(Math.max(requestedCaret, 0), docLen)
- : docLen;
- const identities = identitiesFromEntries(initialIdentities);
- view.dispatch({
- selection: EditorSelection.cursor(caretPos),
- effects: setMetricIdentities.of(identities),
- annotations: isolateHistory.of("full"),
- });
- const coords = view.coordsAtPos(caretPos);
- if (coords) {
- setAnchorRect({ left: coords.left, top: coords.bottom });
- }
- if (shouldOpenDropdown) {
- setCurrentWord("");
- setIsOpen(true);
- }
+ }, []);
+
+ const initializeEditingSession = useCallback(
+ (viewOverride?: EditorView) => {
+ if (isCollapsingRef.current) {
+ return;
}
- }, 0);
- }, [editorRef, metricNamesRef]);
+ // If an editing session is already active (e.g. focus returning from a
+ // dropdown item click via view.focus()), do not reset the text or the
+ // committed baseline.
+ if (isEditingSessionActiveRef.current) {
+ return;
+ }
+ isEditingSessionActiveRef.current = true;
+
+ const { fullText, identities, caretPos, shouldOpenDropdown } =
+ planEditingSession();
+ applySessionState(fullText);
+
+ const view = viewOverride ?? editorRef.current?.view;
+ if (!view) {
+ return;
+ }
+
+ syncViewWithSession(view, fullText, identities, caretPos);
+ scheduleDropdownAtCaret(view, caretPos, shouldOpenDropdown);
+ },
+ [
+ editorRef,
+ planEditingSession,
+ applySessionState,
+ syncViewWithSession,
+ scheduleDropdownAtCaret,
+ ],
+ );
+
+ const handleInputFocus = useCallback(() => {
+ initializeEditingSession();
+ }, [initializeEditingSession]);
+
+ const handleEditorCreate = useCallback(
+ (view: EditorView) => {
+ isEditingSessionActiveRef.current = false;
+ initializeEditingSession(view);
+ },
+ [initializeEditingSession],
+ );
/**
* Transition into focused-formula mode and place the caret at the end of
@@ -254,8 +316,8 @@ export function useFormulaEditor({
/** Commits the current text: parses formula entities, removes unreferenced metrics, and collapses. */
const commitAndCollapse = useCallback(() => {
- const newText = editTextRef.current;
const view = editorRef.current?.view;
+ const newText = view ? view.state.doc.toString() : editTextRef.current;
const trackedIdentities = view ? readMetricIdentities(view) : [];
const parsedEntities = parseFullText(
@@ -304,16 +366,25 @@ export function useFormulaEditor({
onFormulaEntitiesChange(reconciledEntities, slotMapping);
isCollapsingRef.current = true;
isEditingSessionActiveRef.current = false;
+ pendingCaretPositionRef.current = null;
textAtFocusRef.current = newText;
- setTextAtFocus(newText);
setIsFocused(false);
setIsOpen(false);
setCurrentWord("");
editTextRef.current = "";
- setEditText("");
setValidationError(null);
setIsExpressionDirty(false);
- setEditingSessionIdentities([]);
+ if (view && view.state.doc.length > 0) {
+ isSyncingDocRef.current = true;
+ view.dispatch({
+ changes: { from: 0, to: view.state.doc.length },
+ annotations: [
+ Transaction.addToHistory.of(false),
+ programmaticFormulaUpdate.of(true),
+ ],
+ });
+ isSyncingDocRef.current = false;
+ }
}, [
editorRef,
metricNamesRef,
@@ -324,28 +395,29 @@ export function useFormulaEditor({
const handleInputBlur = useCallback(
(event: React.FocusEvent) => {
+ const view = editorRef.current?.view;
+ const docText = view ? view.state.doc.toString() : editTextRef.current;
// If the text hasn't changed since focus, collapse back to pills view
// without requiring the user to click "Run".
if (
- editTextRef.current === textAtFocusRef.current &&
+ docText === textAtFocusRef.current &&
!dropdownRef.current?.containerRef.current?.contains(
event.relatedTarget,
)
) {
isEditingSessionActiveRef.current = false;
+ pendingCaretPositionRef.current = null;
setIsFocused(false);
setIsOpen(false);
setCurrentWord("");
- setEditText("");
setValidationError(null);
setIsExpressionDirty(false);
return;
}
- const view = editorRef.current?.view;
const identities = view ? readMetricIdentities(view) : [];
const invalidRanges = findInvalidRanges(
- editTextRef.current,
+ docText,
metricNamesRef.current,
identities,
);
@@ -361,12 +433,11 @@ export function useFormulaEditor({
const handleChange = useCallback(
(newText: string) => {
- const view = editorRef.current?.view;
- if (view) {
- pendingMetricIdentitiesRef.current = readMetricIdentities(view);
+ if (isSyncingDocRef.current) {
+ return;
}
+ const view = editorRef.current?.view;
editTextRef.current = newText;
- setEditText(newText);
setValidationError(null);
if (isCollapsingRef.current) {
return;
@@ -397,59 +468,6 @@ export function useFormulaEditor({
[editorRef, metricNamesRef],
);
- /**
- * @uiw/react-codemirror can sync the controlled value with a full doc
- * replacement, which drops our custom identity field. Restore identities that
- * still point at the same metric text so validation does not reject them.
- */
- useEffect(() => {
- const view = editorRef.current?.view;
- const pendingIdentities = pendingMetricIdentitiesRef.current;
-
- if (!view || pendingIdentities == null) {
- return;
- }
-
- if (view.state.doc.toString() !== editText) {
- return;
- }
-
- const currentIdentities = readMetricIdentities(view);
- const identityByPosition = new Map<string, MetricIdentityEntry>(
- currentIdentities.map((identity): [string, MetricIdentityEntry] => [
- `${identity.from}:${identity.to}`,
- identity,
- ]),
- );
-
- const recoverableIdentities =
- editText === textAtFocusRef.current
- ? [...editingSessionIdentities, ...pendingIdentities]
- : pendingIdentities;
-
- for (const identity of recoverableIdentities) {
- const positionKey = `${identity.from}:${identity.to}`;
- const metricName = metricNamesRef.current[identity.sourceId];
- if (
- !identityByPosition.has(positionKey) &&
- metricName != null &&
- view.state.doc.sliceString(identity.from, identity.to) === metricName
- ) {
- identityByPosition.set(positionKey, identity);
- }
- }
-
- const recoveredIdentities = Array.from(identityByPosition.values());
- if (currentIdentities.length < recoveredIdentities.length) {
- view.dispatch({
- effects: setMetricIdentities.of(
- identitiesFromEntries(recoveredIdentities),
- ),
- });
- }
- pendingMetricIdentitiesRef.current = null;
- }, [editText, editingSessionIdentities, editorRef, metricNamesRef]);
-
const handleSelect = useCallback(
(metric: SelectedMetric) => {
const view = editorRef.current?.view;
@@ -491,9 +509,6 @@ export function useFormulaEditor({
? createMetricSourceId(metric.id)
: createMeasureSourceId(metric.id);
- // Dispatch through the view (not setEditText) — the value-prop sync
- // in @uiw/react-codemirror does a full doc replacement that destroys
- // all RangeSet-tracked identities.
view.dispatch({
changes: { from: replaceFrom, to: replaceTo, insert: insertText },
selection: EditorSelection.cursor(newCursorPos),
@@ -503,6 +518,7 @@ export function useFormulaEditor({
sourceId,
definition: definitionsRef.current[sourceId]?.definition ?? null,
}),
+ annotations: programmaticFormulaUpdate.of(true),
});
editTextRef.current = view.state.doc.toString();
@@ -661,9 +677,10 @@ export function useFormulaEditor({
/** Validate the expression and either show an error or commit + run the query. */
const handleRun = useCallback(() => {
const view = editorRef.current?.view;
+ const docText = view ? view.state.doc.toString() : editTextRef.current;
const identities = view ? readMetricIdentities(view) : [];
const invalidRanges = findInvalidRanges(
- editTextRef.current,
+ docText,
metricNamesRef.current,
identities,
);
@@ -687,7 +704,7 @@ export function useFormulaEditor({
const ranges =
validationError !== null
? findInvalidRanges(
- editTextRef.current,
+ view.state.doc.toString(),
metricNamesRef.current,
identities,
)
@@ -696,7 +713,6 @@ export function useFormulaEditor({
}, [validationError, editorRef, metricNamesRef]);
return {
- editText,
isFocused,
isOpen,
setIsOpen,
@@ -706,6 +722,7 @@ export function useFormulaEditor({
isExpressionDirty,
pendingFocusRef,
handleInputFocus,
+ handleEditorCreate,
handleInputBlur,
handleEditExpression,
handleChange,