@builder.io/sdk-solid 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.
@@ -107,7 +107,7 @@ import { createContext as createContext2 } from "solid-js";
107
107
  var components_context_default = createContext2({ registeredComponents: {} });
108
108
 
109
109
  // src/components/block/block.tsx
110
- import { Show as Show4, For as For2, createSignal as createSignal4 } from "solid-js";
110
+ import { Show as Show4, For as For2, onMount, createSignal as createSignal4 } from "solid-js";
111
111
 
112
112
  // src/functions/get-block-component-options.ts
113
113
  function getBlockComponentOptions(block) {
@@ -3563,6 +3563,211 @@ function getProcessedBlock({
3563
3563
  }
3564
3564
  }
3565
3565
 
3566
+ // src/components/block/animator.ts
3567
+ function throttle(func, wait, options = {}) {
3568
+ let context;
3569
+ let args;
3570
+ let result;
3571
+ let timeout = null;
3572
+ let previous = 0;
3573
+ const later = function() {
3574
+ previous = options.leading === false ? 0 : Date.now();
3575
+ timeout = null;
3576
+ result = func.apply(context, args);
3577
+ if (!timeout)
3578
+ context = args = null;
3579
+ };
3580
+ return function() {
3581
+ const now = Date.now();
3582
+ if (!previous && options.leading === false)
3583
+ previous = now;
3584
+ const remaining = wait - (now - previous);
3585
+ context = this;
3586
+ args = arguments;
3587
+ if (remaining <= 0 || remaining > wait) {
3588
+ if (timeout) {
3589
+ clearTimeout(timeout);
3590
+ timeout = null;
3591
+ }
3592
+ previous = now;
3593
+ result = func.apply(context, args);
3594
+ if (!timeout)
3595
+ context = args = null;
3596
+ } else if (!timeout && options.trailing !== false) {
3597
+ timeout = setTimeout(later, remaining);
3598
+ }
3599
+ return result;
3600
+ };
3601
+ }
3602
+ function assign(target, ..._args) {
3603
+ const to = Object(target);
3604
+ for (let index = 1; index < arguments.length; index++) {
3605
+ const nextSource = arguments[index];
3606
+ if (nextSource != null) {
3607
+ for (const nextKey in nextSource) {
3608
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
3609
+ to[nextKey] = nextSource[nextKey];
3610
+ }
3611
+ }
3612
+ }
3613
+ }
3614
+ return to;
3615
+ }
3616
+ var camelCaseToKebabCase = (str) => str ? str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`) : "";
3617
+ function bindAnimations(animations) {
3618
+ for (const animation of animations) {
3619
+ switch (animation.trigger) {
3620
+ case "pageLoad":
3621
+ triggerAnimation(animation);
3622
+ break;
3623
+ case "hover":
3624
+ bindHoverAnimation(animation);
3625
+ break;
3626
+ case "scrollInView":
3627
+ bindScrollInViewAnimation(animation);
3628
+ break;
3629
+ }
3630
+ }
3631
+ }
3632
+ function warnElementNotPresent(id2) {
3633
+ }
3634
+ function augmentAnimation(animation, element) {
3635
+ const stylesUsed = getAllStylesUsed(animation);
3636
+ const computedStyle = getComputedStyle(element);
3637
+ const firstStyles = animation.steps[0].styles;
3638
+ const lastStyles = animation.steps[animation.steps.length - 1].styles;
3639
+ const bothStyles = [firstStyles, lastStyles];
3640
+ for (const styles of bothStyles) {
3641
+ for (const style of stylesUsed) {
3642
+ if (!(style in styles)) {
3643
+ styles[style] = computedStyle[style];
3644
+ }
3645
+ }
3646
+ }
3647
+ }
3648
+ function getAllStylesUsed(animation) {
3649
+ const properties = [];
3650
+ for (const step of animation.steps) {
3651
+ for (const key in step.styles) {
3652
+ if (properties.indexOf(key) === -1) {
3653
+ properties.push(key);
3654
+ }
3655
+ }
3656
+ }
3657
+ return properties;
3658
+ }
3659
+ function triggerAnimation(animation) {
3660
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3661
+ if (!elements.length) {
3662
+ warnElementNotPresent(animation.elementId || animation.id || "");
3663
+ return;
3664
+ }
3665
+ Array.from(elements).forEach((element) => {
3666
+ augmentAnimation(animation, element);
3667
+ element.style.transition = "none";
3668
+ element.style.transitionDelay = "0";
3669
+ assign(element.style, animation.steps[0].styles);
3670
+ setTimeout(() => {
3671
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3672
+ if (animation.delay) {
3673
+ element.style.transitionDelay = animation.delay + "s";
3674
+ }
3675
+ assign(element.style, animation.steps[1].styles);
3676
+ setTimeout(() => {
3677
+ element.style.transition = "";
3678
+ element.style.transitionDelay = "";
3679
+ }, (animation.delay || 0) * 1e3 + animation.duration * 1e3 + 100);
3680
+ });
3681
+ });
3682
+ }
3683
+ function bindHoverAnimation(animation) {
3684
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3685
+ if (!elements.length) {
3686
+ warnElementNotPresent(animation.elementId || animation.id || "");
3687
+ return;
3688
+ }
3689
+ Array.from(elements).forEach((element) => {
3690
+ augmentAnimation(animation, element);
3691
+ const defaultState = animation.steps[0].styles;
3692
+ const hoverState = animation.steps[1].styles;
3693
+ function attachDefaultState() {
3694
+ assign(element.style, defaultState);
3695
+ }
3696
+ function attachHoverState() {
3697
+ assign(element.style, hoverState);
3698
+ }
3699
+ attachDefaultState();
3700
+ element.addEventListener("mouseenter", attachHoverState);
3701
+ element.addEventListener("mouseleave", attachDefaultState);
3702
+ setTimeout(() => {
3703
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3704
+ if (animation.delay) {
3705
+ element.style.transitionDelay = animation.delay + "s";
3706
+ }
3707
+ });
3708
+ });
3709
+ }
3710
+ function bindScrollInViewAnimation(animation) {
3711
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
3712
+ if (!elements.length) {
3713
+ warnElementNotPresent(animation.elementId || animation.id || "");
3714
+ return;
3715
+ }
3716
+ Array.from(elements).forEach((element) => {
3717
+ augmentAnimation(animation, element);
3718
+ let triggered = false;
3719
+ let pendingAnimation = false;
3720
+ function immediateOnScroll() {
3721
+ if (!triggered && isScrolledIntoView(element)) {
3722
+ triggered = true;
3723
+ pendingAnimation = true;
3724
+ setTimeout(() => {
3725
+ assign(element.style, animation.steps[1].styles);
3726
+ if (!animation.repeat) {
3727
+ document.removeEventListener("scroll", onScroll);
3728
+ }
3729
+ setTimeout(() => {
3730
+ pendingAnimation = false;
3731
+ if (!animation.repeat) {
3732
+ element.style.transition = "";
3733
+ element.style.transitionDelay = "";
3734
+ }
3735
+ }, (animation.duration + (animation.delay || 0)) * 1e3 + 100);
3736
+ });
3737
+ } else if (animation.repeat && triggered && !pendingAnimation && !isScrolledIntoView(element)) {
3738
+ triggered = false;
3739
+ assign(element.style, animation.steps[0].styles);
3740
+ }
3741
+ }
3742
+ const onScroll = throttle(immediateOnScroll, 200, {
3743
+ leading: false
3744
+ });
3745
+ function isScrolledIntoView(elem) {
3746
+ const rect = elem.getBoundingClientRect();
3747
+ const windowHeight = window.innerHeight;
3748
+ const thresholdPercent = (animation.thresholdPercent || 0) / 100;
3749
+ const threshold = thresholdPercent * windowHeight;
3750
+ return rect.bottom > threshold && rect.top < windowHeight - threshold;
3751
+ }
3752
+ const defaultState = animation.steps[0].styles;
3753
+ function attachDefaultState() {
3754
+ assign(element.style, defaultState);
3755
+ }
3756
+ attachDefaultState();
3757
+ setTimeout(() => {
3758
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
3759
+ if (animation.delay) {
3760
+ element.style.transitionDelay = animation.delay + "s";
3761
+ }
3762
+ });
3763
+ document.addEventListener("scroll", onScroll, {
3764
+ capture: true,
3765
+ passive: true
3766
+ });
3767
+ immediateOnScroll();
3768
+ });
3769
+ }
3770
+
3566
3771
  // src/components/block/block.helpers.ts
3567
3772
  var getComponent = ({
3568
3773
  block,
@@ -4082,6 +4287,18 @@ function Block(props) {
4082
4287
  isInteractive: !blockComponent()?.isRSC
4083
4288
  };
4084
4289
  }
4290
+ onMount(() => {
4291
+ const blockId = processedBlock().id;
4292
+ const animations = processedBlock().animations;
4293
+ if (animations && blockId) {
4294
+ bindAnimations(
4295
+ animations.filter((item) => item.trigger !== "hover").map((animation) => ({
4296
+ ...animation,
4297
+ elementId: blockId
4298
+ }))
4299
+ );
4300
+ }
4301
+ });
4085
4302
  return <Show4 when={canShowBlock()}>
4086
4303
  <Block_styles_default block={props.block} context={props.context} />
4087
4304
  <Show4
@@ -4529,10 +4746,10 @@ function SectionComponent(props) {
4529
4746
  var section_default = SectionComponent;
4530
4747
 
4531
4748
  // src/blocks/symbol/symbol.tsx
4532
- import { onMount as onMount4, on as on3, createEffect as createEffect3, createSignal as createSignal14 } from "solid-js";
4749
+ import { onMount as onMount5, on as on3, createEffect as createEffect3, createSignal as createSignal14 } from "solid-js";
4533
4750
 
4534
4751
  // src/components/content-variants/content-variants.tsx
4535
- import { Show as Show11, For as For5, onMount as onMount3, createSignal as createSignal13 } from "solid-js";
4752
+ import { Show as Show11, For as For5, onMount as onMount4, createSignal as createSignal13 } from "solid-js";
4536
4753
 
4537
4754
  // src/helpers/url.ts
4538
4755
  var getTopLevelDomain = (host) => {
@@ -5010,12 +5227,12 @@ var componentInfo3 = {
5010
5227
  };
5011
5228
 
5012
5229
  // src/blocks/custom-code/custom-code.tsx
5013
- import { onMount, createSignal as createSignal7 } from "solid-js";
5230
+ import { onMount as onMount2, createSignal as createSignal7 } from "solid-js";
5014
5231
  function CustomCode(props) {
5015
5232
  const [scriptsInserted, setScriptsInserted] = createSignal7([]);
5016
5233
  const [scriptsRun, setScriptsRun] = createSignal7([]);
5017
5234
  let elementRef;
5018
- onMount(() => {
5235
+ onMount2(() => {
5019
5236
  if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
5020
5237
  return;
5021
5238
  }
@@ -5733,7 +5950,7 @@ function InlinedScript(props) {
5733
5950
  var Inlined_script_default = InlinedScript;
5734
5951
 
5735
5952
  // src/components/content/components/enable-editor.tsx
5736
- import { Show as Show9, onMount as onMount2, on as on2, createEffect as createEffect2, createSignal as createSignal10 } from "solid-js";
5953
+ import { Show as Show9, onMount as onMount3, on as on2, createEffect as createEffect2, createSignal as createSignal10 } from "solid-js";
5737
5954
  import { Dynamic as Dynamic5 } from "solid-js/web";
5738
5955
 
5739
5956
  // src/helpers/preview-lru-cache/get.ts
@@ -5968,13 +6185,6 @@ async function fetchEntries(options) {
5968
6185
  }
5969
6186
  var getAllContent = fetchEntries;
5970
6187
 
5971
- // src/functions/is-from-trusted-host.ts
5972
- var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
5973
- function isFromTrustedHost(trustedHosts, e) {
5974
- const url = new URL(e.origin), hostname = url.hostname;
5975
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
5976
- }
5977
-
5978
6188
  // src/functions/is-previewing.ts
5979
6189
  function isPreviewing() {
5980
6190
  if (!isBrowser()) {
@@ -6222,8 +6432,15 @@ var getInteractionPropertiesForEvent = (event) => {
6222
6432
  };
6223
6433
  };
6224
6434
 
6435
+ // src/functions/is-from-trusted-host.ts
6436
+ var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
6437
+ function isFromTrustedHost(trustedHosts, e) {
6438
+ const url = new URL(e.origin), hostname = url.hostname;
6439
+ return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
6440
+ }
6441
+
6225
6442
  // src/constants/sdk-version.ts
6226
- var SDK_VERSION = "0.12.6";
6443
+ var SDK_VERSION = "0.12.8";
6227
6444
 
6228
6445
  // src/functions/register.ts
6229
6446
  var registry = {};
@@ -6359,6 +6576,66 @@ var setupBrowserForEditing = (options = {}) => {
6359
6576
  }
6360
6577
  };
6361
6578
 
6579
+ // src/helpers/subscribe-to-editor.ts
6580
+ var createEditorListener = ({
6581
+ model,
6582
+ trustedHosts,
6583
+ callbacks
6584
+ }) => {
6585
+ return (event) => {
6586
+ if (!isFromTrustedHost(trustedHosts, event)) {
6587
+ return;
6588
+ }
6589
+ const {
6590
+ data
6591
+ } = event;
6592
+ if (data) {
6593
+ switch (data.type) {
6594
+ case "builder.configureSdk": {
6595
+ callbacks.configureSdk(data.data);
6596
+ break;
6597
+ }
6598
+ case "builder.triggerAnimation": {
6599
+ callbacks.animation(data.data);
6600
+ break;
6601
+ }
6602
+ case "builder.contentUpdate": {
6603
+ const messageContent = data.data;
6604
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6605
+ const contentData = messageContent.data;
6606
+ if (key === model) {
6607
+ callbacks.contentUpdate(contentData);
6608
+ }
6609
+ break;
6610
+ }
6611
+ }
6612
+ }
6613
+ };
6614
+ };
6615
+ var subscribeToEditor = (model, callback, options) => {
6616
+ if (!isBrowser) {
6617
+ logger.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server.");
6618
+ return () => {
6619
+ };
6620
+ }
6621
+ setupBrowserForEditing();
6622
+ const listener = createEditorListener({
6623
+ callbacks: {
6624
+ contentUpdate: callback,
6625
+ animation: () => {
6626
+ },
6627
+ configureSdk: () => {
6628
+ }
6629
+ },
6630
+ model,
6631
+ trustedHosts: options?.trustedHosts
6632
+ });
6633
+ window.addEventListener("message", listener);
6634
+ return () => {
6635
+ window.removeEventListener("message", listener);
6636
+ };
6637
+ };
6638
+
6362
6639
  // src/components/content/components/enable-editor.tsx
6363
6640
  function EnableEditor(props) {
6364
6641
  const [forceReRenderCount, setForceReRenderCount] = createSignal10(0);
@@ -6404,14 +6681,11 @@ function EnableEditor(props) {
6404
6681
  }));
6405
6682
  }
6406
6683
  function processMessage(event) {
6407
- if (!isFromTrustedHost(props.trustedHosts, event)) {
6408
- return;
6409
- }
6410
- const { data } = event;
6411
- if (data) {
6412
- switch (data.type) {
6413
- case "builder.configureSdk": {
6414
- const messageContent = data.data;
6684
+ return createEditorListener({
6685
+ model: props.model,
6686
+ trustedHosts: props.trustedHosts,
6687
+ callbacks: {
6688
+ configureSdk: (messageContent) => {
6415
6689
  const { breakpoints, contentId } = messageContent;
6416
6690
  if (!contentId || contentId !== props.builderContextSignal.content?.id) {
6417
6691
  return;
@@ -6422,22 +6696,18 @@ function EnableEditor(props) {
6422
6696
  breakpoints
6423
6697
  }
6424
6698
  });
6425
- }
6426
- setForceReRenderCount(forceReRenderCount() + 1);
6427
- break;
6428
- }
6429
- case "builder.contentUpdate": {
6430
- const messageContent = data.data;
6431
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6432
- const contentData = messageContent.data;
6433
- if (key === props.model) {
6434
- mergeNewContent(contentData);
6435
6699
  setForceReRenderCount(forceReRenderCount() + 1);
6436
6700
  }
6437
- break;
6701
+ },
6702
+ animation: (animation) => {
6703
+ triggerAnimation(animation);
6704
+ },
6705
+ contentUpdate: (newContent) => {
6706
+ mergeNewContent(newContent);
6707
+ setForceReRenderCount(forceReRenderCount() + 1);
6438
6708
  }
6439
6709
  }
6440
- }
6710
+ })(event);
6441
6711
  }
6442
6712
  function evaluateJsCode() {
6443
6713
  const jsCode = props.builderContextSignal.content?.data?.jsCode;
@@ -6522,7 +6792,7 @@ function EnableEditor(props) {
6522
6792
  }
6523
6793
  }
6524
6794
  let elementRef;
6525
- onMount2(() => {
6795
+ onMount3(() => {
6526
6796
  if (isBrowser()) {
6527
6797
  if (isEditing() && true) {
6528
6798
  setForceReRenderCount(forceReRenderCount() + 1);
@@ -6587,7 +6857,7 @@ function EnableEditor(props) {
6587
6857
  }
6588
6858
  }
6589
6859
  });
6590
- onMount2(() => {
6860
+ onMount3(() => {
6591
6861
  if (!props.apiKey) {
6592
6862
  logger.error(
6593
6863
  "No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
@@ -6927,7 +7197,7 @@ function ContentVariants(props) {
6927
7197
  canTrack: getDefaultCanTrack(props.canTrack)
6928
7198
  });
6929
7199
  }
6930
- onMount3(() => {
7200
+ onMount4(() => {
6931
7201
  setShouldRenderVariants(false);
6932
7202
  });
6933
7203
  return <>
@@ -7039,7 +7309,7 @@ function Symbol2(props) {
7039
7309
  }
7040
7310
  });
7041
7311
  }
7042
- onMount4(() => {
7312
+ onMount5(() => {
7043
7313
  setContent();
7044
7314
  });
7045
7315
  function onUpdateFn_0() {
@@ -7130,5 +7400,6 @@ export {
7130
7400
  isPreviewing,
7131
7401
  register,
7132
7402
  setEditorSettings,
7403
+ subscribeToEditor,
7133
7404
  track
7134
7405
  };