@file-viewer/renderer-text 3.1.2 → 3.2.1
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.
- package/README.en.md +3 -135
- package/README.md +3 -124
- package/bin/copy-xml-profile-assets.mjs +3 -3
- package/dist/code.d.ts +0 -7
- package/dist/code.js +6 -138
- package/dist/html.js +2 -4
- package/dist/index.js +6 -2
- package/dist/largeText.js +39 -24
- package/dist/lrc.d.ts +2 -0
- package/dist/lrc.js +185 -0
- package/dist/lrcParser.d.ts +23 -0
- package/dist/lrcParser.js +83 -0
- package/dist/markdown.d.ts +1 -1
- package/dist/markdown.js +95 -31
- package/dist/markdownAnchors.d.ts +1 -0
- package/dist/markdownAnchors.js +66 -0
- package/dist/optionalCapabilities.d.ts +0 -4
- package/dist/optionalCapabilities.js +0 -4
- package/dist/patch.js +2 -2
- package/dist/prettyPrint.d.ts +0 -7
- package/dist/prettyPrint.js +0 -9
- package/dist/sanitizeHtml.d.ts +0 -1
- package/dist/sanitizeHtml.js +0 -1
- package/dist/syntaxGrammars/data.d.ts +10 -0
- package/dist/syntaxGrammars/data.js +8 -0
- package/dist/syntaxGrammars/documents.d.ts +5 -0
- package/dist/syntaxGrammars/documents.js +3 -0
- package/dist/syntaxGrammars/managed.d.ts +7 -0
- package/dist/syntaxGrammars/managed.js +5 -0
- package/dist/syntaxGrammars/native.d.ts +10 -0
- package/dist/syntaxGrammars/native.js +8 -0
- package/dist/syntaxGrammars/php.d.ts +5 -0
- package/dist/syntaxGrammars/php.js +3 -0
- package/dist/syntaxGrammars/scripting.d.ts +9 -0
- package/dist/syntaxGrammars/scripting.js +7 -0
- package/dist/syntaxGrammars/tooling.d.ts +8 -0
- package/dist/syntaxGrammars/tooling.js +6 -0
- package/dist/syntaxGrammars/web.d.ts +8 -0
- package/dist/syntaxGrammars/web.js +6 -0
- package/dist/syntaxHighlight.d.ts +5 -0
- package/dist/syntaxHighlight.js +167 -0
- package/dist/xml-profiles.d.ts +0 -1
- package/dist/xml-profiles.js +0 -1
- package/dist/xml.js +6 -11
- package/dist/xmlEngines.js +3 -4
- package/dist/xmlSafety.d.ts +0 -1
- package/dist/xmlSafety.js +0 -3
- package/dist/xmlWasmMemory.d.ts +0 -6
- package/dist/xmlWasmMemory.js +0 -6
- package/file-viewer.capability.json +1 -1
- package/licenses/xml-profiles/NOTICE.md +1 -1
- package/package.json +84 -29
package/dist/largeText.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEFAULT_FILE_VIEWER_SEARCH_MAX_MATCHES, createFileViewerTextDecoder, createEmptyFileViewerSearchState, createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, normalizeFileViewerSearchOptions, resolveFileViewerTextEncoding, registerFileViewerSearchProvider, registerFileViewerZoomProvider, unregisterFileViewerSearchProvider, unregisterFileViewerZoomProvider } from '@file-viewer/core';
|
|
1
|
+
import { DEFAULT_FILE_VIEWER_SEARCH_MAX_MATCHES, createFileViewerTextDecoder, createEmptyFileViewerSearchState, createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, isSingleByteFileViewerTextEncoding, normalizeFileViewerSearchOptions, resolveFileViewerTextEncoding, registerFileViewerSearchProvider, registerFileViewerZoomProvider, unregisterFileViewerSearchProvider, unregisterFileViewerZoomProvider } from '@file-viewer/core';
|
|
2
2
|
import { codeStyle } from './codeStyle.js';
|
|
3
3
|
export const DEFAULT_LARGE_TEXT_THRESHOLD_BYTES = 512 * 1024;
|
|
4
4
|
export const DEFAULT_LARGE_TEXT_LINE_SEGMENT_BYTES = 16 * 1024;
|
|
@@ -75,9 +75,22 @@ const alignGb18030Boundary = (bytes, knownBoundary, requestedOffset, limit) => {
|
|
|
75
75
|
return cursor;
|
|
76
76
|
};
|
|
77
77
|
const alignTextBoundary = (index, knownBoundary, requestedOffset, limit) => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
if (index.encoding === 'gb18030') {
|
|
79
|
+
return alignGb18030Boundary(index.bytes, knownBoundary, requestedOffset, limit);
|
|
80
|
+
}
|
|
81
|
+
if (isSingleByteFileViewerTextEncoding(index.encoding)) {
|
|
82
|
+
return clamp(requestedOffset, 0, limit);
|
|
83
|
+
}
|
|
84
|
+
return alignUtf8End(index.bytes, requestedOffset, limit);
|
|
85
|
+
};
|
|
86
|
+
const alignSegmentStart = (index, lineStart, requestedOffset, limit) => {
|
|
87
|
+
if (index.encoding === 'gb18030') {
|
|
88
|
+
return alignTextBoundary(index, lineStart, requestedOffset, limit);
|
|
89
|
+
}
|
|
90
|
+
if (isSingleByteFileViewerTextEncoding(index.encoding)) {
|
|
91
|
+
return clamp(requestedOffset, 0, limit);
|
|
92
|
+
}
|
|
93
|
+
return alignUtf8Start(index.bytes, requestedOffset, limit);
|
|
81
94
|
};
|
|
82
95
|
const isLineBreak = (bytes, offset) => {
|
|
83
96
|
const byte = bytes[offset];
|
|
@@ -178,9 +191,7 @@ const decodeLargeTextSegment = (index, line, segmentIndex, segmentBytes) => {
|
|
|
178
191
|
const normalizedSegment = clamp(Math.trunc(segmentIndex), 0, segmentCount - 1);
|
|
179
192
|
const rawStart = line.start + (normalizedSegment * segmentBytes);
|
|
180
193
|
const rawEnd = Math.min(line.end, rawStart + segmentBytes);
|
|
181
|
-
const start = index.
|
|
182
|
-
? alignTextBoundary(index, line.start, rawStart, line.end)
|
|
183
|
-
: alignUtf8Start(index.bytes, rawStart, line.end);
|
|
194
|
+
const start = alignSegmentStart(index, line.start, rawStart, line.end);
|
|
184
195
|
const end = alignTextBoundary(index, start, rawEnd, line.end);
|
|
185
196
|
return {
|
|
186
197
|
text: createFileViewerTextDecoder(index.encoding).decode(index.bytes.subarray(start, end)),
|
|
@@ -207,7 +218,7 @@ const cloneLargeTextSearchState = (state) => ({
|
|
|
207
218
|
matches: state.matches.map(match => ({ ...match }))
|
|
208
219
|
});
|
|
209
220
|
export const shouldVirtualizeTextBuffer = (buffer, context) => {
|
|
210
|
-
var _a, _b, _c, _d;
|
|
221
|
+
var _a, _b, _c, _d, _e, _f;
|
|
211
222
|
const configured = (_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.virtualizeAboveBytes;
|
|
212
223
|
const threshold = Number.isFinite(configured)
|
|
213
224
|
? Math.max(0, Number(configured))
|
|
@@ -216,11 +227,11 @@ export const shouldVirtualizeTextBuffer = (buffer, context) => {
|
|
|
216
227
|
return false;
|
|
217
228
|
}
|
|
218
229
|
const bytes = new Uint8Array(buffer);
|
|
219
|
-
const { encoding } = resolveFileViewerTextEncoding(bytes, (_d = (_c = context === null || context === void 0 ? void 0 : context.options) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.encoding);
|
|
230
|
+
const { encoding } = resolveFileViewerTextEncoding(bytes, (_d = (_c = context === null || context === void 0 ? void 0 : context.options) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.encoding, (_f = (_e = context === null || context === void 0 ? void 0 : context.options) === null || _e === void 0 ? void 0 : _e.text) === null || _f === void 0 ? void 0 : _f.fallbackEncoding);
|
|
220
231
|
return encoding !== 'utf-16le' && encoding !== 'utf-16be';
|
|
221
232
|
};
|
|
222
233
|
export const shouldVirtualizeMarkdownBuffer = (buffer, context) => {
|
|
223
|
-
var _a, _b, _c, _d;
|
|
234
|
+
var _a, _b, _c, _d, _e, _f;
|
|
224
235
|
const configured = (_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.markdownVirtualizeAboveBytes;
|
|
225
236
|
if (!Number.isFinite(configured)) {
|
|
226
237
|
return false;
|
|
@@ -229,7 +240,7 @@ export const shouldVirtualizeMarkdownBuffer = (buffer, context) => {
|
|
|
229
240
|
return false;
|
|
230
241
|
}
|
|
231
242
|
const bytes = new Uint8Array(buffer);
|
|
232
|
-
const { encoding } = resolveFileViewerTextEncoding(bytes, (_d = (_c = context === null || context === void 0 ? void 0 : context.options) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.encoding);
|
|
243
|
+
const { encoding } = resolveFileViewerTextEncoding(bytes, (_d = (_c = context === null || context === void 0 ? void 0 : context.options) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.encoding, (_f = (_e = context === null || context === void 0 ? void 0 : context.options) === null || _e === void 0 ? void 0 : _e.text) === null || _f === void 0 ? void 0 : _f.fallbackEncoding);
|
|
233
244
|
return encoding !== 'utf-16le' && encoding !== 'utf-16be';
|
|
234
245
|
};
|
|
235
246
|
const largeTextStyle = `
|
|
@@ -255,25 +266,23 @@ const largeTextStyle = `
|
|
|
255
266
|
.code-viewer--virtual.code-viewer--wrap-lines .code-virtual-content{display:block;min-width:0;flex:1 1 auto;padding:0 18px;white-space:pre-wrap;overflow-wrap:anywhere;word-break:break-word}
|
|
256
267
|
`;
|
|
257
268
|
export default async function renderLargeText(buffer, target, type = 'txt', context) {
|
|
258
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
269
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
|
259
270
|
const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
|
|
260
271
|
const documentRef = target.ownerDocument;
|
|
261
272
|
const sourceBytes = new Uint8Array(buffer);
|
|
262
|
-
const source = resolveFileViewerTextEncoding(sourceBytes, (_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.encoding);
|
|
273
|
+
const source = resolveFileViewerTextEncoding(sourceBytes, (_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.encoding, (_d = (_c = context === null || context === void 0 ? void 0 : context.options) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.fallbackEncoding);
|
|
263
274
|
const bytes = sourceBytes.subarray(source.bomLength);
|
|
264
|
-
const configuredSegmentBytes = (
|
|
275
|
+
const configuredSegmentBytes = (_f = (_e = context === null || context === void 0 ? void 0 : context.options) === null || _e === void 0 ? void 0 : _e.text) === null || _f === void 0 ? void 0 : _f.maxRenderedLineBytes;
|
|
265
276
|
const segmentBytes = Number.isFinite(configuredSegmentBytes)
|
|
266
277
|
? clamp(Math.trunc(Number(configuredSegmentBytes)), 1024, 256 * 1024)
|
|
267
278
|
: DEFAULT_LARGE_TEXT_LINE_SEGMENT_BYTES;
|
|
268
|
-
const configuredOverscan = (
|
|
279
|
+
const configuredOverscan = (_h = (_g = context === null || context === void 0 ? void 0 : context.options) === null || _g === void 0 ? void 0 : _g.text) === null || _h === void 0 ? void 0 : _h.virtualOverscanLines;
|
|
269
280
|
const overscan = Number.isFinite(configuredOverscan)
|
|
270
281
|
? clamp(Math.trunc(Number(configuredOverscan)), 2, 100)
|
|
271
282
|
: DEFAULT_LARGE_TEXT_OVERSCAN_LINES;
|
|
272
|
-
const showToolbar = ((
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const showLineNumbers = ((_k = (_j = context === null || context === void 0 ? void 0 : context.options) === null || _j === void 0 ? void 0 : _j.text) === null || _k === void 0 ? void 0 : _k.lineNumbers) !== false;
|
|
276
|
-
const wrapLongLines = ((_m = (_l = context === null || context === void 0 ? void 0 : context.options) === null || _l === void 0 ? void 0 : _l.text) === null || _m === void 0 ? void 0 : _m.wrapLongLines) === true;
|
|
283
|
+
const showToolbar = ((_k = (_j = context === null || context === void 0 ? void 0 : context.options) === null || _j === void 0 ? void 0 : _j.text) === null || _k === void 0 ? void 0 : _k.toolbar) !== false;
|
|
284
|
+
const showLineNumbers = ((_m = (_l = context === null || context === void 0 ? void 0 : context.options) === null || _l === void 0 ? void 0 : _l.text) === null || _m === void 0 ? void 0 : _m.lineNumbers) !== false;
|
|
285
|
+
const wrapLongLines = ((_p = (_o = context === null || context === void 0 ? void 0 : context.options) === null || _o === void 0 ? void 0 : _o.text) === null || _p === void 0 ? void 0 : _p.wrapLongLines) === true;
|
|
277
286
|
let disposed = false;
|
|
278
287
|
let zoom = 1;
|
|
279
288
|
let scheduledFrame = 0;
|
|
@@ -313,7 +322,7 @@ export default async function renderLargeText(buffer, target, type = 'txt', cont
|
|
|
313
322
|
root.append(toolbar);
|
|
314
323
|
}
|
|
315
324
|
target.replaceChildren(style, root);
|
|
316
|
-
(
|
|
325
|
+
(_q = context === null || context === void 0 ? void 0 : context.onProgressiveRender) === null || _q === void 0 ? void 0 : _q.call(context);
|
|
317
326
|
const index = await buildLargeTextIndex(bytes, source.encoding, target, progress => {
|
|
318
327
|
if (!disposed) {
|
|
319
328
|
status.textContent = t('text.code.indexingLargeFile', { progress });
|
|
@@ -651,11 +660,17 @@ export default async function renderLargeText(buffer, target, type = 'txt', cont
|
|
|
651
660
|
const maxMatches = Math.max(1, options.maxMatches || DEFAULT_FILE_VIEWER_SEARCH_MAX_MATCHES);
|
|
652
661
|
const expression = createLargeTextSearchRegExp(query, options);
|
|
653
662
|
const encoder = new TextEncoder();
|
|
663
|
+
const singleByte = isSingleByteFileViewerTextEncoding(index.encoding);
|
|
654
664
|
const encodedQueryBytes = index.encoding === 'gb18030'
|
|
655
665
|
? query.length * 4
|
|
656
|
-
:
|
|
666
|
+
: singleByte
|
|
667
|
+
? query.length
|
|
668
|
+
: encoder.encode(query).byteLength;
|
|
657
669
|
const overlap = clamp(encodedQueryBytes * 2, 256, 64 * 1024);
|
|
658
670
|
const advanceBytesForCharacters = (start, end, characterCount) => {
|
|
671
|
+
if (singleByte) {
|
|
672
|
+
return clamp(characterCount, 0, Math.max(0, end - start));
|
|
673
|
+
}
|
|
659
674
|
if (index.encoding !== 'gb18030') {
|
|
660
675
|
const decoded = createFileViewerTextDecoder(index.encoding).decode(index.bytes.subarray(start, end));
|
|
661
676
|
return encoder.encode(decoded.slice(0, characterCount)).byteLength;
|
|
@@ -753,7 +768,7 @@ export default async function renderLargeText(buffer, target, type = 'txt', cont
|
|
|
753
768
|
getState: getZoomState,
|
|
754
769
|
subscribe: zoomEmitter.subscribe
|
|
755
770
|
});
|
|
756
|
-
(
|
|
771
|
+
(_r = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _r === void 0 ? void 0 : _r.call(context, { print: false, exportHtml: false });
|
|
757
772
|
viewport.addEventListener('scroll', scheduleRender, { passive: true });
|
|
758
773
|
viewport.addEventListener('click', event => {
|
|
759
774
|
var _a, _b;
|
|
@@ -779,7 +794,7 @@ export default async function renderLargeText(buffer, target, type = 'txt', cont
|
|
|
779
794
|
lineSegments.set(lineIndex, clamp(next, 0, segmentCount - 1));
|
|
780
795
|
renderWindow(true);
|
|
781
796
|
});
|
|
782
|
-
const ResizeObserverCtor = (
|
|
797
|
+
const ResizeObserverCtor = (_s = getWindow(target)) === null || _s === void 0 ? void 0 : _s.ResizeObserver;
|
|
783
798
|
const resizeObserver = ResizeObserverCtor
|
|
784
799
|
? new ResizeObserverCtor(() => {
|
|
785
800
|
if (wrapLongLines) {
|
package/dist/lrc.d.ts
ADDED
package/dist/lrc.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { createFileViewerZoomChangeEmitter, decodeFileViewerTextBuffer, registerFileViewerZoomProvider, resolveFileViewerLocale, unregisterFileViewerZoomProvider } from '@file-viewer/core';
|
|
2
|
+
import { formatLrcTimestamp, parseLrc } from './lrcParser.js';
|
|
3
|
+
import renderCode from './code.js';
|
|
4
|
+
const labels = {
|
|
5
|
+
'en-US': ['Annotated', 'Lyrics', 'Source', 'Wrap lines', 'Static lyrics preview; no audio playback.'],
|
|
6
|
+
'zh-CN': ['时间标注', '歌词', '原文', '自动换行', '静态歌词预览,不播放音频。'],
|
|
7
|
+
'ja-JP': ['時間付き', '歌詞', '原文', '折り返し', '歌詞の静的プレビューです。音声は再生しません。'],
|
|
8
|
+
'de-DE': ['Mit Zeitangaben', 'Liedtext', 'Quelltext', 'Zeilenumbruch', 'Statische Liedtextvorschau ohne Audiowiedergabe.']
|
|
9
|
+
};
|
|
10
|
+
const css = `
|
|
11
|
+
.fv-lrc{--lrc-bg:#fff;--lrc-fg:#1f2937;height:100%;min-height:0;display:flex;flex-direction:column;color:var(--lrc-fg);background:var(--lrc-bg);font:14px/1.65 system-ui,sans-serif}
|
|
12
|
+
.fv-lrc .lrc-toolbar{display:flex;flex-wrap:wrap;gap:6px;padding:10px;border-bottom:1px solid #8885;align-items:center}
|
|
13
|
+
.fv-lrc .lrc-toolbar[hidden]{display:none}
|
|
14
|
+
.fv-lrc button{font:inherit;color:inherit;background:transparent;border:1px solid #8887;border-radius:5px;padding:4px 10px;cursor:pointer}
|
|
15
|
+
.fv-lrc button[aria-pressed=true]{background:#8883;font-weight:600}
|
|
16
|
+
.fv-lrc .lrc-scroll{flex:1;min-height:0;overflow:auto;padding:14px;scrollbar-gutter:stable}
|
|
17
|
+
.fv-lrc .lrc-content{font-size:var(--lrc-font-size,16px);min-width:0}
|
|
18
|
+
.fv-lrc .lrc-cue{display:flex;gap:1em;min-height:1.8em;align-items:baseline}
|
|
19
|
+
.fv-lrc .lrc-time{font:0.8em ui-monospace,monospace;white-space:nowrap;flex:0 0 8.5em;position:sticky;left:0;background:var(--lrc-bg);z-index:1}
|
|
20
|
+
.fv-lrc .lrc-phrase{white-space:pre-wrap;overflow-wrap:anywhere;min-width:0;flex:1}
|
|
21
|
+
.fv-lrc .lrc-role{font-size:.65em;margin-right:.6em}
|
|
22
|
+
.fv-lrc ruby{display:inline-flex;flex-direction:column-reverse;vertical-align:bottom;white-space:pre}.fv-lrc rt{display:block;font:0.55em ui-monospace,monospace;opacity:.72}
|
|
23
|
+
.fv-lrc .lrc-source{font:inherit;white-space:pre-wrap;overflow-wrap:anywhere;margin:0}
|
|
24
|
+
.fv-lrc[data-wrap=false] .lrc-content{width:max-content;min-width:100%}
|
|
25
|
+
.fv-lrc[data-wrap=false] .lrc-source,.fv-lrc[data-wrap=false] .lrc-phrase{white-space:pre;overflow-wrap:normal}
|
|
26
|
+
.fv-lrc .lrc-metadata{white-space:pre-wrap;overflow-wrap:anywhere;font-size:.85em;opacity:.8;margin-bottom:1em}
|
|
27
|
+
.fv-lrc .lrc-note{font-size:.85em;opacity:.75}
|
|
28
|
+
[data-viewer-theme=dark] .fv-lrc,.fv-lrc[data-viewer-theme=dark]{--lrc-bg:#101820;--lrc-fg:#e5e7eb;color-scheme:dark}
|
|
29
|
+
@media (prefers-color-scheme:dark){[data-viewer-theme=system] .fv-lrc,.fv-lrc[data-viewer-theme=system]{--lrc-bg:#101820;--lrc-fg:#e5e7eb;color-scheme:dark}}
|
|
30
|
+
@media print{.fv-lrc{--lrc-bg:#fff;--lrc-fg:#1f2937;height:auto}.fv-lrc .lrc-toolbar{display:none}.fv-lrc .lrc-scroll{overflow:visible}.fv-lrc .lrc-time{position:static}}
|
|
31
|
+
`;
|
|
32
|
+
export default async function renderLrc(buffer, target, type, context) {
|
|
33
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
34
|
+
if ((_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.aborted)
|
|
35
|
+
throw new DOMException('Aborted', 'AbortError');
|
|
36
|
+
if (buffer.byteLength > 4000000)
|
|
37
|
+
return renderCode(buffer, target, type, context);
|
|
38
|
+
const source = decodeFileViewerTextBuffer(buffer, (_c = (_b = context === null || context === void 0 ? void 0 : context.options) === null || _b === void 0 ? void 0 : _b.text) === null || _c === void 0 ? void 0 : _c.encoding, (_e = (_d = context === null || context === void 0 ? void 0 : context.options) === null || _d === void 0 ? void 0 : _d.text) === null || _e === void 0 ? void 0 : _e.fallbackEncoding).text;
|
|
39
|
+
let model;
|
|
40
|
+
try {
|
|
41
|
+
model = parseLrc(source);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (!(error instanceof RangeError))
|
|
45
|
+
throw error;
|
|
46
|
+
return renderCode(buffer, target, type, context);
|
|
47
|
+
}
|
|
48
|
+
const doc = target.ownerDocument;
|
|
49
|
+
const words = labels[resolveFileViewerLocale(context === null || context === void 0 ? void 0 : context.options)];
|
|
50
|
+
const root = doc.createElement('div');
|
|
51
|
+
root.className = 'fv-lrc';
|
|
52
|
+
root.dataset.wrap = String(((_g = (_f = context === null || context === void 0 ? void 0 : context.options) === null || _f === void 0 ? void 0 : _f.text) === null || _g === void 0 ? void 0 : _g.wrapLongLines) !== false);
|
|
53
|
+
root.dataset.viewerTheme = ((_h = context === null || context === void 0 ? void 0 : context.options) === null || _h === void 0 ? void 0 : _h.theme) || 'system';
|
|
54
|
+
const style = doc.createElement('style');
|
|
55
|
+
style.textContent = css;
|
|
56
|
+
const toolbar = doc.createElement('div');
|
|
57
|
+
toolbar.className = 'lrc-toolbar';
|
|
58
|
+
toolbar.hidden = ((_k = (_j = context === null || context === void 0 ? void 0 : context.options) === null || _j === void 0 ? void 0 : _j.text) === null || _k === void 0 ? void 0 : _k.toolbar) === false;
|
|
59
|
+
const scroller = doc.createElement('div');
|
|
60
|
+
scroller.className = 'lrc-scroll';
|
|
61
|
+
const content = doc.createElement('div');
|
|
62
|
+
content.className = 'lrc-content';
|
|
63
|
+
scroller.append(content);
|
|
64
|
+
const buttons = words.slice(0, 3).map((text, index) => {
|
|
65
|
+
const button = doc.createElement('button');
|
|
66
|
+
button.type = 'button';
|
|
67
|
+
button.textContent = text;
|
|
68
|
+
button.dataset.lrcMode = String(index);
|
|
69
|
+
toolbar.append(button);
|
|
70
|
+
return button;
|
|
71
|
+
});
|
|
72
|
+
const wrap = doc.createElement('button');
|
|
73
|
+
wrap.type = 'button';
|
|
74
|
+
wrap.textContent = words[3];
|
|
75
|
+
wrap.dataset.lrcWrap = '';
|
|
76
|
+
wrap.setAttribute('aria-pressed', root.dataset.wrap);
|
|
77
|
+
toolbar.append(wrap);
|
|
78
|
+
const note = doc.createElement('span');
|
|
79
|
+
note.className = 'lrc-note';
|
|
80
|
+
note.textContent = words[4];
|
|
81
|
+
toolbar.append(note);
|
|
82
|
+
root.append(style, toolbar, scroller);
|
|
83
|
+
target.replaceChildren(root);
|
|
84
|
+
let disposed = false;
|
|
85
|
+
let mode = 0;
|
|
86
|
+
let zoom = 1;
|
|
87
|
+
const emitter = createFileViewerZoomChangeEmitter();
|
|
88
|
+
function getState() {
|
|
89
|
+
return { scale: zoom, label: `${Math.round(zoom * 100)}%`, canZoomIn: zoom < 2.6, canZoomOut: zoom > 0.6, canReset: zoom !== 1, minScale: 0.6, maxScale: 2.6 };
|
|
90
|
+
}
|
|
91
|
+
function setZoom(scale) {
|
|
92
|
+
if (!disposed && Number.isFinite(scale)) {
|
|
93
|
+
zoom = Math.max(0.6, Math.min(2.6, Math.round(scale * 100) / 100));
|
|
94
|
+
root.style.setProperty('--lrc-font-size', `${16 * zoom}px`);
|
|
95
|
+
emitter.emit();
|
|
96
|
+
}
|
|
97
|
+
return getState();
|
|
98
|
+
}
|
|
99
|
+
function show() {
|
|
100
|
+
if (disposed)
|
|
101
|
+
return;
|
|
102
|
+
buttons.forEach((button, index) => button.setAttribute('aria-pressed', String(mode === index)));
|
|
103
|
+
const fragment = doc.createDocumentFragment();
|
|
104
|
+
if (mode === 2) {
|
|
105
|
+
const pre = doc.createElement('pre');
|
|
106
|
+
pre.className = 'lrc-source';
|
|
107
|
+
pre.textContent = source;
|
|
108
|
+
fragment.append(pre);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const metadata = doc.createElement('div');
|
|
112
|
+
metadata.className = 'lrc-metadata';
|
|
113
|
+
metadata.textContent = model.metadata.map(entry => `${entry.key}: ${entry.value}`).join('\n');
|
|
114
|
+
fragment.append(metadata);
|
|
115
|
+
for (const cue of model.cues) {
|
|
116
|
+
const row = doc.createElement('div');
|
|
117
|
+
row.className = 'lrc-cue';
|
|
118
|
+
row.dataset.sourceLine = String(cue.line);
|
|
119
|
+
if (cue.timeMs !== null)
|
|
120
|
+
row.dataset.timeMs = String(cue.timeMs);
|
|
121
|
+
if (mode === 0) {
|
|
122
|
+
const time = doc.createElement('span');
|
|
123
|
+
time.className = 'lrc-time';
|
|
124
|
+
time.textContent = cue.timeMs === null ? '·' : formatLrcTimestamp(cue.timeMs);
|
|
125
|
+
row.append(time);
|
|
126
|
+
}
|
|
127
|
+
const phrase = doc.createElement('span');
|
|
128
|
+
phrase.className = 'lrc-phrase';
|
|
129
|
+
phrase.dir = 'auto';
|
|
130
|
+
if (cue.role) {
|
|
131
|
+
const role = doc.createElement('sup');
|
|
132
|
+
role.className = 'lrc-role';
|
|
133
|
+
role.textContent = cue.role;
|
|
134
|
+
phrase.append(role);
|
|
135
|
+
}
|
|
136
|
+
for (const word of cue.words) {
|
|
137
|
+
if (mode === 0 && word.timeMs !== null) {
|
|
138
|
+
const ruby = doc.createElement('ruby');
|
|
139
|
+
ruby.dataset.wordTimeMs = String(word.timeMs);
|
|
140
|
+
ruby.append(doc.createTextNode(word.text));
|
|
141
|
+
const rt = doc.createElement('rt');
|
|
142
|
+
rt.textContent = formatLrcTimestamp(word.timeMs);
|
|
143
|
+
ruby.append(rt);
|
|
144
|
+
phrase.append(ruby);
|
|
145
|
+
}
|
|
146
|
+
else
|
|
147
|
+
phrase.append(doc.createTextNode(word.text));
|
|
148
|
+
}
|
|
149
|
+
row.append(phrase);
|
|
150
|
+
fragment.append(row);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
content.replaceChildren(fragment);
|
|
154
|
+
}
|
|
155
|
+
function click(event) {
|
|
156
|
+
const element = event.target;
|
|
157
|
+
const button = element === null || element === void 0 ? void 0 : element.closest('button');
|
|
158
|
+
if (!button || !toolbar.contains(button))
|
|
159
|
+
return;
|
|
160
|
+
if (button === wrap) {
|
|
161
|
+
root.dataset.wrap = String(root.dataset.wrap !== 'true');
|
|
162
|
+
wrap.setAttribute('aria-pressed', root.dataset.wrap);
|
|
163
|
+
}
|
|
164
|
+
else if (button.dataset.lrcMode !== undefined) {
|
|
165
|
+
mode = Number(button.dataset.lrcMode);
|
|
166
|
+
show();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function unmount() {
|
|
170
|
+
var _a;
|
|
171
|
+
if (disposed)
|
|
172
|
+
return;
|
|
173
|
+
disposed = true;
|
|
174
|
+
toolbar.removeEventListener('click', click);
|
|
175
|
+
(_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener('abort', unmount);
|
|
176
|
+
unregisterFileViewerZoomProvider(root);
|
|
177
|
+
emitter.clear();
|
|
178
|
+
root.remove();
|
|
179
|
+
}
|
|
180
|
+
toolbar.addEventListener('click', click);
|
|
181
|
+
(_l = context === null || context === void 0 ? void 0 : context.signal) === null || _l === void 0 ? void 0 : _l.addEventListener('abort', unmount, { once: true });
|
|
182
|
+
registerFileViewerZoomProvider(root, { getState, setZoom, zoomIn: () => setZoom(zoom + .1), zoomOut: () => setZoom(zoom - .1), resetZoom: () => setZoom(1), subscribe: emitter.subscribe });
|
|
183
|
+
show();
|
|
184
|
+
return { $el: root, unmount };
|
|
185
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface LrcWord {
|
|
2
|
+
text: string;
|
|
3
|
+
timeMs: number | null;
|
|
4
|
+
}
|
|
5
|
+
export interface LrcCue {
|
|
6
|
+
line: number;
|
|
7
|
+
timeMs: number | null;
|
|
8
|
+
text: string;
|
|
9
|
+
role: 'M' | 'F' | 'D' | null;
|
|
10
|
+
words: LrcWord[];
|
|
11
|
+
}
|
|
12
|
+
export interface LrcDocument {
|
|
13
|
+
source: string;
|
|
14
|
+
offsetMs: number;
|
|
15
|
+
metadata: {
|
|
16
|
+
key: string;
|
|
17
|
+
value: string;
|
|
18
|
+
}[];
|
|
19
|
+
cues: LrcCue[];
|
|
20
|
+
}
|
|
21
|
+
export declare function parseLrcTimestamp(value: string): number | null;
|
|
22
|
+
export declare function formatLrcTimestamp(ms: number): string;
|
|
23
|
+
export declare function parseLrc(source: string): LrcDocument;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const STAMP = /^(-?)(\d{1,8}):([0-5]\d)(?:[.:](\d{1,3}))?$/;
|
|
2
|
+
export function parseLrcTimestamp(value) {
|
|
3
|
+
const match = STAMP.exec(value);
|
|
4
|
+
if (!match)
|
|
5
|
+
return null;
|
|
6
|
+
const ms = Number(match[2]) * 60000 + Number(match[3]) * 1000 + Number((match[4] || '').padEnd(3, '0'));
|
|
7
|
+
return (match[1] ? -1 : 1) * ms;
|
|
8
|
+
}
|
|
9
|
+
export function formatLrcTimestamp(ms) {
|
|
10
|
+
const absolute = Math.abs(ms);
|
|
11
|
+
return `${ms < 0 ? '-' : ''}${String(Math.floor(absolute / 60000)).padStart(2, '0')}:${String(Math.floor(absolute / 1000) % 60).padStart(2, '0')}.${String(absolute % 1000).padStart(3, '0')}`;
|
|
12
|
+
}
|
|
13
|
+
export function parseLrc(source) {
|
|
14
|
+
if (source.length > 2000000)
|
|
15
|
+
throw new RangeError('LRC source preview required');
|
|
16
|
+
const document = { source, offsetMs: 0, metadata: [], cues: [] };
|
|
17
|
+
let role = null;
|
|
18
|
+
let wordCount = 0;
|
|
19
|
+
const lines = source.replace(/^\uFEFF/, '').split(/\r\n|\r|\n/);
|
|
20
|
+
for (let index = 0; index < lines.length; index++) {
|
|
21
|
+
const line = lines[index];
|
|
22
|
+
const metadata = /^\[([a-z][a-z0-9_-]*):(.*)\]\s*$/i.exec(line);
|
|
23
|
+
if (metadata) {
|
|
24
|
+
document.metadata.push({ key: metadata[1], value: metadata[2] });
|
|
25
|
+
if (metadata[1].toLowerCase() === 'offset' && /^[+-]?\d+$/.test(metadata[2].trim())) {
|
|
26
|
+
const offset = Number(metadata[2]);
|
|
27
|
+
if (Number.isSafeInteger(offset) && Math.abs(offset) <= 1e12)
|
|
28
|
+
document.offsetMs = offset;
|
|
29
|
+
}
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
let cursor = 0;
|
|
33
|
+
const timestamps = [];
|
|
34
|
+
while (line[cursor] === '[') {
|
|
35
|
+
const end = line.indexOf(']', cursor + 1);
|
|
36
|
+
if (end < 0 || end - cursor > 24)
|
|
37
|
+
break;
|
|
38
|
+
const timestamp = parseLrcTimestamp(line.slice(cursor + 1, end));
|
|
39
|
+
if (timestamp === null)
|
|
40
|
+
break;
|
|
41
|
+
timestamps.push(timestamp);
|
|
42
|
+
cursor = end + 1;
|
|
43
|
+
if (timestamps.length + document.cues.length > 10000)
|
|
44
|
+
throw new RangeError('Too many LRC cues');
|
|
45
|
+
}
|
|
46
|
+
let body = line.slice(cursor);
|
|
47
|
+
const speaker = timestamps.length ? /^([MFD]):/.exec(body) : null;
|
|
48
|
+
if (speaker) {
|
|
49
|
+
role = speaker[1];
|
|
50
|
+
body = body.slice(2);
|
|
51
|
+
}
|
|
52
|
+
const words = [];
|
|
53
|
+
const marker = /<(-?\d{1,8}:[0-5]\d(?:[.:]\d{1,3})?)>/g;
|
|
54
|
+
let wordStart = 0;
|
|
55
|
+
let timeMs = null;
|
|
56
|
+
for (let match = marker.exec(body); match; match = marker.exec(body)) {
|
|
57
|
+
if (match.index > wordStart)
|
|
58
|
+
words.push({ text: body.slice(wordStart, match.index), timeMs });
|
|
59
|
+
timeMs = parseLrcTimestamp(match[1]);
|
|
60
|
+
wordStart = match.index + match[0].length;
|
|
61
|
+
}
|
|
62
|
+
if (wordStart < body.length || !words.length)
|
|
63
|
+
words.push({ text: body.slice(wordStart), timeMs });
|
|
64
|
+
wordCount += words.length * Math.max(1, timestamps.length);
|
|
65
|
+
if (wordCount > 50000 || document.cues.length >= 10000)
|
|
66
|
+
throw new RangeError('LRC source preview required');
|
|
67
|
+
const text = words.map(word => word.text).join('');
|
|
68
|
+
const times = timestamps.length ? timestamps : [null];
|
|
69
|
+
for (const timestamp of times) {
|
|
70
|
+
const shift = timestamp === null ? 0 : timestamp - timestamps[0];
|
|
71
|
+
document.cues.push({ line: index + 1, timeMs: timestamp, text, role: timestamps.length ? role : null,
|
|
72
|
+
words: words.map(word => ({ text: word.text, timeMs: word.timeMs === null ? null : word.timeMs + shift })) });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const cue of document.cues) {
|
|
76
|
+
if (cue.timeMs !== null)
|
|
77
|
+
cue.timeMs -= document.offsetMs;
|
|
78
|
+
for (const word of cue.words)
|
|
79
|
+
if (word.timeMs !== null)
|
|
80
|
+
word.timeMs -= document.offsetMs;
|
|
81
|
+
}
|
|
82
|
+
return document;
|
|
83
|
+
}
|
package/dist/markdown.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { type FileRenderContext, type FileViewerRenderedInstance } from '@file-viewer/core';
|
|
2
|
-
export declare const sanitizeMermaidSvg: (documentRef: Document, svg: string) => SVGSVGElement;
|
|
2
|
+
export declare const sanitizeMermaidSvg: (documentRef: Document, svg: string, mathLabels?: boolean) => SVGSVGElement;
|
|
3
3
|
export declare const stripMarkdownFrontmatter: (text: string) => string;
|
|
4
4
|
export default function renderMarkdown(buffer: ArrayBuffer, target: HTMLDivElement, context?: FileRenderContext): Promise<FileViewerRenderedInstance>;
|