@amplifyup/sdk 0.1.9 → 0.1.11

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.d.mts CHANGED
@@ -37,6 +37,7 @@ declare class AmplifyUpSDK {
37
37
  private componentRegistry;
38
38
  private messageListener;
39
39
  private mutationObserver;
40
+ private composerDragHandlers;
40
41
  private isVisualEditorMode;
41
42
  private eventSelectionMode;
42
43
  private eventSelectionClickHandler;
@@ -200,6 +201,8 @@ declare class AmplifyUpSDK {
200
201
  * Manually trigger event tracking setup (useful after dynamic content loads)
201
202
  */
202
203
  refreshEventTracking(): void;
204
+ /** Origins allowed to send Composer postMessage commands to the SDK. */
205
+ private isAllowedComposerOrigin;
203
206
  /**
204
207
  * Set up postMessage listener for highlighting commands from orchestrator
205
208
  */
@@ -217,6 +220,9 @@ declare class AmplifyUpSDK {
217
220
  * Enable component highlighting
218
221
  */
219
222
  private enableHighlighting;
223
+ /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
224
+ private setupComposerDragDrop;
225
+ private teardownComposerDragDrop;
220
226
  /** Visual outlines for Composer (direct iframe path — no preview-frame proxy). */
221
227
  private injectComposerHighlightStyles;
222
228
  /**
package/dist/index.d.ts CHANGED
@@ -37,6 +37,7 @@ declare class AmplifyUpSDK {
37
37
  private componentRegistry;
38
38
  private messageListener;
39
39
  private mutationObserver;
40
+ private composerDragHandlers;
40
41
  private isVisualEditorMode;
41
42
  private eventSelectionMode;
42
43
  private eventSelectionClickHandler;
@@ -200,6 +201,8 @@ declare class AmplifyUpSDK {
200
201
  * Manually trigger event tracking setup (useful after dynamic content loads)
201
202
  */
202
203
  refreshEventTracking(): void;
204
+ /** Origins allowed to send Composer postMessage commands to the SDK. */
205
+ private isAllowedComposerOrigin;
203
206
  /**
204
207
  * Set up postMessage listener for highlighting commands from orchestrator
205
208
  */
@@ -217,6 +220,9 @@ declare class AmplifyUpSDK {
217
220
  * Enable component highlighting
218
221
  */
219
222
  private enableHighlighting;
223
+ /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
224
+ private setupComposerDragDrop;
225
+ private teardownComposerDragDrop;
220
226
  /** Visual outlines for Composer (direct iframe path — no preview-frame proxy). */
221
227
  private injectComposerHighlightStyles;
222
228
  /**
package/dist/index.js CHANGED
@@ -343,6 +343,7 @@ var AmplifyUpSDK = class {
343
343
  this.componentRegistry = [];
344
344
  this.messageListener = null;
345
345
  this.mutationObserver = null;
346
+ this.composerDragHandlers = null;
346
347
  this.isVisualEditorMode = false;
347
348
  this.eventSelectionMode = false;
348
349
  this.eventSelectionClickHandler = null;
@@ -1075,6 +1076,39 @@ Available Console Commands:
1075
1076
  this.attachEventListeners();
1076
1077
  }
1077
1078
  }
1079
+ /** Origins allowed to send Composer postMessage commands to the SDK. */
1080
+ isAllowedComposerOrigin(origin) {
1081
+ if (typeof window === "undefined") return false;
1082
+ if (origin === window.location.origin) return true;
1083
+ const staticAllowed = [
1084
+ "http://localhost:5001",
1085
+ "http://localhost:3001",
1086
+ "https://amplifyup.ai",
1087
+ "https://orchestrator.amplifyup.com"
1088
+ ];
1089
+ if (staticAllowed.includes(origin)) return true;
1090
+ try {
1091
+ const host = new URL(origin).hostname;
1092
+ if (host === "amplifyup.ai" || host.endsWith(".amplifyup.ai")) return true;
1093
+ } catch {
1094
+ }
1095
+ const orchestratorFromQuery = (() => {
1096
+ try {
1097
+ const value = new URLSearchParams(window.location.search).get("orchestratorUrl");
1098
+ return value ? new URL(value).origin : null;
1099
+ } catch {
1100
+ return null;
1101
+ }
1102
+ })();
1103
+ if (orchestratorFromQuery && origin === orchestratorFromQuery) return true;
1104
+ if (this.orchestratorUrl) {
1105
+ try {
1106
+ if (new URL(this.orchestratorUrl).origin === origin) return true;
1107
+ } catch {
1108
+ }
1109
+ }
1110
+ return false;
1111
+ }
1078
1112
  /**
1079
1113
  * Set up postMessage listener for highlighting commands from orchestrator
1080
1114
  */
@@ -1090,18 +1124,10 @@ Available Console Commands:
1090
1124
  currentOrigin: window.location.origin,
1091
1125
  data: event.data
1092
1126
  });
1093
- const allowedOrigins = [
1094
- "http://localhost:5001",
1095
- "http://localhost:3001",
1096
- "https://orchestrator.amplifyup.com"
1097
- ];
1098
- const isSameOrigin = event.origin === window.location.origin;
1099
- const isAllowedOrigin = allowedOrigins.some((origin) => event.origin.startsWith(origin));
1100
- if (!isSameOrigin && !isAllowedOrigin) {
1127
+ if (!this.isAllowedComposerOrigin(event.origin)) {
1101
1128
  console.warn("[AmplifyUp SDK] Rejected message from unauthorized origin:", {
1102
1129
  eventOrigin: event.origin,
1103
- currentOrigin: window.location.origin,
1104
- allowedOrigins
1130
+ currentOrigin: window.location.origin
1105
1131
  });
1106
1132
  return;
1107
1133
  }
@@ -1281,6 +1307,111 @@ Available Console Commands:
1281
1307
  this.applyComponentHighlights(highlightComponents, showZones);
1282
1308
  this.setupMutationObserver();
1283
1309
  }
1310
+ if (showZones) {
1311
+ this.setupComposerDragDrop();
1312
+ }
1313
+ }
1314
+ /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
1315
+ setupComposerDragDrop() {
1316
+ if (typeof document === "undefined") return;
1317
+ this.teardownComposerDragDrop();
1318
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1319
+ const clearDragOverState = () => {
1320
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1321
+ el.classList.remove("amplifyup-drag-over-slot");
1322
+ });
1323
+ document.body.classList.remove("amplifyup-drag-no-drop");
1324
+ };
1325
+ const dragenter = (e) => {
1326
+ e.preventDefault();
1327
+ if (e.dataTransfer?.types?.length) {
1328
+ document.body.classList.add("amplifyup-drag-no-drop");
1329
+ }
1330
+ };
1331
+ const dragover = (e) => {
1332
+ e.preventDefault();
1333
+ const target = e.target;
1334
+ const slot = target?.closest?.(slotSelector);
1335
+ if (e.dataTransfer) {
1336
+ e.dataTransfer.dropEffect = slot ? "copy" : "none";
1337
+ }
1338
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1339
+ el.classList.remove("amplifyup-drag-over-slot");
1340
+ });
1341
+ if (slot) {
1342
+ slot.classList.add("amplifyup-drag-over-slot");
1343
+ document.body.classList.remove("amplifyup-drag-no-drop");
1344
+ } else {
1345
+ document.body.classList.add("amplifyup-drag-no-drop");
1346
+ }
1347
+ };
1348
+ const dragleave = (e) => {
1349
+ const related = e.relatedTarget;
1350
+ if (!related || !document.body.contains(related)) {
1351
+ clearDragOverState();
1352
+ }
1353
+ };
1354
+ const drop = (e) => {
1355
+ e.preventDefault();
1356
+ const target = e.target;
1357
+ const slot = target?.closest?.(slotSelector);
1358
+ clearDragOverState();
1359
+ if (!slot || !this.isVisualEditorMode) return;
1360
+ const slotName = slot.getAttribute("data-amplifyup-slot") || slot.getAttribute("data-slot-id");
1361
+ const isEmpty = slot.getAttribute("data-slot-empty") === "true";
1362
+ const win = window;
1363
+ if (slotName && win.parent && win.parent !== win) {
1364
+ win.parent.postMessage({
1365
+ type: "DROP_ON_SLOT",
1366
+ slotName,
1367
+ slotElement: { path: slot.getAttribute("data-slot-path"), isEmpty }
1368
+ }, "*");
1369
+ }
1370
+ };
1371
+ const mouseover = (e) => {
1372
+ if (!this.showZonesEnabled) return;
1373
+ const target = e.target;
1374
+ const slot = target?.closest?.(slotSelector);
1375
+ if (!slot) return;
1376
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1377
+ el.classList.remove("amplifyup-slot-hover");
1378
+ });
1379
+ slot.classList.add("amplifyup-slot-hover");
1380
+ };
1381
+ const mouseout = (e) => {
1382
+ if (!this.showZonesEnabled) return;
1383
+ const related = e.relatedTarget;
1384
+ if (!related?.closest?.(slotSelector)) {
1385
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1386
+ el.classList.remove("amplifyup-slot-hover");
1387
+ });
1388
+ }
1389
+ };
1390
+ document.addEventListener("dragenter", dragenter, false);
1391
+ document.addEventListener("dragover", dragover, false);
1392
+ document.addEventListener("dragleave", dragleave, false);
1393
+ document.addEventListener("drop", drop, false);
1394
+ document.addEventListener("mouseover", mouseover, true);
1395
+ document.addEventListener("mouseout", mouseout, true);
1396
+ this.composerDragHandlers = { dragenter, dragover, dragleave, drop, mouseover, mouseout };
1397
+ }
1398
+ teardownComposerDragDrop() {
1399
+ if (!this.composerDragHandlers || typeof document === "undefined") return;
1400
+ const h = this.composerDragHandlers;
1401
+ document.removeEventListener("dragenter", h.dragenter, false);
1402
+ document.removeEventListener("dragover", h.dragover, false);
1403
+ document.removeEventListener("dragleave", h.dragleave, false);
1404
+ document.removeEventListener("drop", h.drop, false);
1405
+ document.removeEventListener("mouseover", h.mouseover, true);
1406
+ document.removeEventListener("mouseout", h.mouseout, true);
1407
+ this.composerDragHandlers = null;
1408
+ document.body.classList.remove("amplifyup-drag-no-drop");
1409
+ document.querySelectorAll("[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot").forEach((el) => {
1410
+ el.classList.remove("amplifyup-drag-over-slot");
1411
+ });
1412
+ document.querySelectorAll("[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover").forEach((el) => {
1413
+ el.classList.remove("amplifyup-slot-hover");
1414
+ });
1284
1415
  }
