mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 16:42:17 +03:00
ea531e8c19
refs https://github.com/TryGhost/Team/issues/1664 This allows us to have access to the currently logged in admin user as well as future access to show and hide comments via the Admin API.
69 lines
2.1 KiB
JavaScript
69 lines
2.1 KiB
JavaScript
import React from 'react';
|
|
import ReactDOM from 'react-dom';
|
|
import './index.css';
|
|
import App from './App';
|
|
const ROOT_DIV_ID = 'ghost-comments-root';
|
|
|
|
function addRootDiv() {
|
|
const scriptTag = document.querySelector('script[data-ghost-comments]');
|
|
|
|
// We need to inject the comment box at the same place as the script tag
|
|
if (scriptTag) {
|
|
const elem = document.createElement('div');
|
|
elem.id = ROOT_DIV_ID;
|
|
scriptTag.parentElement.insertBefore(elem, scriptTag);
|
|
} else if (process.env.NODE_ENV === 'development') {
|
|
const elem = document.createElement('div');
|
|
elem.id = ROOT_DIV_ID;
|
|
document.body.appendChild(elem);
|
|
} else {
|
|
// eslint-disable-next-line no-console
|
|
console.warn('Comment box location was not found: could not load comments box.');
|
|
}
|
|
}
|
|
|
|
function getSiteData() {
|
|
/**
|
|
* @type {HTMLElement}
|
|
*/
|
|
const scriptTag = document.querySelector('script[data-ghost-comments]');
|
|
if (scriptTag) {
|
|
const siteUrl = scriptTag.dataset.ghostComments;
|
|
const apiKey = scriptTag.dataset.key;
|
|
const apiUrl = scriptTag.dataset.api;
|
|
const adminUrl = scriptTag.dataset.admin;
|
|
const sentryDsn = scriptTag.dataset.sentryDsn;
|
|
const postId = scriptTag.dataset.postId;
|
|
return {siteUrl, apiKey, apiUrl, sentryDsn, postId, adminUrl};
|
|
}
|
|
return {};
|
|
}
|
|
|
|
function handleTokenUrl() {
|
|
const url = new URL(window.location.href);
|
|
if (url.searchParams.get('token')) {
|
|
url.searchParams.delete('token');
|
|
window.history.replaceState({}, document.title, url.href);
|
|
}
|
|
}
|
|
|
|
function setup({siteUrl}) {
|
|
addRootDiv();
|
|
handleTokenUrl();
|
|
}
|
|
|
|
function init() {
|
|
// const customSiteUrl = getSiteUrl();
|
|
const {siteUrl: customSiteUrl, sentryDsn, postId, adminUrl} = getSiteData();
|
|
const siteUrl = customSiteUrl || window.location.origin;
|
|
setup({siteUrl});
|
|
ReactDOM.render(
|
|
<React.StrictMode>
|
|
{<App adminUrl={adminUrl} siteUrl={siteUrl} customSiteUrl={customSiteUrl} sentryDsn={sentryDsn} postId={postId} />}
|
|
</React.StrictMode>,
|
|
document.getElementById(ROOT_DIV_ID)
|
|
);
|
|
}
|
|
|
|
init();
|