@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/react.js CHANGED
@@ -351,6 +351,7 @@ var AmplifyUpSDK = class {
351
351
  this.componentRegistry = [];
352
352
  this.messageListener = null;
353
353
  this.mutationObserver = null;
354
+ this.composerDragHandlers = null;
354
355
  this.isVisualEditorMode = false;
355
356
  this.eventSelectionMode = false;
356
357
  this.eventSelectionClickHandler = null;
@@ -1083,6 +1084,39 @@ Available Console Commands:
1083
1084
  this.attachEventListeners();
1084
1085
  }
1085
1086
  }
1087
+ /** Origins allowed to send Composer postMessage commands to the SDK. */
1088
+ isAllowedComposerOrigin(origin) {
1089
+ if (typeof window === "undefined") return false;
1090
+ if (origin === window.location.origin) return true;
1091
+ const staticAllowed = [
1092
+ "http://localhost:5001",
1093
+ "http://localhost:3001",
1094
+ "https://amplifyup.ai",
1095
+ "https://orchestrator.amplifyup.com"
1096
+ ];
1097
+ if (staticAllowed.includes(origin)) return true;
1098
+ try {
1099
+ const host = new URL(origin).hostname;
1100
+ if (host === "amplifyup.ai" || host.endsWith(".amplifyup.ai")) return true;
1101
+ } catch {
1102
+ }
1103
+ const orchestratorFromQuery = (() => {
1104
+ try {
1105
+ const value = new URLSearchParams(window.location.search).get("orchestratorUrl");
1106
+ return value ? new URL(value).origin : null;
1107
+ } catch {
1108
+ return null;
1109
+ }
1110
+ })();
1111
+ if (orchestratorFromQuery && origin === orchestratorFromQuery) return true;
1112
+ if (this.orchestratorUrl) {
1113
+ try {
1114
+ if (new URL(this.orchestratorUrl).origin === origin) return true;
1115
+ } catch {
1116
+ }
1117
+ }
1118
+ return false;
1119
+ }
1086
1120
  /**
1087
1121
  * Set up postMessage listener for highlighting commands from orchestrator
1088
1122
  */
@@ -1098,18 +1132,10 @@ Available Console Commands:
1098
1132
  currentOrigin: window.location.origin,
1099
1133
  data: event.data
1100
1134
  });
1101
- const allowedOrigins = [
1102
- "http://localhost:5001",
1103
- "http://localhost:3001",
1104
- "https://orchestrator.amplifyup.com"
1105
- ];
1106
- const isSameOrigin = event.origin === window.location.origin;
1107
- const isAllowedOrigin = allowedOrigins.some((origin) => event.origin.startsWith(origin));
1108
- if (!isSameOrigin && !isAllowedOrigin) {
1135
+ if (!this.isAllowedComposerOrigin(event.origin)) {
1109
1136
  console.warn("[AmplifyUp SDK] Rejected message from unauthorized origin:", {
1110
1137
  eventOrigin: event.origin,
1111
- currentOrigin: window.location.origin,
1112
- allowedOrigins
1138
+ currentOrigin: window.location.origin
1113
1139
  });
1114
1140
  return;
1115
1141
  }
@@ -1289,6 +1315,111 @@ Available Console Commands:
1289
1315
  this.applyComponentHighlights(highlightComponents, showZones);
1290
1316
  this.setupMutationObserver();
1291
1317
  }
