MCPcopy
hub / github.com/clientIO/joint / setCellHelper

Function setCellHelper

packages/joint-react/src/hooks/use-update-element.ts:22–47  ·  view source on GitHub ↗

* 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
)

Source from the content-addressed store, hash-verified

20 * @param value - The value to set.
21 */
22function 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.

Callers 1

useUpdateElementFunction · 0.85

Calls 3

isSetterFunction · 0.90
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected