@storyblok/svelte 1.0.0 → 1.0.1
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/StoryblokComponent.svelte +23 -0
- package/index.js +60 -0
- package/package.json +4 -2
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { getComponent } from "./index";
|
|
3
|
+
export let blok;
|
|
4
|
+
let resolvedComponent;
|
|
5
|
+
|
|
6
|
+
if (blok) {
|
|
7
|
+
const component = getComponent(blok.component);
|
|
8
|
+
if (component) {
|
|
9
|
+
const loadComponent = async () => {
|
|
10
|
+
const { default: loadedComponent } = await component();
|
|
11
|
+
resolvedComponent = loadedComponent;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const isSvelteComponent = component.name.startsWith("Proxy");
|
|
15
|
+
if (isSvelteComponent) resolvedComponent = component;
|
|
16
|
+
else loadComponent();
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
console.error("Please provide a 'blok' property to the StoryblokComponent");
|
|
20
|
+
}
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<svelte:component this={resolvedComponent} {blok} {...$$restProps} />
|
package/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {
|
|
2
|
+
storyblokEditable as sbEdit,
|
|
3
|
+
storyblokInit as sbInit,
|
|
4
|
+
} from "@storyblok/js";
|
|
5
|
+
export { useStoryblokBridge, apiPlugin } from "@storyblok/js";
|
|
6
|
+
|
|
7
|
+
export const storyblokEditable = (node, value) => {
|
|
8
|
+
const updateDom = (value) => {
|
|
9
|
+
const options = sbEdit(value);
|
|
10
|
+
if (options["data-blok-c"]) {
|
|
11
|
+
node.setAttribute("data-blok-c", options["data-blok-c"]);
|
|
12
|
+
node.setAttribute("data-blok-uid", options["data-blok-uid"]);
|
|
13
|
+
node.classList.add("storyblok__outline");
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
updateDom(value); // when is mounted
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
update(newValue) {
|
|
21
|
+
// when value changes
|
|
22
|
+
updateDom(newValue);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let storyblokApiInstance = null;
|
|
28
|
+
export const useStoryblokApi = () => {
|
|
29
|
+
if (!storyblokApiInstance) {
|
|
30
|
+
console.log(
|
|
31
|
+
`You can't use useStoryblokApi if you're not loading apiPlugin.`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
return storyblokApiInstance;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
let componentsMap = null;
|
|
38
|
+
export const storyblokInit = (options) => {
|
|
39
|
+
const { storyblokApi } = sbInit(options);
|
|
40
|
+
storyblokApiInstance = storyblokApi;
|
|
41
|
+
componentsMap = options.components || {};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const getComponent = (componentName) => {
|
|
45
|
+
const component = componentsMap[componentName];
|
|
46
|
+
if (!component) {
|
|
47
|
+
console.error(`You didn't load the ${componentName} component. Please load it in storyblokInit. For example:
|
|
48
|
+
storyblokInit({
|
|
49
|
+
accessToken: "<your-token>",
|
|
50
|
+
components: {
|
|
51
|
+
"teaser": Teaser
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return component;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export { default as StoryblokComponent } from "./StoryblokComponent.svelte";
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storyblok/svelte",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Storyblok SDK to connect Storyblok with Svelte",
|
|
5
5
|
"main": "./dist/storyblok-svelte.js",
|
|
6
6
|
"module": "./dist/storyblok-svelte.mjs",
|
|
7
7
|
"svelte": "index.js",
|
|
8
8
|
"files": [
|
|
9
|
-
"dist"
|
|
9
|
+
"dist",
|
|
10
|
+
"index.js",
|
|
11
|
+
"StoryblokComponent.svelte"
|
|
10
12
|
],
|
|
11
13
|
"exports": {
|
|
12
14
|
".": {
|