@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.
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;
@@ -1165,6 +1166,24 @@ Available Console Commands:
1165
1166
  }
1166
1167
  return;
1167
1168
  }
1169
+ if (event.data?.type === "COMPOSER_DRAG_AT_POINT") {
1170
+ const { x, y } = event.data;
1171
+ if (typeof x === "number" && typeof y === "number") {
1172
+ this.handleComposerDragAtPoint(x, y);
1173
+ }
1174
+ return;
1175
+ }
1176
+ if (event.data?.type === "COMPOSER_DROP_AT_POINT") {
1177
+ const { x, y } = event.data;
1178
+ if (typeof x === "number" && typeof y === "number") {
1179
+ this.handleComposerDropAtPoint(x, y);
1180
+ }
1181
+ return;
1182
+ }
1183
+ if (event.data?.type === "COMPOSER_DRAG_END") {
1184
+ this.clearComposerDragHighlight();
1185
+ return;
1186
+ }
1168
1187
  if (event.data?.type === "GET_ELEMENT_POSITION") {
1169
1188
  const { componentId, slotName } = event.data;
1170
1189
  const selector = slotName ? `[data-amplifyup-slot="${slotName}"], [data-slot-id="${slotName}"]` : `[data-amplifyup-component="${componentId}"], [data-component-id="${componentId}"]`;
@@ -1298,6 +1317,46 @@ Available Console Commands:
1298
1317
  }
1299
1318
  return el.tagName.toLowerCase();
1300
1319
  }
1320
+ /** Clear drag-over highlight state (Composer overlay path or native drag end). */
1321
+ clearComposerDragHighlight() {
1322
+ if (typeof document === "undefined") return;
1323
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1324
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1325
+ el.classList.remove("amplifyup-drag-over-slot");
1326
+ });
1327
+ document.body.classList.remove("amplifyup-drag-no-drop");
1328
+ }
1329
+ /**
1330
+ * Hit-test a slot at iframe-local coordinates (Composer overlay drag path).
1331
+ * Required for cross-origin drags: the browser blocks native drop inside the iframe.
1332
+ */
1333
+ handleComposerDragAtPoint(x, y) {
1334
+ if (typeof document === "undefined") return;
1335
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1336
+ this.clearComposerDragHighlight();
1337
+ const el = document.elementFromPoint(x, y)?.closest?.(slotSelector);
1338
+ if (el) {
1339
+ el.classList.add("amplifyup-drag-over-slot");
1340
+ } else {
1341
+ document.body.classList.add("amplifyup-drag-no-drop");
1342
+ }
1343
+ }
1344
+ handleComposerDropAtPoint(x, y) {
1345
+ if (typeof document === "undefined") return;
1346
+ const win = window;
1347
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1348
+ this.clearComposerDragHighlight();
1349
+ const htmlEl = document.elementFromPoint(x, y)?.closest?.(slotSelector);
1350
+ if (!htmlEl || !win.parent || win.parent === win) return;
1351
+ const slotName = htmlEl.getAttribute("data-amplifyup-slot") || htmlEl.getAttribute("data-slot-id");
1352
+ if (!slotName) return;
1353
+ const isEmpty = htmlEl.getAttribute("data-slot-empty") === "true";
1354
+ win.parent.postMessage({
1355
+ type: "DROP_ON_SLOT",
1356
+ slotName,
1357
+ slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1358
+ }, "*");
1359
+ }
1301
1360
  /**
1302
1361
  * Enable component highlighting
1303
1362
  */
@@ -1314,6 +1373,111 @@ Available Console Commands:
1314
1373
  this.applyComponentHighlights(highlightComponents, showZones);
1315
1374
  this.setupMutationObserver();
1316
1375
  }