1318
+ if (showZones) {
1319
+ this.setupComposerDragDrop();
1320
+ }
1321
+ }
1322
+ /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
1323
+ setupComposerDragDrop() {
1324
+ if (typeof document === "undefined") return;
1325
+ this.teardownComposerDragDrop();
1326
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1327
+ const clearDragOverState = () => {
1328
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1329
+ el.classList.remove("amplifyup-drag-over-slot");
1330
+ });
1331
+ document.body.classList.remove("amplifyup-drag-no-drop");
1332
+ };
1333
+ const dragenter = (e) => {
1334
+ e.preventDefault();
1335
+ if (e.dataTransfer?.types?.length) {
1336
+ document.body.classList.add("amplifyup-drag-no-drop");
1337
+ }
1338
+ };
1339
+ const dragover = (e) => {
1340
+ e.preventDefault();
1341
+ const target = e.target;
1342
+ const slot = target?.closest?.(slotSelector);
1343
+ if (e.dataTransfer) {
1344
+ e.dataTransfer.dropEffect = slot ? "copy" : "none";
1345
+ }
1346
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1347
+ el.classList.remove("amplifyup-drag-over-slot");
1348
+ });
1349
+ if (slot) {
1350
+ slot.classList.add("amplifyup-drag-over-slot");
1351
+ document.body.classList.remove("amplifyup-drag-no-drop");
1352
+ } else {
1353
+ document.body.classList.add("amplifyup-drag-no-drop");
1354
+ }
1355
+ };
1356
+ const dragleave = (e) => {
1357
+ const related = e.relatedTarget;
1358
+ if (!related || !document.body.contains(related)) {
1359
+ clearDragOverState();
1360
+ }
1361
+ };
1362
+ const drop = (e) => {
1363
+ e.preventDefault();
1364
+ const target = e.target;
1365
+ const slot = target?.closest?.(slotSelector);
1366
+ clearDragOverState();
1367
+ if (!slot || !this.isVisualEditorMode) return;
1368
+ const slotName = slot.getAttribute("data-amplifyup-slot") || slot.getAttribute("data-slot-id");
1369
+ const isEmpty = slot.getAttribute("data-slot-empty") === "true";
1370
+ const win = window;
1371
+ if (slotName && win.parent && win.parent !== win) {
1372
+ win.parent.postMessage({
1373
+ type: "DROP_ON_SLOT",
1374
+ slotName,
1375
+ slotElement: { path: slot.getAttribute("data-slot-path"), isEmpty }
1376
+ }, "*");
1377
+ }
1378
+ };
1379
+ const mouseover = (e) => {
1380
+ if (!this.showZonesEnabled) return;
1381
+ const target = e.target;
1382
+ const slot = target?.closest?.(slotSelector);
1383
+ if (!slot) return;
1384
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1385
+ el.classList.remove("amplifyup-slot-hover");
1386
+ });
1387
+ slot.classList.add("amplifyup-slot-hover");
1388
+ };
1389
+ const mouseout = (e) => {
1390
+ if (!this.showZonesEnabled) return;
1391
+ const related = e.relatedTarget;
1392
+ if (!related?.closest?.(slotSelector)) {
1393
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1394
+ el.classList.remove("amplifyup-slot-hover");
1395
+ });
1396
+ }
1397
+ };
1398
+ document.addEventListener("dragenter", dragenter, false);
1399
+ document.addEventListener("dragover", dragover, false);
1400
+ document.addEventListener("dragleave", dragleave, false);
1401
+ document.addEventListener("drop", drop, false);
1402
+ document.addEventListener("mouseover", mouseover, true);
1403
+ document.addEventListener("mouseout", mouseout, true);
1404
+ this.composerDragHandlers = { dragenter, dragover, dragleave, drop, mouseover, mouseout };
1405
+ }
1406
+ teardownComposerDragDrop() {
1407
+ if (!this.composerDragHandlers || typeof document === "undefined") return;
1408
+ const h = this.composerDragHandlers;
1409
+ document.removeEventListener("dragenter", h.dragenter, false);
1410
+ document.removeEventListener("dragover", h.dragover, false);
1411
+ document.removeEventListener("dragleave", h.dragleave, false);
1412
+ document.removeEventListener("drop", h.drop, false);
1413
+ document.removeEventListener("mouseover", h.mouseover, true);
1414
+ document.removeEventListener("mouseout", h.mouseout, true);
1415
+ this.composerDragHandlers = null;
1416
+ document.body.classList.remove("amplifyup-drag-no-drop");
1417
+ document.querySelectorAll("[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot").forEach((el) => {
1418
+ el.classList.remove("amplifyup-drag-over-slot");
1419
+ });
1420
+ document.querySelectorAll("[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover").forEach((el) => {
1421
+ el.classList.remove("amplifyup-slot-hover");
1422
+ });
1292
1423
  }
1293
1424
  /** Visual outlines for Composer (direct iframe path — no preview-frame proxy). */
