@builder.io/sdk-solid 0.12.7 → 0.13.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.
package/lib/edge/index.js CHANGED
@@ -6388,8 +6388,8 @@ var generateContentUrl = (options) => {
6388
6388
  if (!apiKey) {
6389
6389
  throw new Error("Missing API key");
6390
6390
  }
6391
- if (!["v2", "v3"].includes(apiVersion)) {
6392
- throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
6391
+ if (!["v3"].includes(apiVersion)) {
6392
+ throw new Error(`Invalid apiVersion: expected 'v3', received '${apiVersion}'`);
6393
6393
  }
6394
6394
  if ((options.limit === void 0 || options.limit > 1) && !("noTraverse" in options)) {
6395
6395
  noTraverse = true;
@@ -6509,13 +6509,6 @@ async function fetchEntries(options) {
6509
6509
  }
6510
6510
  var getAllContent = fetchEntries;
6511
6511
 
6512
- // src/functions/is-from-trusted-host.ts
6513
- var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
6514
- function isFromTrustedHost(trustedHosts, e) {
6515
- const url = new URL(e.origin), hostname = url.hostname;
6516
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
6517
- }
6518
-
6519
6512
  // src/functions/is-previewing.ts
6520
6513
  function isPreviewing() {
6521
6514
  if (!isBrowser()) {
@@ -6763,8 +6756,15 @@ var getInteractionPropertiesForEvent = (event) => {
6763
6756
  };
6764
6757
  };
6765
6758
 
6759
+ // src/functions/is-from-trusted-host.ts
6760
+ var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
6761
+ function isFromTrustedHost(trustedHosts, e) {
6762
+ const url = new URL(e.origin), hostname = url.hostname;
6763
+ return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
6764
+ }
6765
+
6766
6766
  // src/constants/sdk-version.ts
6767
- var SDK_VERSION = "0.12.7";
6767
+ var SDK_VERSION = "0.13.0";
6768
6768
 
6769
6769
  // src/functions/register.ts
6770
6770
  var registry = {};
@@ -6900,6 +6900,66 @@ var setupBrowserForEditing = (options = {}) => {
6900
6900
  }
6901
6901
  };
6902
6902
 
6903
+ // src/helpers/subscribe-to-editor.ts
6904
+ var createEditorListener = ({
6905
+ model,
6906
+ trustedHosts,
6907
+ callbacks
6908
+ }) => {
6909
+ return (event) => {
6910
+ if (!isFromTrustedHost(trustedHosts, event)) {
6911
+ return;
6912
+ }
6913
+ const {
6914
+ data
6915
+ } = event;
6916
+ if (data) {
6917
+ switch (data.type) {
6918
+ case "builder.configureSdk": {
6919
+ callbacks.configureSdk(data.data);
6920
+ break;
6921
+ }
6922
+ case "builder.triggerAnimation": {
6923
+ callbacks.animation(data.data);
6924
+ break;
6925
+ }
6926
+ case "builder.contentUpdate": {
6927
+ const messageContent = data.data;
6928
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6929
+ const contentData = messageContent.data;
6930
+ if (key === model) {
6931
+ callbacks.contentUpdate(contentData);
6932
+ }
6933
+ break;
6934
+ }
6935
+ }
6936
+ }
6937
+ };
6938
+ };
6939
+ var subscribeToEditor = (model, callback, options) => {
6940
+ if (!isBrowser) {
6941
+ logger.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server.");
6942
+ return () => {
6943
+ };
6944
+ }
6945
+ setupBrowserForEditing();
6946
+ const listener = createEditorListener({
6947
+ callbacks: {
6948
+ contentUpdate: callback,
6949
+ animation: () => {
6950
+ },
6951
+ configureSdk: () => {
6952
+ }
6953
+ },
6954
+ model,
6955
+ trustedHosts: options?.trustedHosts
6956
+ });
6957
+ window.addEventListener("message", listener);
6958
+ return () => {
6959
+ window.removeEventListener("message", listener);
6960
+ };
6961
+ };
6962
+
6903
6963
  // src/components/content/components/enable-editor.tsx
6904
6964
  function EnableEditor(props) {
6905
6965
  const [forceReRenderCount, setForceReRenderCount] = createSignal(0);
@@ -6943,16 +7003,11 @@ function EnableEditor(props) {
6943
7003
  }));
6944
7004
  }
6945
7005
  function processMessage(event) {
6946
- if (!isFromTrustedHost(props.trustedHosts, event)) {
6947
- return;
6948
- }
6949
- const {
6950
- data
6951
- } = event;
6952
- if (data) {
6953
- switch (data.type) {
6954
- case "builder.configureSdk": {
6955
- const messageContent = data.data;
7006
+ return createEditorListener({
7007
+ model: props.model,
7008
+ trustedHosts: props.trustedHosts,
7009
+ callbacks: {
7010
+ configureSdk: (messageContent) => {
6956
7011
  const {
6957
7012
  breakpoints,
6958
7013
  contentId
@@ -6966,26 +7021,18 @@ function EnableEditor(props) {
6966
7021
  breakpoints
6967
7022
  }
6968
7023
  });
6969
- }
6970
- setForceReRenderCount(forceReRenderCount() + 1);
6971
- break;
6972
- }
6973
- case "builder.triggerAnimation": {
6974
- triggerAnimation(data.data);
6975
- break;
6976
- }
6977
- case "builder.contentUpdate": {
6978
- const messageContent = data.data;
6979
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6980
- const contentData = messageContent.data;
6981
- if (key === props.model) {
6982
- mergeNewContent(contentData);
6983
7024
  setForceReRenderCount(forceReRenderCount() + 1);
6984
7025
  }
6985
- break;
7026
+ },
7027
+ animation: (animation) => {
7028
+ triggerAnimation(animation);
7029
+ },
7030
+ contentUpdate: (newContent) => {
7031
+ mergeNewContent(newContent);
7032
+ setForceReRenderCount(forceReRenderCount() + 1);
6986
7033
  }
6987
7034
  }
6988
- }
7035
+ })(event);
6989
7036
  }
6990
7037
  function evaluateJsCode() {
6991
7038
  const jsCode = props.builderContextSignal.content?.data?.jsCode;
@@ -7828,4 +7875,4 @@ var fetchBuilderProps = async (_args) => {
7828
7875
  };
7829
7876
  };
7830
7877
 
7831
- export { blocks_default as Blocks, button_default as Button, columns_default as Columns, content_variants_default as Content, fragment_default as Fragment, image_default as Image, RenderBlocks, RenderContent, section_default as Section, symbol_default as Symbol, text_default as Text, video_default as Video, _processContentResult, createRegisterComponentMessage, fetchBuilderProps, fetchEntries, fetchOneEntry, getAllContent, getBuilderSearchParams, getContent, isEditing, isPreviewing, register, setEditorSettings, track };
7878
+ export { blocks_default as Blocks, button_default as Button, columns_default as Columns, content_variants_default as Content, fragment_default as Fragment, image_default as Image, RenderBlocks, RenderContent, section_default as Section, symbol_default as Symbol, text_default as Text, video_default as Video, _processContentResult, createRegisterComponentMessage, fetchBuilderProps, fetchEntries, fetchOneEntry, getAllContent, getBuilderSearchParams, getContent, isEditing, isPreviewing, register, setEditorSettings, subscribeToEditor, track };
@@ -6064,8 +6064,8 @@ var generateContentUrl = (options) => {
6064
6064
  if (!apiKey) {
6065
6065
  throw new Error("Missing API key");
6066
6066
  }
6067
- if (!["v2", "v3"].includes(apiVersion)) {
6068
- throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
6067
+ if (!["v3"].includes(apiVersion)) {
6068
+ throw new Error(`Invalid apiVersion: expected 'v3', received '${apiVersion}'`);
6069
6069
  }
6070
6070
  if ((options.limit === void 0 || options.limit > 1) && !("noTraverse" in options)) {
6071
6071
  noTraverse = true;
@@ -6185,13 +6185,6 @@ async function fetchEntries(options) {
6185
6185
  }
6186
6186
  var getAllContent = fetchEntries;
6187
6187
 
6188
- // src/functions/is-from-trusted-host.ts
6189
- var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
6190
- function isFromTrustedHost(trustedHosts, e) {
6191
- const url = new URL(e.origin), hostname = url.hostname;
6192
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
6193
- }
6194
-
6195
6188
  // src/functions/is-previewing.ts
6196
6189
  function isPreviewing() {
6197
6190
  if (!isBrowser()) {
@@ -6439,8 +6432,15 @@ var getInteractionPropertiesForEvent = (event) => {
6439
6432
  };
6440
6433
  };
6441
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
+
6442
6442
  // src/constants/sdk-version.ts
6443
- var SDK_VERSION = "0.12.7";
6443
+ var SDK_VERSION = "0.13.0";
6444
6444
 
6445
6445
  // src/functions/register.ts
6446
6446
  var registry = {};
@@ -6576,6 +6576,66 @@ var setupBrowserForEditing = (options = {}) => {
6576
6576
  }
6577
6577
  };
6578
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
+
6579
6639
  // src/components/content/components/enable-editor.tsx
6580
6640
  function EnableEditor(props) {
6581
6641
  const [forceReRenderCount, setForceReRenderCount] = createSignal10(0);
@@ -6621,14 +6681,11 @@ function EnableEditor(props) {
6621
6681
  }));
6622
6682
  }
6623
6683
  function processMessage(event) {
6624
- if (!isFromTrustedHost(props.trustedHosts, event)) {
6625
- return;
6626
- }
6627
- const { data } = event;
6628
- if (data) {
6629
- switch (data.type) {
6630
- case "builder.configureSdk": {
6631
- const messageContent = data.data;
6684
+ return createEditorListener({
6685
+ model: props.model,
6686
+ trustedHosts: props.trustedHosts,
6687
+ callbacks: {
6688
+ configureSdk: (messageContent) => {
6632
6689
  const { breakpoints, contentId } = messageContent;
6633
6690
  if (!contentId || contentId !== props.builderContextSignal.content?.id) {
6634
6691
  return;
@@ -6639,26 +6696,18 @@ function EnableEditor(props) {
6639
6696
  breakpoints
6640
6697
  }
6641
6698
  });
6642
- }
6643
- setForceReRenderCount(forceReRenderCount() + 1);
6644
- break;
6645
- }
6646
- case "builder.triggerAnimation": {
6647
- triggerAnimation(data.data);
6648
- break;
6649
- }
6650
- case "builder.contentUpdate": {
6651
- const messageContent = data.data;
6652
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
6653
- const contentData = messageContent.data;
6654
- if (key === props.model) {
6655
- mergeNewContent(contentData);
6656
6699
  setForceReRenderCount(forceReRenderCount() + 1);
6657
6700
  }
6658
- break;
6701
+ },
6702
+ animation: (animation) => {
6703
+ triggerAnimation(animation);
6704
+ },
6705
+ contentUpdate: (newContent) => {
6706
+ mergeNewContent(newContent);
6707
+ setForceReRenderCount(forceReRenderCount() + 1);
6659
6708
  }
6660
6709
  }
6661
- }
6710
+ })(event);
6662
6711
  }
6663
6712
  function evaluateJsCode() {
6664
6713
  const jsCode = props.builderContextSignal.content?.data?.jsCode;
@@ -7351,5 +7400,6 @@ export {
7351
7400
  isPreviewing,
7352
7401
  register,
7353
7402
  setEditorSettings,
7403
+ subscribeToEditor,
7354
7404
  track
7355
7405
  };
package/lib/node/dev.js CHANGED
@@ -3371,8 +3371,8 @@ var generateContentUrl = (options) => {
3371
3371
  if (!apiKey) {
3372
3372
  throw new Error("Missing API key");
3373
3373
  }
3374
- if (!["v2", "v3"].includes(apiVersion)) {
3375
- throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
3374
+ if (!["v3"].includes(apiVersion)) {
3375
+ throw new Error(`Invalid apiVersion: expected 'v3', received '${apiVersion}'`);
3376
3376
  }
3377
3377
  if ((options.limit === void 0 || options.limit > 1) && !("noTraverse" in options)) {
3378
3378
  noTraverse = true;
@@ -3492,13 +3492,6 @@ async function fetchEntries(options) {
3492
3492
  }
3493
3493
  var getAllContent = fetchEntries;
3494
3494
 
3495
- // src/functions/is-from-trusted-host.ts
3496
- var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
3497
- function isFromTrustedHost(trustedHosts, e) {
3498
- const url = new URL(e.origin), hostname = url.hostname;
3499
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
3500
- }
3501
-
3502
3495
  // src/functions/is-previewing.ts
3503
3496
  function isPreviewing() {
3504
3497
  if (!isBrowser()) {
@@ -3749,8 +3742,15 @@ var getInteractionPropertiesForEvent = (event) => {
3749
3742
  };
3750
3743
  };
3751
3744
 
3745
+ // src/functions/is-from-trusted-host.ts
3746
+ var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
3747
+ function isFromTrustedHost(trustedHosts, e) {
3748
+ const url = new URL(e.origin), hostname = url.hostname;
3749
+ return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
3750
+ }
3751
+
3752
3752
  // src/constants/sdk-version.ts
3753
- var SDK_VERSION = "0.12.7";
3753
+ var SDK_VERSION = "0.13.0";
3754
3754
 
3755
3755
  // src/functions/register.ts
3756
3756
  var registry = {};
@@ -3887,6 +3887,66 @@ var setupBrowserForEditing = (options = {}) => {
3887
3887
  }
3888
3888
  };
3889
3889
 
3890
+ // src/helpers/subscribe-to-editor.ts
3891
+ var createEditorListener = ({
3892
+ model,
3893
+ trustedHosts,
3894
+ callbacks
3895
+ }) => {
3896
+ return (event) => {
3897
+ if (!isFromTrustedHost(trustedHosts, event)) {
3898
+ return;
3899
+ }
3900
+ const {
3901
+ data
3902
+ } = event;
3903
+ if (data) {
3904
+ switch (data.type) {
3905
+ case "builder.configureSdk": {
3906
+ callbacks.configureSdk(data.data);
3907
+ break;
3908
+ }
3909
+ case "builder.triggerAnimation": {
3910
+ callbacks.animation(data.data);
3911
+ break;
3912
+ }
3913
+ case "builder.contentUpdate": {
3914
+ const messageContent = data.data;
3915
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
3916
+ const contentData = messageContent.data;
3917
+ if (key === model) {
3918
+ callbacks.contentUpdate(contentData);
3919
+ }
3920
+ break;
3921
+ }
3922
+ }
3923
+ }
3924
+ };
3925
+ };
3926
+ var subscribeToEditor = (model, callback, options) => {
3927
+ if (!isBrowser) {
3928
+ logger.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server.");
3929
+ return () => {
3930
+ };
3931
+ }
3932
+ setupBrowserForEditing();
3933
+ const listener = createEditorListener({
3934
+ callbacks: {
3935
+ contentUpdate: callback,
3936
+ animation: () => {
3937
+ },
3938
+ configureSdk: () => {
3939
+ }
3940
+ },
3941
+ model,
3942
+ trustedHosts: options?.trustedHosts
3943
+ });
3944
+ window.addEventListener("message", listener);
3945
+ return () => {
3946
+ window.removeEventListener("message", listener);
3947
+ };
3948
+ };
3949
+
3890
3950
  // src/components/content/components/enable-editor.tsx
3891
3951
  function EnableEditor(props) {
3892
3952
  const [forceReRenderCount, setForceReRenderCount] = createSignal(0);
@@ -3930,16 +3990,11 @@ function EnableEditor(props) {
3930
3990
  }));
3931
3991
  }
3932
3992
  function processMessage(event) {
3933
- if (!isFromTrustedHost(props.trustedHosts, event)) {
3934
- return;
3935
- }
3936
- const {
3937
- data
3938
- } = event;
3939
- if (data) {
3940
- switch (data.type) {
3941
- case "builder.configureSdk": {
3942
- const messageContent = data.data;
3993
+ return createEditorListener({
3994
+ model: props.model,
3995
+ trustedHosts: props.trustedHosts,
3996
+ callbacks: {
3997
+ configureSdk: (messageContent) => {
3943
3998
  const {
3944
3999
  breakpoints,
3945
4000
  contentId
@@ -3953,26 +4008,18 @@ function EnableEditor(props) {
3953
4008
  breakpoints
3954
4009
  }
3955
4010
  });
3956
- }
3957
- setForceReRenderCount(forceReRenderCount() + 1);
3958
- break;
3959
- }
3960
- case "builder.triggerAnimation": {
3961
- triggerAnimation(data.data);
3962
- break;
3963
- }
3964
- case "builder.contentUpdate": {
3965
- const messageContent = data.data;
3966
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
3967
- const contentData = messageContent.data;
3968
- if (key === props.model) {
3969
- mergeNewContent(contentData);
3970
4011
  setForceReRenderCount(forceReRenderCount() + 1);
3971
4012
  }
3972
- break;
4013
+ },
4014
+ animation: (animation) => {
4015
+ triggerAnimation(animation);
4016
+ },
4017
+ contentUpdate: (newContent) => {
4018
+ mergeNewContent(newContent);
4019
+ setForceReRenderCount(forceReRenderCount() + 1);
3973
4020
  }
3974
4021
  }
3975
- }
4022
+ })(event);
3976
4023
  }
3977
4024
  function evaluateJsCode() {
3978
4025
  const jsCode = props.builderContextSignal.content?.data?.jsCode;
@@ -4816,4 +4863,4 @@ var fetchBuilderProps = async (_args) => {
4816
4863
  };
4817
4864
  };
4818
4865
 
4819
- export { blocks_default as Blocks, button_default as Button, columns_default as Columns, content_variants_default as Content, fragment_default as Fragment, image_default as Image, RenderBlocks, RenderContent, section_default as Section, symbol_default as Symbol, text_default as Text, video_default as Video, _processContentResult, createRegisterComponentMessage, fetchBuilderProps, fetchEntries, fetchOneEntry, getAllContent, getBuilderSearchParams, getContent, isEditing, isPreviewing, register, setEditorSettings, track };
4866
+ export { blocks_default as Blocks, button_default as Button, columns_default as Columns, content_variants_default as Content, fragment_default as Fragment, image_default as Image, RenderBlocks, RenderContent, section_default as Section, symbol_default as Symbol, text_default as Text, video_default as Video, _processContentResult, createRegisterComponentMessage, fetchBuilderProps, fetchEntries, fetchOneEntry, getAllContent, getBuilderSearchParams, getContent, isEditing, isPreviewing, register, setEditorSettings, subscribeToEditor, track };
package/lib/node/dev.jsx CHANGED
@@ -3047,8 +3047,8 @@ var generateContentUrl = (options) => {
3047
3047
  if (!apiKey) {
3048
3048
  throw new Error("Missing API key");
3049
3049
  }
3050
- if (!["v2", "v3"].includes(apiVersion)) {
3051
- throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
3050
+ if (!["v3"].includes(apiVersion)) {
3051
+ throw new Error(`Invalid apiVersion: expected 'v3', received '${apiVersion}'`);
3052
3052
  }
3053
3053
  if ((options.limit === void 0 || options.limit > 1) && !("noTraverse" in options)) {
3054
3054
  noTraverse = true;
@@ -3168,13 +3168,6 @@ async function fetchEntries(options) {
3168
3168
  }
3169
3169
  var getAllContent = fetchEntries;
3170
3170
 
3171
- // src/functions/is-from-trusted-host.ts
3172
- var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
3173
- function isFromTrustedHost(trustedHosts, e) {
3174
- const url = new URL(e.origin), hostname = url.hostname;
3175
- return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
3176
- }
3177
-
3178
3171
  // src/functions/is-previewing.ts
3179
3172
  function isPreviewing() {
3180
3173
  if (!isBrowser()) {
@@ -3425,8 +3418,15 @@ var getInteractionPropertiesForEvent = (event) => {
3425
3418
  };
3426
3419
  };
3427
3420
 
3421
+ // src/functions/is-from-trusted-host.ts
3422
+ var DEFAULT_TRUSTED_HOSTS = ["*.beta.builder.io", "beta.builder.io", "builder.io", "localhost", "qa.builder.io"];
3423
+ function isFromTrustedHost(trustedHosts, e) {
3424
+ const url = new URL(e.origin), hostname = url.hostname;
3425
+ return (trustedHosts || DEFAULT_TRUSTED_HOSTS).findIndex((trustedHost) => trustedHost.startsWith("*.") ? hostname.endsWith(trustedHost.slice(1)) : trustedHost === hostname) > -1;
3426
+ }
3427
+
3428
3428
  // src/constants/sdk-version.ts
3429
- var SDK_VERSION = "0.12.7";
3429
+ var SDK_VERSION = "0.13.0";
3430
3430
 
3431
3431
  // src/functions/register.ts
3432
3432
  var registry = {};
@@ -3563,6 +3563,66 @@ var setupBrowserForEditing = (options = {}) => {
3563
3563
  }
3564
3564
  };
3565
3565
 
3566
+ // src/helpers/subscribe-to-editor.ts
3567
+ var createEditorListener = ({
3568
+ model,
3569
+ trustedHosts,
3570
+ callbacks
3571
+ }) => {
3572
+ return (event) => {
3573
+ if (!isFromTrustedHost(trustedHosts, event)) {
3574
+ return;
3575
+ }
3576
+ const {
3577
+ data
3578
+ } = event;
3579
+ if (data) {
3580
+ switch (data.type) {
3581
+ case "builder.configureSdk": {
3582
+ callbacks.configureSdk(data.data);
3583
+ break;
3584
+ }
3585
+ case "builder.triggerAnimation": {
3586
+ callbacks.animation(data.data);
3587
+ break;
3588
+ }
3589
+ case "builder.contentUpdate": {
3590
+ const messageContent = data.data;
3591
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
3592
+ const contentData = messageContent.data;
3593
+ if (key === model) {
3594
+ callbacks.contentUpdate(contentData);
3595
+ }
3596
+ break;
3597
+ }
3598
+ }
3599
+ }
3600
+ };
3601
+ };
3602
+ var subscribeToEditor = (model, callback, options) => {
3603
+ if (!isBrowser) {
3604
+ logger.warn("`subscribeToEditor` only works in the browser. It currently seems to be running on the server.");
3605
+ return () => {
3606
+ };
3607
+ }
3608
+ setupBrowserForEditing();
3609
+ const listener = createEditorListener({
3610
+ callbacks: {
3611
+ contentUpdate: callback,
3612
+ animation: () => {
3613
+ },
3614
+ configureSdk: () => {
3615
+ }
3616
+ },
3617
+ model,
3618
+ trustedHosts: options?.trustedHosts
3619
+ });
3620
+ window.addEventListener("message", listener);
3621
+ return () => {
3622
+ window.removeEventListener("message", listener);
3623
+ };
3624
+ };
3625
+
3566
3626
  // src/components/content/components/enable-editor.tsx
3567
3627
  function EnableEditor(props) {
3568
3628
  const [forceReRenderCount, setForceReRenderCount] = createSignal10(0);
@@ -3608,14 +3668,11 @@ function EnableEditor(props) {
3608
3668
  }));
3609
3669
  }
3610
3670
  function processMessage(event) {
3611
- if (!isFromTrustedHost(props.trustedHosts, event)) {
3612
- return;
3613
- }
3614
- const { data } = event;
3615
- if (data) {
3616
- switch (data.type) {
3617
- case "builder.configureSdk": {
3618
- const messageContent = data.data;
3671
+ return createEditorListener({
3672
+ model: props.model,
3673
+ trustedHosts: props.trustedHosts,
3674
+ callbacks: {
3675
+ configureSdk: (messageContent) => {
3619
3676
  const { breakpoints, contentId } = messageContent;
3620
3677
  if (!contentId || contentId !== props.builderContextSignal.content?.id) {
3621
3678
  return;
@@ -3626,26 +3683,18 @@ function EnableEditor(props) {
3626
3683
  breakpoints
3627
3684
  }
3628
3685
  });
3629
- }
3630
- setForceReRenderCount(forceReRenderCount() + 1);
3631
- break;
3632
- }
3633
- case "builder.triggerAnimation": {
3634
- triggerAnimation(data.data);
3635
- break;
3636
- }
3637
- case "builder.contentUpdate": {
3638
- const messageContent = data.data;
3639
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
3640
- const contentData = messageContent.data;
3641
- if (key === props.model) {
3642
- mergeNewContent(contentData);
3643
3686
  setForceReRenderCount(forceReRenderCount() + 1);
3644
3687
  }
3645
- break;
3688
+ },
3689
+ animation: (animation) => {
3690
+ triggerAnimation(animation);
3691
+ },
3692
+ contentUpdate: (newContent) => {
3693
+ mergeNewContent(newContent);
3694
+ setForceReRenderCount(forceReRenderCount() + 1);
3646
3695
  }
3647
3696
  }
3648
- }
3697
+ })(event);
3649
3698
  }
3650
3699
  function evaluateJsCode() {
3651
3700
  const jsCode = props.builderContextSignal.content?.data?.jsCode;
@@ -4339,5 +4388,6 @@ export {
4339
4388
  isPreviewing,
4340
4389
  register,
4341
4390
  setEditorSettings,
4391
+ subscribeToEditor,
4342
4392
  track
4343
4393
  };