1376
+ if (showZones) {
1377
+ this.setupComposerDragDrop();
1378
+ }
1379
+ }
1380
+ /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
1381
+ setupComposerDragDrop() {
1382
+ if (typeof document === "undefined") return;
1383
+ this.teardownComposerDragDrop();
1384
+ const slotSelector = "[data-slot-id], [data-amplifyup-slot]";
1385
+ const clearDragOverState = () => {
1386
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1387
+ el.classList.remove("amplifyup-drag-over-slot");
1388
+ });
1389
+ document.body.classList.remove("amplifyup-drag-no-drop");
1390
+ };
1391
+ const dragenter = (e) => {
1392
+ e.preventDefault();
1393
+ if (e.dataTransfer?.types?.length) {
1394
+ document.body.classList.add("amplifyup-drag-no-drop");
1395
+ }
1396
+ };
1397
+ const dragover = (e) => {
1398
+ e.preventDefault();
1399
+ const target = e.target;
1400
+ const slot = target?.closest?.(slotSelector);
1401
+ if (e.dataTransfer) {
1402
+ e.dataTransfer.dropEffect = slot ? "copy" : "none";
1403
+ }
1404
+ document.querySelectorAll(`${slotSelector}.amplifyup-drag-over-slot`).forEach((el) => {
1405
+ el.classList.remove("amplifyup-drag-over-slot");
1406
+ });
1407
+ if (slot) {
1408
+ slot.classList.add("amplifyup-drag-over-slot");
1409
+ document.body.classList.remove("amplifyup-drag-no-drop");
1410
+ } else {
1411
+ document.body.classList.add("amplifyup-drag-no-drop");
1412
+ }
1413
+ };
1414
+ const dragleave = (e) => {
1415
+ const related = e.relatedTarget;
1416
+ if (!related || !document.body.contains(related)) {
1417
+ clearDragOverState();
1418
+ }
1419
+ };
1420
+ const drop = (e) => {
1421
+ e.preventDefault();
1422
+ const target = e.target;
1423
+ const slot = target?.closest?.(slotSelector);
1424
+ clearDragOverState();
1425
+ if (!slot || !this.isVisualEditorMode) return;
1426
+ const slotName = slot.getAttribute("data-amplifyup-slot") || slot.getAttribute("data-slot-id");
1427
+ const isEmpty = slot.getAttribute("data-slot-empty") === "true";
1428
+ const win = window;
1429
+ if (slotName && win.parent && win.parent !== win) {
1430
+ win.parent.postMessage({
1431
+ type: "DROP_ON_SLOT",
1432
+ slotName,
1433
+ slotElement: { path: slot.getAttribute("data-slot-path"), isEmpty }
1434
+ }, "*");
1435
+ }
1436
+ };
1437
+ const mouseover = (e) => {
1438
+ if (!this.showZonesEnabled) return;
1439
+ const target = e.target;
1440
+ const slot = target?.closest?.(slotSelector);
1441
+ if (!slot) return;
1442
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1443
+ el.classList.remove("amplifyup-slot-hover");
1444
+ });
1445
+ slot.classList.add("amplifyup-slot-hover");
1446
+ };
1447
+ const mouseout = (e) => {
1448
+ if (!this.showZonesEnabled) return;
1449
+ const related = e.relatedTarget;
1450
+ if (!related?.closest?.(slotSelector)) {
1451
+ document.querySelectorAll(`${slotSelector}.amplifyup-slot-hover`).forEach((el) => {
1452
+ el.classList.remove("amplifyup-slot-hover");
1453
+ });
1454
+ }
1455
+ };
1456
+ document.addEventListener("dragenter", dragenter, false);
1457
+ document.addEventListener("dragover", dragover, false);
1458
+ document.addEventListener("dragleave", dragleave, false);
1459
+ document.addEventListener("drop", drop, false);
1460
+ document.addEventListener("mouseover", mouseover, true);
1461
+ document.addEventListener("mouseout", mouseout, true);
1462
+ this.composerDragHandlers = { dragenter, dragover, dragleave, drop, mouseover, mouseout };
1463
+ }
1464
+ teardownComposerDragDrop() {
1465
+ if (!this.composerDragHandlers || typeof document === "undefined") return;
1466
+ const h = this.composerDragHandlers;
1467
+ document.removeEventListener("dragenter", h.dragenter, false);
1468
+ document.removeEventListener("dragover", h.dragover, false);
1469
+ document.removeEventListener("dragleave", h.dragleave, false);
1470
+ document.removeEventListener("drop", h.drop, false);
1471
+ document.removeEventListener("mouseover", h.mouseover, true);
1472
+ document.removeEventListener("mouseout", h.mouseout, true);
1473
+ this.composerDragHandlers = null;
1474
+ document.body.classList.remove("amplifyup-drag-no-drop");
1475
+ document.querySelectorAll("[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot").forEach((el) => {
1476
+ el.classList.remove("amplifyup-drag-over-slot");
1477
+ });
1478
+ document.querySelectorAll("[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover").forEach((el) => {
1479
+ el.classList.remove("amplifyup-slot-hover");
1480
+ });
1317
1481
  }
1318
1482
  /** Visual outlines for Composer (direct iframe path — no preview-frame proxy). */
