@builder.io/sdk-qwik 0.12.6 → 0.12.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.
@@ -313,6 +313,201 @@ function getProcessedBlock({ block, context, shouldEvaluateBindings, localState,
313
313
  else
314
314
  return transformedBlock;
315
315
  }
316
+ function throttle(func, wait, options = {}) {
317
+ let context;
318
+ let args;
319
+ let result;
320
+ let timeout = null;
321
+ let previous = 0;
322
+ const later = function() {
323
+ previous = options.leading === false ? 0 : Date.now();
324
+ timeout = null;
325
+ result = func.apply(context, args);
326
+ if (!timeout)
327
+ context = args = null;
328
+ };
329
+ return function() {
330
+ const now = Date.now();
331
+ if (!previous && options.leading === false)
332
+ previous = now;
333
+ const remaining = wait - (now - previous);
334
+ context = this;
335
+ args = arguments;
336
+ if (remaining <= 0 || remaining > wait) {
337
+ if (timeout) {
338
+ clearTimeout(timeout);
339
+ timeout = null;
340
+ }
341
+ previous = now;
342
+ result = func.apply(context, args);
343
+ if (!timeout)
344
+ context = args = null;
345
+ } else if (!timeout && options.trailing !== false)
346
+ timeout = setTimeout(later, remaining);
347
+ return result;
348
+ };
349
+ }
350
+ function assign(target, ..._args) {
351
+ const to = Object(target);
352
+ for (let index = 1; index < arguments.length; index++) {
353
+ const nextSource = arguments[index];
354
+ if (nextSource != null) {
355
+ for (const nextKey in nextSource)
356
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey))
357
+ to[nextKey] = nextSource[nextKey];
358
+ }
359
+ }
360
+ return to;
361
+ }
362
+ const camelCaseToKebabCase = (str) => str ? str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`) : "";
363
+ function bindAnimations(animations) {
364
+ for (const animation of animations)
365
+ switch (animation.trigger) {
366
+ case "pageLoad":
367
+ triggerAnimation(animation);
368
+ break;
369
+ case "hover":
370
+ bindHoverAnimation(animation);
371
+ break;
372
+ case "scrollInView":
373
+ bindScrollInViewAnimation(animation);
374
+ break;
375
+ }
376
+ }
377
+ function warnElementNotPresent(id) {
378
+ console.warn(`Cannot animate element: element with ID ${id} not found!`);
379
+ }
380
+ function augmentAnimation(animation, element) {
381
+ const stylesUsed = getAllStylesUsed(animation);
382
+ const computedStyle = getComputedStyle(element);
383
+ const firstStyles = animation.steps[0].styles;
384
+ const lastStyles = animation.steps[animation.steps.length - 1].styles;
385
+ const bothStyles = [
386
+ firstStyles,
387
+ lastStyles
388
+ ];
389
+ for (const styles of bothStyles) {
390
+ for (const style of stylesUsed)
391
+ if (!(style in styles))
392
+ styles[style] = computedStyle[style];
393
+ }
394
+ }
395
+ function getAllStylesUsed(animation) {
396
+ const properties = [];
397
+ for (const step of animation.steps) {
398
+ for (const key in step.styles)
399
+ if (properties.indexOf(key) === -1)
400
+ properties.push(key);
401
+ }
402
+ return properties;
403
+ }
404
+ function triggerAnimation(animation) {
405
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
406
+ if (!elements.length) {
407
+ warnElementNotPresent(animation.elementId || animation.id || "");
408
+ return;
409
+ }
410
+ Array.from(elements).forEach((element) => {
411
+ augmentAnimation(animation, element);
412
+ element.style.transition = "none";
413
+ element.style.transitionDelay = "0";
414
+ assign(element.style, animation.steps[0].styles);
415
+ setTimeout(() => {
416
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
417
+ if (animation.delay)
418
+ element.style.transitionDelay = animation.delay + "s";
419
+ assign(element.style, animation.steps[1].styles);
420
+ setTimeout(() => {
421
+ element.style.transition = "";
422
+ element.style.transitionDelay = "";
423
+ }, (animation.delay || 0) * 1e3 + animation.duration * 1e3 + 100);
424
+ });
425
+ });
426
+ }
427
+ function bindHoverAnimation(animation) {
428
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
429
+ if (!elements.length) {
430
+ warnElementNotPresent(animation.elementId || animation.id || "");
431
+ return;
432
+ }
433
+ Array.from(elements).forEach((element) => {
434
+ augmentAnimation(animation, element);
435
+ const defaultState = animation.steps[0].styles;
436
+ const hoverState = animation.steps[1].styles;
437
+ function attachDefaultState() {
438
+ assign(element.style, defaultState);
439
+ }
440
+ function attachHoverState() {
441
+ assign(element.style, hoverState);
442
+ }
443
+ attachDefaultState();
444
+ element.addEventListener("mouseenter", attachHoverState);
445
+ element.addEventListener("mouseleave", attachDefaultState);
446
+ setTimeout(() => {
447
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
448
+ if (animation.delay)
449
+ element.style.transitionDelay = animation.delay + "s";
450
+ });
451
+ });
452
+ }
453
+ function bindScrollInViewAnimation(animation) {
454
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
455
+ if (!elements.length) {
456
+ warnElementNotPresent(animation.elementId || animation.id || "");
457
+ return;
458
+ }
459
+ Array.from(elements).forEach((element) => {
460
+ augmentAnimation(animation, element);
461
+ let triggered = false;
462
+ let pendingAnimation = false;
463
+ function immediateOnScroll() {
464
+ if (!triggered && isScrolledIntoView(element)) {
465
+ triggered = true;
466
+ pendingAnimation = true;
467
+ setTimeout(() => {
468
+ assign(element.style, animation.steps[1].styles);
469
+ if (!animation.repeat)
470
+ document.removeEventListener("scroll", onScroll);
471
+ setTimeout(() => {
472
+ pendingAnimation = false;
473
+ if (!animation.repeat) {
474
+ element.style.transition = "";
475
+ element.style.transitionDelay = "";
476
+ }
477
+ }, (animation.duration + (animation.delay || 0)) * 1e3 + 100);
478
+ });
479
+ } else if (animation.repeat && triggered && !pendingAnimation && !isScrolledIntoView(element)) {
480
+ triggered = false;
481
+ assign(element.style, animation.steps[0].styles);
482
+ }
483
+ }
484
+ const onScroll = throttle(immediateOnScroll, 200, {
485
+ leading: false
486
+ });
487
+ function isScrolledIntoView(elem) {
488
+ const rect = elem.getBoundingClientRect();
489
+ const windowHeight = window.innerHeight;
490
+ const thresholdPercent = (animation.thresholdPercent || 0) / 100;
491
+ const threshold = thresholdPercent * windowHeight;
492
+ return rect.bottom > threshold && rect.top < windowHeight - threshold;
493
+ }
494
+ const defaultState = animation.steps[0].styles;
495
+ function attachDefaultState() {
496
+ assign(element.style, defaultState);
497
+ }
498
+ attachDefaultState();
499
+ setTimeout(() => {
500
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
501
+ if (animation.delay)
502
+ element.style.transitionDelay = animation.delay + "s";
503
+ });
504
+ document.addEventListener("scroll", onScroll, {
505
+ capture: true,
506
+ passive: true
507
+ });
508
+ immediateOnScroll();
509
+ });
510
+ }
316
511
  const getComponent = ({ block, context, registeredComponents }) => {
317
512
  var _a;
318
513
  const componentName = (_a = getProcessedBlock({
@@ -859,6 +1054,18 @@ const Block = /* @__PURE__ */ qwik.componentQrl(/* @__PURE__ */ qwik.inlinedQrl(
859
1054
  props,
860
1055
  state
861
1056
  ]));
1057
+ qwik.useVisibleTaskQrl(/* @__PURE__ */ qwik.inlinedQrl(() => {
1058
+ const [processedBlock2] = qwik.useLexicalScope();
1059
+ const blockId = processedBlock2.value.id;
1060
+ const animations = processedBlock2.value.animations;
1061
+ if (animations && blockId)
1062
+ bindAnimations(animations.filter((item) => item.trigger !== "hover").map((animation) => ({
1063
+ ...animation,
1064
+ elementId: blockId
1065
+ })));
1066
+ }, "Block_component_useVisibleTask_hrTYvqw0Tbs", [
1067
+ processedBlock
1068
+ ]));
862
1069
  return /* @__PURE__ */ qwik._jsxC(jsxRuntime.Fragment, {
863
1070
  children: canShowBlock.value ? /* @__PURE__ */ qwik._jsxC(jsxRuntime.Fragment, {
864
1071
  children: [
@@ -3145,17 +3352,6 @@ async function fetchEntries(options) {
3145
3352
  }
3146
3353
  }
3147
3354
  const getAllContent = fetchEntries;
3148
- const DEFAULT_TRUSTED_HOSTS = [
3149
- "*.beta.builder.io",
3150
- "beta.builder.io",
3151
- "builder.io",
3152
- "localhost",
3153
- "qa.builder.io"
3154
- ];
3155
- function isFromTrustedHost(trustedHosts, e) {
3156
- const url = new URL(e.origin), hostname = url.hostname;
3157
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
3158
- }
3159
3355
  function isPreviewing() {
3160
3356
  if (!isBrowser())
3161
3357
  return false;
@@ -3355,7 +3551,18 @@ const getInteractionPropertiesForEvent = (event) => {
3355
3551
  }
3356
3552
  };
3357
3553
  };
3358
- const SDK_VERSION = "0.12.6";
3554
+ const DEFAULT_TRUSTED_HOSTS = [
3555
+ "*.beta.builder.io",
3556
+ "beta.builder.io",
3557
+ "builder.io",
3558
+ "localhost",
3559
+ "qa.builder.io"
3560
+ ];
3561
+ function isFromTrustedHost(trustedHosts, e) {
3562
+ const url = new URL(e.origin), hostname = url.hostname;
3563
+ return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
3564
+ }
3565
+ const SDK_VERSION = "0.12.8";
3359
3566
  const registry = {};
3360
3567
  function register(type, info) {
3361
3568
  let typeList = registry[type];
@@ -3491,6 +3698,53 @@ const setupBrowserForEditing = (options = {}) => {
3491
3698
  });
3492
3699
  }
3493
3700
  };
3701
+ const createEditorListener = ({ model, trustedHosts, callbacks }) => {
3702
+ return (event) => {
3703
+ if (!isFromTrustedHost(trustedHosts, event))
3704
+ return;
3705
+ const { data } = event;
3706
+ if (data)
3707
+ switch (data.type) {
3708
+ case "builder.configureSdk":
3709
+ callbacks.configureSdk(data.data);
3710
+ break;
3711
+ case "builder.triggerAnimation":
3712
+ callbacks.animation(data.data);
3713
+ break;
3714
+ case "builder.contentUpdate": {
3715
+ const messageContent = data.data;
3716
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
3717
+ const contentData = messageContent.data;
3718
+ if (key === model)
3719
+ callbacks.contentUpdate(contentData);
3720
+ break;
3721
+ }
3722
+ }
3723
+ };
3724
+ };
3725
+ const subscribeToEditor = (model, callback, options) => {
3726
+ if (!isBrowser) {
3727
+ logger.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server.");
3728
+ return () => {
3729
+ };
3730
+ }
3731
+ setupBrowserForEditing();
3732
+ const listener = createEditorListener({
3733
+ callbacks: {
3734
+ contentUpdate: callback,
3735
+ animation: () => {
3736
+ },
3737
+ configureSdk: () => {
3738
+ }
3739
+ },
3740
+ model,
3741
+ trustedHosts: options == null ? void 0 : options.trustedHosts
3742
+ });
3743
+ window.addEventListener("message", listener);
3744
+ return () => {
3745
+ window.removeEventListener("message", listener);
3746
+ };
3747
+ };
3494
3748
  const mergeNewRootState = function mergeNewRootState2(props, state, elementRef, newData) {
3495
3749
  var _a, _b;
3496
3750
  const combinedState = {
@@ -3520,37 +3774,33 @@ const mergeNewContent = function mergeNewContent2(props, state, elementRef, newC
3520
3774
  props.builderContextSignal.content = newContentValue;
3521
3775
  };
3522
3776
  const processMessage = function processMessage2(props, state, elementRef, event) {
3523
- var _a;
3524
- if (!isFromTrustedHost(props.trustedHosts, event))
3525
- return;
3526
- const { data } = event;
3527
- if (data)
3528
- switch (data.type) {
3529
- case "builder.configureSdk": {
3530
- const messageContent = data.data;
3777
+ return createEditorListener({
3778
+ model: props.model,
3779
+ trustedHosts: props.trustedHosts,
3780
+ callbacks: {
3781
+ configureSdk: (messageContent) => {
3782
+ var _a;
3531
3783
  const { breakpoints, contentId } = messageContent;
3532
3784
  if (!contentId || contentId !== ((_a = props.builderContextSignal.content) == null ? void 0 : _a.id))
3533
3785
  return;
3534
- if (breakpoints)
3786
+ if (breakpoints) {
3535
3787
  mergeNewContent(props, state, elementRef, {
3536
3788
  meta: {
3537
3789
  breakpoints
3538
3790
  }
3539
3791
  });
3540
- state.forceReRenderCount = state.forceReRenderCount + 1;
3541
- break;
3542
- }
3543
- case "builder.contentUpdate": {
3544
- const messageContent = data.data;
3545
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
3546
- const contentData = messageContent.data;
3547
- if (key === props.model) {
3548
- mergeNewContent(props, state, elementRef, contentData);
3549
3792
  state.forceReRenderCount = state.forceReRenderCount + 1;
3550
3793
  }
3551
- break;
3794
+ },
3795
+ animation: (animation) => {
3796
+ triggerAnimation(animation);
3797
+ },
3798
+ contentUpdate: (newContent) => {
3799
+ mergeNewContent(props, state, elementRef, newContent);
3800
+ state.forceReRenderCount = state.forceReRenderCount + 1;
3552
3801
  }
3553
3802
  }
3803
+ })(event);
3554
3804
  };
3555
3805
  const evaluateJsCode = function evaluateJsCode2(props, state, elementRef) {
3556
3806
  var _a, _b;
@@ -4683,4 +4933,5 @@ exports.isEditing = isEditing;
4683
4933
  exports.isPreviewing = isPreviewing;
4684
4934
  exports.register = register;
4685
4935
  exports.setEditorSettings = setEditorSettings;
4936
+ exports.subscribeToEditor = subscribeToEditor;
4686
4937
  exports.track = track;