docbrown/app/javascript/utilities/codeFullscreenModeSwitcher.js
Mac Siri afa73e06e3
Replace Webpacker with Esbuild (#20470)
* Migrate to esbuild WIP

* Add exclude

* Remove redundant file

* Move file

* Move to javascript_include_tag

* Lint fix

* WIP

* WIP

* Add watch mode to esbuild WIP

* Get jest working

* Remove babel

* Revert "Remove babel"

This reverts commit 6da35260aa19d6f97f586deb66c0ecaf48433b73.

* More WIP

* Got image to load

* WIP

* Resolve audit

* Lint fix

* WIP

* Fix jest spec

* [CI] Remove asset-restore for test build stage

* Production compliant

* Temp disable sourcemap

* Update glob

* Add esbuild helper to stimulus

* Import fragment

* Temp disable coverage to see failing tests

* Fix broken spec

* Address lint

* Set proper es6 target

* Use esbuild for everything

* wait what

* Revert "Set proper es6 target"

This reverts commit 98f5278093421baa8ffe2ca580845b01c1a1eadf.

* Revert "Use esbuild for everything"

This reverts commit 0ac46738f07ffcb6af095ccb1ffa5e439b7fefa3.

* Replace uglifier with terser

* New compiled assets version

* Remvoe honeybadger-io/webpack

* Remove cypress coverage checks for now

* Update jsconfig.json

* Update docker-compose

* Remove public/packs-test from ci cache
2024-01-25 20:29:21 +00:00

102 lines
2.5 KiB
JavaScript

let isFullScreenModeCodeOn = false;
let screenScroll = 0;
const { body } = document;
export function getFullScreenModeStatus() {
return isFullScreenModeCodeOn;
}
function setAfterFullScreenScrollPosition() {
window.scrollTo(0, screenScroll);
}
function getBeforeFullScreenScrollPosition() {
screenScroll = window.scrollY;
}
export function onPressEscape(event) {
if (event.key == 'Escape') {
fullScreenModeControl(event);
}
}
function listenToKeyboardForEscape(listen) {
if (listen) {
document.body.addEventListener('keyup', onPressEscape);
} else {
document.body.removeEventListener('keyup', onPressEscape);
}
}
export function onPopstate() {
fullScreenModeControl();
}
function listenToWindowForPopstate(listen) {
if (listen) {
window.addEventListener('popstate', onPopstate);
} else {
window.removeEventListener('popstate', onPopstate);
}
}
function toggleOverflowForDocument(overflow) {
if (overflow) {
body.style.overflow = 'hidden';
} else {
body.style.overflow = '';
}
}
export function addFullScreenModeControl(elements) {
if (elements) {
for (const element of elements) {
element.addEventListener('click', fullScreenModeControl);
}
}
}
function removeFullScreenModeControl(elements) {
if (elements) {
for (const element of elements) {
element.removeEventListener('click', fullScreenModeControl);
}
}
}
function fullScreenModeControl(event) {
const fullScreenWindow =
document.getElementsByClassName('js-fullscreen-code')[0];
const codeBlock = event?.currentTarget.closest('.js-code-highlight')
? event.currentTarget.closest('.js-code-highlight').cloneNode(true)
: null;
const codeBlockControls = codeBlock
? codeBlock.getElementsByClassName('js-fullscreen-code-action')
: null;
if (isFullScreenModeCodeOn) {
toggleOverflowForDocument(false);
setAfterFullScreenScrollPosition();
listenToKeyboardForEscape(false);
listenToWindowForPopstate(false);
removeFullScreenModeControl(codeBlockControls);
fullScreenWindow.classList.remove('is-open');
fullScreenWindow.removeChild(fullScreenWindow.childNodes[0]);
isFullScreenModeCodeOn = false;
} else {
toggleOverflowForDocument(true);
getBeforeFullScreenScrollPosition();
listenToKeyboardForEscape(true);
listenToWindowForPopstate(true);
codeBlock.classList.add('is-fullscreen');
fullScreenWindow.appendChild(codeBlock);
fullScreenWindow.classList.add('is-open');
addFullScreenModeControl(codeBlockControls);
isFullScreenModeCodeOn = true;
}
}