Coder Social home page Coder Social logo

dagraphix / storyblok-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from storyblok/storyblok-js

0.0 0.0 0.0 854 KB

JavaScript SDK to connect Storyblok with your favourite framework that we don't have an official SDK for.

Home Page: http://storyblok.com/

Shell 1.43% JavaScript 41.87% TypeScript 51.49% HTML 5.21%

storyblok-js's Introduction

Storyblok Logo

@storyblok/js

The JavaScript SDK you need to interact with Storyblok API and enable the Real-time Visual Editing Experience.


Storyblok JS npm

Follow @Storyblok Follow @Storyblok

๐Ÿš€ Usage

If you are first-time user of the Storyblok, read the Getting Started guide to get a project ready in less than 5 minutes.

Installation

Install @storyblok/js:

npm install @storyblok/js
// yarn add @storyblok/js

From a CDN

Install the file from the CDN:

<script src="https://unpkg.com/@storyblok/js"></script>

Initialization

Register the plugin on your application and add the access token of your Storyblok space. You can also add the apiPlugin in case that you want to use the Storyblok API Client:

import { storyblokInit, apiPlugin } from "@storyblok/js";

const { storyblokApi } = storyblokInit({
  accessToken: "YOUR_ACCESS_TOKEN",
  use: [apiPlugin],
});

That's it! All the features are enabled for you: the Api Client for interacting with Storyblok CDN API, and Storyblok Bridge for real-time visual editing experience.

You can enable/disable some of these features if you don't need them, so you save some KB. Please read the "Features and API" section

Getting Started

@storyblok/js does three actions when you initialize it:

  • Provides a storyblokApi object in your app, which is an instance of storyblok-js-client.
  • Loads Storyblok Bridge for real-time visual updates.
  • Provides a storyblokEditable function to link editable components to the Storyblok Visual Editor.

1. Fetching Content

Inject storyblokApi:

import { storyblokInit, apiPlugin } from "@storyblok/js";

const { storyblokApi } = storyblokInit({
  accessToken: "YOUR_ACCESS_TOKEN",
  use: [apiPlugin],
});

const { data } = await storyblokApi.get("cdn/stories", { version: "draft" });

Note: if you don't use apiPlugin, you can use your prefered method or function to fetch your data.

2. Listen to Storyblok Visual Editor events

Use useStoryblokBridge 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:

import { storyblokInit, apiPlugin, useStoryblokBridge } from "@storyblok/js";

const { storyblokApi } = storyblokInit({
  accessToken: "YOUR_ACCESS_TOKEN",
  use: [apiPlugin],
});

const { data } = await storyblokApi.get("cdn/stories", { version: "draft" });

const story = data ? data.story : null;

useStoryblokBridge(story.id, (story) => (state.story = story));

You can pass Bridge options as a third parameter as well:

useStoryblokBridge(story.id, (story) => (state.story = story), {
  resolveRelations: ["Article.author"],
});

3. Link your components to Storyblok Visual Editor

To link your app and Storyblok components together will depend on the framework you are using. But, in the end, you must add the data-blok-c and data-blok-uid attributes, and the storyblok__outline class.

We provide you a storyblokEditable function to make that easier. As an example, you can check in @storyblok/vue how we use a v-editable directive for that:

import { storyblokEditable } from "@storyblok/js";

const vEditableDirective = {
  bind(el, binding) {
    if (binding.value) {
      const options = storyblokEditable(binding.value);
      el.setAttribute("data-blok-c", options["data-blok-c"]);
      el.setAttribute("data-blok-uid", options["data-blok-uid"]);
      el.classList.add("storyblok__outline");
    }
  },
};

At this point, you'll have your app connected to Storyblok with the real-time editing experience fully enabled.

Features and API

You can choose the features to use when you initialize the plugin. In that way, you can improve Web Performance by optimizing your page load and save some bytes.

Storyblok API

You can use an apiOptions object. This is passed down to the (storyblok-js-client config object](https://github.com/storyblok/storyblok-js-client#class-storyblok):

const { storyblokApi } = storyblokInit({
  accessToken: "YOUR_ACCESS_TOKEN",
  apiOptions: {
    // storyblok-js-client config object
    cache: { type: "memory" },
  },
  use: [apiPlugin],
});

If you prefer to use your own fetch method, just remove the apiPlugin and storyblok-js-client won't be added to your application.

storyblokInit({});

Storyblok Bridge

You can conditionally load it by using the bridge option. Very useful if you want to disable it in production:

const { storyblokApi } = storyblokInit({
  bridge: process.env.NODE_ENV !== "production",
});

If you don't use useStoryblokBridge, you have still access to the raw window.StoryblokBridge:

const sbBridge = new window.StoryblokBridge(options);

sbBridge.on(["input", "published", "change"], (event) => {
  // ...
});

๐Ÿ”— Related Links

  • Storyblok Technology Hub: Storyblok integrates with every framework so that you are free to choose the best fit for your project. We prepared the technology hub so that you can find selected beginner tutorials, videos, boilerplates, and even cheatsheets all in one place.
  • Getting Started: Get a project ready in less than 5 minutes.
  • Storyblok CLI: A simple CLI for scaffolding Storyblok projects and fieldtypes.

โ„น๏ธ More Resources

Support

Contributing

Please see our contributing guidelines and our code of conduct. This project use semantic-release for generate new versions by using commit messages and we use the Angular Convention to naming the commits. Check this question about it in semantic-release FAQ.

storyblok-js's People

Contributors

alexjoverm avatar dependabot[bot] avatar edwardz8 avatar fgiuliani avatar github-actions[bot] avatar imabp avatar manuelschroederdev avatar ts-storyblok avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.