@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.
@@ -99,7 +99,7 @@ import { createContext as createContext2 } from "solid-js";
99
99
  var components_context_default = createContext2({ registeredComponents: {} });
100
100
 
101
101
  // src/components/block/block.tsx
102
- import { Show as Show4, For as For2, createSignal as createSignal4 } from "solid-js";
102
+ import { Show as Show4, For as For2, onMount, createSignal as createSignal4 } from "solid-js";
103
103
 
104
104
  // src/functions/get-block-component-options.ts
105
105
  function getBlockComponentOptions(block) {
@@ -533,6 +533,211 @@ function getProcessedBlock({
533
533
  }
534
534
  }
535
535
 
536
+ // src/components/block/animator.ts
537
+ function throttle(func, wait, options = {}) {
538
+ let context;
539
+ let args;
540
+ let result;
541
+ let timeout = null;
542
+ let previous = 0;
543
+ const later = function() {
544
+ previous = options.leading === false ? 0 : Date.now();
545
+ timeout = null;
546
+ result = func.apply(context, args);
547
+ if (!timeout)
548
+ context = args = null;
549
+ };
550
+ return function() {
551
+ const now = Date.now();
552
+ if (!previous && options.leading === false)
553
+ previous = now;
554
+ const remaining = wait - (now - previous);
555
+ context = this;
556
+ args = arguments;
557
+ if (remaining <= 0 || remaining > wait) {
558
+ if (timeout) {
559
+ clearTimeout(timeout);
560
+ timeout = null;
561
+ }
562
+ previous = now;
563
+ result = func.apply(context, args);
564
+ if (!timeout)
565
+ context = args = null;
566
+ } else if (!timeout && options.trailing !== false) {
567
+ timeout = setTimeout(later, remaining);
568
+ }
569
+ return result;
570
+ };
571
+ }
572
+ function assign(target, ..._args) {
573
+ const to = Object(target);
574
+ for (let index = 1; index < arguments.length; index++) {
575
+ const nextSource = arguments[index];
576
+ if (nextSource != null) {
577
+ for (const nextKey in nextSource) {
578
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
579
+ to[nextKey] = nextSource[nextKey];
580
+ }
581
+ }
582
+ }
583
+ }
584
+ return to;
585
+ }
586
+ var camelCaseToKebabCase = (str) => str ? str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`) : "";
587
+ function bindAnimations(animations) {
588
+ for (const animation of animations) {
589
+ switch (animation.trigger) {
590
+ case "pageLoad":
591
+ triggerAnimation(animation);
592
+ break;
593
+ case "hover":
594
+ bindHoverAnimation(animation);
595
+ break;
596
+ case "scrollInView":
597
+ bindScrollInViewAnimation(animation);
598
+ break;
599
+ }
600
+ }
601
+ }
602
+ function warnElementNotPresent(id) {
603
+ }
604
+ function augmentAnimation(animation, element) {
605
+ const stylesUsed = getAllStylesUsed(animation);
606
+ const computedStyle = getComputedStyle(element);
607
+ const firstStyles = animation.steps[0].styles;
608
+ const lastStyles = animation.steps[animation.steps.length - 1].styles;
609
+ const bothStyles = [firstStyles, lastStyles];
610
+ for (const styles of bothStyles) {
611
+ for (const style of stylesUsed) {
612
+ if (!(style in styles)) {
613
+ styles[style] = computedStyle[style];
614
+ }
615
+ }
616
+ }
617
+ }
618
+ function getAllStylesUsed(animation) {
619
+ const properties = [];
620
+ for (const step of animation.steps) {
621
+ for (const key in step.styles) {
622
+ if (properties.indexOf(key) === -1) {
623
+ properties.push(key);
624
+ }
625
+ }
626
+ }
627
+ return properties;
628
+ }
629
+ function triggerAnimation(animation) {
630
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
631
+ if (!elements.length) {
632
+ warnElementNotPresent(animation.elementId || animation.id || "");
633
+ return;
634
+ }
635
+ Array.from(elements).forEach((element) => {
636
+ augmentAnimation(animation, element);
637
+ element.style.transition = "none";
638
+ element.style.transitionDelay = "0";
639
+ assign(element.style, animation.steps[0].styles);
640
+ setTimeout(() => {
641
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
642
+ if (animation.delay) {
643
+ element.style.transitionDelay = animation.delay + "s";
644
+ }
645
+ assign(element.style, animation.steps[1].styles);
646
+ setTimeout(() => {
647
+ element.style.transition = "";
648
+ element.style.transitionDelay = "";
649
+ }, (animation.delay || 0) * 1e3 + animation.duration * 1e3 + 100);
650
+ });
651
+ });
652
+ }
653
+ function bindHoverAnimation(animation) {
654
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
655
+ if (!elements.length) {
656
+ warnElementNotPresent(animation.elementId || animation.id || "");
657
+ return;
658
+ }
659
+ Array.from(elements).forEach((element) => {
660
+ augmentAnimation(animation, element);
661
+ const defaultState = animation.steps[0].styles;
662
+ const hoverState = animation.steps[1].styles;
663
+ function attachDefaultState() {
664
+ assign(element.style, defaultState);
665
+ }
666
+ function attachHoverState() {
667
+ assign(element.style, hoverState);
668
+ }
669
+ attachDefaultState();
670
+ element.addEventListener("mouseenter", attachHoverState);
671
+ element.addEventListener("mouseleave", attachDefaultState);
672
+ setTimeout(() => {
673
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
674
+ if (animation.delay) {
675
+ element.style.transitionDelay = animation.delay + "s";
676
+ }
677
+ });
678
+ });
679
+ }
680
+ function bindScrollInViewAnimation(animation) {
681
+ const elements = Array.prototype.slice.call(document.getElementsByClassName(animation.elementId || animation.id || ""));
682
+ if (!elements.length) {
683
+ warnElementNotPresent(animation.elementId || animation.id || "");
684
+ return;
685
+ }
686
+ Array.from(elements).forEach((element) => {
687
+ augmentAnimation(animation, element);
688
+ let triggered = false;
689
+ let pendingAnimation = false;
690
+ function immediateOnScroll() {
691
+ if (!triggered && isScrolledIntoView(element)) {
692
+ triggered = true;
693
+ pendingAnimation = true;
694
+ setTimeout(() => {
695
+ assign(element.style, animation.steps[1].styles);
696
+ if (!animation.repeat) {
697
+ document.removeEventListener("scroll", onScroll);
698
+ }
699
+ setTimeout(() => {
700
+ pendingAnimation = false;
701
+ if (!animation.repeat) {
702
+ element.style.transition = "";
703
+ element.style.transitionDelay = "";
704
+ }
705
+ }, (animation.duration + (animation.delay || 0)) * 1e3 + 100);
706
+ });
707
+ } else if (animation.repeat && triggered && !pendingAnimation && !isScrolledIntoView(element)) {
708
+ triggered = false;
709
+ assign(element.style, animation.steps[0].styles);
710
+ }
711
+ }
712
+ const onScroll = throttle(immediateOnScroll, 200, {
713
+ leading: false
714
+ });
715
+ function isScrolledIntoView(elem) {
716
+ const rect = elem.getBoundingClientRect();
717
+ const windowHeight = window.innerHeight;
718
+ const thresholdPercent = (animation.thresholdPercent || 0) / 100;
719
+ const threshold = thresholdPercent * windowHeight;
720
+ return rect.bottom > threshold && rect.top < windowHeight - threshold;
721
+ }
722
+ const defaultState = animation.steps[0].styles;
723
+ function attachDefaultState() {
724
+ assign(element.style, defaultState);
725
+ }
726
+ attachDefaultState();
727
+ setTimeout(() => {
728
+ element.style.transition = `all ${animation.duration}s ${camelCaseToKebabCase(animation.easing)}`;
729
+ if (animation.delay) {
730
+ element.style.transitionDelay = animation.delay + "s";
731
+ }
732
+ });
733
+ document.addEventListener("scroll", onScroll, {
734
+ capture: true,
735
+ passive: true
736
+ });
737
+ immediateOnScroll();
738
+ });
739
+ }
740
+
536
741
  // src/components/block/block.helpers.ts
537
742
  var getComponent = ({
538
743
  block,
@@ -1052,6 +1257,18 @@ function Block(props) {
1052
1257
  isInteractive: !blockComponent()?.isRSC
1053
1258
  };
1054
1259
  }
1260
+ onMount(() => {
1261
+ const blockId = processedBlock().id;
1262
+ const animations = processedBlock().animations;
1263
+ if (animations && blockId) {
1264
+ bindAnimations(
1265
+ animations.filter((item) => item.trigger !== "hover").map((animation) => ({
1266
+ ...animation,
1267
+ elementId: blockId
1268
+ }))
1269
+ );
1270
+ }
1271
+ });
1055
1272
  return <Show4 when={canShowBlock()}>
1056
1273
  <Block_styles_default block={props.block} context={props.context} />
1057
1274
  <Show4
@@ -1499,10 +1716,10 @@ function SectionComponent(props) {
1499
1716
  var section_default = SectionComponent;
1500
1717
 
1501
1718
  // src/blocks/symbol/symbol.tsx
1502
- import { onMount as onMount4, on as on3, createEffect as createEffect3, createSignal as createSignal14 } from "solid-js";
1719
+ import { onMount as onMount5, on as on3, createEffect as createEffect3, createSignal as createSignal14 } from "solid-js";
1503
1720
 
1504
1721
  // src/components/content-variants/content-variants.tsx
1505
- import { Show as Show11, For as For5, onMount as onMount3, createSignal as createSignal13 } from "solid-js";
1722
+ import { Show as Show11, For as For5, onMount as onMount4, createSignal as createSignal13 } from "solid-js";
1506
1723
 
1507
1724
  // src/helpers/url.ts
1508
1725
  var getTopLevelDomain = (host) => {
@@ -1980,12 +2197,12 @@ var componentInfo3 = {
1980
2197
  };
1981
2198
 
1982
2199
  // src/blocks/custom-code/custom-code.tsx
1983
- import { onMount, createSignal as createSignal7 } from "solid-js";
2200
+ import { onMount as onMount2, createSignal as createSignal7 } from "solid-js";
1984
2201
  function CustomCode(props) {
1985
2202
  const [scriptsInserted, setScriptsInserted] = createSignal7([]);
1986
2203
  const [scriptsRun, setScriptsRun] = createSignal7([]);
1987
2204
  let elementRef;
1988
- onMount(() => {
2205
+ onMount2(() => {
1989
2206
  if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
1990
2207
  return;
1991
2208
  }
@@ -2703,7 +2920,7 @@ function InlinedScript(props) {
2703
2920
  var Inlined_script_default = InlinedScript;
2704
2921
 
2705
2922
  // src/components/content/components/enable-editor.tsx
2706
- import { Show as Show9, onMount as onMount2, on as on2, createEffect as createEffect2, createSignal as createSignal10 } from "solid-js";
2923
+ import { Show as Show9, onMount as onMount3, on as on2, createEffect as createEffect2, createSignal as createSignal10 } from "solid-js";
2707
2924
  import { Dynamic as Dynamic5 } from "solid-js/web";
2708
2925
 
2709
2926
  // src/helpers/preview-lru-cache/get.ts
@@ -2938,13 +3155,6 @@ async function fetchEntries(options) {
2938
3155
  }
2939
3156
  var getAllContent = fetchEntries;
2940
3157
 
2941
- // src/functions/is-from-trusted-host.ts
2942
- var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
2943
- function isFromTrustedHost(trustedHosts, e) {
2944
- const url = new URL(e.origin), hostname = url.hostname;
2945
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
2946
- }
2947
-
2948
3158
  // src/functions/is-previewing.ts
2949
3159
  function isPreviewing() {
2950
3160
  if (!isBrowser()) {
@@ -3192,8 +3402,15 @@ var getInteractionPropertiesForEvent = (event) => {
3192
3402
  };
3193
3403
  };
3194
3404
 
3405
+ // src/functions/is-from-trusted-host.ts
3406
+ var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
3407
+ function isFromTrustedHost(trustedHosts, e) {
3408
+ const url = new URL(e.origin), hostname = url.hostname;
3409
+ return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
3410
+ }
3411
+
3195
3412
  // src/constants/sdk-version.ts
3196
- var SDK_VERSION = "0.12.6";
3413
+ var SDK_VERSION = "0.12.8";
3197
3414
 
3198
3415
  // src/functions/register.ts
3199
3416
  var registry = {};
@@ -3329,6 +3546,66 @@ var setupBrowserForEditing = (options = {}) => {
3329
3546
  }
3330
3547
  };
3331
3548
 
3549
+ // src/helpers/subscribe-to-editor.ts
3550
+ var createEditorListener = ({
3551
+ model,
3552
+ trustedHosts,
3553
+ callbacks
3554
+ }) => {
3555
+ return (event) => {
3556
+ if (!isFromTrustedHost(trustedHosts, event)) {
3557
+ return;
3558
+ }
3559
+ const {
3560
+ data
3561
+ } = event;
3562
+ if (data) {
3563
+ switch (data.type) {
3564
+ case "builder.configureSdk": {
3565
+ callbacks.configureSdk(data.data);
3566
+ break;
3567
+ }
3568
+ case "builder.triggerAnimation": {
3569
+ callbacks.animation(data.data);
3570
+ break;
3571
+ }
3572
+ case "builder.contentUpdate": {
3573
+ const messageContent = data.data;
3574
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
3575
+ const contentData = messageContent.data;
3576
+ if (key === model) {
3577
+ callbacks.contentUpdate(contentData);
3578
+ }
3579
+ break;
3580
+ }
3581
+ }
3582
+ }
3583
+ };
3584
+ };
3585
+ var subscribeToEditor = (model, callback, options) => {
3586
+ if (!isBrowser) {
3587
+ logger.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server.");
3588
+ return () => {
3589
+ };
3590
+ }
3591
+ setupBrowserForEditing();
3592
+ const listener = createEditorListener({
3593
+ callbacks: {
3594
+ contentUpdate: callback,
3595
+ animation: () => {
3596
+ },
3597
+ configureSdk: () => {
3598
+ }
3599
+ },
3600
+ model,
3601
+ trustedHosts: options?.trustedHosts
3602
+ });
3603
+ window.addEventListener("message", listener);
3604
+ return () => {
3605
+ window.removeEventListener("message", listener);
3606
+ };
3607
+ };
3608
+
3332
3609
  // src/components/content/components/enable-editor.tsx
3333
3610
  function EnableEditor(props) {
3334
3611
  const [forceReRenderCount, setForceReRenderCount] = createSignal10(0);
@@ -3374,14 +3651,11 @@ function EnableEditor(props) {
3374
3651
  }));
3375
3652
  }
3376
3653
  function processMessage(event) {
3377
- if (!isFromTrustedHost(props.trustedHosts, event)) {
3378
- return;
3379
- }
3380
- const { data } = event;
3381
- if (data) {
3382
- switch (data.type) {
3383
- case "builder.configureSdk": {
3384
- const messageContent = data.data;
3654
+ return createEditorListener({
3655
+ model: props.model,
3656
+ trustedHosts: props.trustedHosts,
3657
+ callbacks: {
3658
+ configureSdk: (messageContent) => {
3385
3659
  const { breakpoints, contentId } = messageContent;
3386
3660
  if (!contentId || contentId !== props.builderContextSignal.content?.id) {
3387
3661
  return;
@@ -3392,22 +3666,18 @@ function EnableEditor(props) {
3392
3666
  breakpoints
3393
3667
  }
3394
3668
  });
3395
- }
3396
- setForceReRenderCount(forceReRenderCount() + 1);
3397
- break;
3398
- }
3399
- case "builder.contentUpdate": {
3400
- const messageContent = data.data;
3401
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
3402
- const contentData = messageContent.data;
3403
- if (key === props.model) {
3404
- mergeNewContent(contentData);
3405
3669
  setForceReRenderCount(forceReRenderCount() + 1);
3406
3670
  }
3407
- break;
3671
+ },
3672
+ animation: (animation) => {
3673
+ triggerAnimation(animation);
3674
+ },
3675
+ contentUpdate: (newContent) => {
3676
+ mergeNewContent(newContent);
3677
+ setForceReRenderCount(forceReRenderCount() + 1);
3408
3678
  }
3409
3679
  }
3410
- }
3680
+ })(event);
3411
3681
  }
3412
3682
  function evaluateJsCode() {
3413
3683
  const jsCode = props.builderContextSignal.content?.data?.jsCode;
@@ -3492,7 +3762,7 @@ function EnableEditor(props) {
3492
3762
  }
3493
3763
  }
3494
3764
  let elementRef;
3495
- onMount2(() => {
3765
+ onMount3(() => {
3496
3766
  if (isBrowser()) {
3497
3767
  if (isEditing() && true) {
3498
3768
  setForceReRenderCount(forceReRenderCount() + 1);
@@ -3557,7 +3827,7 @@ function EnableEditor(props) {
3557
3827
  }
3558
3828
  }
3559
3829
  });
3560
- onMount2(() => {
3830
+ onMount3(() => {
3561
3831
  if (!props.apiKey) {
3562
3832
  logger.error(
3563
3833
  "No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
@@ -3897,7 +4167,7 @@ function ContentVariants(props) {
3897
4167
  canTrack: getDefaultCanTrack(props.canTrack)
3898
4168
  });
3899
4169
  }
3900
- onMount3(() => {
4170
+ onMount4(() => {
3901
4171
  setShouldRenderVariants(false);
3902
4172
  });
3903
4173
  return <>
@@ -4009,7 +4279,7 @@ function Symbol(props) {
4009
4279
  }
4010
4280
  });
4011
4281
  }
4012
- onMount4(() => {
4282
+ onMount5(() => {
4013
4283
  setContent();
4014
4284
  });
4015
4285
  function onUpdateFn_0() {
@@ -4100,5 +4370,6 @@ export {
4100
4370
  isPreviewing,
4101
4371
  register,
4102
4372
  setEditorSettings,
4373
+ subscribeToEditor,
4103
4374
  track
4104
4375
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-solid",
3
- "version": "0.12.6",
3
+ "version": "0.12.8",
4
4
  "description": "",
5
5
  "files": [
6
6
  "dist",