@sitecore-feaas/clientside 0.0.9 → 0.1.0

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.
Files changed (57) hide show
  1. package/README.md +5 -5
  2. package/dist/browser/basic.cjs +67 -0
  3. package/dist/browser/basic.js +67 -0
  4. package/dist/browser/index.cjs +68 -79
  5. package/dist/browser/index.js +68 -79
  6. package/dist/node/{react.cjs → basic.cjs} +436 -443
  7. package/dist/node/{react.js → basic.js} +432 -439
  8. package/dist/node/index.cjs +666 -291
  9. package/dist/node/index.js +666 -295
  10. package/examples/index.html +1 -1
  11. package/package.json +17 -17
  12. package/src/basic.ts +5 -0
  13. package/src/cloud.ts +9 -5
  14. package/src/components/FEAASBase.tsx +265 -0
  15. package/src/components/FEAASComponent.ts +98 -0
  16. package/src/components/FEAASContext.tsx +105 -0
  17. package/src/components/FEAASEditor.tsx +291 -0
  18. package/src/components/FEAASLoader.ts +113 -0
  19. package/src/components/FEAASPicker.tsx +98 -0
  20. package/src/components/FEAASStylesheet.ts +43 -0
  21. package/src/components/index.tsx +7 -0
  22. package/src/index.ts +4 -11
  23. package/test/components.js +11 -0
  24. package/test/components.ts +12 -0
  25. package/test/render.js +688 -0
  26. package/tsconfig.json +11 -6
  27. package/types/basic.d.ts +5 -0
  28. package/types/cloud.d.ts +18 -0
  29. package/types/components/FEAASBase.d.ts +70 -0
  30. package/types/components/FEAASComponent.d.ts +39 -0
  31. package/types/components/FEAASContext.d.ts +43 -0
  32. package/types/components/FEAASEditor.d.ts +70 -0
  33. package/types/components/FEAASLoader.d.ts +20 -0
  34. package/types/components/FEAASPicker.d.ts +61 -0
  35. package/types/components/FEAASStylesheet.d.ts +25 -0
  36. package/types/components/index.d.ts +7 -0
  37. package/types/dom.d.ts +29 -0
  38. package/types/fetch.d.ts +1 -0
  39. package/types/index.d.ts +4 -0
  40. package/types/tsbuildinfo +1 -0
  41. package/dist/browser/index.d.ts +0 -25
  42. package/dist/browser/react.cjs +0 -83
  43. package/dist/browser/react.d.ts +0 -36
  44. package/dist/browser/react.js +0 -83
  45. package/dist/browser/webcomponent-6b45b819.d.ts +0 -120
  46. package/dist/node/index.d.ts +0 -25
  47. package/dist/node/react.d.ts +0 -36
  48. package/dist/node/webcomponent-6b45b819.d.ts +0 -120
  49. package/index.cjs +0 -83
  50. package/index.d.ts +0 -25
  51. package/index.js +0 -83
  52. package/react.cjs +0 -83
  53. package/react.d.ts +0 -36
  54. package/react.js +0 -83
  55. package/src/react.ts +0 -66
  56. package/src/webcomponent.ts +0 -393
  57. package/webcomponent-6b45b819.d.ts +0 -120
