update<E extends DiagramElement> static method

DiagramVerb update<E extends DiagramElement>({
  1. required String id,
  2. required String label,
  3. required E? update(
    1. E element,
    2. DiagramVerbCall call
    ),
  4. String description = '',
  5. List<DiagramVerbParameter<Object?>> parameters = const [],
  6. DiagramVerbTarget target = DiagramVerbTarget.elements,
  7. bool appliesTo(
    1. DiagramElement element,
    2. ShapeRegistry registry
    )?,
  8. bool admitsLockedTargets = false,
  9. EditResult? check(
    1. DiagramVerbCall call
    )?,
  10. String editLabel(
    1. DiagramVerbCall call
    )?,
})

A setter that replaces each target element of type E with update's result, as one edit labeled label. update returns the element unchanged, or null, when there is nothing to change.

Implementation

static DiagramVerb update<E extends DiagramElement>({
  required String id,
  required String label,
  required E? Function(E element, DiagramVerbCall call) update,
  String description = '',
  List<DiagramVerbParameter<Object?>> parameters = const [],
  DiagramVerbTarget target = DiagramVerbTarget.elements,
  bool Function(DiagramElement element, ShapeRegistry registry)? appliesTo,
  bool admitsLockedTargets = false,
  EditResult? Function(DiagramVerbCall call)? check,
  String Function(DiagramVerbCall call)? editLabel,
}) => DiagramVerb._(
  id: id,
  label: label,
  symmetry: DiagramVerbSymmetry.setter,
  update: (element, call) => update(element as E, call),
  description: description,
  parameters: parameters,
  target: target,
  appliesTo: (element, registry) =>
      element is E && (appliesTo?.call(element, registry) ?? true),
  admitsLockedTargets: admitsLockedTargets,
  check: check,
  perform: (call) {
    final document = call.document;
    final steps = <DiagramStep>[];
    for (final id in call.ids) {
      final before = document.elementById(id)! as E;
      final after = update(before, call);
      if (after != null && after != before) {
        steps.add(ReplaceElementStep(before: before, after: after));
      }
    }
    if (steps.isEmpty) return const EditResult.allow();
    return call.submit(
      DiagramTransaction(label: editLabel?.call(call) ?? label, steps: steps),
    );
  },
);