* Helper function. * Parameters - [graph, id, attribute, value] * @param graph - The graph to set the element in. * @param id - The ID of the element to set. * @param attribute - The attribute to set. * @param value - The value to set.
( graph: dia.Graph, id: dia.Cell.ID, attribute: Attribute, value: unknown )
| 20 | * @param value - The value to set. |
| 21 | */ |
| 22 | function setCellHelper<Attributes, Attribute extends keyof Attributes>( |
| 23 | graph: dia.Graph, |
| 24 | id: dia.Cell.ID, |
| 25 | attribute: Attribute, |
| 26 | value: unknown |
| 27 | ) { |
| 28 | const stringAttribute = attribute as string; |
| 29 | const element = graph.getCell(id); |
| 30 | |
| 31 | if (!element) { |
| 32 | return; |
| 33 | } |
| 34 | if (isSetter(value)) { |
| 35 | const previousValue: Attributes[Attribute] = element.get(stringAttribute); |
| 36 | const nextValue = value(previousValue); |
| 37 | if (nextValue === previousValue) { |
| 38 | // skip if the reference is same, same as react state does |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | element.set(stringAttribute, nextValue); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | element.set(stringAttribute, value); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Set the element attribute in the graph. |
no test coverage detected