1319
1483
  injectComposerHighlightStyles(highlightComponents, showZones) {
@@ -1340,19 +1504,64 @@ Available Console Commands:
1340
1504
  " outline: 2px dotted rgba(34, 197, 94, 0.75) !important;",
1341
1505
  " outline-offset: 4px !important;",
1342
1506
  " min-height: 3rem !important;",
1507
+ " padding: 20px !important;",
1343
1508
  " box-sizing: border-box !important;",
1344
1509
  " cursor: pointer !important;",
1510
+ " position: relative !important;",
1345
1511
  "}",
1346
1512
  '[data-slot-id="main"], [data-amplifyup-slot="main"] {',
1347
1513
  " min-height: 50vh !important;",
1348
1514
  "}",
1515
+ '[data-slot-id="main"][data-slot-empty="true"]::before,',
1516
+ '[data-amplifyup-slot="main"][data-slot-empty="true"]::before {',
1517
+ ' content: "Drop components here" !important;',
1518
+ " position: absolute !important;",
1519
+ " top: 50% !important; left: 50% !important;",
1520
+ " transform: translate(-50%, -50%) !important;",
1521
+ " font-size: 14px !important; font-weight: 500 !important;",
1522
+ " color: rgba(34, 197, 94, 0.65) !important;",
1523
+ " pointer-events: none !important;",
1524
+ " white-space: nowrap !important;",
1525
+ "}",
1349
1526
  "[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover {",
1350
1527
  " outline-color: rgba(34, 197, 94, 1) !important;",
1351
1528
  " background: rgba(34, 197, 94, 0.06) !important;",
1352
1529
  "}",
1353
1530
  "[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot {",
1354
1531
  " outline: 2px solid rgba(34, 197, 94, 1) !important;",
1355
- " background: rgba(34, 197, 94, 0.1) !important;",
1532
+ " background: rgba(34, 197, 94, 0.18) !important;",
1533
+ " position: relative !important;",
1534
+ "}",
1535
+ "[data-slot-id].amplifyup-drag-over-slot::after,",
1536
+ "[data-amplifyup-slot].amplifyup-drag-over-slot::after {",
1537
+ ' content: "\\2713 Drop here" !important;',
1538
+ " position: absolute !important;",
1539
+ " top: 50% !important; left: 50% !important;",
1540
+ " transform: translate(-50%, -50%) !important;",
1541
+ " font-size: 14px !important; font-weight: 600 !important;",
1542
+ " color: rgb(22, 163, 74) !important;",
1543
+ " pointer-events: none !important;",
1544
+ " white-space: nowrap !important;",
1545
+ " z-index: 1 !important;",
1546
+ "}",
1547
+ "body.amplifyup-drag-no-drop { cursor: no-drop !important; }",
1548
+ "body.amplifyup-drag-no-drop::before {",
1549
+ ' content: "" !important;',
1550
+ " position: fixed !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important;",
1551
+ " background: rgba(239, 68, 68, 0.06) !important;",
1552
+ " pointer-events: none !important;",
1553
+ " z-index: 2147483646 !important;",
1554
+ "}",
1555
+ "body.amplifyup-drag-no-drop::after {",
1556
+ ' content: "\\2717 Not a drop zone" !important;',
1557
+ " position: fixed !important; bottom: 12px !important; right: 12px !important;",
1558
+ " font-size: 12px !important; font-weight: 600 !important;",
1559
+ " color: rgb(185, 28, 28) !important;",
1560
+ " background: rgba(254, 226, 226, 0.95) !important;",
1561
+ " padding: 6px 10px !important; border-radius: 6px !important;",
1562
+ " pointer-events: none !important;",
1563
+ " z-index: 2147483647 !important;",
1564
+ " box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;",
1356
1565
  "}"
1357
1566
  );
1358
1567
  }
@@ -1412,6 +1621,7 @@ Available Console Commands:
1412
1621
  disableHighlighting() {
1413
1622
  if (typeof document === "undefined") return;
1414
1623
  this.highlightingEnabled = false;
1624
+ this.teardownComposerDragDrop();
1415
1625
  if (this.mutationObserver) {
1416
1626
  this.mutationObserver.disconnect();
1417
1627
  this.mutationObserver = null;
@@ -1572,9 +1782,15 @@ Available Console Commands:
1572
1782
  e.preventDefault();
1573
1783
  e.stopPropagation();
1574
1784
  htmlEl.classList.remove("amplifyup-drag-over-slot");
1785
+ if (!win.parent || win.parent === win) return;
1786
+ win.parent.postMessage({
1787
+ type: "DROP_ON_SLOT",
1788
+ slotName,
1789
+ slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1790
+ }, "*");
1575
1791
  try {
1576
1792
  const data = JSON.parse(e.dataTransfer.getData("application/json"));
1577
- if (data.type === "component" && win.parent && win.parent !== win) {
1793
+ if (data.type === "component") {
1578
1794
  win.parent.postMessage({
1579
1795
  type: "COMPONENT_DROPPED",
1580
1796
  componentId: data.componentId,
@@ -1582,7 +1798,6 @@ Available Console Commands:
1582
1798
  slotName,
1583
1799
  slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1584
1800
  }, "*");
1585
- win.parent.postMessage({ type: "DROP_ON_SLOT", slotName }, "*");
1586
1801
  }
1587
1802
  } catch (_err) {
1588
1803
  }
@@ -1953,7 +2168,7 @@ function defaultPreviewCanvas() {
1953
2168
  "data-slot-id": "main",
1954
2169
  "data-amplifyup-slot": "main",
1955
2170
  "data-slot-empty": "true",
1956
- className: "min-h-[50vh] w-full"
2171
+ style: { minHeight: "50vh", width: "100%", display: "block" }
1957
2172
  }
1958
2173
  );
1959
2174
  }