@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.
@@ -325,6 +325,7 @@ var AmplifyUpSDK = class {
325
325
  this.componentRegistry = [];
326
326
  this.messageListener = null;
327
327
  this.mutationObserver = null;
328
+ this.composerDragHandlers = null;
328
329
  this.isVisualEditorMode = false;
329
330
  this.eventSelectionMode = false;
330
331
  this.eventSelectionClickHandler = null;
@@ -1057,6 +1058,39 @@ Available Console Commands:
1057
1058
  this.attachEventListeners();
1058
1059
  }
1059
1060
  }
1061
+ /** Origins allowed to send Composer postMessage commands to the SDK. */
1062
+ isAllowedComposerOrigin(origin) {
1063
+ if (typeof window === "undefined") return false;
1064
+ if (origin === window.location.origin) return true;
1065
+ const staticAllowed = [
1066
+ "http://localhost:5001",
1067
+ "http://localhost:3001",
1068
+ "https://amplifyup.ai",
1069
+ "https://orchestrator.amplifyup.com"
1070
+ ];
1071
+ if (staticAllowed.includes(origin)) return true;
1072
+ try {
1073
+ const host = new URL(origin).hostname;
1074
+ if (host === "amplifyup.ai" || host.endsWith(".amplifyup.ai")) return true;
1075
+ } catch {
1076
+ }
1077
+ const orchestratorFromQuery = (() => {
1078
+ try {
1079
+ const value = new URLSearchParams(window.location.search).get("orchestratorUrl");
1080
+ return value ? new URL(value).origin : null;
1081
+ } catch {
1082
+ return null;
1083
+ }
1084
+ })();
1085
+ if (orchestratorFromQuery && origin === orchestratorFromQuery) return true;
1086
+ if (this.orchestratorUrl) {
1087
+ try {
1088
+ if (new URL(this.orchestratorUrl).origin === origin) return true;
1089
+ } catch {
1090
+ }
1091
+ }
1092
+ return false;
1093
+ }
1060
1094
  /**
1061
1095
  * Set up postMessage listener for highlighting commands from orchestrator
1062
1096
  */
@@ -1072,18 +1106,10 @@ Available Console Commands:
1072
1106
  currentOrigin: window.location.origin,
1073
1107
  data: event.data
1074
1108
  });
1075
- const allowedOrigins = [
1076
- "http://localhost:5001",
1077
- "http://localhost:3001",
1078
- "https://orchestrator.amplifyup.com"
1079
- ];
1080
- const isSameOrigin = event.origin === window.location.origin;
1081
- const isAllowedOrigin = allowedOrigins.some((origin) => event.origin.startsWith(origin));
1082
- if (!isSameOrigin && !isAllowedOrigin) {
1109
+ if (!this.isAllowedComposerOrigin(event.origin)) {
1083
1110
  console.warn("[AmplifyUp SDK] Rejected message from unauthorized origin:", {
1084
1111
  eventOrigin: event.origin,
1085
- currentOrigin: window.location.origin,
1086
- allowedOrigins
1112
+ currentOrigin: window.location.origin
1087
1113
  });
1088
1114
  return;
1089
1115
  }
@@ -1263,6 +1289,111 @@ Available Console Commands:
1263
1289
  this.applyComponentHighlights(highlightComponents, showZones);
1264
1290
  this.setupMutationObserver();
1265
1291
  }
