mirror of
https://github.com/TryGhost/Ghost.git
synced 2023-12-13 21:00:40 +01:00
f1b51729fc
refs https://github.com/TryGhost/Product/issues/3504 - App component now uses React hooks intead of React class component - App is now written in TypeScript - All JavaScript is now removed from the Comments-UI project - Removed `PopupNotification` because these were never displayed - Removed `action` from AppContext (never used) - Moved options parsing out of `index.ts` into a separate utility file, similar to the signup-form - Improved reliability of some editor tests by always waiting for the editor to be focused (was not always the case) + added an utility method for this
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import App from './App';
|
|
import React from 'react';
|
|
import ReactDOM from 'react-dom';
|
|
import {ROOT_DIV_ID} from './utils/constants';
|
|
|
|
function getScriptTag(): HTMLElement {
|
|
let scriptTag = document.currentScript as HTMLElement | null;
|
|
|
|
if (!scriptTag && import.meta.env.DEV) {
|
|
// In development mode, use any script tag (because in ESM mode, document.currentScript is not set)
|
|
scriptTag = document.querySelector('script[data-ghost-comments]');
|
|
}
|
|
|
|
if (!scriptTag) {
|
|
throw new Error('[Comments-UI] Cannot find current script tag');
|
|
}
|
|
|
|
return scriptTag;
|
|
}
|
|
|
|
/**
|
|
* Returns a div to mount the React application into, creating it if necessary
|
|
*/
|
|
function getRootDiv(scriptTag: HTMLElement) {
|
|
if (scriptTag.previousElementSibling && scriptTag.previousElementSibling.id === ROOT_DIV_ID) {
|
|
return scriptTag.previousElementSibling;
|
|
}
|
|
|
|
if (!scriptTag.parentElement) {
|
|
throw new Error('[Comments-UI] Script tag does not have a parent element');
|
|
}
|
|
|
|
const elem = document.createElement('div');
|
|
elem.id = ROOT_DIV_ID;
|
|
scriptTag.parentElement.insertBefore(elem, scriptTag);
|
|
return elem;
|
|
}
|
|
|
|
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 init() {
|
|
const scriptTag = getScriptTag();
|
|
const root = getRootDiv(scriptTag);
|
|
|
|
try {
|
|
handleTokenUrl();
|
|
|
|
ReactDOM.render(
|
|
<React.StrictMode>
|
|
{<App scriptTag={scriptTag} />}
|
|
</React.StrictMode>,
|
|
root
|
|
);
|
|
} catch (e) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(e);
|
|
}
|
|
}
|
|
|
|
init();
|