@8btc/mditor 0.0.31 → 0.0.32

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.
@@ -188,8 +188,7 @@ export const mathRender = (
188
188
  },
189
189
  startup: {
190
190
  typeset: true,
191
- elements:
192
- element instanceof HTMLElement ? [element] : [],
191
+ elements: element instanceof HTMLElement ? [element] : [],
193
192
  },
194
193
  tex: {
195
194
  inlineMath: [
@@ -213,11 +212,50 @@ export const mathRender = (
213
212
  // https://github.com/Vanessa219/vditor/issues/1453
214
213
  Object.assign(window.MathJax, options.math.mathJaxOptions);
215
214
  }
216
- // 循环加载会抛异常
217
- addScriptSync(
218
- `${options.cdn}/dist/js/mathjax/tex-svg-full.js`,
219
- "protyleMathJaxScript"
220
- );
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
+ }
221
259
  const renderMath = (mathElement: Element, next?: () => void) => {
222
260
  const rawText = code160to32(mathElement.textContent).trim();
223
261
  const math = normalizeTex(rawText);
@@ -238,14 +276,26 @@ export const mathRender = (
238
276
  // 绑定自定义右键菜单(编辑区生效,预览区自动跳过)
239
277
  bindMathContextMenu(mathElement as HTMLElement);
240
278
  // 限定 document 范围为当前容器后再 clear/updateDocument,避免处理整页导致编辑器外的 $...$ 被重新渲染;不调用会导致同一公式被渲染 2 次
241
- const startup = window.MathJax.startup;
242
- const prevElements = startup.elements;
243
- if (element instanceof HTMLElement) {
244
- startup.elements = [element];
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
+ }
245
298
  }
246
- startup.document.clear();
247
- startup.document.updateDocument();
248
- startup.elements = prevElements !== undefined ? prevElements : [];
249
299
  const errorTextElement = node.querySelector(
250
300
  '[data-mml-node="merror"]'
251
301
  );
@@ -263,36 +313,5 @@ export const mathRender = (
263
313
  }
264
314
  );
265
315
  };
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
- });
297
316
  }
298
317
  };
@@ -4,8 +4,10 @@ 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("Accept",
8
- "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01");
7
+ xhrObj.setRequestHeader(
8
+ "Accept",
9
+ "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"
10
+ );
9
11
  xhrObj.send("");
10
12
  const scriptElement = document.createElement("script");
11
13
  scriptElement.type = "text/javascript";
@@ -28,7 +30,7 @@ export const addScript = (path: string, id: string) => {
28
30
  document.head.appendChild(scriptElement);
29
31
  scriptElement.onerror = (event) => {
30
32
  reject(event);
31
- }
33
+ };
32
34
  scriptElement.onload = () => {
33
35
  if (document.getElementById(id)) {
34
36
  // 循环调用需清除 DOM 中的 script 标签