a2ui-react 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -139,6 +139,32 @@ var c = {
139
139
  ...props
140
140
  })
141
141
  };
142
+ function isV09Format(update) {
143
+ return typeof update.component === "string";
144
+ }
145
+ function normalizeComponentUpdate(update) {
146
+ if (isV09Format(update)) {
147
+ const { id, component: type, ...rest } = update;
148
+ return { id, type, ...rest };
149
+ }
150
+ const component = update.component;
151
+ if (!component.id) {
152
+ component.id = update.id;
153
+ }
154
+ if (component.type === "Text") {
155
+ const textComponent = component;
156
+ if (textComponent.content && !textComponent.text) {
157
+ textComponent.text = textComponent.content;
158
+ }
159
+ }
160
+ return component;
161
+ }
162
+ function getTextContent(component) {
163
+ return component.text ?? component.content ?? "";
164
+ }
165
+ function getComponentsArray(msg) {
166
+ return msg.components ?? msg.updates ?? [];
167
+ }
142
168
  var MessageParseError = class extends Error {
143
169
  constructor(message) {
144
170
  super(message);
@@ -171,6 +197,17 @@ function validateBeginRendering(msg) {
171
197
  throw new MessageParseError("beginRendering.style must be an object if provided");
172
198
  }
173
199
  }
200
+ function validateComponentUpdate(update, context) {
201
+ if (!isObject(update)) {
202
+ throw new MessageParseError(`${context}[] must be objects`);
203
+ }
204
+ if (!isNonEmptyString(update.id)) {
205
+ throw new MessageParseError(`${context}[].id must be a non-empty string`);
206
+ }
207
+ if (typeof update.component !== "string" && !isObject(update.component)) {
208
+ throw new MessageParseError(`${context}[].component must be a string (v0.9) or object (legacy)`);
209
+ }
210
+ }
174
211
  function validateSurfaceUpdate(msg) {
175
212
  if (!isObject(msg)) {
176
213
  throw new MessageParseError("surfaceUpdate must be an object");
@@ -178,19 +215,12 @@ function validateSurfaceUpdate(msg) {
178
215
  if (!isNonEmptyString(msg.surfaceId)) {
179
216
  throw new MessageParseError("surfaceUpdate.surfaceId must be a non-empty string");
180
217
  }
181
- if (!isArray(msg.updates)) {
182
- throw new MessageParseError("surfaceUpdate.updates must be an array");
218
+ const components = msg.updates ?? msg.components;
219
+ if (!isArray(components)) {
220
+ throw new MessageParseError("surfaceUpdate.updates (or .components) must be an array");
183
221
  }
184
- for (const update of msg.updates) {
185
- if (!isObject(update)) {
186
- throw new MessageParseError("surfaceUpdate.updates[] must be objects");
187
- }
188
- if (!isNonEmptyString(update.id)) {
189
- throw new MessageParseError("surfaceUpdate.updates[].id must be a non-empty string");
190
- }
191
- if (!isObject(update.component)) {
192
- throw new MessageParseError("surfaceUpdate.updates[].component must be an object");
193
- }
222
+ for (const update of components) {
223
+ validateComponentUpdate(update, "surfaceUpdate.updates");
194
224
  }
195
225
  }
196
226
  function validateDataModelUpdate(msg) {
@@ -254,15 +284,7 @@ function validateUpdateComponents(msg) {
254
284
  throw new MessageParseError("updateComponents.components must be an array");
255
285
  }
256
286
  for (const component of msg.components) {
257
- if (!isObject(component)) {
258
- throw new MessageParseError("updateComponents.components[] must be objects");
259
- }
260
- if (!isNonEmptyString(component.id)) {
261
- throw new MessageParseError("updateComponents.components[].id must be a non-empty string");
262
- }
263
- if (!isObject(component.component)) {
264
- throw new MessageParseError("updateComponents.components[].component must be an object");
265
- }
287
+ validateComponentUpdate(component, "updateComponents.components");
266
288
  }
267
289
  }
268
290
  function validateUpdateDataModel(msg) {
@@ -593,15 +615,9 @@ function processMessage(message, store) {
593
615
  data: {}
594
616
  });
595
617
  } else if (isUpdateComponentsMessage(message) || isSurfaceUpdateMessage(message)) {
596
- let surfaceId;
597
- let componentUpdates;
598
- if (isUpdateComponentsMessage(message)) {
599
- surfaceId = message.updateComponents.surfaceId;
600
- componentUpdates = message.updateComponents.components;
601
- } else {
602
- surfaceId = message.surfaceUpdate.surfaceId;
603
- componentUpdates = message.surfaceUpdate.updates;
604
- }
618
+ const payload = isUpdateComponentsMessage(message) ? message.updateComponents : message.surfaceUpdate;
619
+ const surfaceId = payload.surfaceId;
620
+ const componentUpdates = getComponentsArray(payload);
605
621
  const surface = store.getSurface(surfaceId);
606
622
  if (!surface) {
607
623
  console.warn(`Surface not found for update: ${surfaceId}`);
@@ -609,7 +625,8 @@ function processMessage(message, store) {
609
625
  }
610
626
  const updatedComponents = { ...surface.components };
611
627
  for (const update of componentUpdates) {
612
- updatedComponents[update.id] = update.component;
628
+ const component = normalizeComponentUpdate(update);
629
+ updatedComponents[update.id] = component;
613
630
  }
614
631
  surface.components = updatedComponents;
615
632
  store.setSurface(surfaceId, { ...surface });
@@ -6025,7 +6042,7 @@ var AnimatedTextOverride = {
6025
6042
  type: "Text",
6026
6043
  render: ({ component, data }) => {
6027
6044
  const className = styleClasses6[component.style || "body"] || styleClasses6.body;
6028
- let content = component.content;
6045
+ let content = getTextContent(component);
6029
6046
  if (component.dataPath) {
6030
6047
  const value = data.get(component.dataPath);
6031
6048
  if (value !== void 0) {
@@ -7512,8 +7529,9 @@ var ImageRenderer = {
7512
7529
  var TextRenderer = {
7513
7530
  type: "Text",
7514
7531
  render: ({ component, data }) => {
7515
- const { content, style = "body", dataPath } = component;
7516
- const displayContent = dataPath ? data.get(dataPath) ?? content : content;
7532
+ const { style = "body", dataPath } = component;
7533
+ const textContent = getTextContent(component);
7534
+ const displayContent = dataPath ? data.get(dataPath) ?? textContent : textContent;
7517
7535
  const styleMap = {
7518
7536
  h1: { tag: "h1", className: "text-4xl font-bold" },
7519
7537
  h2: { tag: "h2", className: "text-3xl font-semibold" },
@@ -7557,7 +7575,7 @@ var TextRenderer = {
7557
7575
  component: {
7558
7576
  type: "Text",
7559
7577
  id: "h1",
7560
- content: "Heading 1 - Large Title",
7578
+ text: "Heading 1 - Large Title",
7561
7579
  style: "h1"
7562
7580
  }
7563
7581
  },
@@ -7566,7 +7584,7 @@ var TextRenderer = {
7566
7584
  component: {
7567
7585
  type: "Text",
7568
7586
  id: "h2",
7569
- content: "Heading 2 - Section Title",
7587
+ text: "Heading 2 - Section Title",
7570
7588
  style: "h2"
7571
7589
  }
7572
7590
  },
@@ -7575,7 +7593,7 @@ var TextRenderer = {
7575
7593
  component: {
7576
7594
  type: "Text",
7577
7595
  id: "h3",
7578
- content: "Heading 3 - Subsection",
7596
+ text: "Heading 3 - Subsection",
7579
7597
  style: "h3"
7580
7598
  }
7581
7599
  },
@@ -7584,7 +7602,7 @@ var TextRenderer = {
7584
7602
  component: {
7585
7603
  type: "Text",
7586
7604
  id: "h4",
7587
- content: "Heading 4 - Component Title",
7605
+ text: "Heading 4 - Component Title",
7588
7606
  style: "h4"
7589
7607
  }
7590
7608
  },
@@ -7593,7 +7611,7 @@ var TextRenderer = {
7593
7611
  component: {
7594
7612
  type: "Text",
7595
7613
  id: "h5",
7596
- content: "Heading 5 - Small Title",
7614
+ text: "Heading 5 - Small Title",
7597
7615
  style: "h5"
7598
7616
  }
7599
7617
  },
@@ -7602,7 +7620,7 @@ var TextRenderer = {
7602
7620
  component: {
7603
7621
  type: "Text",
7604
7622
  id: "body",
7605
- content: "Body text paragraph with regular content and default styling.",
7623
+ text: "Body text paragraph with regular content and default styling.",
7606
7624
  style: "body"
7607
7625
  }
7608
7626
  },
@@ -7611,7 +7629,7 @@ var TextRenderer = {
7611
7629
  component: {
7612
7630
  type: "Text",
7613
7631
  id: "caption",
7614
- content: "Caption text - muted and small",
7632
+ text: "Caption text - muted and small",
7615
7633
  style: "caption"
7616
7634
  }
7617
7635
  }