1292
+ if (showZones) {
1293
+ this.setupComposerDragDrop();
1294
+ }
1295
+ }
1296
+ /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
1297
+ setupComposerDragDrop() {
1298
+ if (typeof document === "undefined") return;
1299
+ this.teardownComposerDragDrop();
1300
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1301
+ const clearDragOverState = () => {
1302
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1303
+ el.classList.remove("amplifyup-drag-over-slot");
1304
+ });
1305
+ document.body.classList.remove("amplifyup-drag-no-drop");
1306
+ };
1307
+ const dragenter = (e) => {
1308
+ e.preventDefault();
1309
+ if (e.dataTransfer?.types?.length) {
1310
+ document.body.classList.add("amplifyup-drag-no-drop");
1311
+ }
1312
+ };
1313
+ const dragover = (e) => {
1314
+ e.preventDefault();
1315
+ const target = e.target;
1316
+ const slot = target?.closest?.(slotSelector);
1317
+ if (e.dataTransfer) {
1318
+ e.dataTransfer.dropEffect = slot ? "copy" : "none";
1319
+ }
1320
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1321
+ el.classList.remove("amplifyup-drag-over-slot");
1322
+ });
1323
+ if (slot) {
1324
+ slot.classList.add("amplifyup-drag-over-slot");
1325
+ document.body.classList.remove("amplifyup-drag-no-drop");
1326
+ } else {
1327
+ document.body.classList.add("amplifyup-drag-no-drop");
1328
+ }
1329
+ };
1330
+ const dragleave = (e) => {
1331
+ const related = e.relatedTarget;
1332
+ if (!related || !document.body.contains(related)) {
1333
+ clearDragOverState();
1334
+ }
1335
+ };
1336
+ const drop = (e) => {
1337
+ e.preventDefault();
1338
+ const target = e.target;
1339
+ const slot = target?.closest?.(slotSelector);
1340
+ clearDragOverState();
1341
+ if (!slot || !this.isVisualEditorMode) return;
1342
+ const slotName = slot.getAttribute("data-amplifyup-slot") || slot.getAttribute("data-slot-id");
1343
+ const isEmpty = slot.getAttribute("data-slot-empty") === "true";
1344
+ const win = window;
1345
+ if (slotName && win.parent && win.parent !== win) {
1346
+ win.parent.postMessage({
1347
+ type: "DROP_ON_SLOT",
1348
+ slotName,
1349
+ slotElement: { path: slot.getAttribute("data-slot-path"), isEmpty }
1350
+ }, "*");
1351
+ }
1352
+ };
1353
+ const mouseover = (e) => {
1354
+ if (!this.showZonesEnabled) return;
1355
+ const target = e.target;
1356
+ const slot = target?.closest?.(slotSelector);
1357
+ if (!slot) return;
1358
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1359
+ el.classList.remove("amplifyup-slot-hover");
1360
+ });
1361
+ slot.classList.add("amplifyup-slot-hover");
1362
+ };
1363
+ const mouseout = (e) => {
1364
+ if (!this.showZonesEnabled) return;
1365
+ const related = e.relatedTarget;
1366
+ if (!related?.closest?.(slotSelector)) {
1367
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1368
+ el.classList.remove("amplifyup-slot-hover");
1369
+ });
1370
+ }
1371
+ };
1372
+ document.addEventListener("dragenter", dragenter, false);
1373
+ document.addEventListener("dragover", dragover, false);
1374
+ document.addEventListener("dragleave", dragleave, false);
1375
+ document.addEventListener("drop", drop, false);
1376
+ document.addEventListener("mouseover", mouseover, true);
1377
+ document.addEventListener("mouseout", mouseout, true);
1378
+ this.composerDragHandlers = { dragenter, dragover, dragleave, drop, mouseover, mouseout };
1379
+ }
1380
+ teardownComposerDragDrop() {
1381
+ if (!this.composerDragHandlers || typeof document === "undefined") return;
1382
+ const h = this.composerDragHandlers;
1383
+ document.removeEventListener("dragenter", h.dragenter, false);
1384
+ document.removeEventListener("dragover", h.dragover, false);
1385
+ document.removeEventListener("dragleave", h.dragleave, false);
1386
+ document.removeEventListener("drop", h.drop, false);
1387
+ document.removeEventListener("mouseover", h.mouseover, true);
1388
+ document.removeEventListener("mouseout", h.mouseout, true);
1389
+ this.composerDragHandlers = null;
1390
+ document.body.classList.remove("amplifyup-drag-no-drop");
1391
+ document.querySelectorAll("[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot").forEach((el) => {
1392
+ el.classList.remove("amplifyup-drag-over-slot");
1393
+ });
1394
+ document.querySelectorAll("[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover").forEach((el) => {
1395
+ el.classList.remove("amplifyup-slot-hover");
1396
+ });
1266
1397
  }
1267
1398
  /** Visual outlines for Composer (direct iframe path — no preview-frame proxy). */
