Skip to content

Commit 4248cd0

Browse files
committed
Fix circular dependency
Signed-off-by: Andrew Stein <steinlink@gmail.com>
1 parent da1e971 commit 4248cd0

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

packages/perspective-viewer-d3fc/src/ts/plugin/d3fc_global_styles.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@
1010
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
1111
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1212

13-
export const D3FC_GLOBAL_STYLES = [];
14-
1513
// Capture (and restore) `document.querySelector` to prevent D3FC from
1614
// attaching styles to the document `<head>`.
1715
export async function init() {
1816
const old_doc = window.document.querySelector;
17+
const stylesheets = [];
18+
1919
// @ts-ignore
2020
window.document.querySelector = () => {
2121
return {
2222
appendChild(elem) {
23-
D3FC_GLOBAL_STYLES.push(elem);
23+
stylesheets.push(elem);
2424
return elem;
2525
},
2626
};
2727
};
2828

29-
const { register } = await import("./plugin");
29+
const { register, createStyleSheets } = await import("./plugin");
3030
window.document.querySelector = old_doc;
31+
createStyleSheets(stylesheets);
3132
register();
3233
}

packages/perspective-viewer-d3fc/src/ts/plugin/plugin.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { HTMLPerspectiveViewerElement } from "@finos/perspective-viewer";
1818
import type * as psp_types from "@finos/perspective-viewer";
1919

2020
import * as d3 from "d3";
21-
import { D3FC_GLOBAL_STYLES } from "./d3fc_global_styles";
21+
2222
import { Chart, Settings, Type } from "../types";
2323

2424
const DEFAULT_PLUGIN_SETTINGS = {
@@ -30,17 +30,7 @@ const DEFAULT_PLUGIN_SETTINGS = {
3030
selectMode: "select",
3131
};
3232

33-
const styleSheets = [];
34-
for (const style of D3FC_GLOBAL_STYLES) {
35-
const sheet = new CSSStyleSheet();
36-
sheet.replaceSync(style.textContent);
37-
styleSheets.push(sheet);
38-
}
39-
40-
const sheet = new CSSStyleSheet();
41-
sheet.replaceSync(style);
42-
43-
styleSheets.push(sheet);
33+
const D3FC_GLOBAL_STYLES = [];
4434

4535
const EXCLUDED_SETTINGS = [
4636
"crossValues",
@@ -63,6 +53,18 @@ async function register_element(plugin_name: string) {
6353
await perspectiveViewerClass.registerPlugin(plugin_name);
6454
}
6555

56+
export function createStyleSheets(sheets) {
57+
for (const style of sheets) {
58+
const sheet = new CSSStyleSheet();
59+
sheet.replaceSync(style.textContent);
60+
D3FC_GLOBAL_STYLES.push(sheet);
61+
}
62+
63+
const sheet = new CSSStyleSheet();
64+
sheet.replaceSync(style);
65+
D3FC_GLOBAL_STYLES.push(sheet);
66+
}
67+
6668
export function register(...plugin_names: string[]) {
6769
const plugins = new Set(
6870
plugin_names.length > 0
@@ -115,7 +117,7 @@ class HTMLPerspectiveViewerD3fcPluginElement extends HTMLElement {
115117
connectedCallback() {
116118
if (!this._initialized) {
117119
this.attachShadow({ mode: "open" });
118-
for (const sheet of styleSheets) {
120+
for (const sheet of D3FC_GLOBAL_STYLES) {
119121
this.shadowRoot.adoptedStyleSheets.push(sheet);
120122
}
121123

0 commit comments

Comments
 (0)