From 7bd4c2d17bf8eaae339d0d8ec6805e0659ae03d3 Mon Sep 17 00:00:00 2001 From: AnhNgoo <110978888+AnhNgoo@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:25:02 +0700 Subject: [PATCH] fix(filter): distinguish formatted displays in cell tokens and deduplicate slicer tiles --- crates/engine/src/cmd/data.rs | 2 +- crates/engine/src/cmd/data/filter.rs | 106 +++++++++++++++++++++------ crates/engine/src/cmd/slicer.rs | 41 ++++++----- 3 files changed, 106 insertions(+), 43 deletions(-) diff --git a/crates/engine/src/cmd/data.rs b/crates/engine/src/cmd/data.rs index c43f314d3..9f48486e5 100644 --- a/crates/engine/src/cmd/data.rs +++ b/crates/engine/src/cmd/data.rs @@ -24,7 +24,7 @@ mod tools; mod validation; pub use filter::{FilterChoice, filter_by_cell_params, filter_choices, filter_choices_params, filter_values}; -pub(crate) use filter::{apply_filter, filter_dropdown_blank, grow_autofilter, migrate_loaded_autofilters}; +pub(crate) use filter::{apply_filter, grow_autofilter, migrate_loaded_autofilters, slicer_identity_token, slicer_tile_text}; use filter::{clear_filter, filter_by, reapply, toggle_filter}; use grouping::{detail, group, outline_settings, show_levels, subtotal}; pub use sort::sort_context; diff --git a/crates/engine/src/cmd/data/filter.rs b/crates/engine/src/cmd/data/filter.rs index d23fdad96..597b29681 100644 --- a/crates/engine/src/cmd/data/filter.rs +++ b/crates/engine/src/cmd/data/filter.rs @@ -219,52 +219,101 @@ fn visible_formula_text<'a>(wb: &Workbook, sh: &'a Sheet, c: CellRef) -> Option< (!crate::display::formula_hidden(wb, sh, c)).then_some(f.text.as_str()) } -/// Canonical checklist identity for `c`: `"v:"` when formulas and constants share a value -/// group, or `"f::"` / `"c:"` when formulas are distinguished. -fn cell_identity_token(wb: &Workbook, sh: &Sheet, c: CellRef, v: &Value, split_formulas: bool) -> String { +/// Mode-independent `::` base key of a non-blank cell. +/// +/// Cells with distinct non-empty formatted displays (`42` vs `42.00`) stay distinct, whereas a cell +/// whose custom format displays only whitespace falls back to its underlying value (`42`), keeping +/// General `42` stable if later formatted as spaces. +fn cell_base_token(wb: &Workbook, sh: &Sheet, c: CellRef, v: &Value) -> String { let typed = typed_value_token(v); + let vtext = crate::display::value_text_as(wb, v, wb.styles.get(sh.style_id(c)).num_fmt.as_str()); + let eff = if vtext.trim().is_empty() { v.display().trim().to_ascii_lowercase() } else { vtext.trim().to_ascii_lowercase() }; + format!("{typed}:{}", pct_encode(&eff)) +} + +/// Slicer tile text for `c`: trimmed `cell_text` (respecting Show Formulas), falling back to the +/// underlying value only when a non-blank cell's formatted display is empty/whitespace. +pub(crate) fn slicer_tile_text(wb: &Workbook, sh: &Sheet, c: CellRef) -> Option { + let v = sh.value(c); + if filter_dropdown_blank(&v) { + return None; + } + let text = crate::display::cell_text(wb, sh, c); + let tile = if text.trim().is_empty() { v.display().trim().to_string() } else { text.trim().to_string() }; + (!tile.is_empty()).then_some(tile) +} + +/// Encodes a slicer tile's display key as an identity token (`s:`). +pub(crate) fn slicer_identity_token(tile: &str) -> String { + format!("s:{}", pct_encode(&tile.trim().to_ascii_lowercase())) +} + +/// Canonical checklist identity for `c`: `"d:"` when formulas and constants share a value +/// group, `"f::"` / `"c:"` when formulas are distinguished, or `"w::"` +/// when disambiguating legacy formatted-whitespace criteria. +fn cell_identity_token(wb: &Workbook, sh: &Sheet, c: CellRef, v: &Value, text: &str, split_formulas: bool, split_fmtws: bool) -> String { + let base = cell_base_token(wb, sh, c, v); + if split_fmtws && text.trim().is_empty() { + return format!("w:{base}:{}", pct_encode(&text.to_ascii_lowercase())); + } if !split_formulas { - return format!("v:{typed}"); + return format!("d:{base}"); } match visible_formula_text(wb, sh, c) { - Some(ft) => format!("f:{typed}:{}", pct_encode(&ft.trim().to_ascii_lowercase())), - None => format!("c:{typed}"), + Some(ft) => format!("f:{base}:{}", pct_encode(&ft.trim().to_ascii_lowercase())), + None => format!("c:{base}"), } } -/// Whether stored checklist identity `id` distinguishes formula vs constant rows for `typed`. -fn id_splits_typed(id: &str, typed: &str) -> bool { - id.strip_prefix("c:") == Some(typed) || id.strip_prefix("f:").and_then(|rest| rest.strip_prefix(typed)).is_some_and(|rest| rest.starts_with(':')) +/// Whether stored checklist identity `id` distinguishes formula vs constant rows for `base`. +fn id_splits_base(id: &str, base: &str) -> bool { + id.strip_prefix("c:") == Some(base) || id.strip_prefix("f:").and_then(|rest| rest.strip_prefix(base)).is_some_and(|rest| rest.starts_with(':')) } -/// Checks whether a non-blank cell matches any stored checklist identity (`v:`, `c:`, or `f:`). -fn row_matches_identities(wb: &Workbook, sh: &Sheet, c: CellRef, v: &Value, identities: &[String]) -> bool { - let typed = typed_value_token(v); +/// Checks whether a non-blank cell matches any primary stored identity (`d:`, `c:`, `f:`, `w:`, `s:`). +fn row_matches_primary_identities(wb: &Workbook, sh: &Sheet, c: CellRef, v: &Value, text: &str, identities: &[String]) -> bool { + let base = cell_base_token(wb, sh, c, v); let mut formula_enc: Option> = None; + let mut slicer_enc: Option> = None; identities.iter().any(|id| { - if let Some(rest) = id.strip_prefix("v:") { - rest == typed + if let Some(rest) = id.strip_prefix("d:") { + rest == base } else if let Some(rest) = id.strip_prefix("c:") { - if rest != typed { + if rest != base { return false; } formula_enc.get_or_insert_with(|| visible_formula_text(wb, sh, c).map(|ft| pct_encode(&ft.trim().to_ascii_lowercase()))).is_none() } else if let Some(rest) = id.strip_prefix("f:") { - let Some(tail) = rest.strip_prefix(&typed).and_then(|s| s.strip_prefix(':')) else { + let Some(tail) = rest.strip_prefix(&base).and_then(|s| s.strip_prefix(':')) else { return false; }; formula_enc.get_or_insert_with(|| visible_formula_text(wb, sh, c).map(|ft| pct_encode(&ft.trim().to_ascii_lowercase()))).as_deref() == Some(tail) + } else if let Some(rest) = id.strip_prefix("w:") { + let Some(tail) = rest.strip_prefix(&base).and_then(|s| s.strip_prefix(':')) else { + return false; + }; + text.trim().is_empty() && pct_encode(&text.to_ascii_lowercase()) == tail + } else if let Some(rest) = id.strip_prefix("s:") { + slicer_enc.get_or_insert_with(|| slicer_tile_text(wb, sh, c).map(|t| pct_encode(&t.to_ascii_lowercase()))).as_deref() == Some(rest) } else { false } }) } -/// Checks whether a non-blank cell matches a `Values` criterion: exact checklist identities when -/// submitted by UI checklist/cell actions, or string matching for direct `data.filterBy` callers. +/// Checks whether a non-blank cell matches a `Values` criterion: legacy `fmtws:` formatted-space +/// tokens, exact checklist/slicer identities when present, or string matching for `data.filterBy`. fn row_matches_criterion(wb: &Workbook, sh: &Sheet, c: CellRef, v: &Value, text: &str, values: &[String], identities: &[String]) -> bool { - if !identities.is_empty() { row_matches_identities(wb, sh, c, v, identities) } else { row_matches_values(values, text, v) } + if identities + .iter() + .filter_map(|id| id.strip_prefix("fmtws:")) + .any(|ws_enc| text.trim().is_empty() && pct_encode(&text.to_ascii_lowercase()) == ws_enc) + { + return true; + } + let has_primary = identities.iter().any(|id| !id.starts_with("fmtws:")); + if has_primary { row_matches_primary_identities(wb, sh, c, v, text, identities) } else { row_matches_values(values, text, v) } } /// Header-and-data range covered by a table's own AutoFilter (excluding any totals row). @@ -411,9 +460,15 @@ pub(crate) fn migrate_loaded_autofilters(wb: &mut Workbook) { let migrate_list = |criteria: &mut [(u32, FilterCriterion)]| -> bool { let mut changed = false; for (_, crit) in criteria { - if let FilterCriterion::Values { values, blanks, .. } = crit + if let FilterCriterion::Values { values, blanks, identities } = crit && values.iter().any(|v| v.trim().is_empty()) { + for v in values.iter().filter(|v| v.trim().is_empty()) { + let tok = format!("fmtws:{}", pct_encode(&v.to_ascii_lowercase())); + if !identities.contains(&tok) { + identities.push(tok); + } + } *blanks = true; values.retain(|v| !v.trim().is_empty()); changed = true; @@ -575,7 +630,7 @@ pub fn filter_by_cell_params(wb: &Workbook, sh: &Sheet, c: CellRef, col: &str) - let t = crate::display::cell_text(wb, sh, c); let has_visible_formula = sh.show_formulas && visible_formula_text(wb, sh, c).is_some(); let raw = if has_visible_formula || t.trim().is_empty() { v.display().trim().to_string() } else { t.trim().to_string() }; - let id = cell_identity_token(wb, sh, c, &v, sh.show_formulas); + let id = cell_identity_token(wb, sh, c, &v, &t, sh.show_formulas, false); json!({"column": col, "values": [raw], "identities": [id], "blanks": false}) } @@ -626,9 +681,10 @@ pub fn filter_choices(wb: &Workbook, sheet: usize, col: u32) -> Vec Vec let mut seen = std::collections::BTreeSet::new(); let mut out = Vec::new(); for row in dr.start.row..=dr.end.row { - let c = CellRef::new(row, col); - let v = sh.value(c); - if super::data::filter_dropdown_blank(&v) { + let Some(tile) = super::data::slicer_tile_text(wb, sh, CellRef::new(row, col)) else { continue; - } - let text = crate::display::cell_text(wb, sh, c); - let tile = if text.trim().is_empty() { v.display().trim().to_string() } else { text.trim().to_string() }; - if tile.is_empty() { - continue; - } - if seen.insert(tile.to_lowercase()) { + }; + if seen.insert(tile.to_ascii_lowercase()) { out.push(tile); } } @@ -98,7 +91,8 @@ fn reapply_slicer(wb: &mut Workbook, sheet: usize, id: u32) { let off = abs_col.saturating_sub(af.range.start.col); af.criteria.retain(|(o, _)| *o != off); if let Some(values) = selected { - af.criteria.push((off, FilterCriterion::Values { values, blanks: false, identities: Vec::new() })); + let identities = values.iter().map(|v| super::data::slicer_identity_token(v)).collect(); + af.criteria.push((off, FilterCriterion::Values { values, blanks: false, identities })); } } super::data::apply_filter(wb, sheet); @@ -184,19 +178,30 @@ fn slicer_toggle(s: &mut Session, p: &Json) -> Result { if !all.iter().any(|v| v.trim().eq_ignore_ascii_case(trimmed_val)) { return Err(bad("slicer.toggle", "value is not in the column")); } - let new_selected: Option> = if multi { - let mut set: Vec = current.unwrap_or_else(|| all.clone()); - match set.iter().position(|x| x.trim().eq_ignore_ascii_case(trimmed_val)) { - Some(pos) => { - set.remove(pos); + let dedup_tiles = |items: Vec| -> Vec { + let mut seen = std::collections::BTreeSet::new(); + let mut out = Vec::new(); + for item in items { + let t = item.trim(); + if !t.is_empty() && seen.insert(t.to_ascii_lowercase()) { + out.push(t.to_string()); } - None => set.push(trimmed_val.to_string()), + } + out + }; + let normalized_current = current.map(dedup_tiles); + let new_selected: Option> = if multi { + let mut set: Vec = normalized_current.unwrap_or_else(|| all.clone()); + let before = set.len(); + set.retain(|x| !x.trim().eq_ignore_ascii_case(trimmed_val)); + if set.len() == before { + set.push(trimmed_val.to_string()); } // Everything selected is the same as no filter. if set.len() == all.len() && all.iter().all(|a| set.iter().any(|x| x.trim().eq_ignore_ascii_case(a.trim()))) { None } else { Some(set) } } else { // Single click: show only this value; clicking the sole selected value clears the filter. - match ¤t { + match &normalized_current { Some(set) if set.len() == 1 && set.iter().any(|x| x.trim().eq_ignore_ascii_case(trimmed_val)) => None, _ => Some(vec![trimmed_val.to_string()]), }