Skip to content

Commit

Permalink
remove inject css vite plugin to manually insert the content of aladi…
Browse files Browse the repository at this point in the history
…n.css in js inside the aladin lite div container
  • Loading branch information
bmatthieu3 committed Sep 12, 2024
1 parent 914f296 commit cbfe278
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [test] Add support of playwright. Instructions in the readme for running the test matching snapshots [PR #176]
* [fixed] Order of overlays in the stack now matches the addMOC/addCatalog/addOverlay calls ordering
* [doc] Expose the API of Coo class
* [fix] Insert aladin css inside the aladin lite so that it should be compliant with the use of shadow DOMs [cds-astro/ipyaladin#113], [marimo-team/marimo#2106]

## 3.5.0-beta

Expand Down
39 changes: 39 additions & 0 deletions examples/al-in-shadow-dom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!doctype html>
<html>
<head>
</head>
<body>

<div class="shadow-host"></div>

<script type="text/javascript" src="./../dist/aladin.umd.cjs" charset="utf-8"></script>
<script type="text/javascript">
const shadowEl = document.querySelector(".shadow-host");
const shadow = shadowEl.attachShadow({mode: 'open'});

let aladinLiteEl = document.createElement('div');
aladinLiteEl.id = "aladin-lite-div"
aladinLiteEl.style = "width: 768px; height: 512px";
shadow.appendChild(aladinLiteEl)
console.log(aladinLiteEl.getRootNode() instanceof ShadowRoot)
A.init.then(() => {
let aladin = A.aladin(
aladinLiteEl,
{
survey: 'P/allWISE/color', // set initial image survey
projection: 'AIT', // set a projection
fov: 1.5, // initial field of view in degrees
target: 'orion', // initial target
}
);

//aladin.removeHiPSFromFavorites('CDS/P/allWISE/color')
});
</script>
<style>
.aladin-cat-browser-box {
width: 600px;
}
</style>
</body>
</html>
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"@playwright/test": "^1.47.0",
"jsdoc": "^4.0.2",
"vite": "^4.3.8",
"vite-plugin-css-injected-by-js": "^3.1.1",
"vite-plugin-glsl": "^1.1.2",
"vite-plugin-top-level-await": "^1.4.1",
"vite-plugin-wasm": "^3.2.2",
Expand Down
10 changes: 8 additions & 2 deletions src/js/A.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ import { Sesame } from "./Sesame.js";
import init, * as module from './../core/pkg';

// Import aladin css inside the project
import './../css/aladin.css';

import aladinCSS from './../css/aladin.css?inline';

///////////////////////////////
/////// Aladin Lite API ///////
Expand Down Expand Up @@ -97,6 +96,13 @@ A.aladin = function (divSelector, options) {
} else {
divElement = divSelector;
}

// Associate the CSS inside the div
var cssStyleSheet = document.createElement('style')
cssStyleSheet.classList.add("aladin-css");
cssStyleSheet.innerHTML = aladinCSS;
divElement.appendChild(cssStyleSheet)

return new Aladin(divElement, options);
};

Expand Down
2 changes: 1 addition & 1 deletion src/js/gui/Box/StackBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ export class OverlayStackBox extends Box {
);
layout = layout.concat(this._createSurveysList());

return Layout.vertical({ layout, classList: ["content"] });
return Layout.vertical({ layout });
}

_createOverlaysList() {
Expand Down
41 changes: 1 addition & 40 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import wasmPack from 'vite-plugin-wasm-pack';
// To include and minify glsl into the bundle
import glsl from 'vite-plugin-glsl';
// To include css into the bundle
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
//import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'

export default defineConfig({
build: {
Expand Down Expand Up @@ -38,45 +38,6 @@ export default defineConfig({
glsl({
compress: true,
}),
// make sure that the CSS is also included in ShadowDOMs.
// Extracted from https://github.com/marco-prontera/vite-plugin-css-injected-by-js/issues/128
cssInjectedByJsPlugin({
styleId: "aladin-css",
injectCodeFunction: async function injectCodeCustomRunTimeFunction(cssCode) {
function getAladinDiv(divName: string): Promise<Element | null> {
return new Promise((resolve) => {
if (document.querySelector(divName)) {
return resolve(document.querySelector(divName));
}
const observer = new MutationObserver(() => {
if (document.querySelector(divName)) {
observer.disconnect();
resolve(document.querySelector(divName));
}
});

observer.observe(document.body, {
childList: true,
subtree: true,
});
});
}


try {
if (typeof document != 'undefined') {
let elementStyle = document.createElement('style');
elementStyle.id = "aladin-css";
elementStyle.appendChild(document.createTextNode(cssCode));
document.head.appendChild(elementStyle);
let aladinDiv = await getAladinDiv("aladin-container")
aladinDiv?.shadowRoot?.appendChild(elementStyle);
}
} catch (e) {
console.error('vite-plugin-css-injected-by-js', e);
}
}
}),
],
resolve: {
alias: [
Expand Down

0 comments on commit cbfe278

Please sign in to comment.