@amplifyup/sdk 0.1.10 → 0.1.12

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.
@@ -323,6 +323,7 @@ var AmplifyUpSDK = class {
323
323
  this.componentRegistry = [];
324
324
  this.messageListener = null;
325
325
  this.mutationObserver = null;
326
+ this.composerDragHandlers = null;
326
327
  this.isVisualEditorMode = false;
327
328
  this.eventSelectionMode = false;
328
329
  this.eventSelectionClickHandler = null;
@@ -1137,6 +1138,24 @@ Available Console Commands:
1137
1138
  }
1138
1139
  return;
1139
1140
  }
1141
+ if (event.data?.type === "COMPOSER_DRAG_AT_POINT") {
1142
+ const { x, y } = event.data;
1143
+ if (typeof x === "number" && typeof y === "number") {
1144
+ this.handleComposerDragAtPoint(x, y);
1145
+ }
1146
+ return;
1147
+ }
1148
+ if (event.data?.type === "COMPOSER_DROP_AT_POINT") {
1149
+ const { x, y } = event.data;
1150
+ if (typeof x === "number" && typeof y === "number") {
1151
+ this.handleComposerDropAtPoint(x, y);
1152
+ }
1153
+ return;
1154
+ }
1155
+ if (event.data?.type === "COMPOSER_DRAG_END") {
1156
+ this.clearComposerDragHighlight();
1157
+ return;
1158
+ }
1140
1159
  if (event.data?.type === "GET_ELEMENT_POSITION") {
1141
1160
  const { componentId, slotName } = event.data;
1142
1161
  const selector = slotName ? `[data-amplifyup-slot="${slotName}"], [data-slot-id="${slotName}"]` : `[data-amplifyup-component="${componentId}"], [data-component-id="${componentId}"]`;
@@ -1270,6 +1289,46 @@ Available Console Commands:
1270
1289
  }
1271
1290
  return el.tagName.toLowerCase();
1272
1291
  }
1292
+ /** Clear drag-over highlight state (Composer overlay path or native drag end). */
1293
+ clearComposerDragHighlight() {
1294
+ if (typeof document === "undefined") return;
1295
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1296
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1297
+ el.classList.remove("amplifyup-drag-over-slot");
1298
+ });
1299
+ document.body.classList.remove("amplifyup-drag-no-drop");
1300
+ }
1301
+ /**
1302
+ * Hit-test a slot at iframe-local coordinates (Composer overlay drag path).
1303
+ * Required for cross-origin drags: the browser blocks native drop inside the iframe.
1304
+ */
1305
+ handleComposerDragAtPoint(x, y) {
1306
+ if (typeof document === "undefined") return;
1307
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1308
+ this.clearComposerDragHighlight();
1309
+ const el = document.elementFromPoint(x, y)?.closest?.(slotSelector);
1310
+ if (el) {
1311
+ el.classList.add("amplifyup-drag-over-slot");
1312
+ } else {
1313
+ document.body.classList.add("amplifyup-drag-no-drop");
1314
+ }
1315
+ }
1316
+ handleComposerDropAtPoint(x, y) {
1317
+ if (typeof document === "undefined") return;
1318
+ const win = window;
1319
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1320
+ this.clearComposerDragHighlight();
1321
+ const htmlEl = document.elementFromPoint(x, y)?.closest?.(slotSelector);
1322
+ if (!htmlEl || !win.parent || win.parent === win) return;
1323
+ const slotName = htmlEl.getAttribute("data-amplifyup-slot") || htmlEl.getAttribute("data-slot-id");
1324
+ if (!slotName) return;
1325
+ const isEmpty = htmlEl.getAttribute("data-slot-empty") === "true";
1326
+ win.parent.postMessage({
1327
+ type: "DROP_ON_SLOT",
1328
+ slotName,
1329
+ slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1330
+ }, "*");
1331
+ }
1273
1332
  /**
1274
1333
  * Enable component highlighting
1275
1334
  */
@@ -1286,6 +1345,111 @@ Available Console Commands:
1286
1345
  this.applyComponentHighlights(highlightComponents, showZones);
1287
1346
  this.setupMutationObserver();
1288
1347
  }
