Skip to content

Commit

Permalink
Merge branch 'SLB-415-gutenberg-editor-preview' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
colorfield committed Jun 20, 2024
2 parents dc26f6c + 6dba0f5 commit da1d7fb
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 1 deletion.
3 changes: 2 additions & 1 deletion apps/cms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
"Autosave preview": "./patches/fetch-entity.patch"
},
"drupal/gutenberg": {
"Gutenberg enabled hook": "https://www.drupal.org/files/issues/2024-05-07/gutenberg_enabled_hook_3445677-2.patch"
"Gutenberg enabled hook": "https://www.drupal.org/files/issues/2024-05-07/gutenberg_enabled_hook_3445677-2.patch",
"Remove !important from sidebar": "./patches/gutenberg_remove-important-sidebar.patch"
}
},
"patchLevel": {
Expand Down
16 changes: 16 additions & 0 deletions apps/cms/patches/gutenberg_remove-important-sidebar.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --git a/css/claro.css b/css/claro.css
index 720c3a8..9c5ad05 100644
--- a/css/claro.css
+++ b/css/claro.css
@@ -44,8 +44,8 @@

@media screen and (min-width: 85.375rem) {
.interface-complementary-area.edit-post-sidebar {
- width: 380px !important;
+ width: 380px;
}
}

-/*# sourceMappingURL=claro.css.map */
\ No newline at end of file
+/*# sourceMappingURL=claro.css.map */
28 changes: 28 additions & 0 deletions packages/drupal/gutenberg_blocks/css/edit.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,31 @@
.gutenberg__editor .edit-post-visual-editor__content-area a.no-underline {
text-decoration: none;
}

.gutenberg__editor .drupal-preview-sidebar {
min-width: 350px;
border-right: 1px solid #e0e0e0;
display: flex;
flex-direction: column;
flex-grow: 1;
}

.gutenberg__editor .drupal-preview-sidebar--iframe-wrapper iframe {
height: 100vh;
border: none;
}

.gutenberg__editor .drupal-preview-sidebar .interface-complementary-area-header,
.gutenberg__editor
.drupal-preview-sidebar
.interface-complementary-area.edit-post-sidebar {
width: 100%;
}

.gutenberg__editor .drupal-preview-sidebar--header {
margin: 10px;
}

.gutenberg__editor .drupal-preview-sidebar--header button {
margin-right: 5px;
}
19 changes: 19 additions & 0 deletions packages/drupal/gutenberg_blocks/gutenberg_blocks.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\silverback_gutenberg\Utils;
use Drupal\webform\Entity\Webform;
use Drupal\Core\Url;

/**
* Implements hook_library_info_alter().
Expand All @@ -26,6 +27,24 @@ function gutenberg_blocks_form_node_form_alter(&$form, FormStateInterface $form_
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
if (Utils::getGutenbergFields($node)) {
$form['actions']['preview_link'] = [
'#type' => 'link',
'#title' => t('Preview'),
'#url' => Url::fromRoute('<current>'),
'#attributes' => [
'class' => [
'button',
'button--primary',
],
],
];
// Add preview url for editor preview.
/** @var \Drupal\silverback_external_preview\ExternalPreviewLink $externalPreviewLink */
$externalPreviewLink = \Drupal::service('silverback_external_preview.external_preview_link');
$previewUrl = $externalPreviewLink->createPreviewUrlFromEntity($node)->toString();
$form['#attached']['drupalSettings']['preview'] = [
'previewUrl' => $previewUrl,
];

// Load all open webforms and populate them into drupalSettings.
$languageManager = \Drupal::languageManager();
Expand Down
1 change: 1 addition & 0 deletions packages/drupal/gutenberg_blocks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './customisations';
import './preview';
import './blocks/hero';
import './blocks/content';
import './blocks/heading';
Expand Down
126 changes: 126 additions & 0 deletions packages/drupal/gutenberg_blocks/src/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
(($, Drupal, once, drupalSettings) => {
Drupal.behaviors.gutenbergBlocksPreview = {
/* @ts-ignore */
attach: function (context) {
$(once('gutenbergBlocksPreview', '.gutenberg-full-editor', context)).each(
function () {
$('#edit-preview-link').on('click', function (e: Event) {
e.preventDefault();

const previewUrl = drupalSettings.preview.previewUrl;
const previewSizes = [
{
label: Drupal.t('Mobile'),
id: 'mobile',
width: 375,
height: 725,
iconPath:
'/modules/contrib/silverback_external_preview/icons/mobile.svg',
},
{
label: Drupal.t('Tablet'),
id: 'tablet',
width: 1024,
height: 824,
iconPath:
'/modules/contrib/silverback_external_preview/icons/tablet.svg',
},
{
label: Drupal.t('Laptop'),
id: 'laptop',
width: 1366,
height: 786,
iconPath:
'/modules/contrib/silverback_external_preview/icons/laptop.svg',
},
{
label: Drupal.t('Desktop'),
id: 'desktop',
width: 1920,
height: 1080,
iconPath:
'/modules/contrib/silverback_external_preview/icons/desktop.svg',
},
{
label: Drupal.t('Full'),
id: 'full',
width: -1,
height: -1,
iconPath:
'/modules/contrib/silverback_external_preview/icons/full.svg',
},
];

let previewButtons = '';
previewSizes.forEach((size) => {
previewButtons += `
<button
class="components-button is-primary drupal-preview-sidebar--button__${size.id}"
>${size.label}</button>`;
});

const previewSidebarMarkup = `
<div class="interface-interface-skeleton__secondary-sidebar drupal-preview-sidebar" role="region" aria-label="Drupal preview" tabindex="-1">
<div class="interface-complementary-area edit-post-sidebar">
<div class="components-panel__header interface-complementary-area-header__small">
<span class="interface-complementary-area-header__small-title">${Drupal.t('Preview')}</span>
<button type="button" class="components-button has-icon" aria-label="Close plugin">
<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false">
<path d="M12 13.06l3.712 3.713 1.061-1.06L13.061 12l3.712-3.712-1.06-1.06L12 10.938 8.288 7.227l-1.061 1.06L10.939 12l-3.712 3.712 1.06 1.061L12 13.061z"></path>
</svg>
</button>
</div>
<div class="components-panel__header interface-complementary-area-header" tabindex="-1">
<strong>${Drupal.t('Preview')}</strong>
<button type="button" aria-pressed="true" aria-expanded="true" class="components-button interface-complementary-area__pin-unpin-item is-pressed has-icon" aria-label="Unpin from toolbar">
<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false">
<path d="M12 13.06l3.712 3.713 1.061-1.06L13.061 12l3.712-3.712-1.06-1.06L12 10.938 8.288 7.227l-1.061 1.06L10.939 12l-3.712 3.712 1.06 1.061L12 13.061z"></path>
</svg>
</button>
</div>
<div class="drupal-preview-sidebar components-panel">
<div class="drupal-preview-sidebar--header">
${previewButtons}
</div>
<div class="drupal-preview-sidebar--iframe-wrapper">
<iframe width="100%" height="800px"
src="${previewUrl}">
</iframe>
</div>
</div>
</div>
</div>`;

const $previewSidebar = $('.drupal-preview-sidebar');
if (!$previewSidebar.length) {
$('.interface-interface-skeleton__body').prepend(
previewSidebarMarkup,
);
$('.drupal-preview-sidebar .components-panel__header button').on(
'click',
function () {
$('.drupal-preview-sidebar').remove();
},
);
previewSizes.forEach((size) => {
$('.drupal-preview-sidebar--button__' + size.id).on(
'click',
function () {
const windowFeatures =
size.width !== -1 && size.height !== -1
? `resizable,height=${size.height},width=${size.width}`
: `resizable,height=${screen.height},width=${screen.width}`;
window.open(previewUrl, 'preview', windowFeatures);
},
);
});
} else {
$('.drupal-preview-sidebar').remove();
}
});
},
);
},
};
/* @ts-ignore */
})(jQuery, Drupal, once, drupalSettings);

0 comments on commit da1d7fb

Please sign in to comment.