@brightspace-ui/core 2.62.0 → 2.63.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/helpers/mathjax.js +60 -46
  2. package/package.json +1 -1
@@ -30,9 +30,6 @@ const mathjaxFontMappings = new Map([
30
30
  ['MJXTEX-VB', 'MathJax_Vector-Bold']
31
31
  ]);
32
32
 
33
- let mathJaxLoaded;
34
- let renderingPromise = Promise.resolve();
35
-
36
33
  class HtmlBlockMathRenderer {
37
34
 
38
35
  get contextAttributes() {
@@ -45,34 +42,13 @@ class HtmlBlockMathRenderer {
45
42
  if (contextVal === undefined) return elem;
46
43
 
47
44
  const context = JSON.parse(contextVal) || {};
48
- const isLatexSupported = context.renderLatex;
49
-
50
- if (!elem.querySelector('math') && !(isLatexSupported && /\$\$|\\\(|\\\[|\\begin{|\\ref{|\\eqref{/.test(elem.innerHTML))) return elem;
51
45
 
52
- const mathJaxConfig = {
46
+ await typesetMath(elem, {
53
47
  deferTypeset: true,
54
- renderLatex: isLatexSupported,
55
- outputScale: context.outputScale || 1
56
- };
57
-
58
- await loadMathJax(mathJaxConfig);
59
-
60
- // MathJax 3 does not support newlines, but it does persist styles, so add custom styles to mimic a linebreak
61
- // This work-around should be removed when linebreaks are natively supported.
62
- // MathJax issue: https://github.com/mathjax/MathJax/issues/2312
63
- // A duplicate that explains our exact issue: https://github.com/mathjax/MathJax/issues/2495
64
- elem.querySelectorAll('mspace[linebreak="newline"]').forEach(elm => {
65
- elm.style.display = 'block';
66
- elm.style.height = '0.5rem';
48
+ renderLatex: context.renderLatex,
49
+ outputScale: context.outputScale || 1,
50
+ window: window
67
51
  });
68
-
69
- // If we're using deferred rendering, we need to create a document structure
70
- // within the element so MathJax can appropriately process math.
71
- if (!options.noDeferredRendering) elem.innerHTML = `<mjx-doc><mjx-head></mjx-head><mjx-body>${elem.innerHTML}</mjx-body></mjx-doc>`;
72
-
73
- await window.MathJax.startup.promise;
74
- renderingPromise = renderingPromise.then(() => window.MathJax.typesetShadow(elem.getRootNode(), elem));
75
- await renderingPromise;
76
52
  }
77
53
 
78
54
  }
@@ -83,9 +59,12 @@ export function createHtmlBlockRenderer() {
83
59
 
84
60
  export function loadMathJax(mathJaxConfig) {
85
61
 
86
- if (mathJaxLoaded) return mathJaxLoaded;
62
+ const win = (mathJaxConfig && mathJaxConfig.window) || window;
87
63
 
88
- window.MathJax = {
64
+ win.D2L = win.D2L || {};
65
+ if (win.D2L.mathJaxLoaded) return win.D2L.mathJaxLoaded;
66
+
67
+ win.MathJax = {
89
68
  chtml: {
90
69
  adaptiveCSS: false,
91
70
  scale: (mathJaxConfig && mathJaxConfig.outputScale) || 1
@@ -110,11 +89,11 @@ export function loadMathJax(mathJaxConfig) {
110
89
  // just an example and so we use an expedient method of
111
90
  // accessing these for now.)
112
91
  //
113
- const mathjax = window.MathJax._.mathjax.mathjax;
114
- const HTMLAdaptor = window.MathJax._.adaptors.HTMLAdaptor.HTMLAdaptor;
115
- const HTMLHandler = window.MathJax._.handlers.html.HTMLHandler.HTMLHandler;
116
- const AbstractHandler = window.MathJax._.core.Handler.AbstractHandler.prototype;
117
- const startup = window.MathJax.startup;
92
+ const mathjax = win.MathJax._.mathjax.mathjax;
93
+ const HTMLAdaptor = win.MathJax._.adaptors.HTMLAdaptor.HTMLAdaptor;
94
+ const HTMLHandler = win.MathJax._.handlers.html.HTMLHandler.HTMLHandler;
95
+ const AbstractHandler = win.MathJax._.core.Handler.AbstractHandler.prototype;
96
+ const startup = win.MathJax.startup;
118
97
 
119
98
  const getFirstChild = doc => {
120
99
  const child = doc.firstChild;
@@ -155,7 +134,7 @@ export function loadMathJax(mathJaxConfig) {
155
134
  const adaptor = this.adaptor;
156
135
  if (typeof(document) === 'string') {
157
136
  document = adaptor.parse(document, 'text/html');
158
- } else if ((document instanceof adaptor.window.HTMLElement || document instanceof adaptor.window.DocumentFragment) && !(document instanceof window.ShadowRoot)) {
137
+ } else if ((document instanceof adaptor.window.HTMLElement || document instanceof adaptor.window.DocumentFragment) && !(document instanceof win.ShadowRoot)) {
159
138
  const child = document;
160
139
  document = adaptor.parse('', 'text/html');
161
140
  adaptor.append(adaptor.body(document), child);
@@ -173,7 +152,7 @@ export function loadMathJax(mathJaxConfig) {
173
152
  // Register the new handler and adaptor
174
153
  //
175
154
  startup.registerConstructor('HTMLHandler', ShadowHandler);
176
- startup.registerConstructor('browserAdaptor', () => new ShadowAdaptor(window));
155
+ startup.registerConstructor('browserAdaptor', () => new ShadowAdaptor(win));
177
156
 
178
157
  //
179
158
  // A service function that creates a new MathDocument from the
@@ -181,7 +160,7 @@ export function loadMathJax(mathJaxConfig) {
181
160
  // renders the document. The MathDocument is returned in case
182
161
  // you need to rerender the shadowRoot later.
183
162
  //
184
- window.MathJax.typesetShadow = async function(root, elem) {
163
+ win.MathJax.typesetShadow = async function(root, elem) {
185
164
  const InputJax = startup.getInputJax();
186
165
  const OutputJax = startup.getOutputJax();
187
166
  const html = mathjax.document(root, { InputJax, OutputJax });
@@ -195,15 +174,15 @@ export function loadMathJax(mathJaxConfig) {
195
174
  //
196
175
  // Now do the usual startup now that the extensions are in place
197
176
  //
198
- window.MathJax.startup.defaultReady();
177
+ win.MathJax.startup.defaultReady();
199
178
  },
200
179
  // Defer typesetting if the config is present and deferring is set
201
180
  typeset: !(mathJaxConfig && mathJaxConfig.deferTypeset)
202
181
  }
203
182
  };
204
183
 
205
- if (mathJaxConfig && mathJaxConfig.deferTypeset && !document.head.querySelector('#d2l-mathjax-fonts') && !document.head.querySelector('#MJX-CHTML-styles')) {
206
- const styleElem = document.createElement('style');
184
+ if (mathJaxConfig && mathJaxConfig.deferTypeset && !win.document.head.querySelector('#d2l-mathjax-fonts') && !win.document.head.querySelector('#MJX-CHTML-styles')) {
185
+ const styleElem = win.document.createElement('style');
207
186
  styleElem.id = 'd2l-mathjax-fonts';
208
187
 
209
188
  let fontImportStyles = '';
@@ -216,11 +195,11 @@ export function loadMathJax(mathJaxConfig) {
216
195
  });
217
196
 
218
197
  styleElem.textContent = fontImportStyles;
219
- document.head.appendChild(styleElem);
198
+ win.document.head.appendChild(styleElem);
220
199
  }
221
200
 
222
- mathJaxLoaded = new Promise(resolve => {
223
- const script = document.createElement('script');
201
+ win.D2L.mathJaxLoaded = new Promise(resolve => {
202
+ const script = win.document.createElement('script');
224
203
  script.async = 'async';
225
204
  script.onload = resolve;
226
205
 
@@ -229,9 +208,44 @@ export function loadMathJax(mathJaxConfig) {
229
208
  : 'mml-chtml';
230
209
 
231
210
  script.src = `${mathjaxBaseUrl}/${component}.js`;
232
- document.head.appendChild(script);
211
+ win.document.head.appendChild(script);
212
+ });
213
+
214
+ return win.D2L.mathJaxLoaded;
215
+
216
+ }
217
+
218
+ export async function typesetMath(elem, options) {
219
+ if (!elem.querySelector('math') && !(options.renderLatex && /\$\$|\\\(|\\\[|\\begin{|\\ref{|\\eqref{/.test(elem.innerHTML))) return elem;
220
+
221
+ const win = options.window;
222
+
223
+ const mathJaxConfig = {
224
+ deferTypeset: options.deferTypeset,
225
+ renderLatex: options.renderLatex,
226
+ outputScale: options.outputScale || 1,
227
+ window: win
228
+ };
229
+
230
+ await loadMathJax(mathJaxConfig);
231
+
232
+ // MathJax 3 does not support newlines, but it does persist styles, so add custom styles to mimic a linebreak
233
+ // This work-around should be removed when linebreaks are natively supported.
234
+ // MathJax issue: https://github.com/mathjax/MathJax/issues/2312
235
+ // A duplicate that explains our exact issue: https://github.com/mathjax/MathJax/issues/2495
236
+ elem.querySelectorAll('mspace[linebreak="newline"]').forEach(elm => {
237
+ elm.style.display = 'block';
238
+ elm.style.height = '0.5rem';
233
239
  });
234
240
 
235
- return mathJaxLoaded;
241
+ // If we're using deferred rendering, we need to create a document structure
242
+ // within the element so MathJax can appropriately process math.
243
+ if (!options.noDeferredRendering) elem.innerHTML = `<mjx-doc><mjx-head></mjx-head><mjx-body>${elem.innerHTML}</mjx-body></mjx-doc>`;
244
+
245
+ await win.MathJax.startup.promise;
246
+ win.D2L = win.D2L || {};
236
247
 
248
+ if (!win.D2L.renderingPromise) win.D2L.renderingPromise = Promise.resolve();
249
+ win.D2L.renderingPromise = win.D2L.renderingPromise.then(() => win.MathJax.typesetShadow(elem.getRootNode(), elem));
250
+ await win.D2L.renderingPromise;
237
251
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brightspace-ui/core",
3
- "version": "2.62.0",
3
+ "version": "2.63.0",
4
4
  "description": "A collection of accessible, free, open-source web components for building Brightspace applications",
5
5
  "type": "module",
6
6
  "repository": "https://github.com/BrightspaceUI/core.git",