1268
1399
  injectComposerHighlightStyles(highlightComponents, showZones) {
@@ -1289,19 +1420,64 @@ Available Console Commands:
1289
1420
  " outline: 2px dotted rgba(34, 197, 94, 0.75) !important;",
1290
1421
  " outline-offset: 4px !important;",
1291
1422
  " min-height: 3rem !important;",
1423
+ " padding: 20px !important;",
1292
1424
  " box-sizing: border-box !important;",
1293
1425
  " cursor: pointer !important;",
1426
+ " position: relative !important;",
1294
1427
  "}",
1295
1428
  '[data-slot-id="main"], [data-amplifyup-slot="main"] {',
1296
1429
  " min-height: 50vh !important;",
1297
1430
  "}",
1431
+ '[data-slot-id="main"][data-slot-empty="true"]::before,',
1432
+ '[data-amplifyup-slot="main"][data-slot-empty="true"]::before {',
1433
+ ' content: "Drop components here" !important;',
1434
+ " position: absolute !important;",
1435
+ " top: 50% !important; left: 50% !important;",
1436
+ " transform: translate(-50%, -50%) !important;",
1437
+ " font-size: 14px !important; font-weight: 500 !important;",
1438
+ " color: rgba(34, 197, 94, 0.65) !important;",
1439
+ " pointer-events: none !important;",
1440
+ " white-space: nowrap !important;",
1441
+ "}",
1298
1442
  "[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover {",
1299
1443
  " outline-color: rgba(34, 197, 94, 1) !important;",
1300
1444
  " background: rgba(34, 197, 94, 0.06) !important;",
1301
1445
  "}",
1302
1446
  "[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot {",
1303
1447
  " outline: 2px solid rgba(34, 197, 94, 1) !important;",
1304
- " background: rgba(34, 197, 94, 0.1) !important;",
1448
+ " background: rgba(34, 197, 94, 0.18) !important;",
1449
+ " position: relative !important;",
1450
+ "}",
1451
+ "[data-slot-id].amplifyup-drag-over-slot::after,",
1452
+ "[data-amplifyup-slot].amplifyup-drag-over-slot::after {",
1453
+ ' content: "\\2713 Drop here" !important;',
1454
+ " position: absolute !important;",
1455
+ " top: 50% !important; left: 50% !important;",
1456
+ " transform: translate(-50%, -50%) !important;",
1457
+ " font-size: 14px !important; font-weight: 600 !important;",
1458
+ " color: rgb(22, 163, 74) !important;",
1459
+ " pointer-events: none !important;",
1460
+ " white-space: nowrap !important;",
1461
+ " z-index: 1 !important;",
1462
+ "}",
1463
+ "body.amplifyup-drag-no-drop { cursor: no-drop !important; }",
1464
+ "body.amplifyup-drag-no-drop::before {",
1465
+ ' content: "" !important;',
1466
+ " position: fixed !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important;",
1467
+ " background: rgba(239, 68, 68, 0.06) !important;",
1468
+ " pointer-events: none !important;",
1469
+ " z-index: 2147483646 !important;",
1470
+ "}",
1471
+ "body.amplifyup-drag-no-drop::after {",
1472
+ ' content: "\\2717 Not a drop zone" !important;',
1473
+ " position: fixed !important; bottom: 12px !important; right: 12px !important;",
1474
+ " font-size: 12px !important; font-weight: 600 !important;",
1475
+ " color: rgb(185, 28, 28) !important;",
1476
+ " background: rgba(254, 226, 226, 0.95) !important;",
1477
+ " padding: 6px 10px !important; border-radius: 6px !important;",
1478
+ " pointer-events: none !important;",
1479
+ " z-index: 2147483647 !important;",
1480
+ " box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;",
1305
1481
  "}"
1306
1482
  );
1307
1483
  }
@@ -1361,6 +1537,7 @@ Available Console Commands:
1361
1537
  disableHighlighting() {
1362
1538
  if (typeof document === "undefined") return;
1363
1539
  this.highlightingEnabled = false;
1540
+ this.teardownComposerDragDrop();
1364
1541
  if (this.mutationObserver) {
1365
1542
  this.mutationObserver.disconnect();
1366
1543
  this.mutationObserver = null;
@@ -1521,9 +1698,15 @@ Available Console Commands:
1521
1698
  e.preventDefault();
1522
1699
  e.stopPropagation();
1523
1700
  htmlEl.classList.remove("amplifyup-drag-over-slot");
1701
+ if (!win.parent || win.parent === win) return;
1702
+ win.parent.postMessage({
1703
+ type: "DROP_ON_SLOT",
1704
+ slotName,
1705
+ slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1706
+ }, "*");
1524
1707
  try {
1525
1708
  const data = JSON.parse(e.dataTransfer.getData("application/json"));
1526
- if (data.type === "component" && win.parent && win.parent !== win) {
1709
+ if (data.type === "component") {
1527
1710
  win.parent.postMessage({
1528
1711
  type: "COMPONENT_DROPPED",
1529
1712
  componentId: data.componentId,
@@ -1531,7 +1714,6 @@ Available Console Commands:
1531
1714
  slotName,
1532
1715
  slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1533
1716
  }, "*");
1534
- win.parent.postMessage({ type: "DROP_ON_SLOT", slotName }, "*");
1535
1717
  }
1536
1718
  } catch (_err) {
1537
1719
  }