@builder.io/sdk-svelte 0.0.1-21 → 0.0.1-22

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-svelte",
3
3
  "description": "Builder.io SDK for Svelte",
4
- "version": "0.0.1-21",
4
+ "version": "0.0.1-22",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "src"
@@ -9,7 +9,26 @@ const componentInfo = {
9
9
  required: true,
10
10
  defaultValue: "",
11
11
  helperText: "e.g. enter a youtube url, google map, etc",
12
- onChange: " const url = options.get('url'); if (url) { options.set('content', 'Loading...'); // TODO: get this out of here! const apiKey = 'ae0e60e78201a3f2b0de4b'; return fetch(`https://iframe.ly/api/iframely?url=${url}&api_key=${apiKey}`) .then(res => res.json()) .then(data => { if (options.get('url') === url) { if (data.html) { options.set('content', data.html); } else { options.set('content', 'Invalid url, please try another'); } } }) .catch(err => { options.set( 'content', 'There was an error embedding this URL, please try again or another URL' ); }); } else { options.delete('content'); } "
12
+ onChange: (options) => {
13
+ const url = options.get("url");
14
+ if (url) {
15
+ options.set("content", "Loading...");
16
+ const apiKey = "ae0e60e78201a3f2b0de4b";
17
+ return fetch(`https://iframe.ly/api/iframely?url=${url}&api_key=${apiKey}`).then((res) => res.json()).then((data) => {
18
+ if (options.get("url") === url) {
19
+ if (data.html) {
20
+ options.set("content", data.html);
21
+ } else {
22
+ options.set("content", "Invalid url, please try another");
23
+ }
24
+ }
25
+ }).catch((_err) => {
26
+ options.set("content", "There was an error embedding this URL, please try again or another URL");
27
+ });
28
+ } else {
29
+ options.delete("content");
30
+ }
31
+ }
13
32
  },
14
33
  {
15
34
  name: "content",
@@ -1,45 +1,29 @@
1
1
  <script>
2
- import { onMount } from "svelte";
2
+ import { afterUpdate } from "svelte";
3
+
4
+ import { isJsScript } from "./helpers";
3
5
 
4
6
  export let elem;
5
7
  export let content;
6
8
 
7
9
  const findAndRunScripts = function findAndRunScripts() {
8
- // TODO: Move this function to standalone one in '@builder.io/utils'
9
- if (elem && typeof window !== "undefined") {
10
- const scripts = elem.getElementsByTagName("script");
11
-
12
- for (let i = 0; i < scripts.length; i++) {
13
- const script = scripts[i];
14
-
15
- if (script.src) {
16
- if (scriptsInserted.includes(script.src)) {
17
- continue;
18
- }
19
-
20
- scriptsInserted.push(script.src);
21
- const newScript = document.createElement("script");
22
- newScript.async = true;
23
- newScript.src = script.src;
24
- document.head.appendChild(newScript);
25
- } else if (
26
- !script.type ||
27
- [
28
- "text/javascript",
29
- "application/javascript",
30
- "application/ecmascript",
31
- ].includes(script.type)
32
- ) {
33
- if (scriptsRun.includes(script.innerText)) {
34
- continue;
35
- }
36
-
37
- try {
38
- scriptsRun.push(script.innerText);
39
- new Function(script.innerText)();
40
- } catch (error) {
41
- console.warn("`Embed`: Error running script:", error);
42
- }
10
+ const scripts = elem.getElementsByTagName("script");
11
+
12
+ for (let i = 0; i < scripts.length; i++) {
13
+ const script = scripts[i];
14
+
15
+ if (script.src && !scriptsInserted.includes(script.src)) {
16
+ scriptsInserted.push(script.src);
17
+ const newScript = document.createElement("script");
18
+ newScript.async = true;
19
+ newScript.src = script.src;
20
+ document.head.appendChild(newScript);
21
+ } else if (isJsScript(script) && !scriptsRun.includes(script.innerText)) {
22
+ try {
23
+ scriptsRun.push(script.innerText);
24
+ new Function(script.innerText)();
25
+ } catch (error) {
26
+ console.warn("`Embed`: Error running script:", error);
43
27
  }
44
28
  }
45
29
  }
@@ -47,10 +31,15 @@
47
31
 
48
32
  let scriptsInserted = [];
49
33
  let scriptsRun = [];
34
+ let ranInitFn = false;
50
35
 
51
- onMount(() => {
52
- findAndRunScripts();
53
- });
36
+ function onUpdateFn_0() {
37
+ if (elem && !ranInitFn) {
38
+ ranInitFn = true;
39
+ findAndRunScripts();
40
+ }
41
+ }
42
+ $: onUpdateFn_0(...[elem, ranInitFn]);
54
43
  </script>
55
44
 
56
45
  <div class="builder-embed" bind:this={elem}>{@html content}</div>
@@ -0,0 +1,9 @@
1
+ const SCRIPT_MIME_TYPES = [
2
+ "text/javascript",
3
+ "application/javascript",
4
+ "application/ecmascript"
5
+ ];
6
+ const isJsScript = (script) => SCRIPT_MIME_TYPES.includes(script.type);
7
+ export {
8
+ isJsScript
9
+ };
@@ -35,14 +35,7 @@
35
35
  });
36
36
  }
37
37
  }
38
- $: onUpdateFn_0(
39
- ...[
40
- props.symbol?.content,
41
- props.symbol?.model,
42
- props.symbol?.entry,
43
- state.content,
44
- ]
45
- );
38
+ $: onUpdateFn_0(...[symbol?.content, symbol?.model, symbol?.entry, content]);
46
39
  </script>
47
40
 
48
41
  <div
@@ -29,7 +29,7 @@
29
29
  borderRadius: 1,
30
30
  }}
