Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- **Tidier PDF app code, continued** (v62, PDF app). The split into smaller files continues. Nothing changes for teachers.

- **Tidier PDF app code** (v61, PDF app). app/main.js is split into smaller files. Nothing changes for teachers.

- **Plan for tidying the PDF app's code** (v60, docs only). `docs/plans/APP_MAIN_SPLIT.md` describes, step by step, how to split `app/main.js` into smaller files without changing anything the app does. Nothing changes for teachers yet.
Expand Down
76 changes: 76 additions & 0 deletions app/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// The PDF app's main objects: the page viewer, the ink layer, Find, Select and Type, plus the status line.

import { makeSelect, makeTyping } from '../shared/ink-core.js';
import { Finder } from './find.js';
import { Ink, PT_PER_UNIT } from './ink.js';
import { Viewer } from './viewer.js';
import { closeMenus, fitToolbar } from './menus.js';
import { markDirty, writeNow } from './saving.js';
import { D, doc, findPanel, pagesEl, statusEl, T, tr } from './state.js';
import { refreshRedo } from './toolbar.js';

export let viewer, finder, ink, typing;
export function initCore() {
viewer = new Viewer(pagesEl, {
show: (P) => ink.show(P),
hide: (P) => ink.hide(P),
resize: (P) => ink.resize(P),
textReady: (P) => finder.showHit(P),
turn: (n) => (D && D.rot && D.rot[n]) || 0,
});
finder = new Finder(viewer, findPanel, setStatus, () => tr);
ink = new Ink(viewer, T, () => {
refreshRedo();
markDirty();
});
ink.onTextCopied = () => setStatus(tr('st_text_copied'));
ink.attach(pagesEl);
/* Select (shared with the overlay): circle ink to send a copy to another period, copy and paste it, move it or delete it */
ink.sel = makeSelect({
t: () => tr,
status: setStatus,
pageList: (n) => ink.pageList(n),
all: () => ink.strokes,
replace: (next) => ink.replaceAll(next),
addTo: (name, list) => ink.addTo(name, list),
periods: () => Object.keys(D.periods),
active: () => D.active,
preview: (n, hide, add) => ink.preview(n, hide, add),
host: (n) => ink.host(n),
pastePage: (n) => (n && viewer.pages[n - 1] && isOnScreen(n) ? n : viewer.currentPage()),
onOpen: () => closeMenus('sel'),
});
typing = ink.typing = makeTyping({
t: () => tr,
status: setStatus,
pageList: (n) => ink.pageList(n),
all: () => ink.strokes,
replace: (next) => ink.replaceAll(next),
preview: (n, hide, add) => ink.preview(n, hide, add),
host: (n) => ink.host(n),
style: () => ({ c: T.penColor, f: Math.round((10 + T.width * 4) * PT_PER_UNIT * 100) / 100 }),
page: (n) => n,
});
window.__overboard = {
viewer,
ink,
get doc() {
return doc;
},
get data() {
return D;
},
flush: () => writeNow(),
}; // for tests and the curious
}

export function setStatus(msg) {
// a message that takes more (or fewer) lines changes the toolbar's height, so fit it again
const h = statusEl.offsetHeight;
statusEl.textContent = msg;
if (statusEl.offsetHeight !== h && doc) queueMicrotask(() => fitToolbar());
}
export function isOnScreen(n) {
const r = viewer.pages[n - 1].el.getBoundingClientRect();
return r.bottom > 0 && r.top < window.innerHeight;
}
114 changes: 114 additions & 0 deletions app/exporting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Export PDF (with the warning about covers) and the .overboard.json ink file (Save ink / Load ink).

import { flashBtn } from '../shared/ink-core.js';
import { stripExt } from './image.js';
import * as store from './store.js';
import { ink, setStatus, viewer } from './core.js';
import { markDirty, writeNow } from './saving.js';
import { $, D, doc, setInkData, tr } from './state.js';
import { refreshRedo } from './toolbar.js';
import { refresh, renderPeriods } from './tools.js';