@@ -17,10 +17,6 @@ var __spreadValues = (a, b) => {
17
17
  return a;
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- var __export = (target, all) => {
21
- for (var name in all)
22
- __defProp(target, name, { get: all[name], enumerable: true });
23
- };
24
20
  var __async = (__this, __arguments, generator) => {
25
21
  return new Promise((resolve, reject) => {
26
22
  var fulfilled = (value) => {
@@ -42,25 +38,6 @@ var __async = (__this, __arguments, generator) => {
42
38
  });
43
39
  };
44
40
 
45
- // src/dom.ts
46
- var dom_exports = {};
47
- __export(dom_exports, {
48
- autoloadScripts: () => autoloadScripts,
49
- cleanCollectionBit: () => cleanCollectionBit,
50
- loadScript: () => loadScript,
51
- normalizeCollectionScope: () => normalizeCollectionScope,
52
- observeDOMElement: () => observeDOMElement,
53
- queryScopes: () => queryScopes,
54
- renderComponent: () => renderComponent,
55
- renderComponentElement: () => renderComponentElement,
56
- renderComponentPromise: () => renderComponentPromise,
57
- renderDOMContent: () => renderDOMContent,
58
- renderDOMElement: () => renderDOMElement,
59
- renderStylesheet: () => renderStylesheet,
60
- renderStylesheetPromise: () => renderStylesheetPromise,
61
- setDOMAttribute: () => setDOMAttribute
62
- });
63
-
64
41
  // ../node_modules/jsonpath-plus/dist/index-browser-esm.js
65
42
  function _typeof(obj) {
66
43
  "@babel/helpers - typeof";
@@ -796,20 +773,22 @@ function fetchAndRevalidate(url, options, validator, callback) {
796
773
 
797
774
  // src/cloud.ts
798
775
  function getComponentURL({
799
- hostname = "https://feaascomponentsapidev.blob.core.windows.net",
776
+ cdn = "https://feaascomponentsapidev.blob.core.windows.net",
777
+ hostname,
800
778
  library,
801
779
  component,
802
780
  version,
803
781
  revision = "published"
804
782
  }) {
805
- return `${hostname}/components/${library}/${component}/${version}/${revision}.html`;
783
+ return `${cdn || hostname}/components/${library}/${component}/${version}/${revision}.html`;
806
784
  }
807
785
  function getStylesheetURL({
808
- hostname = `https://feaascomponentsapidev.blob.core.windows.net`,
786
+ cdn = `https://feaascomponentsapidev.blob.core.windows.net`,
787
+ hostname,
809
788
  library,
810
789
  revision = "published"
811
790
  }) {
812
- return `${hostname}/styles/${library}/${revision}.css`;
791
+ return `${cdn || hostname}/styles/${library}/${revision}.css`;
813
792
  }
814
793
  function fetchComponent(props, callback) {
815
794
  return __async(this, null, function* () {
@@ -1119,59 +1098,356 @@ function renderComponentPromise(props, root) {
1119
1098
  });
1120
1099
  }
1121
1100
 
1122
- // src/webcomponent.ts
1123
- var webcomponent_exports = {};
1124
- __export(webcomponent_exports, {
1125
- HTMLFeaasComponent: () => HTMLFeaasComponent,
1126
- HTMLFeaasStylesheet: () => HTMLFeaasStylesheet
1127
- });
1128
- var WebComponent = typeof HTMLElement == "undefined" ? class {
1129
- } : HTMLElement;
1130
- var HTMLFeaasComponent = class extends WebComponent {
1101
+ // src/components/FEAASBase.tsx
1102
+ var FEAASBase = class extends HTMLElement {
1131
1103
  constructor() {
1132
1104
  super(...arguments);
1133
- this.connected = false;
1134
- this.template = null;
1135
- this.data = void 0;
1136
- this.isFetching = false;
1105
+ this.connected = null;
1106
+ this.refs = {};
1107
+ this.descendants = [];
1108
+ this.onLoad = (payload) => {
1109
+ if (payload == null)
1110
+ return;
1111
+ this.payload = payload;
1112
+ this.render();
1113
+ };
1114
+ }
1115
+ getProps(context) {
1116
+ return {};
1137
1117
  }
1138
- static get observedAttributes() {
1139
- return ["data", "library", "component", "version", "revision", "hostname", "template"];
1118
+ getRoot() {
1119
+ return this;
1120
+ }
1121
+ attributeChangedCallback(name, oldValue, newValue) {
1122
+ this.set();
1140
1123
  }
1141
1124
  connectedCallback() {
1142
- this.connected = true;
1143
- this.render();
1125
+ console.log("FEAAS: Connected", this);
1126
+ this.connected = new Event("feaasConnected", { bubbles: true, composed: true });
1127
+ this.dispatchEvent(this.connected);
1128
+ this.addEventListener("feaasConnected", this);
1129
+ this.addEventListener("feaasDisconnected", this);
1130
+ this.set();
1144
1131
  }
1145
1132
  disconnectedCallback() {
1133
+ console.log("FEAAS: Disconnected", this);
1134
+ this.removeEventListener("feaasConnected", this);
1135
+ this.removeEventListener("feaasDisconnected", this);
1136
+ this.dispatchEvent(new Event("feaasDisconnected", { bubbles: true, composed: true }));
1137
+ this.connected = null;
1138
+ }
1139
+ handleEvent(event) {
1140
+ switch (event.type) {
1141
+ case "feaasConnected":
1142
+ this.adoptedCallback(event);
1143
+ break;
1144
+ case "feaasDisconnected":
1145
+ this.orphanCallback(event);
1146
+ break;
1147
+ }
1146
1148
  }
1147
- attributeChangedCallback(name, oldValue, newValue) {
1148
- if (!this.connected)
1149
+ orphanCallback(event) {
1150
+ const target = event.composedPath()[0];
1151
+ const index = this.descendants.indexOf(target);
1152
+ if (index == -1)
1149
1153
  return;
1150
- if (name == "template") {
1151
- this.template = newValue;
1152
- } else if (name != "date") {
1153
- this.template = null;
1154
+ console.log("FEAAS: Orphaning", this, target);
1155
+ this.descendants.splice(index, -1);
1156
+ this.refs[target.getComponentName()] = null;
1157
+ if (target.refs.parent == this)
1158
+ target.refs.parent = null;
1159
+ if (target.refs.top == this) {
1160
+ target.refs.top = target.refs.parent;
1154
1161
  }
1155
- if (name == "data" && this.children.length > 0) {
1156
- this.update();
1157
- } else {
1158
- cancelAnimationFrame(this.rendering);
1159
- this.rendering = requestAnimationFrame(() => {
1162
+ }
1163
+ adoptedCallback(event) {
1164
+ var _a, _b;
1165
+ const target = event.composedPath()[0];
1166
+ this.descendants.push(target);
1167
+ console.log("FEAAS: Adopting", this, target);
1168
+ this.refs[target.getComponentName()] = target;
1169
+ target.refs.top = this;
1170
+ (_a = target.refs).parent || (_a.parent = this);
1171
+ if (target.renderReact) {
1172
+ for (var p = target.refs.parent; p; p = p.refs.parent) {
1173
+ if (p.renderReact) {
1174
+ (_b = target.refs).parentReact || (_b.parentReact = p);
1175
+ target.refs.topReact = p;
1176
+ }
1177
+ }
1178
+ }
1179
+ }
1180
+ getComponentName() {
1181
+ return this.tagName.replace("FEAAS-", "").toLowerCase();
1182
+ }
1183
+ isReadyToLoad() {
1184
+ return this.payload === void 0 && this.getAttribute("hidden") == null;
1185
+ }
1186
+ reify() {
1187
+ return __async(this, null, function* () {
1188
+ if (this.payload !== void 0) {
1160
1189
  this.render();
1190
+ } else if (this.isReadyToLoad()) {
1191
+ this.payload = null;
1192
+ this.load().then(this.onLoad);
1193
+ }
1194
+ });
1195
+ }
1196
+ load() {
1197
+ return Promise.resolve(null);
1198
+ }
1199
+ render(nested = false) {
1200
+ var _a, _b, _c;
1201
+ console.log("FEAAS: React", this.tagName, this.payload);
1202
+ if (this.payload == null)
1203
+ return;
1204
+ const { React, ReactDOM } = this.payload || {};
1205
+ if (!React)
1206
+ throw new Error(`${this.tagName}: React is not present in payload`);
1207
+ if (!ReactDOM)
1208
+ throw new Error(`${this.tagName}: ReactDOM is not present in payload`);
1209
+ if (!this.refs.topReact && !this.forceUpdateReact) {
1210
+ ReactDOM.render(React.createElement(this.getReactWrapper()), this.getReactRoot());
1211
+ } else if (this.forceUpdateReact) {
1212
+ (_a = this.forceUpdateReact) == null ? void 0 : _a.call(this);
1213
+ } else {
1214
+ (_c = (_b = this.refs.topReact).forceUpdateReact) == null ? void 0 : _c.call(_b);
1215
+ }
1216
+ }
1217
+ invokeReactRendering() {
1218
+ const { React, ReactDOM } = this.payload || {};
1219
+ const children = this.descendants.filter((r) => {
1220
+ return r.refs.parentReact == this && r.payload;
1221
+ }).map((child, index) => {
1222
+ return React.createElement(this.getReactWrapper(child), {
1223
+ key: this.tagName + "-" + index,
1224
+ slot: child.getAttribute("slot")
1161
1225
  });
1226
+ });
1227
+ return this.renderReact(__spreadProps(__spreadValues({}, this.props), { children }), this.payload);
1228
+ }
1229
+ getReactWrapper(target = this, displayName = target.tagName) {
1230
+ const Wrapper = target.reactWrapper || (target.reactWrapper = (props) => {
1231
+ if (target.renderReact) {
1232
+ const { React, ReactDOM } = target.payload || {};
1233
+ const [value, forceUpdateReact] = React.useReducer((r) => r + 1, 0);
1234
+ target.forceUpdateReact = forceUpdateReact;
1235
+ return ReactDOM.createPortal(<>{target.invokeReactRendering()}</>, target.getReactRoot());
1236
+ } else {
1237
+ return target.render();
1238
+ }
1239
+ });
1240
+ Object.assign(Wrapper, {
1241
+ displayName
1242
+ });
1243
+ return Wrapper;
1244
+ }
1245
+ getUID() {
1246
+ return this.uid || (this.uid = String(Math.random()));
1247
+ }
1248
+ getReactRoot() {
1249
+ return this.getRoot();
1250
+ }
1251
+ set(overrides) {
1252
+ this.overrides = __spreadValues(__spreadValues({}, this.overrides), overrides);
1253
+ this.props = __spreadValues(__spreadValues({}, this.getProps()), this.overrides);
1254
+ for (var property in this.props) {
1255
+ const value = this.props[property];
1256
+ if (this.props[property] == null) {
1257
+ this.removeAttribute(property);
1258
+ } else if (typeof value == "string" || typeof value == "number") {
1259
+ if (this.getAttribute(property) != String(value))
1260
+ this.setAttribute(property, String(value));
1261
+ }
1162
1262
  }
1263
+ this.scheduleRender();
1163
1264
  }
1164
- update() {
1165
- renderDOMElement(this, this.getProps().data);
1265
+ scheduleRender() {
1266
+ cancelAnimationFrame(this.nextRender);
1267
+ this.nextRender = requestAnimationFrame(() => {
1268
+ this.reify();
1269
+ });
1166
1270
  }
1167
- setData(data) {
1168
- this.data = data;
1169
- if (this.children.length > 0) {
1170
- this.update();
1271
+ formatURL(src, hostname) {
1272
+ if (typeof src == "string" && src.match(/\/[^\/]/)) {
1273
+ return hostname + src;
1171
1274
  } else {
1172
- this.render();
1275
+ return src;
1173
1276
  }
1174
1277
  }
1278
+ getContextAttribute(name, element = this) {
1279
+ var _a, _b;
1280
+ return (_b = (_a = element.closest(`[${name}]`)) == null ? void 0 : _a.getAttribute(name)) != null ? _b : this.getAttribute(name);
1281
+ }
1282
+ static register(tagName) {
1283
+ if (!window.customElements.get(tagName)) {
1284
+ window.customElements.define(tagName, this);
1285
+ }
1286
+ }
1287
+ };
1288
+
1289
+ // src/components/FEAASLoader.ts
1290
+ function FEAASLoaderProps(element = this) {
1291
+ return {
1292
+ hidden: (element == null ? void 0 : element.getAttribute("hidden")) != null
1293
+ };
1294
+ }
1295
+ var FEAASLoader = class extends FEAASBase {
1296
+ attributeChangedCallback(name, oldValue, newValue) {
1297
+ if (name == "hidden") {
1298
+ this.style.setProperty("display", "block", "important");
1299
+ this.style.setProperty("transition", "opacity 0.3s", "important");
1300
+ if (newValue) {
1301
+ this.style.setProperty("opacity", "0", "important");
1302
+ this.style.pointerEvents = "none";
1303
+ } else {
1304
+ this.style.setProperty("opacity", "1", "important");
1305
+ this.style.pointerEvents = "all";
1306
+ }
1307
+ }
1308
+ }
1309
+ connectedCallback() {
1310
+ this.attributeChangedCallback("hidden", this.getAttribute("hidden"), this.getAttribute("hidden"));
1311
+ this.stylesheet = document.createElement("style");
1312
+ this.stylesheet.textContent = `
1313
+ #loader {
1314
+ position: absolute;
1315
+ top: 0;
1316
+ left: 0;
1317
+ right: 0;
1318
+ bottom: 0;
1319
+ display: flex;
1320
+ align-items: center;
1321
+ justify-content: center;
1322
+ background: #f6f6ff;
1323
+ z-index: 1000;
1324
+ }
1325
+ @keyframes loader-circular-track {
1326
+ 0% {
1327
+ stroke-dasharray: 1, 400;
1328
+ stroke-dashoffset: 0;
1329
+ }
1330
+ 50% {
1331
+ stroke-dasharray: 400, 400;
1332
+ stroke-dashoffset: -100;
1333
+ }
1334
+ 100% {
1335
+ stroke-dasharray: 400, 400;
1336
+ stroke-dashoffset: -260;
1337
+ }
1338
+ }
1339
+
1340
+ @keyframes loader-circular-spinner {
1341
+ 0% {
1342
+ transform: rotate(0deg);
1343
+ }
1344
+
1345
+ 100% {
1346
+ transform: rotate(360deg);
1347
+ }
1348
+ }
1349
+ #loader .spinner {
1350
+ width: 48px;
1351
+ height: 48px;
1352
+ }
1353
+ `;
1354
+ this.loader = document.createElement("div");
1355
+ this.loader.id = "loader";
1356
+ this.loader.innerHTML = `
1357
+ <div class="spinner">
1358
+ <svg
1359
+ viewBox="0 0 100 100"
1360
+ style="
1361
+ animation: 2s linear 0s infinite normal none running loader-circular-spinner;
1362
+ display: block;
1363
+ width: 48px;
1364
+ height: 48px;
1365
+ "
1366
+ >
1367
+ <circle fill="transparent" cx="50" cy="50" r="42" stroke="rgba(0,0,0,0.1)" stroke-width="10px"></circle>
1368
+ <circle
1369
+ fill="transparent"
1370
+ stroke="#5548D9"
1371
+ cx="50"
1372
+ cy="50"
1373
+ r="42"
1374
+ stroke-width="10px"
1375
+ stroke-linecap="round"
1376
+ style="animation: 1.5s linear 0s infinite normal none running loader-circular-track"
1377
+ ></circle>
1378
+ </svg>
1379
+ </div>`;
1380
+ this.getRoot().appendChild(this.stylesheet);
1381
+ this.getRoot().appendChild(this.loader);
1382
+ }
1383
+ };
1384
+ FEAASLoader.observedAttributes = ["hidden"];
1385
+ FEAASLoader.register("feaas-loader");
1386
+
1387
+ // src/components/FEAASStylesheet.ts
1388
+ function FEAASStylesheetProps(element = this) {
1389
+ var _a;
1390
+ return {
1391
+ library: element == null ? void 0 : element.getContextAttribute("library"),
1392
+ revision: (_a = element == null ? void 0 : element.getAttribute("revision")) != null ? _a : "published",
1393
+ cdn: element == null ? void 0 : element.getContextAttribute("cdn")
1394
+ };
1395
+ }
1396
+ var FEAASStylesheet = class extends FEAASBase {
1397
+ getProps() {
1398
+ return FEAASStylesheetProps(this);
1399
+ }
1400
+ isReadyToLoad() {
1401
+ return this.props.library != null && this.props.revision != null && this.props.cdn != null;
1402
+ }
1403
+ load() {
1404
+ return __async(this, null, function* () {
1405
+ fetchStylesheet(this.props, this.onLoad);
1406
+ });
1407
+ }
1408
+ render() {
1409
+ if (!this.stylesheet) {
1410
+ this.stylesheet = document.createElement("style");
1411
+ this.appendChild(this.stylesheet);
1412
+ }
1413
+ this.stylesheet.textContent = this.payload;
1414
+ }
1415
+ };
1416
+ FEAASStylesheet.register("feaas-stylesheet");
1417
+
1418
+ // src/components/FEAASComponent.ts
1419
+ function FEAASComponentsProps(element = this) {
1420
+ let data;
1421
+ const dataValue = element.getAttribute("data");
1422
+ if (dataValue != null) {
1423
+ try {
1424
+ data = typeof dataValue == "string" ? JSON.parse(dataValue) : {};
1425
+ } catch (e) {
1426
+ }
1427
+ }
1428
+ return {
1429
+ data: data != null ? data : {},
1430
+ library: element.getAttribute("library"),
1431
+ component: element.getAttribute("component"),
1432
+ version: element.getAttribute("version"),
1433
+ revision: element.getAttribute("revision"),
1434
+ hostname: element.getAttribute("hostname"),
1435
+ cdn: element.getContextAttribute("cdn"),
1436
+ template: element.getAttribute("template")
1437
+ };
1438
+ }
1439
+ var FEAASComponent = class extends FEAASBase {
1440
+ setData(data) {
1441
+ this.set({ data });
1442
+ }
1443
+ getProps() {
1444
+ return FEAASComponentsProps(this);
1445
+ }
1446
+ attributeChangedCallback(name, oldValue, newValue) {
1447
+ this.payload = void 0;
1448
+ this.needsRefresh = true;
1449
+ super.attributeChangedCallback(name, oldValue, newValue);
1450
+ }
1175
1451
  getOriginal() {
1176
1452
  const scope = this.getAttribute("data-path-scope");
1177
1453
  if (!scope)
@@ -1185,277 +1461,372 @@ var HTMLFeaasComponent = class extends WebComponent {
1185
1461
  }
1186
1462
  return last;
1187
1463
  }
1188
- getData() {
1189
- var _a;
1190
- if (this.data) {
1191
- return this.data;
1192
- }
1193
- if (this.getAttribute("data")) {
1194
- const dataValue = this.getAttribute("data");
1195
- return typeof dataValue == "string" ? JSON.parse(dataValue) : {};
1196
- }
1197
- return ((_a = this.getOriginal()) == null ? void 0 : _a.getData()) || {};
1198
- return {};
1464
+ isReadyToLoad() {
1465
+ return super.isReadyToLoad() && this.props.library != null && this.props.component != null && this.props.version != null;
1199
1466
  }
1200
- render() {
1201
- if (this.getOriginal()) {
1202
- return;
1203
- }
1204
- const newProps = this.getProps();
1205
- if (this.template == null) {
1206
- if (!this.isFetching) {
1207
- if (newProps.component != null && newProps.version != null) {
1208
- this.isFetching = true;
1209
- fetchComponent(newProps, (template) => {
1210
- this.template = template;
1211
- this.isFetching = false;
1212
- renderDOMContent(this, this.template, newProps.data);
1213
- }).catch(() => {
1214
- console.error(
1215
- `FEAAS: Could not fetch component: ${newProps.component}/${newProps.version}/${newProps.revision}`
1216
- );
1217
- });
1218
- }
1467
+ load() {
1468
+ return __async(this, null, function* () {
1469
+ if (this.getOriginal()) {
1470
+ return;
1471
+ }
1472
+ try {
1473
+ fetchComponent(this.props, this.onLoad);
1474
+ } catch (e) {
1475
+ console.error(
1476
+ `FEAAS: Could not fetch component: ${this.props.component}/${this.props.version}/${this.props.revision}`
1477
+ );
1219
1478
  }
1479
+ });
1480
+ }
1481
+ render() {
1482
+ if (Array.from(this.children).filter((c) => c.tagName != "BR").length > 0 && !this.needsRefresh) {
1483
+ renderDOMElement(this, this.props.data);
1220
1484
  } else {
1221
- renderDOMContent(this, this.template, newProps.data);
1485
+ this.needsRefresh = true;
1486
+ if (this.getOriginal()) {
1487
+ return;
1488
+ }
1489
+ renderDOMContent(this, this.payload, this.props.data);
1222
1490
  }
1223
1491
  }
1224
- getProps() {
1225
- return {
1226
- data: this.getData(),
1227
- library: this.getAttribute("library") || void 0,
1228
- component: this.getAttribute("component") || void 0,
1229
- version: this.getAttribute("version") || void 0,
1230
- revision: this.getAttribute("revision") || void 0,
1231
- hostname: patchHostname(this.getAttribute("hostname") || void 0)
1232
- };
1233
- }
1234
1492
  };
1235
- var HTMLFeaasStylesheet = class extends WebComponent {
1493
+ FEAASComponent.observedAttributes = ["library", "component", "version", "revision", "hostname", "cdn", "template", "data"];
1494
+ FEAASComponent.register("feaas-component");
1495
+
1496
+ // src/components/FEAASEditor.tsx
1497
+ function getFramePosition(element) {
1498
+ let positions = [];
1499
+ if (element) {
1500
+ let currentWindow = element.ownerDocument.defaultView;
1501
+ let currentParentWindow;
1502
+ let rect;
1503
+ while (currentWindow !== window.top) {
1504
+ currentParentWindow = currentWindow.parent;
1505
+ for (let idx = 0; idx < currentParentWindow.frames.length; idx++)
1506
+ if (currentParentWindow.frames[idx] === currentWindow) {
1507
+ for (let frameElement of currentParentWindow.document.getElementsByTagName("iframe")) {
1508
+ if (frameElement.contentWindow === currentWindow) {
1509
+ rect = frameElement.getBoundingClientRect();
1510
+ const style = frameElement.contentWindow.getComputedStyle(frameElement);
1511
+ positions.push({
1512
+ left: rect.left + parseInt(style.borderLeftWidth),
1513
+ top: rect.top + parseInt(style.borderTopWidth)
1514
+ });
1515
+ }
1516
+ }
1517
+ currentWindow = currentParentWindow;
1518
+ break;
1519
+ }
1520
+ }
1521
+ }
1522
+ return positions.reduce(
1523
+ (accumulator, currentValue) => {
1524
+ return {
1525
+ left: accumulator.left + currentValue.left,
1526
+ top: accumulator.top + currentValue.top
1527
+ };
1528
+ },
1529
+ { left: 0, top: 0 }
1530
+ );
1531
+ }
1532
+ function FEAASEditorProps(element = this) {
1533
+ var _a;
1534
+ return {
1535
+ context: HTMLElement,
1536
+ frontend: element == null ? void 0 : element.getContextAttribute("frontend"),
1537
+ src: (_a = element == null ? void 0 : element.getAttribute("src")) != null ? _a : "/assets/editor.js",
1538
+ library: element == null ? void 0 : element.getAttribute("library"),
1539
+ component: element == null ? void 0 : element.getAttribute("component"),
1540
+ version: element == null ? void 0 : element.getAttribute("version"),
1541
+ revision: (element == null ? void 0 : element.getAttribute("revision")) || "saved",
1542
+ hidden: (element == null ? void 0 : element.getAttribute("hidden")) != null,
1543
+ for: element == null ? void 0 : element.getAttribute("for")
1544
+ };
1545
+ }
1546
+ var FEAASEditor = class extends FEAASBase {
1236
1547
  constructor() {
1237
1548
  super(...arguments);
1238
- this.connected = false;
1549
+ this.framePoint = { top: 0, left: 0 };
1550
+ this.wrapperRect = { top: 0, left: 0, width: 0, height: 0 };
1551
+ this.bodyRect = { top: 0, left: 0, width: 0, height: 0 };
1552
+ this.getTarget = () => {
1553
+ return document.querySelector("iframe").contentWindow.document.getElementById(this.props.for);
1554
+ };
1555
+ this.onChromeMeasureElement = (element, rect) => {
1556
+ if (rect == null) {
1557
+ rect = element.getBoundingClientRect();
1558
+ }
1559
+ if (element.ownerDocument == document) {
1560
+ return {
1561
+ left: rect.left - this.bodyRect.left,
1562
+ top: rect.top - this.bodyRect.top,
1563
+ width: rect.width,
1564
+ height: rect.height
1565
+ };
1566
+ }
1567
+ return {
1568
+ left: rect.left + this.framePoint.left - this.bodyRect.left,
1569
+ top: rect.top + this.framePoint.top - this.bodyRect.top,
1570
+ width: rect.width,
1571
+ height: rect.height
1572
+ };
1573
+ };
1574
+ this.onBeforeMeasure = (refs) => {
1575
+ if (refs.memoized.focusable)
1576
+ this.framePoint = getFramePosition(refs.memoized.focusable);
1577
+ this.wrapperRect = this.wrapper.getBoundingClientRect();
1578
+ this.bodyRect = this.wrapper.ownerDocument.body.parentElement.getBoundingClientRect();
1579
+ };
1580
+ this.onMeasure = () => {
1581
+ };
1582
+ this.onPosition = ({ focusable, root }) => {
1583
+ const chrome = this.wrapper.querySelector(".editor-chrome");
1584
+ const absolute = this.wrapper.querySelector(".ui.overlays");
1585
+ if (focusable) {
1586
+ const topbarHeight = 64;
1587
+ const padding = 16;
1588
+ chrome.style.width = focusable.width + padding * 2 + "px";
1589
+ chrome.style.height = focusable.height + padding * 1 + topbarHeight + "px";
1590
+ chrome.style.left = focusable.left - padding + "px";
1591
+ chrome.style.top = focusable.top - topbarHeight + "px";
1592
+ if (absolute) {
1593
+ absolute.style.left = root.left + "px";
1594
+ absolute.style.top = root.top + "px";
1595
+ }
1596
+ } else {
1597
+ chrome.style.left = "-1000px";
1598
+ chrome.style.top = "-1000px";
1599
+ }
1600
+ };
1239
1601
  }
1240
- static get observedAttributes() {
1241
- return ["library", "revision", "hostname"];
1602
+ getProps() {
1603
+ return FEAASEditorProps(this);
1242
1604
  }
1243
- connectedCallback() {
1244
- this.connected = true;
1245
- this.render();
1605
+ load() {
1606
+ return import(this.formatURL(this.props.src, this.props.frontend));
1607
+ }
1608
+ renderReact({ children, component: componentId, version: versionId }, {
1609
+ Editor,
1610
+ VersionStatusList,
1611
+ Version,
1612
+ EditorUIUndo,
1613
+ EditorUIMode,
1614
+ React: { useEffect, useMemo, useState },
1615
+ Chakra: { HStack, Box },
1616
+ Chrome,
1617
+ ReactDOM,
1618
+ useSDK,
1619
+ useSlots,
1620
+ useModelObserver
1621
+ }) {
1622
+ if (!this.payload)
1623
+ return;
1624
+ const { sdk } = useSDK();
1625
+ const [component, setComponent] = useState();
1626
+ const library = sdk.library;
1627
+ useEffect(() => {
1628
+ return setComponent(library.components.getItem(componentId));
1629
+ }, [componentId, library]);
1630
+ useEffect(() => {
1631
+ component == null ? void 0 : component.versions.fetch();
1632
+ }, [component]);
1633
+ const target = useMemo(this.getTarget, [this.props.for]);
1634
+ const observedVersions = useModelObserver(component == null ? void 0 : component.versions);
1635
+ const versions = useMemo(() => {
1636
+ return (observedVersions == null ? void 0 : observedVersions.filter((v) => v.id == versionId || !versionId)) || [];
1637
+ }, [observedVersions]);
1638
+ if (!component)
1639
+ return;
1640
+ return useSlots(children, ({ picker, children: children2 }) => <Box ref={(el) => this.wrapper = el}><Box className="editor-wrapper" position="absolute" left={0} top={0}><Editor sdk={sdk} library={sdk.library} component={component} versions={versions} context={{
1641
+ onChromeMeasure: this.onMeasure,
1642
+ onChromeBeforeMeasure: this.onBeforeMeasure,
1643
+ onChromeMeasureElement: this.onChromeMeasureElement,
1644
+ onChromePosition: this.onPosition
1645
+ }}>{({ editor, status, isArchivedDisplayed }) => ({
1646
+ after: children2,
1647
+ picker,
1648
+ chrome: editor && <Chrome />,
1649
+ children: component.reduceVersionRevisions(versions).filter((snapshot) => {
1650
+ var _a;
1651
+ return isArchivedDisplayed || !((_a = snapshot.saved || snapshot.draft) == null ? void 0 : _a.deletedAt);
1652
+ }).map((snapshot, index) => <Box key={snapshot.head.id} position="absolute" left={0} top={0} className="editor-chrome" p={4} boxShadow="2xl" pointerEvents="none">
1653
+ <Box className="borders" pointerEvents="none" left={0} width="4" top={0} bottom={0} backdropFilter="blur(2px)" background="rgb(244 244 244 / 80%)" position="absolute" />
1654
+ <Box className="borders" pointerEvents="none" right={0} width="4" top={0} bottom={0} backdropFilter="blur(2px)" background="rgb(244 244 244 / 80%)" position="absolute" />
1655
+ <Box className="borders" pointerEvents="none" top={0} height="16" left={0} right={0} backdropFilter="blur(2px)" background="rgb(244 244 244 / 80%)" position="absolute" />
1656
+ <Box className="borders" pointerEvents="none" bottom={0} height="4" left={0} right={0} backdropFilter="blur(2px)" background="rgb(244 244 244 / 80%)" position="absolute" />
1657
+ <Version version={snapshot.head} node={target}>{{
1658
+ left: <HStack justifyContent="space-between">
1659
+ <EditorUIUndo size="sm" editor={status == "ready" ? editor : null} />
1660
+ <EditorUIMode size="sm" />
1661
+ </HStack>,
1662
+ middle: <VersionStatusList />
1663
+ }}</Version>
1664
+ </Box>)
1665
+ })}</Editor></Box></Box>);
1666
+ }
1667
+ isReadyToLoad() {
1668
+ return this.props.component != null && this.props.version != null;
1246
1669
  }
1247
- attributeChangedCallback() {
1248
- if (this.connected)
1249
- this.render();
1670
+ };
1671
+ FEAASEditor.register("feaas-editor");
1672
+
1673
+ // src/components/FEAASPicker.tsx
1674
+ function FEAASPickerProps(element = this) {
1675
+ var _a, _b;
1676
+ return {
1677
+ part: (element == null ? void 0 : element.getAttribute("part")) || "picker",
1678
+ slot: (element == null ? void 0 : element.getAttribute("part")) || "slot",
1679
+ mode: element == null ? void 0 : element.getAttribute("mode"),
1680
+ frontend: (_a = element == null ? void 0 : element.getContextAttribute("frontend")) != null ? _a : "https://components.sitecorecloud.io",
1681
+ src: (_b = element == null ? void 0 : element.getAttribute("src")) != null ? _b : "/assets/picker.js",
1682
+ hidden: (element == null ? void 0 : element.getAttribute("hidden")) != null,
1683
+ context: void 0,
1684
+ onClose: void 0
1685
+ };
1686
+ }
1687
+ var FEAASPicker = class extends FEAASBase {
1688
+ getProps() {
1689
+ return FEAASPickerProps(this);
1250
1690
  }
1251
- render() {
1252
- const props = this.getProps();
1253
- fetchStylesheet(props, (cssText) => {
1254
- this.innerHTML = "<style>" + cssText + "</style>";
1255
- }).catch((e) => {
1256
- console.error(`FEAAS: Could not fetch stylesheet for library ${props.library}`);
1257
- });
1691
+ load() {
1692
+ return import(this.formatURL(this.props.src, this.props.frontend));
1258
1693
  }
1259
- getProps() {
1260
- return {
1261
- library: this.getAttribute("library") || void 0,
1262
- revision: this.getAttribute("revision") || void 0,
1263
- hostname: patchHostname(this.getAttribute("hostname") || void 0)
1264
- };
1694
+ renderReact({ context, mode, children }, { Picker, React, ReactDOM }) {
1695
+ return <Picker mode={mode || void 0} onClose={this.props.onClose} context={context}>{children}</Picker>;
1265
1696
  }
1266
1697
  };
1267
- function patchHostname(url) {
1268
- if (typeof url == "string" && (location.hostname.includes("pages-staging") || location.hostname.includes("staging-staging"))) {
1269
- return url.replace("components.sitecorecloud.io", "components-staging.sitecore-staging.cloud").replace("feaascompontsapiprod", "feaascomponentsstaging");
1270
- } else {
1271
- return url;
1272
- }
1698
+ FEAASPicker.register("feaas-picker");
1699
+ function FEAASPickerLazyProps(element = this) {
1700
+ var _a, _b;
1701
+ return {
1702
+ library: (element == null ? void 0 : element.getAttribute("library")) || "library",
1703
+ mode: element == null ? void 0 : element.getAttribute("mode"),
1704
+ frontend: (_a = element == null ? void 0 : element.getContextAttribute("frontend")) != null ? _a : "https://components.sitecorecloud.io",
1705
+ src: (_b = element == null ? void 0 : element.getAttribute("src")) != null ? _b : "/assets/picker.js",
1706
+ hidden: (element == null ? void 0 : element.getAttribute("hidden")) != null,
1707
+ context: null,
1708
+ getToken: null
1709
+ };
1273
1710
  }
1274
- var HTMLFeaasPickerLazy = class extends WebComponent {
1711
+ var FEAASPickerLazy = class extends FEAASBase {
1275
1712
  constructor() {
1276
1713
  super(...arguments);
1277
- this.src = "https://components.sitecorecloud.io/assets/picker.js";
1714
+ this.payload = null;
1278
1715
  this.onClose = () => {
1279
- this.set({
1280
- mode: null
1281
- });
1282
- };
1283
- this.onScriptLoaded = () => {
1284
- this.element = document.createElement("feaas-picker");
1285
- this.element.style.cssText = "border: 0; width: 100%; height: 100%; display: block";
1286
- this.appendChild(this.element);
1287
- this.set(this.getProps());
1716
+ this.setAttribute("hidden", "hidden");
1288
1717
  };
1289
1718
  }
1290
- static get observedAttributes() {
1291
- return ["library", "mode", "hidden", "src"];
1292
- }
1293
- connectedCallback() {
1294
- this.loader = document.createElement("feaas-loader");
1295
- this.loader.style.cssText = "position: absolute; top: 0; left: 0; right: 0;bottom: 0; ";
1296
- this.appendChild(this.loader);
1297
- }
1298
- load() {
1299
- if (!this.script) {
1300
- this.script = document.createElement("script");
1301
- this.script.type = "module";
1302
- this.script.onload = this.onScriptLoaded;
1303
- this.script.src = patchHostname(this.src);
1304
- this.script.crossOrigin = "anonymous";
1305
- this.appendChild(this.script);
1306
- }
1307
- }
1308
1719
  getProps() {
1309
- return {
1310
- mode: this.getAttribute("mode"),
1311
- context: this.context,
1312
- library: this.getAttribute("library"),
1313
- src: this.getAttribute("src")
1314
- };
1720
+ return FEAASPickerLazyProps(this);
1315
1721
  }
1316
- attributeChangedCallback(name, oldValue, newValue) {
1317
- this.set(this.getProps());
1318
- }
1319
- set(options) {
1320
- Object.assign(this, options);
1321
- const keys = Object.keys(this.getProps());
1322
- for (var key of keys) {
1323
- const value = this[key];
1324
- if (value == null) {
1325
- this.removeAttribute(key);
1326
- } else if (this.getAttribute(key) != String(value)) {
1327
- this.setAttribute(key, String(value));
1328
- }
1329
- }
1330
- if (this.mode) {
1331
- this.removeAttribute("hidden");
1332
- } else if (this.getAttribute("hidden") == null) {
1333
- this.setAttribute("hidden", "hidden");
1334
- }
1335
- if (this.mode && !this.hidden) {
1336
- this.load();
1337
- }
1338
- if (this.element && this.element.set) {
1339
- this.element.onClose = this.onClose;
1340
- this.element.getToken = this.getToken;
1341
- this.element.set(options);
1342
- }
1722
+ render() {
1723
+ const { frontend, library, getToken, src, mode, context } = this.props;
1724
+ if (this.children.length == 0)
1725
+ this.innerHTML = `
1726
+ <feaas-context src='/components/providers/ContextProvider.tsx' library='${library}'>
1727
+ <feaas-picker mode='${mode}'>
1728
+ <feaas-loader />
1729
+ </feaas-context>
1730
+ `;
1731
+ this.refs.context.set({
1732
+ frontend,
1733
+ library,
1734
+ getToken
1735
+ });
1736
+ this.refs.picker.set({
1737
+ frontend,
1738
+ context,
1739
+ src,
1740
+ mode,
1741
+ onClose: this.onClose
1742
+ });
1343
1743
  }
1344
1744
  };
1345
- var HTMLFeaasLoader = class extends WebComponent {
1346
- static get observedAttributes() {
1347
- return ["hidden"];
1348
- }
1349
- connectedCallback() {
1350
- this.style.opacity = "1";
1351
- this.style.transition = "opacity 0.1s 0.2s";
1352
- this.stylesheet = document.createElement("style");
1353
- this.stylesheet.textContent = `
1354
- #loader {
1355
- position: absolute;
1356
- top: 0;
1357
- left: 0;
1358
- right: 0;
1359
- bottom: 0;
1360
- display: flex;
1361
- align-items: center;
1362
- justify-content: center;
1363
- background: #f6f6ff;
1364
- z-index: 1000;
1365
- opacity: 1;
1366
- transition: top 0.3s, background-color 0.7s 0.3s;
1367
- }
1368
- @keyframes loader-disappearance {
1369
- 100% {
1370
- opacity: 0;
1371
- }
1745
+ FEAASPickerLazy.register("feaas-picker-lazy");
1746
+
1747
+ // src/components/FEAASContext.tsx
1748
+ function FEAASContextProps(element = this) {
1749
+ var _a, _b, _c, _d;
1750
+ return {
1751
+ library: element == null ? void 0 : element.getAttribute("library"),
1752
+ src: (_a = element == null ? void 0 : element.getAttribute("src")) != null ? _a : "/assets/context.js",
1753
+ frontend: (_b = element == null ? void 0 : element.getAttribute("frontend")) != null ? _b : "https://components.sitecoreclud.io",
1754
+ backend: (_c = element == null ? void 0 : element.getAttribute("backend")) != null ? _c : "https://components-api.sitecoreclud.io",
1755
+ cdn: (_d = element == null ? void 0 : element.getAttribute("cdn")) != null ? _d : "https://feaascompontsapiprod.blob.core.windows.net",
1756
+ getToken: null
1757
+ };
1372
1758
  }
1373
- @keyframes loader-appearance {
1374
- 0% {
1375
- opacity: 0;
1759
+ var FEAASContext = class extends FEAASBase {
1760
+ getProps() {
1761
+ return FEAASContextProps(this);
1376
1762
  }
1377
- }
1378
- @keyframes loader-circular-track {
1379
- 0% {
1380
- stroke-dasharray: 1, 400;
1381
- stroke-dashoffset: 0;
1763
+ load() {
1764
+ return import(this.formatURL(this.props.src, this.props.frontend));
1382
1765
  }
1383
- 50% {
1384
- stroke-dasharray: 400, 400;
1385
- stroke-dashoffset: -100;
1766
+ getTheme(theme) {
1767
+ return theme;
1386
1768
  }
1387
- 100% {
1388
- stroke-dasharray: 400, 400;
1389
- stroke-dashoffset: -260;
1769
+ getRoot() {
1770
+ return this.shadowRoot || this.attachShadow({ mode: "open" });
1390
1771
  }
1391
- }
1392
-
1393
- @keyframes loader-circular-spinner {
1394
- 0% {
1395
- transform: rotate(0deg);
1772
+ getReactRoot() {
1773
+ if (!this.reactRoot) {
1774
+ this.reactRoot = document.createElement("div");
1775
+ this.getRoot().appendChild(this.reactRoot);
1776
+ }
1777
+ return this.reactRoot;
1396
1778
  }
1397
-
1398
- 100% {
1399
- transform: rotate(360deg);
1779
+ adoptedCallback(event) {
1780
+ super.adoptedCallback(event);
1781
+ const target = event.composedPath()[0];
1782
+ if (target.parentNode != this.getRoot()) {
1783
+ this.getRoot().appendChild(target);
1784
+ }
1400
1785
  }
1401
- }
1402
- #loader .spinner {
1403
- animation: loader-disappearance 0.6s both;
1404
- width: 48px;
1405
- height: 48px;
1406
- }
1407
- #loader .spinner {
1408
- animation: loader-appearance 0.6s both;
1409
- }
1410
- `;
1411
- this.root = this.attachShadow({ mode: "open" });
1412
- this.loader = document.createElement("div");
1413
- this.loader.id = "loader";
1414
- this.loader.innerHTML = `
1415
- <div class="spinner">
1416
- <svg
1417
- viewBox="0 0 100 100"
1418
- style="
1419
- animation: 2s linear 0s infinite normal none running loader-circular-spinner;
1420
- display: block;
1421
- width: 48px;
1422
- height: 48px;
1423
- "
1424
- >
1425
- <circle fill="transparent" cx="50" cy="50" r="42" stroke="rgba(0,0,0,0.1)" stroke-width="10px"></circle>
1426
- <circle
1427
- fill="transparent"
1428
- stroke="#5548D9"
1429
- cx="50"
1430
- cy="50"
1431
- r="42"
1432
- stroke-width="10px"
1433
- stroke-linecap="round"
1434
- style="animation: 1.5s linear 0s infinite normal none running loader-circular-track"
1435
- ></circle>
1436
- </svg>
1437
- </div>`;
1438
- this.shadowRoot.appendChild(this.stylesheet);
1439
- this.shadowRoot.appendChild(this.loader);
1786
+ renderReact({ children, library }, { AppContext, Chakra: { Button, Tooltip }, React: { useRef } }) {
1787
+ Tooltip.defaultProps = {
1788
+ hasArrow: true,
1789
+ placement: "top",
1790
+ portalProps: {
1791
+ containerRef: { current: this.getRoot() }
1792
+ }
1793
+ };
1794
+ return <AppContext libraryId={this.props.library} container={this.getRoot()} getTheme={this.getTheme} modalProps={{
1795
+ portalProps: {
1796
+ containerRef: { current: this.getRoot() }
1797
+ }
1798
+ }} chakraProps={{
1799
+ cssVarsRoot: ":host",
1800
+ resetCSS: true,
1801
+ toastOptions: {
1802
+ portalProps: {
1803
+ containerRef: { current: this.getRoot() }
1804
+ }
1805
+ }
1806
+ }}>{children}</AppContext>;
1440
1807
  }
1441
1808
  };
1442
- if (typeof window != "undefined" && !window.customElements.get("feaas-stylesheet")) {
1443
- window.customElements.define("feaas-stylesheet", HTMLFeaasStylesheet);
1444
- window.customElements.define("feaas-component", HTMLFeaasComponent);
1445
- window.customElements.define("feaas-picker-lazy", HTMLFeaasPickerLazy);
1446
- if (!window.customElements.get("feaas-loader"))
1447
- window.customElements.define("feaas-loader", HTMLFeaasLoader);
1448
- }
1449
-
1450
- // src/index.ts
1451
- var src_default = __spreadValues(__spreadValues({}, dom_exports), webcomponent_exports);
1809
+ FEAASContext.observedAttributes = ["library", "component", "version", "revision", "hostname", "template"];
1810
+ FEAASContext.register("feaas-context");
1452
1811
  export {
1453
- HTMLFeaasComponent,
1454
- HTMLFeaasStylesheet,
1812
+ FEAASComponent,
1813
+ FEAASComponentsProps,
1814
+ FEAASContext,
1815
+ FEAASContextProps,
1816
+ FEAASEditor,
1817
+ FEAASEditorProps,
1818
+ FEAASLoader,
1819
+ FEAASLoaderProps,
1820
+ FEAASPicker,
1821
+ FEAASPickerLazy,
1822
+ FEAASPickerLazyProps,
1823
+ FEAASPickerProps,
1824
+ FEAASStylesheet,
1825
+ FEAASStylesheetProps,
1455
1826
  autoloadScripts,
1456
1827
  cleanCollectionBit,
1457
- src_default as default,
1458
1828
  fetchAndRevalidate,
1829
+ getFramePosition,
1459
1830
  loadScript,
1460
1831
  normalizeCollectionScope,
1461
1832
  observeDOMElement,