Ghost/ghost/announcement-bar/src/index.js
Elena Baidakova f69674ff9a
Fixed announcement bar preview (#16715)
refs TryGhost/Team#3122
- Fixed that preview takes data from user input before saving on
backend.

---

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 54d5b2d</samp>

This pull request adds the ability to preview the announcement bar in
the Ghost admin panel and the theme settings. It also adds a
confirmation dialog to discard or save unsaved changes before leaving
the announcement bar settings. It refactors some components and methods
to remove unnecessary or redundant calls to save the settings. It
modifies the `ghost_head` helper, the `theme-management` service, and
the `announcement-bar/src` files to support the preview feature.
2023-04-27 16:40:11 +04:00

60 lines
1.3 KiB
JavaScript

import React from 'react';
import ReactDOM from 'react-dom';
import {App} from './App';
const ROOT_DIV_ID = 'announcement-bar-root';
function addRootDiv() {
if (document.getElementById(ROOT_DIV_ID)) {
return;
}
const elem = document.createElement('div');
elem.id = ROOT_DIV_ID;
document.body.prepend(elem);
}
function getSiteData() {
/**
* @type {HTMLElement}
*/
const scriptTag = document.querySelector('script[data-announcement-bar]');
if (scriptTag) {
const apiUrl = scriptTag.dataset.apiUrl;
return {apiUrl, previewData: getPreviewData(scriptTag)};
}
return {};
}
function getPreviewData(scriptTag) {
if (scriptTag.dataset.preview) {
const announcement = scriptTag.dataset.announcement;
const announcementBackground = scriptTag.dataset.announcementBackground;
return {announcement, announcement_background: announcementBackground};
}
return null;
}
function setup() {
addRootDiv();
}
function init() {
const {apiUrl, previewData} = getSiteData();
setup();
ReactDOM.render(
<React.StrictMode>
<App
apiUrl={apiUrl}
previewData={previewData}
/>
</React.StrictMode>,
document.getElementById(ROOT_DIV_ID)
);
}
init();