Upload files to "renderer"

This commit is contained in:
2026-11-11 00:09:12 +00:00
parent b2e527f8e7
commit 82de74d545
5 changed files with 1465 additions and 0 deletions
+297
View File
@@ -0,0 +1,297 @@
// Spelling and thesaurus panel. The dictionaries live in the main process (offline).
(function () {
'use strict';
function init(opts) {
const ed = opts.editor;
const api = opts.api;
const toast = opts.toast;
const wn = window.wn;
const panel = document.createElement('div');
panel.className = 'proof';
panel.hidden = true;
document.body.appendChild(panel);
const state = { tab: 'spell', cur: null, pos: 0, ignore: new Set(), count: 0, target: null, lastWord: '', cfg: opts.getConfig() };
function el(tag, text, cls) {
const e = document.createElement(tag);
if (text !== undefined && text !== null) e.textContent = text;
if (cls) e.className = cls;
return e;
}
const noFocus = (b) => { b.addEventListener('mousedown', (e) => { if (e.target.tagName !== 'INPUT' && e.target.tagName !== 'SELECT') e.preventDefault(); }); return b; };
function btn(label, fn, cls) { const b = noFocus(el('button', label, cls)); b.addEventListener('click', fn); return b; }
function reveal(a, b) {
ed.focus();
ed.setSelectionRange(a, b);
ed.blur();
ed.focus();
}
/* ---------- shell ---------- */
function close() { panel.hidden = true; }
function open(tab) {
state.tab = tab;
panel.hidden = false;
draw();
if (tab === 'spell') { state.pos = 0; scan(0); } else { startThesaurus(); }
}
function draw() {
panel.innerHTML = '';
const head = el('div', null, 'proofHead');
const tabs = el('div', null, 'proofTabs');
[['spell', 'Spelling'], ['thes', 'Thesaurus']].forEach((t) => {
const b = noFocus(el('button', t[1]));
b.classList.toggle('on', state.tab === t[0]);
b.addEventListener('click', () => open(t[0]));
tabs.appendChild(b);
});
head.appendChild(tabs);
head.appendChild(btn('Close', close, 'proofX'));
panel.appendChild(head);
const body = el('div', null, 'proofBody');
body.id = 'proofBody';
panel.appendChild(body);
if (state.tab === 'spell') drawSpellShell(body); else drawThesShell(body);
}
/* ---------- spelling ---------- */
function drawSpellShell(body) {
const opts1 = el('div', null, 'proofRow');
const lang = document.createElement('select');
[['en-US', 'English (US)'], ['en-GB', 'English (UK)']].forEach((p) => { const o = el('option', p[1]); o.value = p[0]; lang.appendChild(o); });
lang.value = state.cfg.spellLang || 'en-US';
lang.addEventListener('change', () => {
state.cfg.spellLang = lang.value;
wn.setConfig({ spellLang: lang.value });
state.ignore.clear();
state.pos = 0;
scan(0);
});
opts1.appendChild(lang);
const lab = el('label', null, 'proofChk');
const cb = document.createElement('input');
cb.type = 'checkbox';
cb.checked = state.cfg.underline !== false;
cb.addEventListener('change', () => {
state.cfg.underline = cb.checked;
ed.spellcheck = cb.checked;
wn.setConfig({ underline: cb.checked });
});
lab.appendChild(cb);
lab.appendChild(document.createTextNode(' Underline as I type'));
opts1.appendChild(lab);
body.appendChild(opts1);
const area = el('div', null, 'spellArea');
area.id = 'spellArea';
body.appendChild(area);
body.appendChild(el('div', 'Markup such as templates, link targets, tags and web addresses is skipped.', 'proofHint'));
}
async function scan(from) {
const area = document.getElementById('spellArea');
if (!area) return;
area.textContent = 'Checking...';
const r = await wn.spellCheck(ed.value);
if (!r.ok) { area.textContent = 'Spell check failed: ' + r.error; return; }
const list = r.list.filter((x) => !state.ignore.has(x.word.toLowerCase()));
state.count = list.length;
let next = list.find((x) => x.start >= from);
let wrapped = false;
if (!next && list.length) { next = list[0]; wrapped = true; }
if (!next) { state.cur = null; area.innerHTML = ''; area.appendChild(el('div', 'No spelling problems found.', 'proofDone')); return; }
state.cur = next;
showWord(area, next, wrapped);
}
async function showWord(area, item, wrapped) {
reveal(item.start, item.end);
area.innerHTML = '';
area.appendChild(el('div', 'Not in dictionary' + (state.count > 1 ? ' (' + state.count + ' left)' : '') + (wrapped ? ', back at the top' : ''), 'proofLabel'));
area.appendChild(el('div', item.word, 'badWord'));
const v = ed.value;
const ctx = v.slice(Math.max(0, item.start - 30), item.start).replace(/\s+/g, ' ');
const after = v.slice(item.end, item.end + 30).replace(/\s+/g, ' ');
const c = el('div', null, 'proofCtx');
c.appendChild(document.createTextNode('...' + ctx));
c.appendChild(el('b', item.word));
c.appendChild(document.createTextNode(after + '...'));
area.appendChild(c);
const input = document.createElement('input');
input.className = 'proofInput';
input.spellcheck = false;
input.placeholder = 'Type a correction or pick a suggestion';
area.appendChild(input);
const sug = el('div', null, 'sugList');
area.appendChild(sug);
const acts = el('div', null, 'proofActs');
const go = (fn) => () => { if (state.cur === item) fn(); };
acts.appendChild(btn('Replace', go(() => replaceCur(input.value.trim()))));
acts.appendChild(btn('Replace all', go(() => replaceAll(input.value.trim()))));
acts.appendChild(btn('Ignore', go(() => { state.pos = item.end; scan(state.pos); })));
acts.appendChild(btn('Ignore all', go(() => { state.ignore.add(item.word.toLowerCase()); state.pos = item.end; scan(state.pos); })));
acts.appendChild(btn('Add to dictionary', go(async () => { await wn.spellAdd(item.word); toast('Added "' + item.word + '" to your dictionary.'); state.pos = item.end; scan(state.pos); })));
area.appendChild(acts);
input.addEventListener('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); replaceCur(input.value.trim()); } });
const list = await wn.spellSuggest(item.word);
if (state.cur !== item) return;
if (!list.length) sug.appendChild(el('div', 'No suggestions.', 'proofHint'));
list.forEach((w, i) => {
const word = /^[A-Z]/.test(item.word) && !/^[A-Z]/.test(w) ? w.charAt(0).toUpperCase() + w.slice(1) : w;
const b = noFocus(el('button', word, 'tm'));
b.addEventListener('click', () => { input.value = word; });
b.addEventListener('dblclick', () => replaceCur(word));
sug.appendChild(b);
if (i === 0) input.value = word;
});
}
function replaceCur(word) {
const c = state.cur;
if (!c || !word) return;
api.replaceRange(c.start, c.end, word);
state.pos = c.start + word.length;
scan(state.pos);
}
async function replaceAll(word) {
const c = state.cur;
if (!c || !word) return;
const r = await wn.spellCheck(ed.value);
if (!r.ok) return;
const hits = r.list.filter((x) => x.word === c.word).sort((a, b) => b.start - a.start);
hits.forEach((h) => api.replaceRange(h.start, h.end, word));
toast('Replaced ' + hits.length + (hits.length === 1 ? ' word.' : ' words.'));
scan(0);
}
/* ---------- thesaurus ---------- */
function wordAtCaret() {
const v = ed.value;
let a = ed.selectionStart;
let b = ed.selectionEnd;
if (a === b) {
while (a > 0 && /[\p{L}'-]/u.test(v.charAt(a - 1))) a--;
while (b < v.length && /[\p{L}'-]/u.test(v.charAt(b))) b++;
}
const w = v.slice(a, b).trim();
if (!w || w.length > 40 || /\n/.test(w)) return null;
return { a, b, word: w };
}
function drawThesShell(body) {
const row = el('div', null, 'proofRow');
const input = document.createElement('input');
input.id = 'thesInput';
input.placeholder = 'Word to look up';
input.spellcheck = false;
input.value = state.lastWord;
input.addEventListener('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); lookup(input.value); } });
row.appendChild(input);
row.appendChild(btn('Look up', () => lookup(input.value)));
body.appendChild(row);
body.appendChild(el('div', 'Click a word to replace the selected word with it. Ctrl+click a word to look that one up instead.', 'proofHint'));
const res = el('div', null, 'thesRes');
res.id = 'thesRes';
body.appendChild(res);
body.appendChild(el('div', 'Offline thesaurus from Princeton WordNet.', 'proofHint'));
}
function startThesaurus() {
const w = wordAtCaret();
if (w) {
state.target = { a: w.a, b: w.b };
state.lastWord = w.word;
const i = document.getElementById('thesInput');
if (i) i.value = w.word;
lookup(w.word);
} else {
const i = document.getElementById('thesInput');
if (i) i.focus();
const r = document.getElementById('thesRes');
if (r) r.appendChild(el('div', 'Select a word in the editor, or type one above.', 'proofHint'));
}
}
function matchCase(orig, w) {
if (!orig) return w;
if (orig.length > 1 && orig === orig.toUpperCase() && /[A-Z]/.test(orig)) return w.toUpperCase();
if (/^[A-Z]/.test(orig)) return w.charAt(0).toUpperCase() + w.slice(1);
return w;
}
async function lookup(word) {
word = String(word || '').trim();
const res = document.getElementById('thesRes');
if (!res) return;
if (!word) return;
state.lastWord = word;
const i = document.getElementById('thesInput');
if (i) i.value = word;
res.textContent = 'Looking up...';
const r = await wn.thesaurus(word);
res.innerHTML = '';
if (!r.ok) { res.textContent = 'Lookup failed: ' + r.error; return; }
if (!r.groups.length) { res.appendChild(el('div', 'No synonyms found for "' + word + '".', 'proofDone')); return; }
if (r.lemma.toLowerCase() !== word.toLowerCase()) res.appendChild(el('div', 'Showing results for "' + r.lemma + '".', 'proofHint'));
r.groups.forEach((g) => {
res.appendChild(el('div', g.pos, 'thesPos'));
g.senses.forEach((s) => {
const box = el('div', null, 'thesSense');
box.appendChild(el('div', s.gloss, 'thesGloss'));
const chips = el('div', null, 'thesChips');
s.words.forEach((w) => {
const b = noFocus(el('button', w, 'chip'));
b.addEventListener('click', (ev) => {
if (ev.ctrlKey || ev.metaKey) { lookup(w); return; }
replaceTarget(w);
});
chips.appendChild(b);
});
box.appendChild(chips);
res.appendChild(box);
});
});
}
function replaceTarget(w) {
let t = state.target;
if (!t) {
const c = wordAtCaret();
if (!c) { toast('Select the word you want to replace first.'); return; }
t = { a: c.a, b: c.b };
}
const orig = ed.value.slice(t.a, t.b);
const nw = matchCase(orig, w);
api.replaceRange(t.a, t.b, nw, t.a, t.a + nw.length);
state.target = { a: t.a, b: t.a + nw.length };
}
/* ---------- buttons ---------- */
const bs = document.getElementById('btnSpell');
const bt = document.getElementById('btnThes');
[[bs, 'spell'], [bt, 'thes']].forEach((p) => {
if (!p[0]) return;
p[0].addEventListener('mousedown', (e) => e.preventDefault());
p[0].addEventListener('click', () => {
if (!panel.hidden && state.tab === p[1]) { close(); return; }
open(p[1]);
});
});
document.addEventListener('keydown', (e) => { if (e.key === 'Escape' && !panel.hidden && !document.querySelector('.overlay')) close(); });
ed.spellcheck = state.cfg.underline !== false;
return { open, close };
}
window.SwiftProof = { init };
})();