Skip to content

Commit

Permalink
Add storyblok v2 bridge support
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Villanueva committed Aug 5, 2022
1 parent a5619ea commit d338c7d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 40 deletions.
2 changes: 1 addition & 1 deletion view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var config = {
paths: {
storyblok: 'https://app.storyblok.com/f/storyblok-latest'
storyblok: 'https://app.storyblok.com/f/storyblok-v2-latest'
},
shim: {
storyblok: {
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/templates/hyva/script.phtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php /** @var MediaLounge\Storyblok\Block\Script $block */ ?>

<script
src="https://app.storyblok.com/f/storyblok-latest.js?t=<?= $block->escapeHtmlAttr($block->getApiKey()) ?>"
src="https://app.storyblok.com/f/storyblok-v2-latest.js"
defer
></script>
<script
Expand Down
4 changes: 1 addition & 3 deletions view/frontend/templates/script.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
<script type="text/x-magento-init">
{
"*": {
"MediaLounge_Storyblok/js/config": {
"apiKey": "<?= $block->escapeJs($block->getApiKey()) ?>"
}
"MediaLounge_Storyblok/js/config": {}
}
}
</script>
1 change: 0 additions & 1 deletion view/frontend/templates/story/debug.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

<div>
<?= $block->escapeHtml(__('Missing template <strong>%1</strong>', $block->getOriginalTemplate()), ['strong']) ?>
<pre><?= $block->escapeHtml(var_export($block->getData())) ?></pre>
</div>
28 changes: 8 additions & 20 deletions view/frontend/web/js/config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
define(['jquery', 'storyblok'], ($, storyblok) => {
'use strict';

return ({ apiKey }) => {
return () => {
let request = { abort: () => {} };

function enterEditMode() {
if (storyblok.inEditor) {
storyblok.enterEditmode();
}
}
const { StoryblokBridge } = window;
const storyblokInstance = new StoryblokBridge({
preventClicks: true
});

function isStoryblokComment(node) {
if (node.textContent.includes('#storyblok#')) {
Expand Down Expand Up @@ -38,17 +36,9 @@ define(['jquery', 'storyblok'], ($, storyblok) => {
return comments;
}

storyblok.init({
accessToken: apiKey
});

storyblok.on(['published', 'change'], () => window.location.reload());

storyblok.on(['input'], ({ story }) => {
const storyContentWithComments = storyblok.addComments(story.content, story.id);

story.content = storyContentWithComments;
storyblokInstance.on(['published', 'change'], () => window.location.reload());

storyblokInstance.on(['input'], ({ story }) => {
request.abort();

request = $.post({
Expand All @@ -70,10 +60,8 @@ define(['jquery', 'storyblok'], ($, storyblok) => {

$('body').trigger('contentUpdated');

enterEditMode();
storyblokInstance.enterEditmode();
});
});

storyblok.pingEditor(() => enterEditMode());
};
});
21 changes: 7 additions & 14 deletions view/frontend/web/js/hyva/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
(function () {
let controller = { abort: () => {} };

function enterEditMode() {
if (storyblok.inEditor) {
storyblok.enterEditmode();
}
}
const { StoryblokBridge } = window;
const storyblokInstance = new StoryblokBridge({
preventClicks: true
});

function isStoryblokComment(node) {
if (node.textContent.includes('#storyblok#')) {
Expand Down Expand Up @@ -35,16 +33,13 @@
return comments;
}

storyblok.on(['published', 'change'], () => window.location.reload());
storyblokInstance.on(['published', 'change'], () => window.location.reload());

storyblok.on(['input'], async ({ story }) => {
storyblokInstance.on(['input'], async ({ story }) => {
controller.abort();

controller = new AbortController();
const { signal } = controller;
const storyContentWithComments = storyblok.addComments(story.content, story.id);

story.content = storyContentWithComments;

try {
const request = await fetch('/storyblok/index/ajax', {
Expand All @@ -65,11 +60,9 @@
comments[blockId].comment.remove();
comments[blockId].element.outerHTML = response[blockId];

enterEditMode();
storyblokInstance.enterEditmode();
} catch (err) {
return;
}
});

storyblok.pingEditor(() => enterEditMode());
})();

0 comments on commit d338c7d

Please sign in to comment.