@storyblok/js 3.1.8 → 3.2.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/LICENSE +21 -0
- package/README.md +35 -36
- package/dist/{types/modules/api.d.ts → api.d.ts} +1 -1
- package/dist/editable.d.ts +9 -0
- package/dist/index.d.ts +18 -0
- package/dist/{storyblok-js.mjs → storyblok-js.es.js} +412 -358
- package/dist/storyblok-js.umd.js +30 -0
- package/dist/types/index.d.ts +64 -13
- package/package.json +43 -59
- package/dist/storyblok-js.js +0 -24
- package/dist/types/modules/editable.d.ts +0 -9
- package/dist/types/types.d.ts +0 -63
- /package/dist/{types/modules/bridge.d.ts → bridge.d.ts} +0 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Storyblok GmbH
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
<div align="center">
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
<a href="https://www.storyblok.com?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js" align="center">
|
3
|
+
<img src="https://a.storyblok.com/f/88751/1776x360/296788fb19/sb-js.png" alt="Storyblok Logo">
|
4
|
+
</a>
|
5
|
+
<h1 align="center">@storyblok/js</h1>
|
6
6
|
<p align="center">
|
7
7
|
The JavaScript SDK you need to interact with <a href="http://www.storyblok.com?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js" target="_blank">Storyblok API</a> and enable the <a href="https://www.storyblok.com/docs/guide/essentials/visual-editor?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js" target="_blank">Real-time Visual Editing Experience</a>.
|
8
8
|
</p>
|
@@ -57,10 +57,10 @@ Install the file from the CDN:
|
|
57
57
|
Register the plugin on your application and add the [access token](https://www.storyblok.com/docs/api/content-delivery#topics/authentication?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js) of your Storyblok space. You can also add the `apiPlugin` in case that you want to use the Storyblok API Client:
|
58
58
|
|
59
59
|
```js
|
60
|
-
import {
|
60
|
+
import { apiPlugin, storyblokInit } from '@storyblok/js';
|
61
61
|
|
62
62
|
const { storyblokApi } = storyblokInit({
|
63
|
-
accessToken:
|
63
|
+
accessToken: 'YOUR_ACCESS_TOKEN',
|
64
64
|
use: [apiPlugin],
|
65
65
|
});
|
66
66
|
```
|
@@ -82,13 +82,13 @@ Possible values:
|
|
82
82
|
Full example for a space created in the US:
|
83
83
|
|
84
84
|
```js
|
85
|
-
import {
|
85
|
+
import { apiPlugin, storyblokInit } from '@storyblok/js';
|
86
86
|
|
87
87
|
const { storyblokApi } = storyblokInit({
|
88
|
-
accessToken:
|
88
|
+
accessToken: 'YOUR_ACCESS_TOKEN',
|
89
89
|
use: [apiPlugin],
|
90
90
|
apiOptions: {
|
91
|
-
region:
|
91
|
+
region: 'us',
|
92
92
|
},
|
93
93
|
});
|
94
94
|
```
|
@@ -108,14 +108,14 @@ const { storyblokApi } = storyblokInit({
|
|
108
108
|
Inject `storyblokApi`:
|
109
109
|
|
110
110
|
```js
|
111
|
-
import {
|
111
|
+
import { apiPlugin, storyblokInit } from '@storyblok/js';
|
112
112
|
|
113
113
|
const { storyblokApi } = storyblokInit({
|
114
|
-
accessToken:
|
114
|
+
accessToken: 'YOUR_ACCESS_TOKEN',
|
115
115
|
use: [apiPlugin],
|
116
116
|
});
|
117
117
|
|
118
|
-
const { data } = await storyblokApi.get(
|
118
|
+
const { data } = await storyblokApi.get('cdn/stories', { version: 'draft' });
|
119
119
|
```
|
120
120
|
|
121
121
|
> Note: if you don't use `apiPlugin`, you can use your prefered method or function to fetch your data.
|
@@ -125,26 +125,26 @@ const { data } = await storyblokApi.get("cdn/stories", { version: "draft" });
|
|
125
125
|
Use `useStoryblokBridge` or `registerStoryblokBridge` to get the new story every time is triggered a `change` event from the Visual Editor. You need to pass the story id as first param, and a callback function as second param to update the new story:
|
126
126
|
|
127
127
|
```js
|
128
|
-
import {
|
128
|
+
import { apiPlugin, storyblokInit, useStoryblokBridge } from '@storyblok/js';
|
129
129
|
|
130
130
|
const { storyblokApi } = storyblokInit({
|
131
|
-
accessToken:
|
131
|
+
accessToken: 'YOUR_ACCESS_TOKEN',
|
132
132
|
use: [apiPlugin],
|
133
133
|
});
|
134
134
|
|
135
|
-
const { data } = await storyblokApi.get(
|
135
|
+
const { data } = await storyblokApi.get('cdn/stories', { version: 'draft' });
|
136
136
|
|
137
137
|
const story = data ? data.story : null;
|
138
138
|
|
139
|
-
useStoryblokBridge(story.id,
|
139
|
+
useStoryblokBridge(story.id, story => (state.story = story));
|
140
140
|
```
|
141
141
|
|
142
142
|
You can pass [Bridge options](https://www.storyblok.com/docs/Guides/storyblok-latest-js?utm_source=github.com&utm_medium=readme&utm_campaign=storyblok-js) as a third parameter as well:
|
143
143
|
|
144
144
|
```js
|
145
|
-
useStoryblokBridge(story.id,
|
146
|
-
resolveRelations: [
|
147
|
-
resolveLinks:
|
145
|
+
useStoryblokBridge(story.id, story => (state.story = story), {
|
146
|
+
resolveRelations: ['Article.author'],
|
147
|
+
resolveLinks: 'url'
|
148
148
|
});
|
149
149
|
```
|
150
150
|
|
@@ -155,15 +155,15 @@ To link your app and Storyblok components together will depend on the framework
|
|
155
155
|
We provide you a `storyblokEditable` function to make that easier. As an example, you can check in [@storyblok/vue](https://github.com/storyblok/storyblok-vue/blob/master/lib/index.js#L7-L9) how we use a `v-editable` directive for that:
|
156
156
|
|
157
157
|
```js
|
158
|
-
import { storyblokEditable } from
|
158
|
+
import { storyblokEditable } from '@storyblok/js';
|
159
159
|
|
160
160
|
const vEditableDirective = {
|
161
161
|
bind(el, binding) {
|
162
162
|
if (binding.value) {
|
163
163
|
const options = storyblokEditable(binding.value);
|
164
|
-
el.setAttribute(
|
165
|
-
el.setAttribute(
|
166
|
-
el.classList.add(
|
164
|
+
el.setAttribute('data-blok-c', options['data-blok-c']);
|
165
|
+
el.setAttribute('data-blok-uid', options['data-blok-uid']);
|
166
|
+
el.classList.add('storyblok__outline');
|
167
167
|
}
|
168
168
|
},
|
169
169
|
};
|
@@ -181,10 +181,10 @@ You can use an `apiOptions` object. This is passed down to the (storyblok-js-cli
|
|
181
181
|
|
182
182
|
```js
|
183
183
|
const { storyblokApi } = storyblokInit({
|
184
|
-
accessToken:
|
184
|
+
accessToken: 'YOUR_ACCESS_TOKEN',
|
185
185
|
apiOptions: {
|
186
186
|
// storyblok-js-client config object
|
187
|
-
cache: { type:
|
187
|
+
cache: { type: 'memory' },
|
188
188
|
},
|
189
189
|
use: [apiPlugin],
|
190
190
|
});
|
@@ -202,7 +202,7 @@ You can conditionally load it by using the `bridge` option. Very useful if you w
|
|
202
202
|
|
203
203
|
```js
|
204
204
|
const { storyblokApi } = storyblokInit({
|
205
|
-
bridge: process.env.NODE_ENV !==
|
205
|
+
bridge: process.env.NODE_ENV !== 'production',
|
206
206
|
});
|
207
207
|
```
|
208
208
|
|
@@ -211,7 +211,7 @@ If you don't use `useStoryblokBridge` or `registerStoryblokBridge`, you have sti
|
|
211
211
|
```js
|
212
212
|
const sbBridge = new window.StoryblokBridge(options);
|
213
213
|
|
214
|
-
sbBridge.on([
|
214
|
+
sbBridge.on(['input', 'published', 'change'], (event) => {
|
215
215
|
// ...
|
216
216
|
});
|
217
217
|
```
|
@@ -221,7 +221,7 @@ sbBridge.on(["input", "published", "change"], (event) => {
|
|
221
221
|
You can easily render rich text by using the `renderRichText` function that comes with `@storyblok/js`:
|
222
222
|
|
223
223
|
```js
|
224
|
-
import { renderRichText } from
|
224
|
+
import { renderRichText } from '@storyblok/js';
|
225
225
|
|
226
226
|
const renderedRichText = renderRichText(blok.richtext);
|
227
227
|
```
|
@@ -229,23 +229,23 @@ const renderedRichText = renderRichText(blok.richtext);
|
|
229
229
|
You can set a **custom Schema and component resolver globally** at init time by using the `richText` init option:
|
230
230
|
|
231
231
|
```js
|
232
|
-
import { RichTextSchema, storyblokInit } from
|
233
|
-
import cloneDeep from
|
232
|
+
import { RichTextSchema, storyblokInit } from '@storyblok/js';
|
233
|
+
import cloneDeep from 'clone-deep';
|
234
234
|
|
235
235
|
const mySchema = cloneDeep(RichTextSchema); // you can make a copy of the default RichTextSchema
|
236
236
|
// ... and edit the nodes and marks, or add your own.
|
237
237
|
// Check the base RichTextSchema source here https://github.com/storyblok/storyblok-js-client/blob/master/source/schema.js
|
238
238
|
|
239
239
|
storyblokInit({
|
240
|
-
accessToken:
|
240
|
+
accessToken: '<your-token>',
|
241
241
|
richText: {
|
242
242
|
schema: mySchema,
|
243
243
|
resolver: (component, blok) => {
|
244
244
|
switch (component) {
|
245
|
-
case
|
245
|
+
case 'my-custom-component':
|
246
246
|
return `<div class="my-component-class">${blok.text}</div>`;
|
247
247
|
default:
|
248
|
-
return
|
248
|
+
return 'Resolver not defined';
|
249
249
|
}
|
250
250
|
},
|
251
251
|
},
|
@@ -255,15 +255,14 @@ storyblokInit({
|
|
255
255
|
You can also set a **custom Schema and component resolver only once** by passing the options as the second parameter to `renderRichText` function:
|
256
256
|
|
257
257
|
```js
|
258
|
-
import { renderRichText } from
|
258
|
+
import { renderRichText } from '@storyblok/js';
|
259
259
|
|
260
260
|
renderRichText(blok.richTextField, {
|
261
261
|
schema: mySchema,
|
262
262
|
resolver: (component, blok) => {
|
263
263
|
switch (component) {
|
264
|
-
case
|
264
|
+
case 'my-custom-component':
|
265
265
|
return `<div class="my-component-class">${blok.text}</div>`;
|
266
|
-
break;
|
267
266
|
default:
|
268
267
|
return `Component ${component} not found`;
|
269
268
|
}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
import { ISbRichtext, ISbStoryData, SbInitResult, SbRichTextOptions, SbSDKOptions, StoryblokBridgeConfigV2, StoryblokComponentType } from './types';
|
2
|
+
import { RichtextResolver } from 'storyblok-js-client';
|
3
|
+
export interface StoryblokBridgeEvent {
|
4
|
+
action: string;
|
5
|
+
storyId: number;
|
6
|
+
story: ISbStoryData;
|
7
|
+
}
|
8
|
+
export declare const useStoryblokBridge: <T extends StoryblokComponentType<string> = any>(id: number, cb: (newStory: ISbStoryData<T>) => void, options?: StoryblokBridgeConfigV2) => void;
|
9
|
+
export declare const storyblokInit: (pluginOptions?: SbSDKOptions) => SbInitResult;
|
10
|
+
export declare const isRichTextEmpty: (data?: ISbRichtext) => boolean;
|
11
|
+
export declare const renderRichText: (data?: ISbRichtext, options?: SbRichTextOptions, resolverInstance?: RichtextResolver) => string | undefined;
|
12
|
+
export declare const loadStoryblokBridge: () => Promise<unknown>;
|
13
|
+
export { useStoryblokBridge as registerStoryblokBridge };
|
14
|
+
export { default as apiPlugin } from './api';
|
15
|
+
export { default as storyblokEditable } from './editable';
|
16
|
+
export * from './types';
|
17
|
+
export { BlockTypes, MarkTypes, richTextResolver, type StoryblokRichTextDocumentNode, type StoryblokRichTextImageOptimizationOptions, type StoryblokRichTextNode, type StoryblokRichTextNodeResolver, type StoryblokRichTextNodeTypes, type StoryblokRichTextOptions, type StoryblokRichTextResolvers, TextTypes, } from '@storyblok/richtext';
|
18
|
+
export { RichtextResolver as RichTextResolver, RichtextSchema as RichTextSchema, } from 'storyblok-js-client';
|