/* ---------- export annotated PDF ---------- */
const baseName = () => stripExt(doc.name);
// only the active period is exported, so the name says which: Moby_Dick_annotated_P1.pdf
const exportName = () => (baseName() + '_annotated_' + D.active).replace(/\s+/g, '_') + '.pdf';
const coverWarn = $('#ob-coverwarn');
function ask(dialog) {
// resolves true for the dialog's "ok" button
return new Promise((done) => {
const onClick = (e) => {
const b = e.target.closest('button');
if (!b && e.target !== dialog) return;
dialog.removeEventListener('click', onClick);
dialog.close();
done(!!b && b.value === 'ok');
};
dialog.addEventListener('click', onClick);
dialog.addEventListener(
'cancel',
() => {
dialog.removeEventListener('click', onClick);
done(false);
},
{ once: true }
);
dialog.showModal();
});
}
export async function exportPdf(btn) {
if (!doc) return;
let covered = false;
await writeNow();
// an empty notebook still exports: printable paper
// ... and so does a PDF with turned pages: a copy the right way up
if (!ink.count() && !doc.notebook && !Object.keys(D.rot || {}).length) {
flashBtn(btn, tr('no_ink_yet'), 'warn');
setStatus(tr('st_no_ink_export', { p: D.active }));
return;
}
btn.disabled = true;
setStatus(tr('st_making_pdf'));
try {
const { exportAnnotatedPdf, hasCovers } = await import('./export.js');
// covers hide answers in the PDF too, but the text under them is still in the file: say so plainly
covered = hasCovers(ink.byPage);
if (covered && !(await ask(coverWarn))) {
setStatus(tr('st_export_cancelled', { p: D.active }));
return;
}
const bytes = new Uint8Array(await doc.file.slice(0, doc.base).arrayBuffer()); // without the stored ink
const out = await exportAnnotatedPdf(bytes, doc.pdf, ink.byPage, D.rot, (n) =>
setStatus(tr('st_making_pdf_n', { n }))
);
store.download(new Blob([out], { type: 'application/pdf' }), exportName());
setStatus(tr('st_pdf_downloaded', { p: D.active }) + (covered ? tr('st_covered_note') : ''));
flashBtn(btn, tr('downloaded_flash'), 'good');
} catch (e) {
console.error(e);
setStatus(tr('st_export_failed', { e: (e && e.message) || e }));
flashBtn(btn, tr('not_saved'), 'warn');
} finally {
btn.disabled = false;
}
}

/* ---------- ink sidecar file (.overboard.json) ---------- */
export async function saveInkFile(btn) {
if (!doc) return;
await writeNow();
store.download(new Blob([store.makeSidecar(D, doc)], { type: 'application/json' }), baseName() + '.overboard.json');
flashBtn(btn, tr('saved_flash'), 'good');
setStatus(tr('st_ink_file_downloaded'));
}
export async function importInk(file) {
try {
const { data, pdf } = store.parseSidecar(await file.text());
if (
pdf.sha256 &&
pdf.sha256 !== doc.hash &&
!confirm(tr('confirm_other_pdf', { f: pdf.name || tr('another_pdf') }))
)
return;
const has = Object.values(D.periods).some((l) => l.length);
if (has && !confirm(tr('confirm_replace_ink', { f: file.name }))) return;
setInkData(data);
ink.setData(D);
viewer.applyTurns();
refreshRedo();
renderPeriods();
refresh();
markDirty();
await writeNow();
setStatus(tr('st_ink_loaded_from', { f: file.name }));
} catch (e) {
setStatus(e.message || String(e));
}
}
export function initExporting() {
$('#ob-inkfile').addEventListener('change', (e) => {
const f = e.target.files[0];
e.target.value = '';
if (f && doc) importInk(f);
});
}
4 changes: 2 additions & 2 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OverBoard PDF</title>
<meta name="description" content="OverBoard: write on any PDF on your smartboard. Runs in the browser; nothing is uploaded.">
<meta name="ob-version" content="3157eb5b05a0">
<meta name="ob-version" content="5959bb0b7628">
<meta name="theme-color" content="#145da0">
<link rel="manifest" href="manifest.json">
<link rel="icon" href="icons/favicon.ico" sizes="32x32">
Expand Down Expand Up @@ -47,7 +47,7 @@ <h2 data-t="open_again">Open again</h2>
<p class="note"><span data-th="start_note">Your PDF and your ink stay in this browser on this computer. Nothing is uploaded.
Have a Word file? In Word, choose File → Save as PDF, then drop the PDF here.
Have an HTML worksheet? Drop it here to get a <i>_board.html</i> copy you can write on.</span>
<br><a href="https://github.com/BPMData/OverBoard" data-t="about">About OverBoard</a> · <a href="report/?from=start" target="_blank" data-t="board_help">Board not working?</a> · <span id="ob-release" title="OverBoard version">v61</span></p>
<br><a href="https://github.com/BPMData/OverBoard" data-t="about">About OverBoard</a> · <a href="report/?from=start" target="_blank" data-t="board_help">Board not working?</a> · <span id="ob-release" title="OverBoard version">v62</span></p>
<p class="note ob-langpick"><label><span aria-hidden="true">🌐</span> <select id="ob-lang" aria-label="Language" data-ta="language" data-tt="language" title="Language"></select></label></p>
</div>
</main>
Expand Down
4 changes: 2 additions & 2 deletions app/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export class Ink {
this.downAt = 0;
this.lastCleared = null; // { page, before } or { all } so Undo can bring back what Clear took
this.redos = []; // what Undo took back, newest last: a stroke, or { cleared: page }
this.sel = null; // the Select tool (makeSelect in shared/ink-core.js), set by main.js
this.typing = null; // the Type tool (makeTyping), set by main.js
this.sel = null; // the Select tool (makeSelect in shared/ink-core.js), set by core.js
this.typing = null; // the Type tool (makeTyping), set by core.js
this.selP = null; // the page a Select gesture started on
this.pv = null; // { page, hide, add }: a selection being dragged, drawn in its new spot
this.laser = makeLaser(); // laser pointer marks: they fade away and are never ink
Expand Down
Loading
Loading