1 | import * as JSS from 'jss';
|
2 | import jssPresetDefault from 'jss-preset-default';
|
3 | import { Signal } from '@lumino/signaling';
|
4 | import { ROOT } from '.';
|
5 | export class Stylist {
|
6 | constructor() {
|
7 | this.fonts = new Map();
|
8 | this._notebookStyles = new Map();
|
9 | this._jss = JSS.create(jssPresetDefault());
|
10 | this._fontCache = new Map();
|
11 | this._cacheUpdated = new Signal(this);
|
12 | this._globalStyles = document.createElement('style');
|
13 | }
|
14 | get cacheUpdated() {
|
15 | return this._cacheUpdated;
|
16 | }
|
17 | registerNotebook(notebook, register) {
|
18 | if (register) {
|
19 | this._notebookStyles.set(notebook, document.createElement('style'));
|
20 | notebook.disposed.connect(this._onDisposed, this);
|
21 | this.hack();
|
22 | }
|
23 | else {
|
24 | this._onDisposed(notebook);
|
25 | }
|
26 | }
|
27 | _onDisposed(notebook) {
|
28 | var _a;
|
29 | if (this._notebookStyles.has(notebook)) {
|
30 | (_a = this._notebookStyles.get(notebook)) === null || _a === void 0 ? void 0 : _a.remove();
|
31 | this._notebookStyles.delete(notebook);
|
32 | notebook.disposed.disconnect(this._onDisposed, this);
|
33 | }
|
34 | }
|
35 | get stylesheets() {
|
36 | return [this._globalStyles, ...Array.from(this._notebookStyles.values())];
|
37 | }
|
38 | notebooks() {
|
39 | return Array.from(this._notebookStyles.keys());
|
40 | }
|
41 | stylesheet(meta, notebook, clear = false) {
|
42 | let sheet = notebook
|
43 | ? this._notebookStyles.get(notebook)
|
44 | : this._globalStyles;
|
45 | let style = notebook
|
46 | ? this._nbMetaToStyle(meta, notebook)
|
47 | : this._settingsToStyle(meta);
|
48 | let jss = this._jss.createStyleSheet(style);
|
49 | let css = jss.toString();
|
50 | if (sheet && sheet.textContent !== css) {
|
51 | sheet.textContent = css;
|
52 | }
|
53 | this.hack();
|
54 | }
|
55 | _nbMetaToStyle(meta, notebook) {
|
56 | const id = notebook.id;
|
57 | let jss = { '@font-face': [], '@global': {} };
|
58 | let idStyles = (jss['@global'][`.jp-NotebookPanel[id='${id}']`] = {});
|
59 | if (meta.fonts) {
|
60 | for (let fontFamily in meta.fonts) {
|
61 | jss['@font-face'] = jss['@font-face'].concat(meta.fonts[fontFamily]);
|
62 | }
|
63 | }
|
64 | let styles = meta.styles || {};
|
65 | for (let k in styles) {
|
66 | if (k === ROOT) {
|
67 | for (let rootK in styles[k]) {
|
68 | if (styles == null || styles[k] == null) {
|
69 | continue;
|
70 | }
|
71 | idStyles[rootK] = styles[k][rootK];
|
72 | }
|
73 | }
|
74 | else if (typeof styles[k] === 'object') {
|
75 | idStyles[`& ${k}`] = styles[k];
|
76 | }
|
77 | else {
|
78 | idStyles[k] = styles[k];
|
79 | }
|
80 | }
|
81 | return jss;
|
82 | }
|
83 | _settingsToStyle(meta) {
|
84 | let raw = JSON.stringify(meta.styles);
|
85 | let styles = JSON.parse(raw);
|
86 | let faces = {};
|
87 | for (let font of Array.from(this.fonts.keys())) {
|
88 | if (raw.indexOf(`'${font}'`) > -1 && !faces[font]) {
|
89 | const cachedFont = this._fontCache.get(font);
|
90 | if (cachedFont != null) {
|
91 | faces[font] = cachedFont;
|
92 | }
|
93 | else {
|
94 |
|
95 | new Promise((resolve, reject) => {
|
96 | const options = this.fonts.get(font);
|
97 | if (options == null) {
|
98 | reject();
|
99 | return;
|
100 | }
|
101 | else {
|
102 | options.faces().then(faces => {
|
103 | if (this._fontCache.has(font)) {
|
104 | return;
|
105 | }
|
106 | this._fontCache.set(font, faces);
|
107 | this._cacheUpdated.emit(void 0);
|
108 | resolve();
|
109 | }, function (err) {
|
110 | console.error('rejected!', err);
|
111 | reject();
|
112 | });
|
113 | }
|
114 | });
|
115 | }
|
116 | }
|
117 | }
|
118 | let flatFaces = Object.keys(faces).reduce((m, face) => {
|
119 | const foundFaces = faces[face];
|
120 | if (faces && foundFaces != null) {
|
121 | return m.concat(foundFaces);
|
122 | }
|
123 | }, []);
|
124 | return {
|
125 | '@global': styles,
|
126 | '@font-face': flatFaces
|
127 | };
|
128 | }
|
129 | dispose() {
|
130 | this._globalStyles.remove();
|
131 | for (let notebook of Array.from(this._notebookStyles.keys())) {
|
132 | this._onDisposed(notebook);
|
133 | }
|
134 | }
|
135 | hack(show = true) {
|
136 | if (show) {
|
137 | setTimeout(() => this.stylesheets.map(s => {
|
138 | document.body.appendChild(s);
|
139 | }), 0);
|
140 | }
|
141 | else {
|
142 | this.stylesheets.map(el => el.remove());
|
143 | }
|
144 | }
|
145 | }
|
146 |
|
\ | No newline at end of file |