@amplifyup/sdk 0.1.6 → 0.1.8

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.mjs CHANGED
@@ -334,6 +334,8 @@ var AmplifyUpSDK = class {
334
334
  /** Ensures auto page() on init fires once per page load */
335
335
  this.initialPageSent = false;
336
336
  this.highlightingEnabled = false;
337
+ this.highlightComponentsEnabled = true;
338
+ this.showZonesEnabled = false;
337
339
  this.componentRegistry = [];
338
340
  this.messageListener = null;
339
341
  this.mutationObserver = null;
@@ -1128,7 +1130,7 @@ Available Console Commands:
1128
1130
  }
1129
1131
  if (event.data?.type === "GET_ELEMENT_POSITION") {
1130
1132
  const { componentId, slotName } = event.data;
1131
- const selector = slotName ? `[data-amplifyup-slot="${slotName}"]` : `[data-amplifyup-component="${componentId}"], [data-component-id="${componentId}"]`;
1133
+ const selector = slotName ? `[data-amplifyup-slot="${slotName}"], [data-slot-id="${slotName}"]` : `[data-amplifyup-component="${componentId}"], [data-component-id="${componentId}"]`;
1132
1134
  console.log("[AmplifyUp SDK] Finding element position:", { selector, componentId, slotName });
1133
1135
  const element = document.querySelector(selector);
1134
1136
  if (element) {
@@ -1266,12 +1268,62 @@ Available Console Commands:
1266
1268
  if (typeof document === "undefined") return;
1267
1269
  this.componentRegistry = componentRegistry;
1268
1270
  this.highlightingEnabled = true;
1271
+ this.highlightComponentsEnabled = highlightComponents;
1272
+ this.showZonesEnabled = showZones;
1269
1273
  this.disableHighlighting();
1270
- if (highlightComponents) {
1271
- this.applyComponentHighlights();
1274
+ this.highlightingEnabled = true;
1275
+ this.injectComposerHighlightStyles(highlightComponents, showZones);
1276
+ if (highlightComponents || showZones) {
1277
+ this.applyComponentHighlights(highlightComponents, showZones);
1272
1278
  this.setupMutationObserver();
1273
1279
  }
1274
1280
  }
1281
+ /** Visual outlines for Composer (direct iframe path — no preview-frame proxy). */
1282
+ injectComposerHighlightStyles(highlightComponents, showZones) {
1283
+ if (typeof document === "undefined") return;
1284
+ if (!highlightComponents && !showZones) return;
1285
+ const existing = document.getElementById("amplifyup-preview-highlights");
1286
+ if (existing) existing.remove();
1287
+ const lines = [];
1288
+ if (highlightComponents) {
1289
+ lines.push(
1290
+ "[data-amplifyup-component], [data-component-id] {",
1291
+ " outline: 1px dashed rgba(59, 130, 246, 0.8) !important;",
1292
+ " outline-offset: 2px !important;",
1293
+ " cursor: pointer !important;",
1294
+ "}",
1295
+ "[data-amplifyup-component]:hover, [data-component-id]:hover {",
1296
+ " outline-color: rgba(59, 130, 246, 1) !important;",
1297
+ "}"
1298
+ );
1299
+ }
1300
+ if (showZones) {
1301
+ lines.push(
1302
+ "[data-slot-id], [data-amplifyup-slot] {",
1303
+ " outline: 2px dotted rgba(34, 197, 94, 0.75) !important;",
1304
+ " outline-offset: 4px !important;",
1305
+ " min-height: 3rem !important;",
1306
+ " box-sizing: border-box !important;",
1307
+ " cursor: pointer !important;",
1308
+ "}",
1309
+ '[data-slot-id="main"], [data-amplifyup-slot="main"] {',
1310
+ " min-height: 50vh !important;",
1311
+ "}",
1312
+ "[data-slot-id].amplifyup-slot-hover, [data-amplifyup-slot].amplifyup-slot-hover {",
1313
+ " outline-color: rgba(34, 197, 94, 1) !important;",
1314
+ " background: rgba(34, 197, 94, 0.06) !important;",
1315
+ "}",
1316
+ "[data-slot-id].amplifyup-drag-over-slot, [data-amplifyup-slot].amplifyup-drag-over-slot {",
1317
+ " outline: 2px solid rgba(34, 197, 94, 1) !important;",
1318
+ " background: rgba(34, 197, 94, 0.1) !important;",
1319
+ "}"
1320
+ );
1321
+ }
1322
+ const style = document.createElement("style");
1323
+ style.id = "amplifyup-preview-highlights";
1324
+ style.textContent = lines.join("\n");
1325
+ document.head.appendChild(style);
1326
+ }
1275
1327
  /**
1276
1328
  * Set up MutationObserver to watch for new components being added
1277
1329
  */
@@ -1287,14 +1339,14 @@ Available Console Commands:
1287
1339
  mutation.addedNodes.forEach((node) => {
1288
1340
  if (node.nodeType === Node.ELEMENT_NODE) {
1289
1341
  const el = node;
1290
- if (el.hasAttribute("data-amplifyup-component") || el.hasAttribute("data-component-id") || el.hasAttribute("data-amplifyup-slot") || el.querySelector("[data-amplifyup-component], [data-component-id], [data-amplifyup-slot]")) {
1342
+ if (el.hasAttribute("data-amplifyup-component") || el.hasAttribute("data-component-id") || el.hasAttribute("data-amplifyup-slot") || el.hasAttribute("data-slot-id") || el.querySelector("[data-amplifyup-component], [data-component-id], [data-amplifyup-slot], [data-slot-id]")) {
1291
1343
  shouldReapply = true;
1292
1344
  }
1293
1345
  }
1294
1346
  });
1295
1347
  if (mutation.type === "attributes") {
1296
1348
  const target = mutation.target;
1297
- if (target.hasAttribute("data-amplifyup-slot") || target.hasAttribute("data-slot-empty")) {
1349
+ if (target.hasAttribute("data-amplifyup-slot") || target.hasAttribute("data-slot-id") || target.hasAttribute("data-slot-empty")) {
1298
1350
  shouldReapply = true;
1299
1351
  }
1300
1352
  }
@@ -1302,7 +1354,10 @@ Available Console Commands:
1302
1354
  if (shouldReapply && this.highlightingEnabled) {
1303
1355
  setTimeout(() => {
1304
1356
  if (this.highlightingEnabled) {
1305
- this.applyComponentHighlights();
1357
+ this.applyComponentHighlights(
1358
+ this.highlightComponentsEnabled,
1359
+ this.showZonesEnabled
1360
+ );
1306
1361
  }
1307
1362
  }, 100);
1308
1363
  }
@@ -1311,7 +1366,7 @@ Available Console Commands:
1311
1366
  childList: true,
1312
1367
  subtree: true,
1313
1368
  attributes: true,
1314
- attributeFilter: ["data-amplifyup-component", "data-component-id", "data-amplifyup-slot", "data-slot-empty"]
1369
+ attributeFilter: ["data-amplifyup-component", "data-component-id", "data-amplifyup-slot", "data-slot-id", "data-slot-empty"]
1315
1370
  });
1316
1371
  }
1317
1372
  /**
@@ -1324,6 +1379,7 @@ Available Console Commands:
1324
1379
  this.mutationObserver.disconnect();
1325
1380
  this.mutationObserver = null;
1326
1381
  }
1382
+ document.getElementById("amplifyup-preview-highlights")?.remove();
1327
1383
  document.querySelectorAll(".amplifyup-component-highlight").forEach((el) => {
1328
1384
  const htmlEl = el;
1329
1385
  const handlers = htmlEl._amplifyupHandlers;
@@ -1339,6 +1395,7 @@ Available Console Commands:
1339
1395
  if (handlers) {
1340
1396
  if (handlers.handleClick) htmlEl.removeEventListener("click", handlers.handleClick);
1341
1397
  if (handlers.handleDragOver) htmlEl.removeEventListener("dragover", handlers.handleDragOver);
1398
+ if (handlers.handleDragLeave) htmlEl.removeEventListener("dragleave", handlers.handleDragLeave);
1342
1399
  if (handlers.handleDrop) htmlEl.removeEventListener("drop", handlers.handleDrop);
1343
1400
  }
1344
1401
  delete htmlEl._amplifyupSlotHandlers;
@@ -1352,7 +1409,7 @@ Available Console Commands:
1352
1409
  const hierarchy = [];
1353
1410
  let current = element;
1354
1411
  while (current) {
1355
- if (current.hasAttribute("data-amplifyup-component") || current.hasAttribute("data-component-id") || current.hasAttribute("data-amplifyup-slot")) {
1412
+ if (current.hasAttribute("data-amplifyup-component") || current.hasAttribute("data-component-id") || current.hasAttribute("data-amplifyup-slot") || current.hasAttribute("data-slot-id")) {
1356
1413
  hierarchy.push(current);
1357
1414
  }
1358
1415
  current = current.parentElement;
@@ -1365,11 +1422,11 @@ Available Console Commands:
1365
1422
  sendSelectionMessage(element, hierarchy, isEmpty) {
1366
1423
  if (!this.isVisualEditorMode) return;
1367
1424
  const componentId = element.getAttribute("data-amplifyup-component") || element.getAttribute("data-component-id");
1368
- const slotName = element.getAttribute("data-amplifyup-slot");
1425
+ const slotName = element.getAttribute("data-amplifyup-slot") || element.getAttribute("data-slot-id");
1369
1426
  const componentName = element.getAttribute("data-component-name");
1370
1427
  const hierarchyInfo = hierarchy.map((el, index) => {
1371
1428
  const elComponentId = el.getAttribute("data-amplifyup-component") || el.getAttribute("data-component-id");
1372
- const elSlotName = el.getAttribute("data-amplifyup-slot");
1429
+ const elSlotName = el.getAttribute("data-amplifyup-slot") || el.getAttribute("data-slot-id");
1373
1430
  const elComponentName = el.getAttribute("data-component-name");
1374
1431
  return {
1375
1432
  index,
@@ -1407,98 +1464,116 @@ Available Console Commands:
1407
1464
  /**
1408
1465
  * Apply component highlights
1409
1466
  */
1410
- applyComponentHighlights() {
1467
+ applyComponentHighlights(highlightComponents = true, showZones = true) {
1411
1468
  if (typeof document === "undefined") return;
1412
1469
  if (!this.isVisualEditorMode) {
1413
1470
  return;
1414
1471
  }
1415
1472
  const doc = document;
1416
1473
  const win = window;
1417
- const componentElements = doc.querySelectorAll("[data-amplifyup-component], [data-component-id]");
1418
- console.log("[AmplifyUp SDK] Found", componentElements.length, "components to highlight");
1419
- this.sendComponentTree();
1420
- const slotElements = doc.querySelectorAll("[data-amplifyup-slot]");
1421
- console.log("[AmplifyUp SDK] Found", slotElements.length, "slots to highlight");
1422
- componentElements.forEach((el) => {
1423
- const htmlEl = el;
1424
- const componentId = htmlEl.getAttribute("data-amplifyup-component") || htmlEl.getAttribute("data-component-id");
1425
- if (!componentId) return;
1426
- if (htmlEl.classList.contains("amplifyup-component-highlight")) return;
1427
- const component = this.componentRegistry.find((c) => c.component_id === componentId);
1428
- htmlEl.classList.add("amplifyup-component-highlight");
1429
- htmlEl.setAttribute("data-component-id", componentId);
1430
- htmlEl.setAttribute("data-component-name", component?.component_name || componentId);
1431
- const hierarchy = this.buildComponentHierarchy(htmlEl);
1432
- const handleClick = (e) => {
1433
- e.stopPropagation();
1434
- this.sendSelectionMessage(htmlEl, hierarchy);
1435
- if (window.parent && window.parent !== window) {
1436
- window.parent.postMessage({ type: "CHECK_COMPONENT_DELETABLE", componentId }, "*");
1437
- }
1438
- };
1439
- htmlEl.addEventListener("click", handleClick);
1440
- htmlEl._amplifyupHandlers = { handleClick };
1441
- });
1442
- this.componentRegistry.forEach((component) => {
1443
- if (!component.data_attribute) return;
1444
- const elements = doc.querySelectorAll(`[${component.data_attribute}]`);
1445
- elements.forEach((el) => {
1474
+ if (highlightComponents) {
1475
+ const componentElements = doc.querySelectorAll("[data-amplifyup-component], [data-component-id]");
1476
+ console.log("[AmplifyUp SDK] Found", componentElements.length, "components to highlight");
1477
+ componentElements.forEach((el) => {
1446
1478
  const htmlEl = el;
1479
+ const componentId = htmlEl.getAttribute("data-amplifyup-component") || htmlEl.getAttribute("data-component-id");
1480
+ if (!componentId) return;
1447
1481
  if (htmlEl.classList.contains("amplifyup-component-highlight")) return;
1482
+ const component = this.componentRegistry.find((c) => c.component_id === componentId);
1448
1483
  htmlEl.classList.add("amplifyup-component-highlight");
1449
- htmlEl.setAttribute("data-component-id", component.component_id);
1450
- htmlEl.setAttribute("data-component-name", component.component_name);
1484
+ htmlEl.setAttribute("data-component-id", componentId);
1485
+ htmlEl.setAttribute("data-component-name", component?.component_name || componentId);
1451
1486
  const hierarchy = this.buildComponentHierarchy(htmlEl);
1452
1487
  const handleClick = (e) => {
1453
1488
  e.stopPropagation();
1454
1489
  this.sendSelectionMessage(htmlEl, hierarchy);
1455
1490
  if (window.parent && window.parent !== window) {
1456
- window.parent.postMessage({ type: "CHECK_COMPONENT_DELETABLE", componentId: component.component_id }, "*");
1491
+ window.parent.postMessage({ type: "CHECK_COMPONENT_DELETABLE", componentId }, "*");
1457
1492
  }
1458
1493
  };
1459
1494
  htmlEl.addEventListener("click", handleClick);
1460
1495
  htmlEl._amplifyupHandlers = { handleClick };
1461
1496
  });
1462
- });
1463
- slotElements.forEach((el) => {
1464
- const htmlEl = el;
1465
- const slotName = htmlEl.getAttribute("data-amplifyup-slot");
1466
- const isEmpty = htmlEl.getAttribute("data-slot-empty") === "true";
1467
- if (!slotName) return;
1468
- if (htmlEl.classList.contains("amplifyup-slot-highlight")) return;
1469
- htmlEl.classList.add("amplifyup-slot-highlight");
1470
- const hierarchy = this.buildComponentHierarchy(htmlEl);
1471
- const handleClick = (e) => {
1472
- e.stopPropagation();
1473
- this.sendSelectionMessage(htmlEl, hierarchy, isEmpty);
1474
- };
1475
- const handleDrop = (e) => {
1476
- e.preventDefault();
1477
- e.stopPropagation();
1478
- try {
1479
- const data = JSON.parse(e.dataTransfer.getData("application/json"));
1480
- if (data.type === "component" && win.parent && win.parent !== win) {
1481
- win.parent.postMessage({
1482
- type: "COMPONENT_DROPPED",
1483
- componentId: data.componentId,
1484
- componentName: data.componentName,
1485
- slotName,
1486
- slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1487
- }, "*");
1497
+ this.componentRegistry.forEach((component) => {
1498
+ if (!component.data_attribute) return;
1499
+ const elements = doc.querySelectorAll(`[${component.data_attribute}]`);
1500
+ elements.forEach((el) => {
1501
+ const htmlEl = el;
1502
+ if (htmlEl.classList.contains("amplifyup-component-highlight")) return;
1503
+ htmlEl.classList.add("amplifyup-component-highlight");
1504
+ htmlEl.setAttribute("data-component-id", component.component_id);
1505
+ htmlEl.setAttribute("data-component-name", component.component_name);
1506
+ const hierarchy = this.buildComponentHierarchy(htmlEl);
1507
+ const handleClick = (e) => {
1508
+ e.stopPropagation();
1509
+ this.sendSelectionMessage(htmlEl, hierarchy);
1510
+ if (window.parent && window.parent !== window) {
1511
+ window.parent.postMessage({ type: "CHECK_COMPONENT_DELETABLE", componentId: component.component_id }, "*");
1512
+ }
1513
+ };
1514
+ htmlEl.addEventListener("click", handleClick);
1515
+ htmlEl._amplifyupHandlers = { handleClick };
1516
+ });
1517
+ });
1518
+ }
1519
+ if (showZones) {
1520
+ const slotElements = doc.querySelectorAll("[data-amplifyup-slot], [data-slot-id]");
1521
+ console.log("[AmplifyUp SDK] Found", slotElements.length, "slots to highlight");
1522
+ slotElements.forEach((el) => {
1523
+ const htmlEl = el;
1524
+ const slotName = htmlEl.getAttribute("data-amplifyup-slot") || htmlEl.getAttribute("data-slot-id");
1525
+ const isEmpty = htmlEl.getAttribute("data-slot-empty") === "true";
1526
+ if (!slotName) return;
1527
+ if (htmlEl.classList.contains("amplifyup-slot-highlight")) return;
1528
+ htmlEl.classList.add("amplifyup-slot-highlight");
1529
+ const hierarchy = this.buildComponentHierarchy(htmlEl);
1530
+ const handleClick = (e) => {
1531
+ e.stopPropagation();
1532
+ this.sendSelectionMessage(htmlEl, hierarchy, isEmpty);
1533
+ };
1534
+ const handleDrop = (e) => {
1535
+ e.preventDefault();
1536
+ e.stopPropagation();
1537
+ htmlEl.classList.remove("amplifyup-drag-over-slot");
1538
+ try {
1539
+ const data = JSON.parse(e.dataTransfer.getData("application/json"));
1540
+ if (data.type === "component" && win.parent && win.parent !== win) {
1541
+ win.parent.postMessage({
1542
+ type: "COMPONENT_DROPPED",
1543
+ componentId: data.componentId,
1544
+ componentName: data.componentName,
1545
+ slotName,
1546
+ slotElement: { path: htmlEl.getAttribute("data-slot-path"), isEmpty }
1547
+ }, "*");
1548
+ win.parent.postMessage({ type: "DROP_ON_SLOT", slotName }, "*");
1549
+ }
1550
+ } catch (_err) {
1488
1551
  }
1489
- } catch (_err) {
1490
- }
1491
- };
1492
- const handleDragOver = (e) => {
1493
- e.preventDefault();
1494
- e.stopPropagation();
1495
- e.dataTransfer.dropEffect = "copy";
1496
- };
1497
- htmlEl.addEventListener("click", handleClick);
1498
- htmlEl.addEventListener("dragover", handleDragOver);
1499
- htmlEl.addEventListener("drop", handleDrop);
1500
- htmlEl._amplifyupSlotHandlers = { handleClick, handleDragOver, handleDrop };
1501
- });
1552
+ };
1553
+ const handleDragOver = (e) => {
1554
+ e.preventDefault();
1555
+ e.stopPropagation();
1556
+ e.dataTransfer.dropEffect = "copy";
1557
+ htmlEl.classList.add("amplifyup-drag-over-slot");
1558
+ };
1559
+ const handleDragLeave = (e) => {
1560
+ const related = e.relatedTarget;
1561
+ if (!related || !htmlEl.contains(related)) {
1562
+ htmlEl.classList.remove("amplifyup-drag-over-slot");
1563
+ }
1564
+ };
1565
+ htmlEl.addEventListener("click", handleClick);
1566
+ htmlEl.addEventListener("dragover", handleDragOver);
1567
+ htmlEl.addEventListener("dragleave", handleDragLeave);
1568
+ htmlEl.addEventListener("drop", handleDrop);
1569
+ htmlEl._amplifyupSlotHandlers = {
1570
+ handleClick,
1571
+ handleDragOver,
1572
+ handleDragLeave,
1573
+ handleDrop
1574
+ };
1575
+ });
1576
+ }
1502
1577
  }
1503
1578
  /**
1504
1579
  * Cleanup: Remove message listener and observers