@8btc/mditor 0.0.32 → 0.0.33

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.
@@ -212,50 +212,11 @@ export const mathRender = (
212
212
  // https://github.com/Vanessa219/vditor/issues/1453
213
213
  Object.assign(window.MathJax, options.math.mathJaxOptions);
214
214
  }
215
- const mathJaxScriptUrl = `${options.cdn}/dist/js/mathjax/tex-svg-full.js`;
216
- const mathJaxScriptId = "protyleMathJaxScript";
217
- // Qt WebView 中同步 XHR 被限制,addScriptSync 无法加载脚本,改为异步 addScript
218
- const isQtWebView =
219
- typeof navigator !== "undefined" &&
220
- /Qt|QtWebEngine/i.test(navigator.userAgent);
221
- const runRenderChain = () => {
222
- window.MathJax.startup.promise.then(() => {
223
- if (window.MathJax.startup) {
224
- window.MathJax.startup.typeset = true;
225
- window.MathJax.startup.elements =
226
- element instanceof HTMLElement ? [element] : [];
227
- }
228
- const chains: any[] = [];
229
- for (let i = 0; i < mathElements.length; i++) {
230
- const mathElement = mathElements[i];
231
- if (
232
- !mathElement.parentElement.classList.contains(
233
- "vditor-wysiwyg__pre"
234
- ) &&
235
- !mathElement.parentElement.classList.contains(
236
- "vditor-ir__marker--pre"
237
- ) &&
238
- !mathElement.getAttribute("data-math") &&
239
- code160to32(mathElement.textContent).trim()
240
- ) {
241
- chains.push((next: () => void) => {
242
- if (i === mathElements.length - 1) {
243
- renderMath(mathElement);
244
- } else {
245
- renderMath(mathElement, next);
246
- }
247
- });
248
- }
249
- }
250
- chainAsync(chains);
251
- });
252
- };
253
- if (isQtWebView) {
254
- addScript(mathJaxScriptUrl, mathJaxScriptId).then(runRenderChain);
255
- } else {
256
- addScriptSync(mathJaxScriptUrl, mathJaxScriptId);
257
- runRenderChain();
258
- }
215
+ // 循环加载会抛异常
216
+ addScriptSync(
217
+ `${options.cdn}/dist/js/mathjax/tex-svg-full.js`,
218
+ "protyleMathJaxScript"
219
+ );
259
220
  const renderMath = (mathElement: Element, next?: () => void) => {
260
221
  const rawText = code160to32(mathElement.textContent).trim();
261
222
  const math = normalizeTex(rawText);
@@ -276,26 +237,15 @@ export const mathRender = (
276
237
  // 绑定自定义右键菜单(编辑区生效,预览区自动跳过)
277
238
  bindMathContextMenu(mathElement as HTMLElement);
278
239
  // 限定 document 范围为当前容器后再 clear/updateDocument,避免处理整页导致编辑器外的 $...$ 被重新渲染;不调用会导致同一公式被渲染 2 次
279
- // Qt WebView 中 clear/updateDocument 可能清掉刚插入的节点或抛错,故跳过
280
- if (!isQtWebView) {
281
- try {
282
- const startup = window.MathJax.startup;
283
- const prevElements = startup.elements;
284
- if (element instanceof HTMLElement) {
285
- startup.elements = [element];
286
- }
287
- startup.document.clear();
288
- startup.document.updateDocument();
289
- startup.elements =
290
- prevElements !== undefined ? prevElements : [];
291
- } catch (_) {
292
- if (
293
- window.MathJax?.startup?.elements !== undefined
294
- ) {
295
- window.MathJax.startup.elements = [];
296
- }
297
- }
240
+ const startup = window.MathJax.startup;
241
+ const prevElements = startup.elements;
242
+ if (element instanceof HTMLElement) {
243
+ startup.elements = [element];
298
244
  }
245
+ startup.document.clear();
246
+ startup.document.updateDocument();
247
+ startup.elements =
248
+ prevElements !== undefined ? prevElements : [];
299
249
  const errorTextElement = node.querySelector(
300
250
  '[data-mml-node="merror"]'
301
251
  );
@@ -313,5 +263,36 @@ export const mathRender = (
313
263
  }
314
264
  );
315
265
  };
266
+ window.MathJax.startup.promise.then(() => {
267
+ // 限定仅处理当前容器,避免组件内部或用户配置导致处理整页、影响编辑器以外的公式
268
+ if (window.MathJax.startup) {
269
+ window.MathJax.startup.typeset = true;
270
+ window.MathJax.startup.elements =
271
+ element instanceof HTMLElement ? [element] : [];
272
+ }
273
+ const chains: any[] = [];
274
+ for (let i = 0; i < mathElements.length; i++) {
275
+ const mathElement = mathElements[i];
276
+ if (
277
+ !mathElement.parentElement.classList.contains(
278
+ "vditor-wysiwyg__pre"
279
+ ) &&
280
+ !mathElement.parentElement.classList.contains(
281
+ "vditor-ir__marker--pre"
282
+ ) &&
283
+ !mathElement.getAttribute("data-math") &&
284
+ code160to32(mathElement.textContent).trim()
285
+ ) {
286
+ chains.push((next: () => void) => {
287
+ if (i === mathElements.length - 1) {
288
+ renderMath(mathElement);
289
+ } else {
290
+ renderMath(mathElement, next);
291
+ }
292
+ });
293
+ }
294
+ }
295
+ chainAsync(chains);
296
+ });
316
297
  }
317
298
  };
@@ -4,10 +4,8 @@ export const addScriptSync = (path: string, id: string) => {
4
4
  }
5
5
  const xhrObj = new XMLHttpRequest();
6
6
  xhrObj.open("GET", path, false);
7
- xhrObj.setRequestHeader(
8
- "Accept",
9
- "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"
10
- );
7
+ xhrObj.setRequestHeader("Accept",
8
+ "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01");
11
9
  xhrObj.send("");
12
10
  const scriptElement = document.createElement("script");
13
11
  scriptElement.type = "text/javascript";
@@ -30,7 +28,7 @@ export const addScript = (path: string, id: string) => {
30
28
  document.head.appendChild(scriptElement);
31
29
  scriptElement.onerror = (event) => {
32
30
  reject(event);
33
- };
31
+ }
34
32
  scriptElement.onload = () => {
35
33
  if (document.getElementById(id)) {
36
34
  // 循环调用需清除 DOM 中的 script 标签