1285
1416
  /** Visual outlines for Composer (direct iframe path — no preview-frame proxy). */
1286
1417
  injectComposerHighlightStyles(highlightComponents, showZones) {
@@ -1307,19 +1438,64 @@ Available Console Commands:
1307
1438
  " outline: 2px dotted rgba(34, 197, 94, 0.75) !important;",
1308
1439
  " outline-offset: 4px !important;",
1309
1440
  " min-height: 3rem !important;",
1441
+ " padding: 20px !important;",
1310
1442
  " box-sizing: border-box !important;",
1311
1443
  " cursor: pointer !important;",
1444
+ " position: relative !important;",
1312
1445
  "}",
1313
1446
  '[data-slot-id="main"], [data-amplifyup-slot="main"] {',
1314
1447
  " min-height: 50vh !important;",
1315
1448
  "}",
1449
+ '[data-slot-id="main"][data-slot-empty="true"]::before,',
1450
+ '[data-amplifyup-slot="main"][data-slot-empty="true"]::before {',
1451
+ ' content: "Drop components here" !important;',
1452
+ " position: absolute !important;",
1453
+ " top: 50% !important; left: 50% !important;",
1454
+ " transform: translate(-50%, -50%) !important;",
1455
+ " font-size: 14px !important; font-weight: 500 !important;",
1456
+ " color: rgba(34, 197, 94, 0.65) !important;",
1457
+ " pointer-events: none !important;",
1458
+ " white-space: nowrap !important;",
1459
+ "}",
1316
1460
  "[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover {",
1317
1461
  " outline-color: rgba(34, 197, 94, 1) !important;",
1318
1462
  " background: rgba(34, 197, 94, 0.06) !important;",
1319
1463
  "}",
1320
1464
  "[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot {",
1321
1465
  " outline: 2px solid rgba(34, 197, 94, 1) !important;",
1322
- " background: rgba(34, 197, 94, 0.1) !important;",
1466
+ " background: rgba(34, 197, 94, 0.18) !important;",
1467
+ " position: relative !important;",
1468
+ "}",
1469
+ "[data-slot-id].amplifyup-drag-over-slot::after,",
1470
+ "[data-amplifyup-slot].amplifyup-drag-over-slot::after {",
1471
+ ' content: "\\2713 Drop here" !important;',
1472
+ " position: absolute !important;",
1473
+ " top: 50% !important; left: 50% !important;",
1474
+ " transform: translate(-50%, -50%) !important;",
1475
+ " font-size: 14px !important; font-weight: 600 !important;",
1476
+ " color: rgb(22, 163, 74) !important;",
1477
+ " pointer-events: none !important;",
1478
+ " white-space: nowrap !important;",
1479
+ " z-index: 1 !important;",
1480
+ "}",
1481
+ "body.amplifyup-drag-no-drop { cursor: no-drop !important; }",
1482
+ "body.amplifyup-drag-no-drop::before {",
1483
+ ' content: "" !important;',
1484
+ " position: fixed !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important;",
1485
+ " background: rgba(239, 68, 68, 0.06) !important;",
1486
+ " pointer-events: none !important;",
1487
+ " z-index: 2147483646 !important;",
1488
+ "}",
1489
+ "body.amplifyup-drag-no-drop::after {",
1490
+ ' content: "\\2717 Not a drop zone" !important;',
1491
+ " position: fixed !important; bottom: 12px !important; right: 12px !important;",
1492
+ " font-size: 12px !important; font-weight: 600 !important;",
1493
+ " color: rgb(185, 28, 28) !important;",
1494
+ " background: rgba(254, 226, 226, 0.95) !important;",
1495
+ " padding: 6px 10px !important; border-radius: 6px !important;",
1496
+ " pointer-events: none !important;",
1497
+ " z-index: 2147483647 !important;",
1498
+ " box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;",
1323
1499
  "}"
1324
1500
  );
