@storyblok/astro 5.0.2 → 5.1.0-alpha.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.
@@ -1,9 +1,9 @@
1
1
  import type { AstroGlobal, AstroIntegration } from "astro";
2
- import type { ISbConfig, ISbRichtext, ISbStoriesParams, ISbStoryData, SbRichTextOptions, StoryblokBridgeConfigV2, StoryblokClient } from "./types";
2
+ import type { ISbConfig, ISbRichtext, ISbStoryData, SbRichTextOptions, StoryblokBridgeConfigV2, StoryblokClient } from "./types";
3
3
  export { handleStoryblokMessage } from "./live-preview/handleStoryblokMessage";
4
4
  export { storyblokEditable, loadStoryblokBridge, RichTextResolver, RichTextSchema, } from "@storyblok/js";
5
5
  export declare function useStoryblokApi(): StoryblokClient;
6
- export declare function useStoryblok(slug: string, apiOptions: ISbStoriesParams, bridgeOptions: StoryblokBridgeConfigV2, Astro: AstroGlobal): Promise<ISbStoryData<import("@storyblok/js").StoryblokComponentType<string> & {
6
+ export declare function getLiveStory(Astro: AstroGlobal): Promise<ISbStoryData<import("@storyblok/js").StoryblokComponentType<string> & {
7
7
  [index: string]: any;
8
8
  }>>;
9
9
  export declare function renderRichText(data?: ISbRichtext, options?: SbRichTextOptions): string;
@@ -55,5 +55,5 @@ export type IntegrationOptions = {
55
55
  */
56
56
  livePreview?: boolean;
57
57
  };
58
- export default function storyblokIntegration(options: IntegrationOptions): AstroIntegration;
58
+ export default function storyblokIntegration({ useCustomApi, bridge, componentsDir, enableFallbackComponent, livePreview, ...restOptions }: IntegrationOptions): AstroIntegration;
59
59
  export * from "./types";
@@ -0,0 +1,2 @@
1
+ import type { StoryblokBridgeConfigV2 } from "../types";
2
+ export declare function initStoryblokBridge(config: boolean | StoryblokBridgeConfigV2): string;
@@ -22,6 +22,8 @@ export async function handleStoryblokMessage(event: {
22
22
  // Get current focused element in Storyblok
23
23
  const focusedElem = document.querySelector('[data-blok-focused="true"]');
24
24
  updateDOMWithNewBody(currentBody, newBody, focusedElem);
25
+ // Dispatch a custom event after the body update
26
+ document.dispatchEvent(new Event("storyblok-live-preview-updated"));
25
27
  };
26
28
  const debounceDelay = 500; // Adjust the delay as needed
27
29
  clearTimeout(timeout);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storyblok/astro",
3
- "version": "5.0.2",
3
+ "version": "5.1.0-alpha.1",
4
4
  "description": "Official Astro integration for the Storyblok Headless CMS",
5
5
  "main": "./dist/storyblok-astro.js",
6
6
  "module": "./dist/storyblok-astro.mjs",
@@ -0,0 +1,11 @@
1
+ import type { StoryblokBridgeConfigV2 } from "../types";
2
+
3
+ export function initStoryblokBridge(
4
+ config: boolean | StoryblokBridgeConfigV2
5
+ ): string {
6
+ if (typeof config === "object") {
7
+ const bridgeConfig = JSON.stringify(config);
8
+ return `const storyblokInstance = new StoryblokBridge(${bridgeConfig});`;
9
+ }
10
+ return "const storyblokInstance = new StoryblokBridge();";
11
+ }
@@ -1,3 +0,0 @@
1
- import type { StoryblokBridgeConfigV2 } from "@storyblok/js";
2
- import type { RawCode } from "../vite-plugins/vite-plugin-storyblok-bridge";
3
- export declare function generateFinalBridgeObject(rawCode: RawCode): StoryblokBridgeConfigV2;
@@ -1,7 +0,0 @@
1
- import type { RawCodeItemOptions } from "../vite-plugins/vite-plugin-storyblok-bridge";
2
- import type { Rollup } from "vite";
3
- /**
4
- * Parses through the Abstract Syntax Tree (AST) code to locate the 'useStoryblok' function and its properties.
5
- * This functionality is crucial for generating a virtual module that can be utilized during the initialization of the Storyblok bridge.
6
- */
7
- export declare function parseAstRawCode(astCode: Rollup.ProgramNode): RawCodeItemOptions;
@@ -1,12 +0,0 @@
1
- import type { ISbStoriesParams, StoryblokBridgeConfigV2 } from "@storyblok/js";
2
- import type { Plugin } from "vite";
3
- export interface RawCodeItem {
4
- url: string;
5
- options?: RawCodeItemOptions;
6
- }
7
- export type RawCode = RawCodeItem[];
8
- export interface RawCodeItemOptions {
9
- apiOptions?: ISbStoriesParams;
10
- bridgeOptions?: StoryblokBridgeConfigV2;
11
- }
12
- export declare function vitePluginStoryblokBridge(experimentalLivePreview: boolean, output: string): Plugin;
@@ -1,33 +0,0 @@
1
- import type { StoryblokBridgeConfigV2 } from "@storyblok/js";
2
- import type { RawCode } from "../vite-plugins/vite-plugin-storyblok-bridge";
3
-
4
- export function generateFinalBridgeObject(rawCode: RawCode) {
5
- let mergedOptions: StoryblokBridgeConfigV2 = {
6
- resolveRelations: [],
7
- };
8
-
9
- function addToResolveRelations(resolveRelations?: string[] | string) {
10
- if (resolveRelations && Array.isArray(mergedOptions.resolveRelations)) {
11
- mergedOptions.resolveRelations.push(
12
- ...(Array.isArray(resolveRelations)
13
- ? resolveRelations
14
- : [resolveRelations])
15
- );
16
- }
17
- }
18
-
19
- for (const item of rawCode) {
20
- if (item.options) {
21
- const { apiOptions, bridgeOptions } = item.options;
22
- addToResolveRelations(apiOptions?.resolve_relations);
23
- if (bridgeOptions) {
24
- const { resolveRelations, ...rest } = bridgeOptions;
25
- addToResolveRelations(resolveRelations);
26
- Object.assign(mergedOptions, rest);
27
- }
28
- }
29
- }
30
- mergedOptions.resolveRelations = [...new Set(mergedOptions.resolveRelations)];
31
-
32
- return mergedOptions;
33
- }
@@ -1,66 +0,0 @@
1
- import mergeWith from "lodash.mergewith";
2
- import type { ISbStoriesParams, StoryblokBridgeConfigV2 } from "@storyblok/js";
3
- import type { RawCodeItemOptions } from "../vite-plugins/vite-plugin-storyblok-bridge";
4
- import type { Rollup } from "vite";
5
- import type { SpreadElement, Property } from "estree";
6
-
7
- /**
8
- * Parses through the Abstract Syntax Tree (AST) code to locate the 'useStoryblok' function and its properties.
9
- * This functionality is crucial for generating a virtual module that can be utilized during the initialization of the Storyblok bridge.
10
- */
11
-
12
- export function parseAstRawCode(astCode: Rollup.ProgramNode) {
13
- let obj: RawCodeItemOptions = {};
14
-
15
- function customizer(_: any, srcValue: any) {
16
- if (
17
- srcValue?.type === "AwaitExpression" &&
18
- srcValue.argument.type === "CallExpression" &&
19
- srcValue.argument.callee.type === "Identifier" &&
20
- srcValue.argument.callee.name === "useStoryblok"
21
- ) {
22
- const props = srcValue.argument.arguments;
23
- if (props && props[1].type === "ObjectExpression") {
24
- const apiOptions = getAstPropToObj(props[1].properties);
25
- obj = {
26
- ...obj,
27
- apiOptions,
28
- };
29
- }
30
- if (props && props[2].type === "ObjectExpression") {
31
- const bridgeOptions = getAstPropToObj(props[2].properties);
32
- obj = {
33
- ...obj,
34
- bridgeOptions,
35
- };
36
- }
37
- }
38
- }
39
-
40
- mergeWith({}, astCode, customizer);
41
- return obj;
42
- }
43
-
44
- function getAstPropToObj(properties: (SpreadElement | Property)[]) {
45
- const option: ISbStoriesParams | StoryblokBridgeConfigV2 = {};
46
-
47
- return properties.reduce((options, property) => {
48
- if (property.type !== "Property") return options;
49
- const { key, value } = property;
50
- const { type } = value;
51
- if (key.type !== "Identifier") return options;
52
-
53
- if (type === "Literal") {
54
- options[key.name] = value.value;
55
- } else if (type === "ArrayExpression") {
56
- const arrayValues = value.elements.reduce((acc, element) => {
57
- if (element.type === "Literal" && element.value) {
58
- return [...acc, element.value];
59
- }
60
- return acc;
61
- }, []);
62
- options[key.name] = arrayValues;
63
- }
64
- return options;
65
- }, option);
66
- }