Skip to content

Commit 2fd33a6

Browse files
jash-odoorrahir
authored andcommitted
[FIX] find_and_replace: updating the cell will update the search
Previously, updating a cell whose content was already searched in the Find and Replace, it wouldn't be considered a match. However, this commit aims to solve that issue. With this update, whenever a cell is updated, the search will also update and include the cell if it matches closes #2688 Taskid: 3422481 X-original-commit: 4924f7f Signed-off-by: Rémi Rahir (rar) <rar@odoo.com> Signed-off-by: Jaimin Shah (jash) <jash@odoo.com>
1 parent c431619 commit 2fd33a6

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

src/plugins/ui_feature/find_and_replace.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class FindAndReplacePlugin extends UIPlugin {
5353
searchFormulas: false,
5454
};
5555
private toSearch: string = "";
56+
private isSearchDirty = false;
5657

5758
// ---------------------------------------------------------------------------
5859
// Command Handling
@@ -84,13 +85,24 @@ export class FindAndReplacePlugin extends UIPlugin {
8485
case "ADD_COLUMNS_ROWS":
8586
this.clearSearch();
8687
break;
88+
case "EVALUATE_CELLS":
89+
case "UPDATE_CELL":
90+
this.isSearchDirty = true;
91+
break;
8792
case "ACTIVATE_SHEET":
8893
case "REFRESH_SEARCH":
8994
this.refreshSearch();
9095
break;
9196
}
9297
}
9398

99+
finalize() {
100+
if (this.isSearchDirty) {
101+
this.refreshSearch();
102+
this.isSearchDirty = false;
103+
}
104+
}
105+
94106
// ---------------------------------------------------------------------------
95107
// Getters
96108
// ---------------------------------------------------------------------------

tests/plugins/find_and_replace.test.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Model } from "../../src";
2+
import { args, functionRegistry } from "../../src/functions";
23
import { toZone } from "../../src/helpers";
34
import { SearchOptions } from "../../src/plugins/ui_feature/find_and_replace";
45
import {
@@ -52,28 +53,6 @@ describe("basic search", () => {
5253
expect(model.getters.getSelection().zones).toEqual([toZone("A6")]);
5354
});
5455

55-
test("modifying cells won't change the search", () => {
56-
model.dispatch("UPDATE_SEARCH", { toSearch: "1", searchOptions });
57-
let matches = model.getters.getSearchMatches();
58-
let matchIndex = model.getters.getCurrentSelectedMatchIndex();
59-
expect(matches.length).toBe(4);
60-
expect(matchIndex).toStrictEqual(0);
61-
expect(matches[0]).toStrictEqual({ col: 0, row: 1, selected: true });
62-
expect(matches[1]).toStrictEqual({ col: 0, row: 2, selected: false });
63-
expect(matches[2]).toStrictEqual({ col: 0, row: 3, selected: false });
64-
expect(matches[3]).toStrictEqual({ col: 0, row: 4, selected: false });
65-
setCellContent(model, "A2", "hello");
66-
setCellContent(model, "B1", "1");
67-
matches = model.getters.getSearchMatches();
68-
matchIndex = model.getters.getCurrentSelectedMatchIndex();
69-
expect(matches.length).toBe(4);
70-
expect(matchIndex).toStrictEqual(0);
71-
expect(matches[0]).toStrictEqual({ col: 0, row: 1, selected: true });
72-
expect(matches[1]).toStrictEqual({ col: 0, row: 2, selected: false });
73-
expect(matches[2]).toStrictEqual({ col: 0, row: 3, selected: false });
74-
expect(matches[3]).toStrictEqual({ col: 0, row: 4, selected: false });
75-
});
76-
7756
test("change the search", async () => {
7857
model.dispatch("UPDATE_SEARCH", { toSearch: "hello", searchOptions });
7958
model.dispatch("SELECT_SEARCH_NEXT_MATCH");
@@ -95,6 +74,54 @@ describe("basic search", () => {
9574
expect(matches[3]).toStrictEqual({ col: 0, row: 4, selected: false });
9675
});
9776

77+
test("refresh search when cell is updated", async () => {
78+
model.dispatch("UPDATE_SEARCH", { toSearch: "hello", searchOptions });
79+
let matches = model.getters.getSearchMatches();
80+
let matchIndex = model.getters.getCurrentSelectedMatchIndex();
81+
expect(matches).toHaveLength(2);
82+
expect(matchIndex).toStrictEqual(0);
83+
expect(matches[0]).toStrictEqual({ col: 0, row: 0, selected: true });
84+
expect(matches[1]).toStrictEqual({ col: 0, row: 1, selected: false });
85+
setCellContent(model, "B1", "hello");
86+
setCellContent(model, "B2", '="hello"');
87+
matches = model.getters.getSearchMatches();
88+
matchIndex = model.getters.getCurrentSelectedMatchIndex();
89+
expect(matches).toHaveLength(4);
90+
expect(matchIndex).toStrictEqual(0);
91+
expect(matches[0]).toStrictEqual({ col: 0, row: 0, selected: true });
92+
expect(matches[1]).toStrictEqual({ col: 1, row: 0, selected: false });
93+
expect(matches[2]).toStrictEqual({ col: 0, row: 1, selected: false });
94+
expect(matches[3]).toStrictEqual({ col: 1, row: 1, selected: false });
95+
});
96+
97+
test("refresh search when cell is update with EVALUATE_CELLS", async () => {
98+
model.dispatch("UPDATE_SEARCH", { toSearch: "hello", searchOptions });
99+
let matches = model.getters.getSearchMatches();
100+
let matchIndex = model.getters.getCurrentSelectedMatchIndex();
101+
expect(matches).toHaveLength(2);
102+
expect(matchIndex).toStrictEqual(0);
103+
expect(matches[0]).toStrictEqual({ col: 0, row: 0, selected: true });
104+
expect(matches[1]).toStrictEqual({ col: 0, row: 1, selected: false });
105+
let value = "3";
106+
functionRegistry.add("GETVALUE", {
107+
description: "Get value",
108+
compute: () => value,
109+
args: args(``),
110+
returns: ["NUMBER"],
111+
});
112+
setCellContent(model, "B1", "=GETVALUE()");
113+
value = '="hello"';
114+
model.dispatch("EVALUATE_CELLS", { sheetId: model.getters.getActiveSheetId() });
115+
matches = model.getters.getSearchMatches();
116+
matchIndex = model.getters.getCurrentSelectedMatchIndex();
117+
expect(getCellContent(model, "B1")).toBe('="hello"');
118+
expect(matches).toHaveLength(3);
119+
expect(matchIndex).toStrictEqual(0);
120+
expect(matches[0]).toStrictEqual({ col: 0, row: 0, selected: true });
121+
expect(matches[1]).toStrictEqual({ col: 1, row: 0, selected: false });
122+
expect(matches[2]).toStrictEqual({ col: 0, row: 1, selected: false });
123+
});
124+
98125
test("search on empty string does not match anything", () => {
99126
model.dispatch("UPDATE_SEARCH", { toSearch: "", searchOptions });
100127
expect(model.getters.getSearchMatches()).toHaveLength(0);

0 commit comments

Comments
 (0)