@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.
@@ -1,4 +1,4 @@
1
- import { componentQrl, inlinedQrl, _jsxBranch, _jsxC, Slot as Slot$1, _fnSignal, _IMMUTABLE, createContextId, _jsxQ, useComputedQrl, useLexicalScope, useStore, useContextProvider, _wrapProp, useStylesScopedQrl, useContext, Fragment as Fragment$1, _jsxS, useSignal, useOn, useTaskQrl, createElement } from "@builder.io/qwik";
1
+ import { componentQrl, inlinedQrl, _jsxBranch, _jsxC, Slot as Slot$1, _fnSignal, _IMMUTABLE, createContextId, _jsxQ, useComputedQrl, useLexicalScope, useStore, useContextProvider, useVisibleTaskQrl, _wrapProp, useStylesScopedQrl, useContext, Fragment as Fragment$1, _jsxS, useSignal, useOn, useTaskQrl, createElement } from "@builder.io/qwik";
2
2
  import { Fragment } from "@builder.io/qwik/jsx-runtime";
3
3
  import { isBrowser as isBrowser$1 } from "@builder.io/qwik/build";
4
4
  const EMPTY_HTML_ELEMENTS = /* @__PURE__ */ new Set([
@@ -3542,6 +3542,201 @@ function getProcessedBlock({ block, context, shouldEvaluateBindings, localState,
3542
3542
  else
3543
3543
  return transformedBlock;
3544
3544
  }
3545
+ function throttle(func, wait, options = {}) {
3546
+ let context;
3547
+ let args;
3548
+ let result;
3549
+ let timeout = null;
3550
+ let previous = 0;
3551
+ const later = function() {
3552
+ previous = options.leading === false ? 0 : Date.now();
3553
+ timeout = null;
3554
+ result = func.apply(context, args);
3555
+ if (!timeout)
3556
+ context = args = null;
3557
+ };
3558
+ return function() {
3559
+ const now = Date.now();
3560
+ if (!previous && options.leading === false)
3561
+ previous = now;
3562
+ const remaining = wait - (now - previous);
3563
+ context = this;
3564
+ args = arguments;
3565
+ if (remaining <= 0 || remaining > wait) {
3566
+ if (timeout) {
3567
+ clearTimeout(timeout);
3568
+ timeout = null;
3569
+ }
3570
+ previous = now;
3571
+ result = func.apply(context, args);
3572
+ if (!timeout)
3573
+ context = args = null;
3574
+ } else if (!timeout && options.trailing !== false)
3575
+ timeout = setTimeout(later, remaining);
3576
+ return result;
3577
+ };
3578
+ }
3579
+ function assign(target, ..._args) {
3580
+ const to = Object(target);
3581
+ for (let index = 1; index < arguments.length; index++) {
3582
+ const nextSource = arguments[index];
3583
+ if (nextSource != null) {
3584
+ for (const nextKey in nextSource)
3585
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey))
3586
+ to[nextKey] = nextSource[nextKey];
3587
+ }
3588
+ }
3589
+ return to;
3590
+ }
3591
+ const camelCaseToKebabCase = (str) => str ? str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`) : "";
3592
+ function bindAnimations(animations) {
3593
+ for (const animation of animations)
3594
+ switch (animation.trigger) {
3595
+ case "pageLoad":
3596
+ triggerAnimation(animation);
3597
+ break;
3598
+ case "hover":
3599
+ bindHoverAnimation(animation);
3600
+ break;
3601
+ case "scrollInView":
3602
+ bindScrollInViewAnimation(animation);
3603
+ break;
3604
+ }
3605
+ }
3606
+ function warnElementNotPresent(id2) {
3607
+ console.warn(`Cannot animate element: element with ID ${id2} not found!`);
3608
+ }
3609
+ function augmentAnimation(animation, element) {
3610
+ const stylesUsed = getAllStylesUsed(animation);
3611
+ const computedStyle = getComputedStyle(element);
3612
+ const firstStyles = animation.steps[0].styles;
3613
+ const lastStyles = animation.steps[animation.steps.length - 1].styles;
3614
+ const bothStyles = [
3615
+ firstStyles,
3616
+ lastStyles
3617
+ ];
3618
+ for (const styles of bothStyles) {
3619
+ for (const style of stylesUsed)
3620
+ if (!(style in styles))
3621
+ styles[style] = computedStyle[style];
3622
+ }
3623
+ }
3624
+ function getAllStylesUsed(animation) {
3625
+ const properties = [];
3626
+ for (const step of animation.steps) {
3627
+ for (const key in step.styles)
3628
+ if (properties.indexOf(key) === -1)
3629
+ properties.push(key);
3630
+ }
3631
+ return properties;
3632
+ }
3633
+ function triggerAnimation(animation) {
3634
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3635
+ if (!elements.length) {
3636
+ warnElementNotPresent(animation.elementId || animation.id || "");
3637
+ return;
3638
+ }
3639
+ Array.from(elements).forEach((element) => {
3640
+ augmentAnimation(animation, element);
3641
+ element.style.transition = "none";
3642
+ element.style.transitionDelay = "0";
3643
+ assign(element.style, animation.steps[0].styles);
3644
+ setTimeout(() => {
3645
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3646
+ if (animation.delay)
3647
+ element.style.transitionDelay = animation.delay + "s";
3648
+ assign(element.style, animation.steps[1].styles);
3649
+ setTimeout(() => {
3650
+ element.style.transition = "";
3651
+ element.style.transitionDelay = "";
3652
+ }, (animation.delay || 0) * 1e3 + animation.duration * 1e3 + 100);
3653
+ });
3654
+ });
3655
+ }
3656
+ function bindHoverAnimation(animation) {
3657
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3658
+ if (!elements.length) {
3659
+ warnElementNotPresent(animation.elementId || animation.id || "");
3660
+ return;
3661
+ }
3662
+ Array.from(elements).forEach((element) => {
3663
+ augmentAnimation(animation, element);
3664
+ const defaultState = animation.steps[0].styles;
3665
+ const hoverState = animation.steps[1].styles;
3666
+ function attachDefaultState() {
3667
+ assign(element.style, defaultState);
3668
+ }
3669
+ function attachHoverState() {
3670
+ assign(element.style, hoverState);
3671
+ }
3672
+ attachDefaultState();
3673
+ element.addEventListener("mouseenter", attachHoverState);
3674
+ element.addEventListener("mouseleave", attachDefaultState);
3675
+ setTimeout(() => {
3676
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3677
+ if (animation.delay)
3678
+ element.style.transitionDelay = animation.delay + "s";
3679
+ });
3680
+ });
3681
+ }
3682
+ function bindScrollInViewAnimation(animation) {
3683
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3684
+ if (!elements.length) {
3685
+ warnElementNotPresent(animation.elementId || animation.id || "");
3686
+ return;
3687
+ }
3688
+ Array.from(elements).forEach((element) => {
3689
+ augmentAnimation(animation, element);
3690
+ let triggered = false;
3691
+ let pendingAnimation = false;
3692
+ function immediateOnScroll() {
3693
+ if (!triggered && isScrolledIntoView(element)) {
3694
+ triggered = true;
3695
+ pendingAnimation = true;
3696
+ setTimeout(() => {
3697
+ assign(element.style, animation.steps[1].styles);
3698
+ if (!animation.repeat)
3699
+ document.removeEventListener("scroll", onScroll);
3700
+ setTimeout(() => {
3701
+ pendingAnimation = false;
3702
+ if (!animation.repeat) {
3703
+ element.style.transition = "";
3704
+ element.style.transitionDelay = "";
3705
+ }
3706
+ }, (animation.duration + (animation.delay || 0)) * 1e3 + 100);
3707
+ });
3708
+ } else if (animation.repeat && triggered && !pendingAnimation && !isScrolledIntoView(element)) {
3709
+ triggered = false;
3710
+ assign(element.style, animation.steps[0].styles);
3711
+ }
3712
+ }
3713
+ const onScroll = throttle(immediateOnScroll, 200, {
3714
+ leading: false
3715
+ });
3716
+ function isScrolledIntoView(elem) {
3717
+ const rect = elem.getBoundingClientRect();
3718
+ const windowHeight = window.innerHeight;
3719
+ const thresholdPercent = (animation.thresholdPercent || 0) / 100;
3720
+ const threshold = thresholdPercent * windowHeight;
3721
+ return rect.bottom > threshold && rect.top < windowHeight - threshold;
3722
+ }
3723
+ const defaultState = animation.steps[0].styles;
3724
+ function attachDefaultState() {
3725
+ assign(element.style, defaultState);
3726
+ }
3727
+ attachDefaultState();
3728
+ setTimeout(() => {
3729
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3730
+ if (animation.delay)
3731
+ element.style.transitionDelay = animation.delay + "s";
3732
+ });
3733
+ document.addEventListener("scroll", onScroll, {
3734
+ capture: true,
3735
+ passive: true
3736
+ });
3737
+ immediateOnScroll();
3738
+ });
3739
+ }
3545
3740
  const getComponent = ({ block, context, registeredComponents }) => {
3546
3741
  var _a;
3547
3742
  const componentName = (_a = getProcessedBlock({
@@ -4088,6 +4283,18 @@ const Block = /* @__PURE__ */ componentQrl(/* @__PURE__ */ inlinedQrl((props) =>
4088
4283
  props,
4089
4284
  state
4090
4285
  ]));
4286
+ useVisibleTaskQrl(/* @__PURE__ */ inlinedQrl(() => {
4287
+ const [processedBlock2] = useLexicalScope();
4288
+ const blockId = processedBlock2.value.id;
4289
+ const animations = processedBlock2.value.animations;
4290
+ if (animations && blockId)
4291
+ bindAnimations(animations.filter((item) => item.trigger !== "hover").map((animation) => ({
4292
+ ...animation,
4293
+ elementId: blockId
4294
+ })));
4295
+ }, "Block_component_useVisibleTask_hrTYvqw0Tbs", [
4296
+ processedBlock
4297
+ ]));
4091
4298
  return /* @__PURE__ */ _jsxC(Fragment, {
4092
4299
  children: canShowBlock.value ? /* @__PURE__ */ _jsxC(Fragment, {
4093
4300
  children: [
@@ -6374,17 +6581,6 @@ async function fetchEntries(options) {
6374
6581
  }
6375
6582
  }
6376
6583
  const getAllContent = fetchEntries;
6377
- const DEFAULT_TRUSTED_HOSTS = [
6378
- "*.beta.builder.io",
6379
- "beta.builder.io",
6380
- "builder.io",
6381
- "localhost",
6382
- "qa.builder.io"
6383
- ];
6384
- function isFromTrustedHost(trustedHosts, e) {
6385
- const url = new URL(e.origin), hostname = url.hostname;
6386
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
6387
- }
6388
6584
  function isPreviewing() {
6389
6585
  if (!isBrowser())
6390
6586
  return false;
@@ -6584,7 +6780,18 @@ const getInteractionPropertiesForEvent = (event) => {
6584
6780
  }
6585
6781
  };
6586
6782
  };
6587
- const SDK_VERSION = "0.12.6";
6783
+ const DEFAULT_TRUSTED_HOSTS = [
6784
+ "*.beta.builder.io",
6785
+ "beta.builder.io",
6786
+ "builder.io",
6787
+ "localhost",
6788
+ "qa.builder.io"
6789
+ ];
6790
+ function isFromTrustedHost(trustedHosts, e) {
6791
+ const url = new URL(e.origin), hostname = url.hostname;
6792
+ return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
6793
+ }
6794
+ const SDK_VERSION = "0.12.8";
6588
6795
  const registry = {};
6589
6796
  function register(type, info) {
6590
6797
  let typeList = registry[type];
@@ -6720,6 +6927,53 @@ const setupBrowserForEditing = (options = {}) => {
6720
6927
  });
6721
6928
  }
6722
6929
  };
6930
+ const createEditorListener = ({ model, trustedHosts, callbacks }) => {
6931
+ return (event) => {
6932
+ if (!isFromTrustedHost(trustedHosts, event))
6933
+ return;
6934
+ const { data } = event;
6935
+ if (data)
6936
+ switch (data.type) {
6937
+ case "builder.configureSdk":
6938
+ callbacks.configureSdk(data.data);
6939
+ break;
6940
+ case "builder.triggerAnimation":
6941
+ callbacks.animation(data.data);
6942
+ break;
6943
+ case "builder.contentUpdate": {
6944
+ const messageContent = data.data;
6945
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6946
+ const contentData = messageContent.data;
6947
+ if (key === model)
6948
+ callbacks.contentUpdate(contentData);
6949
+ break;
6950
+ }
6951
+ }
6952
+ };
6953
+ };
6954
+ const subscribeToEditor = (model, callback, options) => {
6955
+ if (!isBrowser) {
6956
+ logger.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server.");
6957
+ return () => {
6958
+ };
6959
+ }
6960
+ setupBrowserForEditing();
6961
+ const listener = createEditorListener({
6962
+ callbacks: {
6963
+ contentUpdate: callback,
6964
+ animation: () => {
6965
+ },
6966
+ configureSdk: () => {
6967
+ }
6968
+ },
6969
+ model,
6970
+ trustedHosts: options == null ? void 0 : options.trustedHosts
6971
+ });
6972
+ window.addEventListener("message", listener);
6973
+ return () => {
6974
+ window.removeEventListener("message", listener);
6975
+ };
6976
+ };
6723
6977
  const mergeNewRootState = function mergeNewRootState2(props, state, elementRef, newData) {
6724
6978
  var _a, _b;
6725
6979
  const combinedState = {
@@ -6749,37 +7003,33 @@ const mergeNewContent = function mergeNewContent2(props, state, elementRef, newC
6749
7003
  props.builderContextSignal.content = newContentValue;
6750
7004
  };
6751
7005
  const processMessage = function processMessage2(props, state, elementRef, event) {
6752
- var _a;
6753
- if (!isFromTrustedHost(props.trustedHosts, event))
6754
- return;
6755
- const { data } = event;
6756
- if (data)
6757
- switch (data.type) {
6758
- case "builder.configureSdk": {
6759
- const messageContent = data.data;
7006
+ return createEditorListener({
7007
+ model: props.model,
7008
+ trustedHosts: props.trustedHosts,
7009
+ callbacks: {
7010
+ configureSdk: (messageContent) => {
7011
+ var _a;
6760
7012
  const { breakpoints, contentId } = messageContent;
6761
7013
  if (!contentId || contentId !== ((_a = props.builderContextSignal.content) == null ? void 0 : _a.id))
6762
7014
  return;
6763
- if (breakpoints)
7015
+ if (breakpoints) {
6764
7016
  mergeNewContent(props, state, elementRef, {
6765
7017
  meta: {
6766
7018
  breakpoints
6767
7019
  }
6768
7020
  });
6769
- state.forceReRenderCount = state.forceReRenderCount + 1;
6770
- break;
6771
- }
6772
- case "builder.contentUpdate": {
6773
- const messageContent = data.data;
6774
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6775
- const contentData = messageContent.data;
6776
- if (key === props.model) {
6777
- mergeNewContent(props, state, elementRef, contentData);
6778
7021
  state.forceReRenderCount = state.forceReRenderCount + 1;
6779
7022
  }
6780
- break;
7023
+ },
7024
+ animation: (animation) => {
7025
+ triggerAnimation(animation);
7026
+ },
7027
+ contentUpdate: (newContent) => {
7028
+ mergeNewContent(props, state, elementRef, newContent);
7029
+ state.forceReRenderCount = state.forceReRenderCount + 1;
6781
7030
  }
6782
7031
  }
7032
+ })(event);
6783
7033
  };
6784
7034
  const evaluateJsCode = function evaluateJsCode2(props, state, elementRef) {
6785
7035
  var _a, _b;
@@ -7913,5 +8163,6 @@ export {
7913
8163
  isPreviewing,
7914
8164
  register,
7915
8165
  setEditorSettings,
8166
+ subscribeToEditor,
7916
8167
  track
7917
8168
  };