31
31
  preload="none"
32
- key={video || "no-src"}
32
+ src={video || "no-src"}
33
33
  poster={posterImage}
34
34
  {autoPlay}
35
35
  {muted}
@@ -30,6 +30,8 @@ import { componentInfo as textComponentInfo } from "../blocks/text/component-inf
30
30
  import { default as Text } from "../blocks/text/text.svelte";
31
31
  import { componentInfo as videoComponentInfo } from "../blocks/video/component-info";
32
32
  import { default as Video } from "../blocks/video/video.svelte";
33
+ import { componentInfo as embedComponentInfo } from "../blocks/embed/component-info";
34
+ import { default as embed } from "../blocks/embed/embed.svelte";
33
35
  const getDefaultRegisteredComponents = () => [
34
36
  __spreadValues({ component: Columns }, columnsComponentInfo),
35
37
  __spreadValues({ component: Image }, imageComponentInfo),
@@ -38,7 +40,8 @@ const getDefaultRegisteredComponents = () => [
38
40
  __spreadValues({ component: Symbol }, symbolComponentInfo),
39
41
  __spreadValues({ component: Button }, buttonComponentInfo),
40
42
  __spreadValues({ component: Section }, sectionComponentInfo),
41
- __spreadValues({ component: Fragment }, fragmentComponentInfo)
43
+ __spreadValues({ component: Fragment }, fragmentComponentInfo),
44
+ __spreadValues({ component: embed }, embedComponentInfo)
42
45
  ];
43
46
  export {
44
47
  getDefaultRegisteredComponents
@@ -29,7 +29,6 @@ var __objRest = (source, exclude) => {
29
29
  }
30
30
  return target;
31
31
  };
32
- import { fastClone } from "./fast-clone.js";
33
32
  const components = [];
34
33
  function registerComponent(component, info) {
35
34
  components.push(__spreadValues({ component }, info));
@@ -38,31 +37,24 @@ function registerComponent(component, info) {
38
37
  }
39
38
  const createRegisterComponentMessage = (_a) => {
40
39
  var _b = _a, {
41
- component
40
+ component: _
42
41
  } = _b, info = __objRest(_b, [
43
42
  "component"
44
43
  ]);
45
44
  return {
46
45
  type: "builder.registerComponent",
47
- data: prepareComponentInfoToSend(fastClone(info))
46
+ data: prepareComponentInfoToSend(info)
48
47
  };
49
48
  };
50
- function prepareComponentInfoToSend(info) {
51
- return __spreadValues(__spreadValues({}, info), info.inputs && {
52
- inputs: info.inputs.map((input) => {
53
- const keysToConvertFnToString = ["onChange", "showIf"];
54
- for (const key of keysToConvertFnToString) {
55
- const fn = input[key];
56
- if (fn && typeof fn === "function") {
57
- input = __spreadProps(__spreadValues({}, input), {
58
- [key]: `return (${fn.toString()}).apply(this, arguments)`
59
- });
60
- }
61
- }
62
- return input;
63
- })
49
+ const serializeFn = (fn) => `return (${fn.toString()}).apply(this, arguments)`;
50
+ const prepareComponentInfoToSend = (info) => {
51
+ var _a;
52
+ return __spreadProps(__spreadValues({}, info), {
53
+ inputs: (_a = info.inputs) == null ? void 0 : _a.map((input) => Object.entries(input).reduce((acc, [key, value]) => __spreadProps(__spreadValues({}, acc), {
54
+ [key]: typeof value === "function" ? serializeFn(value) : value
55
+ }), {}))
64
56
  });
65
- }
57
+ };
66
58
  export {
67
59
  components,
68
60
  createRegisterComponentMessage,
@@ -1,4 +0,0 @@
1
- const fastClone = (obj) => JSON.parse(JSON.stringify(obj));
2
- export {
3
- fastClone
4
- };