@8btc/mditor 0.0.30 → 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.
@@ -180,15 +180,15 @@ export const mathRender = (
180
180
  next();
181
181
  };
182
182
  if (!window.MathJax) {
183
- // 关闭自动 typeset、不指定 elements,避免处理整个 document。外部使用本包时无需配置 startup.elements
183
+ // typeset 设为 true 以正常完成公式渲染;elements 限定为传入的容器,外部使用本包时仅处理该容器,不影响编辑器以外的公式。
184
184
  // 渲染时会将 startup.elements 临时设为传入的 element(当前预览/编辑容器),再 clear/updateDocument。
185
185
  window.MathJax = {
186
186
  loader: {
187
187
  paths: { mathjax: `${options.cdn}/dist/js/mathjax` },
188
188
  },
189
189
  startup: {
190
- typeset: false,
191
- elements: [],
190
+ typeset: true,
191
+ elements: element instanceof HTMLElement ? [element] : [],
192
192
  },
193
193
  tex: {
194
194
  inlineMath: [
@@ -212,11 +212,50 @@ export const mathRender = (
212
212
  // https://github.com/Vanessa219/vditor/issues/1453
213
213
  Object.assign(window.MathJax, options.math.mathJaxOptions);
214
214
  }
215
- // 循环加载会抛异常
216
- addScriptSync(
217
- `${options.cdn}/dist/js/mathjax/tex-svg-full.js`,
218
- "protyleMathJaxScript"
219
- );
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
+ }
220
259
  const renderMath = (mathElement: Element, next?: () => void) => {
221
260
  const rawText = code160to32(mathElement.textContent).trim();
222
261
  const math = normalizeTex(rawText);
@@ -237,14 +276,26 @@ export const mathRender = (
237
276
  // 绑定自定义右键菜单(编辑区生效,预览区自动跳过)
238
277
  bindMathContextMenu(mathElement as HTMLElement);
239
278
  // 限定 document 范围为当前容器后再 clear/updateDocument,避免处理整页导致编辑器外的 $...$ 被重新渲染;不调用会导致同一公式被渲染 2 次
240
- const startup = window.MathJax.startup;
241
- const prevElements = startup.elements;
242
- if (element instanceof HTMLElement) {
243
- 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
+ }
244
298
  }
245
- startup.document.clear();
246
- startup.document.updateDocument();
247
- startup.elements = prevElements !== undefined ? prevElements : [];
248
299
  const errorTextElement = node.querySelector(
249
300
  '[data-mml-node="merror"]'
250
301
  );
@@ -262,35 +313,5 @@ export const mathRender = (
262
313
  }
263
314
  );
264
315
  };
265
- window.MathJax.startup.promise.then(() => {
266
- // 脚本加载后再次强制不自动 typeset、不指定 elements,避免组件内部或用户配置导致处理整页
267
- if (window.MathJax.startup) {
268
- window.MathJax.startup.typeset = false;
269
- window.MathJax.startup.elements = [];
270
- }
271
- const chains: any[] = [];
272
- for (let i = 0; i < mathElements.length; i++) {
273
- const mathElement = mathElements[i];
274
- if (
275
- !mathElement.parentElement.classList.contains(
276
- "vditor-wysiwyg__pre"
277
- ) &&
278
- !mathElement.parentElement.classList.contains(
279
- "vditor-ir__marker--pre"
280
- ) &&
281
- !mathElement.getAttribute("data-math") &&
282
- code160to32(mathElement.textContent).trim()
283
- ) {
284
- chains.push((next: () => void) => {
285
- if (i === mathElements.length - 1) {
286
- renderMath(mathElement);
287
- } else {
288
- renderMath(mathElement, next);
289
- }
290
- });
291
- }
292
- }
293
- chainAsync(chains);
294
- });
295
316
  }
296
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 标签