1348
+ if (showZones) {
1349
+ this.setupComposerDragDrop();
1350
+ }
1351
+ }
1352
+ /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
1353
+ setupComposerDragDrop() {
1354
+ if (typeof document === "undefined") return;
1355
+ this.teardownComposerDragDrop();
1356
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1357
+ const clearDragOverState = () => {
1358
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1359
+ el.classList.remove("amplifyup-drag-over-slot");
1360
+ });
1361
+ document.body.classList.remove("amplifyup-drag-no-drop");
1362
+ };
1363
+ const dragenter = (e) => {
1364
+ e.preventDefault();
1365
+ if (e.dataTransfer?.types?.length) {
1366
+ document.body.classList.add("amplifyup-drag-no-drop");
1367
+ }
1368
+ };
1369
+ const dragover = (e) => {
1370
+ e.preventDefault();
1371
+ const target = e.target;
1372
+ const slot = target?.closest?.(slotSelector);
1373
+ if (e.dataTransfer) {
1374
+ e.dataTransfer.dropEffect = slot ? "copy" : "none";
1375
+ }
1376
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1377
+ el.classList.remove("amplifyup-drag-over-slot");
1378
+ });
1379
+ if (slot) {
1380
+ slot.classList.add("amplifyup-drag-over-slot");
1381
+ document.body.classList.remove("amplifyup-drag-no-drop");
1382
+ } else {
1383
+ document.body.classList.add("amplifyup-drag-no-drop");
1384
+ }
1385
+ };
1386
+ const dragleave = (e) => {
1387
+ const related = e.relatedTarget;
1388
+ if (!related || !document.body.contains(related)) {
1389
+ clearDragOverState();
1390
+ }
1391
+ };
1392
+ const drop = (e) => {
1393
+ e.preventDefault();
1394
+ const target = e.target;
1395
+ const slot = target?.closest?.(slotSelector);
1396
+ clearDragOverState();
1397
+ if (!slot || !this.isVisualEditorMode) return;
1398
+ const slotName = slot.getAttribute("data-amplifyup-slot") || slot.getAttribute("data-slot-id");
1399
+ const isEmpty = slot.getAttribute("data-slot-empty") === "true";
1400
+ const win = window;
1401
+ if (slotName && win.parent && win.parent !== win) {
1402
+ win.parent.postMessage({
1403
+ type: "DROP_ON_SLOT",
1404
+ slotName,
1405
+ slotElement: { path: slot.getAttribute("data-slot-path"), isEmpty }
1406
+ }, "*");
1407
+ }
1408
+ };
1409
+ const mouseover = (e) => {
1410
+ if (!this.showZonesEnabled) return;
1411
+ const target = e.target;
1412
+ const slot = target?.closest?.(slotSelector);
1413
+ if (!slot) return;
1414
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1415
+ el.classList.remove("amplifyup-slot-hover");
1416
+ });
1417
+ slot.classList.add("amplifyup-slot-hover");
1418
+ };
1419
+ const mouseout = (e) => {
1420
+ if (!this.showZonesEnabled) return;
1421
+ const related = e.relatedTarget;
1422
+ if (!related?.closest?.(slotSelector)) {
1423
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1424
+ el.classList.remove("amplifyup-slot-hover");
1425
+ });
1426
+ }
1427
+ };
1428
+ document.addEventListener("dragenter", dragenter, false);
1429
+ document.addEventListener("dragover", dragover, false);
1430
+ document.addEventListener("dragleave", dragleave, false);
1431
+ document.addEventListener("drop", drop, false);
1432
+ document.addEventListener("mouseover", mouseover, true);
1433
+ document.addEventListener("mouseout", mouseout, true);
1434
+ this.composerDragHandlers = { dragenter, dragover, dragleave, drop, mouseover, mouseout };
1435
+ }
1436
+ teardownComposerDragDrop() {
1437
+ if (!this.composerDragHandlers || typeof document === "undefined") return;
1438
+ const h = this.composerDragHandlers;
1439
+ document.removeEventListener("dragenter", h.dragenter, false);
1440
+ document.removeEventListener("dragover", h.dragover, false);
1441
+ document.removeEventListener("dragleave", h.dragleave, false);
1442
+ document.removeEventListener("drop", h.drop, false);
1443
+ document.removeEventListener("mouseover", h.mouseover, true);
1444
+ document.removeEventListener("mouseout", h.mouseout, true);
1445
+ this.composerDragHandlers = null;
1446
+ document.body.classList.remove("amplifyup-drag-no-drop");
1447
+ document.querySelectorAll("[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot").forEach((el) => {
1448
+ el.classList.remove("amplifyup-drag-over-slot");
1449
+ });
1450
+ document.querySelectorAll("[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover").forEach((el) => {
1451
+ el.classList.remove("amplifyup-slot-hover");
1452
+ });
1289
1453
  }
1290
1454
  /** Visual outlines for Composer (direct iframe path — no preview-frame proxy). */
