diff --git a/crates/engine/command-surface.txt b/crates/engine/command-surface.txt index cae4bc0686..6416f88b8e 100644 --- a/crates/engine/command-surface.txt +++ b/crates/engine/command-surface.txt @@ -111,7 +111,7 @@ edit.reshape line edit.rotate angle edit.scale factor edit.setGeometry layer? row geometry -edit.setValue layer? table? row field value +edit.setValue layer? table? row field value classStyle? edit.settings showVertices? edit.snap lon lat edit.snapping on? tolerancePx? vertex? edge? end? @@ -227,7 +227,7 @@ layer.addUrl url bytes? format? name? layer? zoom? x? y? wkt? crs? delimiter? pa layer.addZarr path url variable? index? cells? bbox? crs? name? time? start? end? ran? summary? anonymous? layer.aggregation layer? shape size? layer.blend layer? mode -layer.classStyle layer? value? index? other? color? outline? label? +layer.classStyle layer? value? create? field? index? other? color? outline? label? layer.clearDiagram layer? layer.color layer? color layer.copy layer? group? diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 540e117911..d5ceafc03a 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -29,10 +29,10 @@ pub fn commands() -> Vec { .when(super::design::need_editable_or_table) .params(r#"{"layer"?, "table"?: name|id (a standalone table: adds a row, without geometry or template), "geometry": GeoJSON geometry (lon/lat; left out for a table), "attributes"?: {field: value}, "template"?: feature template}"#), C::new("edit.delete", "Delete", "Edit › Features", delete).roles(&[]).effects(crate::policy::Effects::NONE).key("Delete").when(super::design::need_editable_selection), - C::new("edit.setValue", "Edit Attribute", "Attribute Table", set_value).roles(&[]).effects(crate::policy::Effects::NONE) + C::new("edit.setValue", "Edit Attribute", "Attribute Table", set_value).roles(&[]).effects(crate::policy::Effects::NONE).atomic() .when(super::design::need_editable_or_table) - .params(r#"{"layer"?, "table"?: name|id (a standalone table, instead of a layer), "row": number, "field": string (the exact name first; a name matching several fields only ignoring case is refused), "value": any}"#), - C::new("edit.addField", "Add Field", "Data › Data Design › Fields", add_field).roles(&[]).effects(crate::policy::Effects::NONE) + .params(r#"{"layer"?, "table"?: name|id (a standalone table, instead of a layer), "row": number, "field": string (the exact name first; a name matching several fields only ignoring case is refused), "value": any, "classStyle"?: bool (a layer text field: ensure the applied class has a Unique Values colour rule, in the same undoable transaction)}"#), + C::new("edit.addField", "Add Field", "Data › Data Design › Fields", add_field).roles(&[]).effects(crate::policy::Effects::NONE).atomic() .when(need_layer_or_table) .params(r#"{"layer"?, "table"?: name|id (a standalone table, instead of a layer), "name": string, "type": "long"|"double"|"text"|"boolean"|"date"|"datetime"}"#), C::new("edit.deleteField", "Delete Field", "Data › Data Design › Fields", delete_field).roles(&[]).effects(crate::policy::Effects::NONE) @@ -989,6 +989,14 @@ fn numeric_loss(input: &Value, v: &Value, ty: FieldType) -> Option { /// standalone table, as its field holds it ([`typed`]), saying what that lost. A table's first /// copy of shared rows runs on the desktop table worker ([`table_ahead`]). fn set_value(s: &mut Session, p: &Json) -> CmdResult { + let class_style = match p.get("classStyle") { + None => false, + Some(Json::Bool(on)) => *on, + Some(_) => return Err("classStyle must be true or false".into()), + }; + if class_style && matches!(target(s, p)?, Target::Table(_)) { + return Err("automatic class styling requires a layer, not a standalone table".into()); + } if let Some(result) = land_table_edit(s, p, "edit.setValue") { return result; } @@ -1022,6 +1030,12 @@ fn set_value(s: &mut Session, p: &Json) -> CmdResult { let ty = l.dataset.fields.get(fi).map(|f| f.ty).ok_or("no field")?; let field = l.dataset.fields.get(fi).map(|f| f.name.clone()).ok_or("no field")?; let layer = l.dataset.name.clone(); + if class_style && ty != FieldType::Text { + return Err("automatic class styling requires a text field".into()); + } + if class_style && let Some(name) = raw.as_str() { + rustgis_map::symbology::validate_class_name(name)?; + } let v = typed(raw, ty)?; // Keep the existing finite coercion, but say when it discards information. let loss = numeric_loss(&input, &v, ty); @@ -1032,6 +1046,13 @@ fn set_value(s: &mut Session, p: &Json) -> CmdResult { let cell = f.attrs.get_mut(fi).ok_or("no such row")?; *cell = v; super::design::apply(s, id, vec![super::design::Change { before: Some(before), row: Some(row) }])?; + if class_style { + let l = s.map_mut().layer_mut(id).ok_or("no layer")?; + let Value::Text(value) = l.dataset.value(row, fi) else { return Err("a class needs a non-empty text value".into()) }; + let value = value.clone(); + // The command transaction rolls back both the cell and styling if either refuses. + super::layer::ensure_unique_class(l, &field, &value)?; + } if let Some(key) = loss { notify_loss(s, key, &layer, field, row); } @@ -1066,10 +1087,15 @@ fn typed(raw: Json, ty: FieldType) -> Result { }) } -/// Adds an empty field of a type: long, double, text, boolean, date or datetime. Table rows -/// are filled on the desktop worker ([`table_ahead`]), with progress and Cancel. +/// Adds an empty field of a type: long, double, text, boolean, date or datetime. Layer and +/// table rows are filled on the desktop field-edit worker ([`table_ahead`]), with progress +/// and Cancel between rows. The shared row storage's first copy cannot be interrupted. fn add_field(s: &mut Session, p: &Json) -> CmdResult { - if let Some(result) = land_table_edit(s, p, "edit.addField") { + let landed = match target(s, p)? { + Target::Layer(_) => land_layer_schema(s, p, "edit.addField"), + Target::Table(_) => land_table_edit(s, p, "edit.addField"), + }; + if let Some(result) = landed { return result; } let t = write_target(s, p)?; @@ -1090,17 +1116,10 @@ fn add_field(s: &mut Session, p: &Json) -> CmdResult { if ds.field_index(name).is_some() { return Err(format!("a field named '{name}' already exists")); } - let table = matches!(t, Target::Table(_)); ds.fields.push(Field::new(name, ty)); - if table { - rustgis_data::progress::begin(ds.features.len()); - } - // A table's edit runs ahead on a worker, with progress and Cancel ([`table_ahead`]). - ds.features.try_add_columns( - 1, - Value::Null, - || if table { rustgis_data::progress::step(1).map_err(|e| e.to_string()) } else { Ok(()) }, - )?; + rustgis_data::progress::begin(ds.features.len()); + // Shared rows are copied once, each at its new length, and Cancel is checked before each row. + ds.features.try_add_columns(1, Value::Null, || rustgis_data::progress::step(1).map_err(|e| e.to_string()))?; Ok(Json::Null) } @@ -1118,7 +1137,7 @@ fn add_field(s: &mut Session, p: &Json) -> CmdResult { fn convert_field(s: &mut Session, p: &Json) -> CmdResult { // A prepared result has one concrete type: do not consume it while probing another type. let landed = match target(s, p)? { - Target::Layer(_) => land_layer_dates(s, p), + Target::Layer(_) => land_layer_schema(s, p, "edit.convertField"), Target::Table(_) => land_table_edit(s, p, "edit.convertField"), }; if let Some(result) = landed { @@ -1815,7 +1834,7 @@ pub(crate) const TABLE_EDITS: &[(&str, &str, fn(&mut Session, &Json) -> CmdResul ]; /// The registered command and progress label for a table write. Layer invocations keep their -/// existing path; the desktop uses this worker only when parameters name a standalone table. +/// existing path except Add Field and date conversion, which also prepare loaded layer rows. pub fn table_edit_in_background(command: &str) -> Option<(&'static str, &'static str)> { TABLE_EDITS.iter().find(|(id, ..)| *id == command).map(|(id, name, _)| (*id, *name)) } @@ -1894,7 +1913,11 @@ pub(crate) fn script_table( /// shared row storage's first copy and one expression evaluation cannot be interrupted. pub(crate) fn table_ahead(s: &Session, command: &str, p: &Json) -> Result, String> { let Target::Table(i) = target(s, p)? else { - return if command == "edit.convertField" { layer_dates_ahead(s, p).map(Some) } else { Ok(None) }; + return if matches!(command, "edit.convertField" | "edit.addField") { + layer_schema_ahead(s, command, p).map(Some) + } else { + Ok(None) + }; }; let &(_, name, handler) = TABLE_EDITS.iter().find(|(id, ..)| *id == command).ok_or("not a table edit")?; let table = s.project.tables.get(i).ok_or("no such table")?.clone(); @@ -1916,8 +1939,8 @@ pub(crate) fn table_ahead(s: &Session, command: &str, p: &Json) -> Result