Merge pull request #17 from Alpaq92/fix/sidebar-and-text-fallback
Read unknown files as text; fix the light-mode sidebar
This commit is contained in:
@@ -38,6 +38,7 @@ To give it a try without installing anything, visit the [live demo](https://alpa
|
||||
- Compare two document versions (PDF, DjVu, e-books, Office, text) — side-by-side / unified / inline, with word-level highlights, pinnable blame-aware tooltips, and page-by-page diff for multi-page PDF/DjVu
|
||||
- Client-side only — documents are never uploaded or transmitted
|
||||
- Multilingual UI — auto-detects your browser language with a manual switcher (English, Polish, Spanish, French, German, Portuguese, Chinese, Japanese, Russian)
|
||||
- Light / dark / follow-the-system theme, with a localized switcher beside the language one — defaults to following the OS, and the light palette is [Open Color](https://yeun.github.io/open-color/)
|
||||
|
||||
## Embedding
|
||||
|
||||
|
||||
+41
-3
@@ -29,6 +29,29 @@ const CODE_EXTS = ['js','mjs','cjs','jsx','ts','tsx','json','jsonc','xml','html'
|
||||
'yaml','yml','css','scss','less','py','java','c','h','cpp','cc','hpp','cs','go','rs',
|
||||
'rb','php','sh','bash','sql','kt','swift','toml','ini','diff']
|
||||
|
||||
/**
|
||||
* Heuristic: does this look like a binary file rather than text?
|
||||
*
|
||||
* A NUL byte is the classic marker — no text encoding this decoder handles produces one — and a
|
||||
* high density of other C0 control characters catches the rest. Deliberately cheap and
|
||||
* conservative: the cost of a false "binary" is a file the user must rename to read, while a
|
||||
* false "text" is a screen of replacement characters, so it errs toward trying to read.
|
||||
*
|
||||
* @param {ArrayBuffer} buffer First few KB of the file.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function looksBinary(buffer) {
|
||||
const bytes = new Uint8Array(buffer)
|
||||
if (!bytes.length) return false
|
||||
let control = 0
|
||||
for (const b of bytes) {
|
||||
if (b === 0) return true
|
||||
// C0 controls except tab (9), LF (10), CR (13) and form feed (12)
|
||||
if (b < 9 || (b > 13 && b < 32)) control++
|
||||
}
|
||||
return control / bytes.length > 0.3
|
||||
}
|
||||
|
||||
const EXT_MAP = {
|
||||
pdf: 'pdf',
|
||||
ps: 'ps', eps: 'ps', epsf: 'ps',
|
||||
@@ -240,11 +263,26 @@ export class DocumentViewer {
|
||||
// `source` is a Blob/File or an ArrayBuffer.
|
||||
async loadBytes(source, name, mime) {
|
||||
const ext = (name || '').split('.').pop().toLowerCase()
|
||||
const format = EXT_MAP[ext]
|
||||
let format = EXT_MAP[ext]
|
||||
|
||||
if (!format) {
|
||||
this._showError(t('err.unsupported', { ext, list: Object.keys(EXT_MAP).join(', ') }))
|
||||
return
|
||||
// Unknown extension: try to read it as text rather than refusing outright. Most files
|
||||
// without a dedicated renderer — config, logs, dotfiles, source in a language not in
|
||||
// CODE_EXTS — are perfectly readable that way, and refusing them is a worse default than
|
||||
// showing them.
|
||||
//
|
||||
// Binaries are the exception: decoding an executable as UTF-8 produces a screenful of
|
||||
// replacement characters, which is less useful than saying so. Sniff the first 4 KB and
|
||||
// keep the error for that case only. Reading just the head keeps this an early return,
|
||||
// before any loading state has been set up.
|
||||
const head = source instanceof ArrayBuffer
|
||||
? source.slice(0, 4096)
|
||||
: await source.slice(0, 4096).arrayBuffer()
|
||||
if (looksBinary(head)) {
|
||||
this._showError(t('err.unsupported', { ext, list: Object.keys(EXT_MAP).join(', ') }))
|
||||
return
|
||||
}
|
||||
format = 'txt'
|
||||
}
|
||||
|
||||
this._setLoading(true)
|
||||
|
||||
+4
-4
@@ -72,8 +72,8 @@
|
||||
:root:not([data-theme="dark"]) {
|
||||
--bg-app: var(--oc-gray-3);
|
||||
--bg-toolbar: var(--oc-gray-0);
|
||||
--bg-sidebar: #fff;
|
||||
--bg-sidebar-head: var(--oc-gray-1);
|
||||
--bg-sidebar: var(--oc-gray-1);
|
||||
--bg-sidebar-head: var(--oc-gray-2);
|
||||
--bg-field: #fff;
|
||||
--bg-hover: var(--oc-gray-2);
|
||||
--bg-active: var(--oc-gray-3);
|
||||
@@ -99,8 +99,8 @@
|
||||
:root[data-theme="light"] {
|
||||
--bg-app: var(--oc-gray-3);
|
||||
--bg-toolbar: var(--oc-gray-0);
|
||||
--bg-sidebar: #fff;
|
||||
--bg-sidebar-head: var(--oc-gray-1);
|
||||
--bg-sidebar: var(--oc-gray-1);
|
||||
--bg-sidebar-head: var(--oc-gray-2);
|
||||
--bg-field: #fff;
|
||||
--bg-hover: var(--oc-gray-2);
|
||||
--bg-active: var(--oc-gray-3);
|
||||
|
||||
Reference in New Issue
Block a user