1294
1425
  injectComposerHighlightStyles(highlightComponents, showZones) {
@@ -1315,19 +1446,64 @@ Available Console Commands:
1315
1446
  " outline: 2px dotted rgba(34, 197, 94, 0.75) !important;",
1316
1447
  " outline-offset: 4px !important;",
1317
1448
  " min-height: 3rem !important;",
1449
+ " padding: 20px !important;",
1318
1450
  " box-sizing: border-box !important;",
1319
1451
  " cursor: pointer !important;",
1452
+ " position: relative !important;",
1320
1453
  "}",
1321
1454
  '[data-slot-id="main"], [data-amplifyup-slot="main"] {',
1322
1455
  " min-height: 50vh !important;",
1323
1456
  "}",
1457
+ '[data-slot-id="main"][data-slot-empty="true"]::before,',
1458
+ '[data-amplifyup-slot="main"][data-slot-empty="true"]::before {',
1459
+ ' content: "Drop components here" !important;',
1460
+ " position: absolute !important;",
1461
+ " top: 50% !important; left: 50% !important;",
1462
+ " transform: translate(-50%, -50%) !important;",
1463
+ " font-size: 14px !important; font-weight: 500 !important;",
1464
+ " color: rgba(34, 197, 94, 0.65) !important;",
1465
+ " pointer-events: none !important;",
1466
+ " white-space: nowrap !important;",
1467
+ "}",
1324
1468
  "[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover {",
1325
1469
  " outline-color: rgba(34, 197, 94, 1) !important;",
1326
1470
  " background: rgba(34, 197, 94, 0.06) !important;",
1327
1471
  "}",
1328
1472
  "[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot {",
1329
1473
  " outline: 2px solid rgba(34, 197, 94, 1) !important;",
1330
- " background: rgba(34, 197, 94, 0.1) !important;",
1474
+ " background: rgba(34, 197, 94, 0.18) !important;",
1475
+ " position: relative !important;",
1476
+ "}",
1477
+ "[data-slot-id].amplifyup-drag-over-slot::after,",
1478
+ "[data-amplifyup-slot].amplifyup-drag-over-slot::after {",
1479
+ ' content: "\\2713 Drop here" !important;',
1480
+ " position: absolute !important;",
1481
+ " top: 50% !important; left: 50% !important;",
1482
+ " transform: translate(-50%, -50%) !important;",
1483
+ " font-size: 14px !important; font-weight: 600 !important;",
1484
+ " color: rgb(22, 163, 74) !important;",
1485
+ " pointer-events: none !important;",
1486
+ " white-space: nowrap !important;",
1487
+ " z-index: 1 !important;",
1488
+ "}",
1489
+ "body.amplifyup-drag-no-drop { cursor: no-drop !important; }",
1490
+ "body.amplifyup-drag-no-drop::before {",
1491
+ ' content: "" !important;',
1492
+ " position: fixed !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important;",
1493
+ " background: rgba(239, 68, 68, 0.06) !important;",
1494
+ " pointer-events: none !important;",
1495
+ " z-index: 2147483646 !important;",
1496
+ "}",
1497
+ "body.amplifyup-drag-no-drop::after {",
1498
+ ' content: "\\2717 Not a drop zone" !important;',
1499
+ " position: fixed !important; bottom: 12px !important; right: 12px !important;",
1500
+ " font-size: 12px !important; font-weight: 600 !important;",
1501
+ " color: rgb(185, 28, 28) !important;",
1502
+ " background: rgba(254, 226, 226, 0.95) !important;",
1503
+ " padding: 6px 10px !important; border-radius: 6px !important;",
1504
+ " pointer-events: none !important;",
1505
+ " z-index: 2147483647 !important;",
1506
+ " box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;",
1331
1507
  "}"
1332
1508
  );
1333
1509
  }
@@ -1387,6 +1563,7 @@ Available Console Commands:
1387
1563
  disableHighlighting() {
1388
1564
  if (typeof document === "undefined") return;
1389
1565
  this.highlightingEnabled = false;
1566
+ this.teardownComposerDragDrop();
1390
1567
  if (this.mutationObserver) {
1391
1568
  this.mutationObserver.disconnect();
1392
1569
  this.mutationObserver = null;
@@ -1547,9 +1724,15 @@ Available Console Commands:
1547
1724
  e.preventDefault();
1548
1725
  e.stopPropagation();
1549
1726
  htmlEl.classList.remove("amplifyup-drag-over-slot");
1727
+ if (!win.parent || win.parent === win) return;
1728
+ win.parent.postMessage({
1729
+ type: "DROP_ON_SLOT",
1730
+ slotName,
1731
+ slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1732
+ }, "*");
1550
1733
  try {
1551
1734
  const data = JSON.parse(e.dataTransfer.getData("application/json"));
1552
- if (data.type === "component" && win.parent && win.parent !== win) {
1735
+ if (data.type === "component") {
1553
1736
  win.parent.postMessage({
1554
1737
  type: "COMPONENT_DROPPED",
1555
1738
  componentId: data.componentId,
@@ -1557,7 +1740,6 @@ Available Console Commands:
1557
1740
  slotName,
1558
1741
  slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1559
1742
  }, "*");
1560
- win.parent.postMessage({ type: "DROP_ON_SLOT", slotName }, "*");
1561
1743
  }
1562
1744
  } catch (_err) {
1563
1745
  }
@@ -1928,7 +2110,7 @@ function defaultPreviewCanvas() {
1928
2110
  "data-slot-id": "main",
1929
2111
  "data-amplifyup-slot": "main",
1930
2112
  "data-slot-empty": "true",
1931
- className: "min-h-[50vh] w-full"
2113
+ style: { minHeight: "50vh", width: "100%", display: "block" }
1932
2114
  }
1933
2115
  );
1934
2116
  }