1325
1501
  }
@@ -1379,6 +1555,7 @@ Available Console Commands:
1379
1555
  disableHighlighting() {
1380
1556
  if (typeof document === "undefined") return;
1381
1557
  this.highlightingEnabled = false;
1558
+ this.teardownComposerDragDrop();
1382
1559
  if (this.mutationObserver) {
1383
1560
  this.mutationObserver.disconnect();
1384
1561
  this.mutationObserver = null;
@@ -1539,9 +1716,15 @@ Available Console Commands:
1539
1716
  e.preventDefault();
1540
1717
  e.stopPropagation();
1541
1718
  htmlEl.classList.remove("amplifyup-drag-over-slot");
1719
+ if (!win.parent || win.parent === win) return;
1720
+ win.parent.postMessage({
1721
+ type: "DROP_ON_SLOT",
1722
+ slotName,
1723
+ slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1724
+ }, "*");
1542
1725
  try {
1543
1726
  const data = JSON.parse(e.dataTransfer.getData("application/json"));
1544
- if (data.type === "component" && win.parent && win.parent !== win) {
1727
+ if (data.type === "component") {
1545
1728
  win.parent.postMessage({
1546
1729
  type: "COMPONENT_DROPPED",
1547
1730
  componentId: data.componentId,
@@ -1549,7 +1732,6 @@ Available Console Commands:
1549
1732
  slotName,
1550
1733
  slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1551
1734
  }, "*");
1552
- win.parent.postMessage({ type: "DROP_ON_SLOT", slotName }, "*");
1553
1735
  }
1554
1736
  } catch (_err) {
1555
1737
  }