@file-viewer/renderer-text
Advanced tools
+80
-9
| import { marked } from 'marked'; | ||
| import { createFileViewerZoomChangeEmitter as createZoomChangeEmitter, readFileViewerText as readText, registerFileViewerZoomProvider, unregisterFileViewerZoomProvider, } from '@file-viewer/core'; | ||
| import { createFileViewerZoomChangeEmitter as createZoomChangeEmitter, readFileViewerText as readText, registerFileViewerZoomProvider, resolveFileViewerFitScale, unregisterFileViewerZoomProvider, } from '@file-viewer/core'; | ||
| const markdownStyle = ` | ||
@@ -34,10 +34,37 @@ .markdown-viewer{min-height:100%;padding:28px 16px 48px;background:var(--file-viewer-render-surface-background,#eef1f4);overflow:auto;box-sizing:border-box} | ||
| }; | ||
| const MARKDOWN_BASE_MAX_WIDTH = 980; | ||
| const MARKDOWN_BASE_PADDING = 45; | ||
| const MARKDOWN_BASE_FONT_SIZE = 16; | ||
| const MARKDOWN_MIN_SCALE = 0.6; | ||
| const MARKDOWN_MAX_SCALE = 2.4; | ||
| const clampZoom = (value) => { | ||
| return Math.min(2.4, Math.max(0.6, Number(value.toFixed(2)))); | ||
| return Math.min(MARKDOWN_MAX_SCALE, Math.max(MARKDOWN_MIN_SCALE, Number(value.toFixed(2)))); | ||
| }; | ||
| const applyMarkdownZoom = (host, zoom) => { | ||
| host.style.setProperty('--markdown-max-width', `${980 * zoom}px`); | ||
| host.style.setProperty('--markdown-padding', `${45 * zoom}px`); | ||
| host.style.setProperty('--markdown-font-size', `${16 * zoom}px`); | ||
| host.style.setProperty('--markdown-max-width', `${MARKDOWN_BASE_MAX_WIDTH * zoom}px`); | ||
| host.style.setProperty('--markdown-padding', `${MARKDOWN_BASE_PADDING * zoom}px`); | ||
| host.style.setProperty('--markdown-font-size', `${MARKDOWN_BASE_FONT_SIZE * zoom}px`); | ||
| }; | ||
| const readCssPixels = (value) => { | ||
| const pixels = Number.parseFloat(value || ''); | ||
| return Number.isFinite(pixels) ? pixels : 0; | ||
| }; | ||
| const readMarkdownViewport = (root, request) => { | ||
| var _a; | ||
| const viewportWidth = Math.max(1, request.viewportWidth || root.clientWidth || 0); | ||
| const viewportHeight = Math.max(1, request.viewportHeight || root.clientHeight || 0); | ||
| const view = root.ownerDocument.defaultView; | ||
| const computed = (_a = view === null || view === void 0 ? void 0 : view.getComputedStyle) === null || _a === void 0 ? void 0 : _a.call(view, root); | ||
| const mobile = viewportWidth <= 767; | ||
| const horizontalPadding = computed | ||
| ? readCssPixels(computed.paddingLeft) + readCssPixels(computed.paddingRight) | ||
| : mobile ? 20 : 32; | ||
| const verticalPadding = computed | ||
| ? readCssPixels(computed.paddingTop) + readCssPixels(computed.paddingBottom) | ||
| : mobile ? 42 : 76; | ||
| return { | ||
| width: Math.max(1, viewportWidth - horizontalPadding), | ||
| height: Math.max(1, viewportHeight - verticalPadding), | ||
| }; | ||
| }; | ||
| let mermaidRenderSequence = 0; | ||
@@ -157,10 +184,11 @@ let mermaidRenderQueue = Promise.resolve(); | ||
| await renderEmbeddedMermaid(article, (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.theme); | ||
| const baseContentHeight = Math.max(article.scrollHeight || 0, article.offsetHeight || 0, 0); | ||
| const getZoomState = () => ({ | ||
| scale: zoom, | ||
| label: `${Math.round(zoom * 100)}%`, | ||
| canZoomIn: zoom < 2.4, | ||
| canZoomOut: zoom > 0.6, | ||
| canZoomIn: zoom < MARKDOWN_MAX_SCALE, | ||
| canZoomOut: zoom > MARKDOWN_MIN_SCALE, | ||
| canReset: zoom !== 1, | ||
| minScale: 0.6, | ||
| maxScale: 2.4, | ||
| minScale: MARKDOWN_MIN_SCALE, | ||
| maxScale: MARKDOWN_MAX_SCALE, | ||
| }); | ||
@@ -173,2 +201,44 @@ const setZoom = (scale) => { | ||
| }; | ||
| const fitMarkdown = (request) => { | ||
| var _a, _b; | ||
| const viewport = readMarkdownViewport(root, request); | ||
| const minScale = (_a = request.minScale) !== null && _a !== void 0 ? _a : MARKDOWN_MIN_SCALE; | ||
| const requestedMaxScale = (_b = request.maxScale) !== null && _b !== void 0 ? _b : MARKDOWN_MAX_SCALE; | ||
| // A flowing document must not be fitted against its full scroll height. | ||
| // Auto therefore follows the readable content width. It stays at 1:1 by | ||
| // default, but an explicit maxScale can opt into the wider DOCX-like fit | ||
| // requested by the caller. | ||
| const maxScale = request.mode === 'auto' && request.maxScale == null | ||
| ? Math.max(minScale, Math.min(1, requestedMaxScale)) | ||
| : requestedMaxScale; | ||
| const scale = resolveFileViewerFitScale({ | ||
| mode: request.mode === 'auto' ? 'width' : request.mode, | ||
| viewportWidth: viewport.width, | ||
| viewportHeight: viewport.height, | ||
| contentWidth: MARKDOWN_BASE_MAX_WIDTH, | ||
| contentHeight: baseContentHeight || viewport.height, | ||
| currentScale: zoom, | ||
| minScale, | ||
| maxScale, | ||
| }); | ||
| if (!scale) { | ||
| return { | ||
| applied: false, | ||
| mode: request.mode, | ||
| resize: request.resize, | ||
| source: request.source, | ||
| reason: 'unmeasurable', | ||
| provider: 'zoom', | ||
| }; | ||
| } | ||
| const state = setZoom(scale); | ||
| return { | ||
| applied: true, | ||
| mode: request.mode, | ||
| resize: request.resize, | ||
| scale: state.scale, | ||
| source: request.source, | ||
| provider: 'zoom', | ||
| }; | ||
| }; | ||
| registerFileViewerZoomProvider(root, { | ||
@@ -179,2 +249,3 @@ zoomIn: () => setZoom(zoom + 0.1), | ||
| setZoom, | ||
| fit: fitMarkdown, | ||
| getState: getZoomState, | ||
@@ -181,0 +252,0 @@ subscribe: zoomEmitter.subscribe, |
+5
-5
| { | ||
| "name": "@file-viewer/renderer-text", | ||
| "version": "2.2.2", | ||
| "version": "2.2.3", | ||
| "private": false, | ||
| "type": "module", | ||
| "description": "Standalone code, text, Markdown, patch diff, and Git bundle renderer plugin for Flyfish File Viewer.", | ||
| "description": "Standalone code, text, Markdown, patch diff, and Git bundle renderer plugin for File Viewer.", | ||
| "keywords": [ | ||
@@ -26,4 +26,4 @@ "file-viewer", | ||
| "author": { | ||
| "name": "Wangyu", | ||
| "email": "wybaby168@gmail.com" | ||
| "name": "Yu Wang", | ||
| "email": "admin@flyfish.dev" | ||
| }, | ||
@@ -61,3 +61,3 @@ "repository": { | ||
| "dependencies": { | ||
| "@file-viewer/core": "2.2.2", | ||
| "@file-viewer/core": "2.2.3", | ||
| "diff2html": "^3.4.56", | ||
@@ -64,0 +64,0 @@ "highlight.js": "^11.11.1", |
112758
3%1936
3.81%+ Added
- Removed
Updated