@file-viewer/renderer-typst 2.1.2 → 2.1.4

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/dist/typst.js +31 -26
  2. package/package.json +2 -2
package/dist/typst.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { $typst, MemoryAccessModel } from '@myriaddreamin/typst.ts';
2
2
  import { TypstSnippet } from '@myriaddreamin/typst.ts/contrib/snippet';
3
3
  import { resolveFileViewerTypstCompilerWasmUrl, resolveFileViewerTypstFontAssetsUrl, resolveFileViewerTypstRendererWasmUrl, } from '@file-viewer/core/assets';
4
- import { createFileViewerZoomChangeEmitter, formatCssPixels, registerFileViewerZoomProvider, readFileViewerText, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
4
+ import { createFileViewerTranslator, createFileViewerZoomChangeEmitter, formatCssPixels, registerFileViewerZoomProvider, readFileViewerText, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
5
5
  const typstStyle = `
6
6
  .typst-viewer{min-height:100%;overflow:auto;background:#eef1f4;color:#172033}
7
7
  .typst-toolbar{position:sticky;top:0;z-index:2;display:flex;min-height:52px;align-items:center;justify-content:space-between;gap:16px;padding:10px 18px;border-bottom:1px solid rgba(120,134,155,.18);background:rgba(248,250,252,.92);backdrop-filter:blur(16px)}
@@ -33,8 +33,8 @@ const typstStyle = `
33
33
  let typstEngineConfigKey = '';
34
34
  const DEFAULT_TYPST_RENDER_TIMEOUT_MS = 180000;
35
35
  class TypstRenderTimeoutError extends Error {
36
- constructor(timeoutMs) {
37
- super(`Typst WASM / 字体加载或编译超过 ${Math.round(timeoutMs / 1000)} 秒`);
36
+ constructor(timeoutMs, message) {
37
+ super(message || `Typst WASM / font loading or compilation exceeded ${Math.round(timeoutMs / 1000)} seconds`);
38
38
  this.name = 'TypstRenderTimeoutError';
39
39
  }
40
40
  }
@@ -184,12 +184,12 @@ const removeUnsafeSvgContent = (root) => {
184
184
  const serializeNode = (node) => {
185
185
  return new XMLSerializer().serializeToString(node);
186
186
  };
187
- const parseTypstSvgPages = (svgText) => {
187
+ const parseTypstSvgPages = (svgText, svgParseFailedMessage) => {
188
188
  const parser = new DOMParser();
189
189
  const documentSvg = parser.parseFromString(svgText, 'image/svg+xml');
190
190
  const parseError = documentSvg.querySelector('parsererror');
191
191
  if (parseError) {
192
- throw new Error(parseError.textContent || 'Typst SVG 解析失败');
192
+ throw new Error(parseError.textContent || svgParseFailedMessage);
193
193
  }
194
194
  removeUnsafeSvgContent(documentSvg);
195
195
  const root = documentSvg.documentElement;
@@ -246,18 +246,18 @@ const formatTypstError = (error) => {
246
246
  }
247
247
  return String(error);
248
248
  };
249
- const formatTypstRuntimeError = (error) => {
249
+ const formatTypstRuntimeError = (error, t) => {
250
250
  const message = formatTypstError(error);
251
251
  if (error instanceof TypstRenderTimeoutError) {
252
252
  return [
253
253
  message,
254
- '请检查 Typst compiler WASM、renderer WASM、字体目录的下载速度和缓存策略;弱网或跨境部署可通过 options.typst.renderTimeoutMs 调大浏览器端加载/编译超时。'
254
+ t('typst.error.timeoutHint')
255
255
  ].join('\n\n');
256
256
  }
257
257
  if (isTypstAssetLoadError(error)) {
258
258
  return [
259
259
  message,
260
- 'Typst 需要本地 compiler / renderer WASM 和字体目录。请运行 file-viewer-copy-assets,或配置 options.typst.compilerWasmUrl / options.typst.rendererWasmUrl / options.typst.fontAssetsUrl,并确认服务器以 application/wasm 返回 WASM。'
260
+ t('typst.error.assetHint')
261
261
  ].join('\n\n');
262
262
  }
263
263
  return message;
@@ -271,13 +271,13 @@ const normalizeRenderTimeoutMs = (timeoutMs) => {
271
271
  }
272
272
  return Math.max(0, timeoutMs);
273
273
  };
274
- const withRenderTimeout = async (promise, timeoutMs) => {
274
+ const withRenderTimeout = async (promise, timeoutMs, timeoutMessage) => {
275
275
  if (timeoutMs <= 0) {
276
276
  return promise;
277
277
  }
278
278
  let timeoutId;
279
279
  const timeoutPromise = new Promise((_, reject) => {
280
- timeoutId = setTimeout(() => reject(new TypstRenderTimeoutError(timeoutMs)), timeoutMs);
280
+ timeoutId = setTimeout(() => reject(new TypstRenderTimeoutError(timeoutMs, timeoutMessage)), timeoutMs);
281
281
  });
282
282
  try {
283
283
  return await Promise.race([promise, timeoutPromise]);
@@ -372,14 +372,19 @@ const buildExportAdapter = (pages, filename) => {
372
372
  toHtml: () => buildExportHtml(pages, filename),
373
373
  };
374
374
  };
375
- const getPageSummary = (pages) => {
375
+ const getPageSummary = (pages, t) => {
376
376
  if (!pages.length) {
377
- return '0 pages';
377
+ return t('typst.pageSummary.empty');
378
378
  }
379
379
  const firstPage = pages[0];
380
- return `${pages.length} pages / ${Math.round(firstPage.width)} x ${Math.round(firstPage.height)} pt`;
380
+ return t('typst.pageSummary.ready', {
381
+ count: pages.length,
382
+ width: Math.round(firstPage.width),
383
+ height: Math.round(firstPage.height),
384
+ });
381
385
  };
382
386
  export default async function renderTypst(buffer, target, _type, context) {
387
+ const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
383
388
  const source = await readFileViewerText(buffer);
384
389
  const documentRef = target.ownerDocument || document;
385
390
  const zoomEmitter = createFileViewerZoomChangeEmitter();
@@ -395,8 +400,8 @@ export default async function renderTypst(buffer, target, _type, context) {
395
400
  const toolbar = createElement(documentRef, 'header', 'typst-toolbar');
396
401
  const titleGroup = createElement(documentRef, 'div');
397
402
  const title = createElement(documentRef, 'strong', undefined, (context === null || context === void 0 ? void 0 : context.filename) || 'Typst document');
398
- const summary = createElement(documentRef, 'span', undefined, 'Typst WASM renderer');
399
- const status = createElement(documentRef, 'em', undefined, '正在编译');
403
+ const summary = createElement(documentRef, 'span', undefined, t('typst.summaryRenderer'));
404
+ const status = createElement(documentRef, 'em', undefined, t('typst.status.compiling'));
400
405
  const body = createElement(documentRef, 'div');
401
406
  titleGroup.append(title, summary);
402
407
  toolbar.append(titleGroup, status);
@@ -443,12 +448,12 @@ export default async function renderTypst(buffer, target, _type, context) {
443
448
  const renderLoading = () => {
444
449
  const loading = createElement(documentRef, 'div', 'typst-loading');
445
450
  loading.setAttribute('role', 'status');
446
- loading.append(createElement(documentRef, 'span'), createElement(documentRef, 'strong', undefined, '正在解析 Typst'), createElement(documentRef, 'p', undefined, '加载编译器并生成页面预览...'));
451
+ loading.append(createElement(documentRef, 'span'), createElement(documentRef, 'strong', undefined, t('typst.loading.title')), createElement(documentRef, 'p', undefined, t('typst.loading.hint')));
447
452
  body.replaceChildren(loading);
448
453
  };
449
454
  const renderError = () => {
450
455
  const error = createElement(documentRef, 'div', 'typst-error');
451
- error.append(createElement(documentRef, 'strong', undefined, 'Typst 渲染失败'), createElement(documentRef, 'pre', undefined, errorMessage));
456
+ error.append(createElement(documentRef, 'strong', undefined, t('typst.error.title')), createElement(documentRef, 'pre', undefined, errorMessage));
452
457
  body.replaceChildren(error);
453
458
  };
454
459
  const renderPages = () => {
@@ -469,13 +474,13 @@ export default async function renderTypst(buffer, target, _type, context) {
469
474
  };
470
475
  const syncUi = () => {
471
476
  summary.textContent = state === 'ready'
472
- ? getPageSummary(pages)
473
- : 'Typst WASM renderer';
477
+ ? getPageSummary(pages, t)
478
+ : t('typst.summaryRenderer');
474
479
  status.textContent = state === 'loading'
475
- ? '正在编译'
480
+ ? t('typst.status.compiling')
476
481
  : state === 'error'
477
- ? '编译失败'
478
- : '已渲染';
482
+ ? t('typst.status.failed')
483
+ : t('typst.status.rendered');
479
484
  if (state === 'loading') {
480
485
  renderLoading();
481
486
  }
@@ -509,7 +514,7 @@ export default async function renderTypst(buffer, target, _type, context) {
509
514
  css: true,
510
515
  js: false,
511
516
  },
512
- }), timeoutMs);
517
+ }), timeoutMs, t('typst.error.timeout', { seconds: Math.round(timeoutMs / 1000) }));
513
518
  }
514
519
  catch (error) {
515
520
  lastError = error;
@@ -518,7 +523,7 @@ export default async function renderTypst(buffer, target, _type, context) {
518
523
  }
519
524
  }
520
525
  }
521
- throw lastError instanceof Error ? lastError : new Error('Typst WASM 加载失败');
526
+ throw lastError instanceof Error ? lastError : new Error(t('typst.error.wasmLoadFailed'));
522
527
  };
523
528
  const render = async () => {
524
529
  var _a, _b;
@@ -533,7 +538,7 @@ export default async function renderTypst(buffer, target, _type, context) {
533
538
  if (disposed || token !== renderToken) {
534
539
  return;
535
540
  }
536
- pages = parseTypstSvgPages(svg);
541
+ pages = parseTypstSvgPages(svg, t('typst.error.svgParseFailed'));
537
542
  state = 'ready';
538
543
  syncUi();
539
544
  registerExportAdapter();
@@ -543,7 +548,7 @@ export default async function renderTypst(buffer, target, _type, context) {
543
548
  if (disposed || token !== renderToken) {
544
549
  return;
545
550
  }
546
- errorMessage = formatTypstRuntimeError(error);
551
+ errorMessage = formatTypstRuntimeError(error, t);
547
552
  state = 'error';
548
553
  syncUi();
549
554
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/renderer-typst",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Standalone Typst renderer plugin for Flyfish File Viewer powered by browser WASM.",
@@ -54,7 +54,7 @@
54
54
  "LICENSE"
55
55
  ],
56
56
  "dependencies": {
57
- "@file-viewer/core": "^2.1.2",
57
+ "@file-viewer/core": "^2.1.4",
58
58
  "@myriaddreamin/typst-ts-renderer": "0.7.0",
59
59
  "@myriaddreamin/typst-ts-web-compiler": "0.7.0",
60
60
  "@myriaddreamin/typst.ts": "0.7.0"