1291
1455
  injectComposerHighlightStyles(highlightComponents, showZones) {
@@ -1312,19 +1476,64 @@ Available Console Commands:
1312
1476
  " outline: 2px dotted rgba(34, 197, 94, 0.75) !important;",
1313
1477
  " outline-offset: 4px !important;",
1314
1478
  " min-height: 3rem !important;",
1479
+ " padding: 20px !important;",
1315
1480
  " box-sizing: border-box !important;",
1316
1481
  " cursor: pointer !important;",
1482
+ " position: relative !important;",
1317
1483
  "}",
1318
1484
  '[data-slot-id="main"], [data-amplifyup-slot="main"] {',
1319
1485
  " min-height: 50vh !important;",
1320
1486
  "}",
1487
+ '[data-slot-id="main"][data-slot-empty="true"]::before,',
1488
+ '[data-amplifyup-slot="main"][data-slot-empty="true"]::before {',
1489
+ ' content: "Drop components here" !important;',
1490
+ " position: absolute !important;",
1491
+ " top: 50% !important; left: 50% !important;",
1492
+ " transform: translate(-50%, -50%) !important;",
1493
+ " font-size: 14px !important; font-weight: 500 !important;",
1494
+ " color: rgba(34, 197, 94, 0.65) !important;",
1495
+ " pointer-events: none !important;",
1496
+ " white-space: nowrap !important;",
1497
+ "}",
1321
1498
  "[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover {",
1322
1499
  " outline-color: rgba(34, 197, 94, 1) !important;",
1323
1500
  " background: rgba(34, 197, 94, 0.06) !important;",
1324
1501
  "}",
1325
1502
  "[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot {",
1326
1503
  " outline: 2px solid rgba(34, 197, 94, 1) !important;",
1327
- " background: rgba(34, 197, 94, 0.1) !important;",
1504
+ " background: rgba(34, 197, 94, 0.18) !important;",
1505
+ " position: relative !important;",
1506
+ "}",
1507
+ "[data-slot-id].amplifyup-drag-over-slot::after,",
1508
+ "[data-amplifyup-slot].amplifyup-drag-over-slot::after {",
1509
+ ' content: "\\2713 Drop here" !important;',
1510
+ " position: absolute !important;",
1511
+ " top: 50% !important; left: 50% !important;",
1512
+ " transform: translate(-50%, -50%) !important;",
1513
+ " font-size: 14px !important; font-weight: 600 !important;",
1514
+ " color: rgb(22, 163, 74) !important;",
1515
+ " pointer-events: none !important;",
1516
+ " white-space: nowrap !important;",
1517
+ " z-index: 1 !important;",
1518
+ "}",
1519
+ "body.amplifyup-drag-no-drop { cursor: no-drop !important; }",
1520
+ "body.amplifyup-drag-no-drop::before {",
1521
+ ' content: "" !important;',
1522
+ " position: fixed !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important;",
1523
+ " background: rgba(239, 68, 68, 0.06) !important;",
1524
+ " pointer-events: none !important;",
1525
+ " z-index: 2147483646 !important;",
1526
+ "}",
1527
+ "body.amplifyup-drag-no-drop::after {",
1528
+ ' content: "\\2717 Not a drop zone" !important;',
1529
+ " position: fixed !important; bottom: 12px !important; right: 12px !important;",
1530
+ " font-size: 12px !important; font-weight: 600 !important;",
1531
+ " color: rgb(185, 28, 28) !important;",
1532
+ " background: rgba(254, 226, 226, 0.95) !important;",
1533
+ " padding: 6px 10px !important; border-radius: 6px !important;",
1534
+ " pointer-events: none !important;",
1535
+ " z-index: 2147483647 !important;",
1536
+ " box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;",
1328
1537
  "}"
1329
1538
  );
1330
1539
  }
@@ -1384,6 +1593,7 @@ Available Console Commands:
1384
1593
  disableHighlighting() {
1385
1594
  if (typeof document === "undefined") return;
1386
1595
  this.highlightingEnabled = false;
1596
+ this.teardownComposerDragDrop();
1387
1597
  if (this.mutationObserver) {
1388
1598
  this.mutationObserver.disconnect();
1389
1599
  this.mutationObserver = null;
@@ -1544,9 +1754,15 @@ Available Console Commands:
1544
1754
  e.preventDefault();
1545
1755
  e.stopPropagation();
1546
1756
  htmlEl.classList.remove("amplifyup-drag-over-slot");
1757
+ if (!win.parent || win.parent === win) return;
1758
+ win.parent.postMessage({
1759
+ type: "DROP_ON_SLOT",
1760
+ slotName,
1761
+ slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1762
+ }, "*");
1547
1763
  try {
1548
1764
  const data = JSON.parse(e.dataTransfer.getData("application/json"));
1549
- if (data.type === "component" && win.parent && win.parent !== win) {
1765
+ if (data.type === "component") {
1550
1766
  win.parent.postMessage({
1551
1767
  type: "COMPONENT_DROPPED",
1552
1768
  componentId: data.componentId,
@@ -1554,7 +1770,6 @@ Available Console Commands:
1554
1770
  slotName,
1555
1771
  slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1556
1772
  }, "*");
1557
- win.parent.postMessage({ type: "DROP_ON_SLOT", slotName }, "*");
1558
1773
  }
1559
1774
  } catch (_err) {
1560
1775
  }