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
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ You can also [use TreeFern in your browser](https://docrountree.github.io/treefe
- **Editing**: add daughter clades, rename clades and attach free-form notes to them, collapse or expand subtrees, delete subtrees, and move a subtree to a new mother clade with cut and paste.
- **Pictures in the notes**: drag pictures onto a clade, the side panel or the Edit box. They are kept in the cladogram file, scaled to at most 1200 px.
- **Autosave and live sync**: each change is written to disk atomically, and the file is reloaded automatically if another program modifies it.
- **Dynamic labels**: labels too small to read are hidden until you zoom in, and labels reposition themselves to avoid overlapping. Labels can be placed over or beside nodes.
- **Search and navigation**: find clades by name from the side panel, follow the trail of mother clades above the selection, and move around the tree entirely by keyboard or entirely by mouse.
- **Export**: save the current view or the whole tree as a PNG image (at 1x, 2x or 4x resolution) or a scalable SVG, optionally with a transparent background, or copy it to the clipboard.
- **Command palette**: Ctrl+K finds any command by typing part of its name.
- **Dark and light themes.**
- **Remembered preferences**: layout, label placement, theme and recently opened files persist between sessions.
- **Remembered preferences**: layout, theme and recently opened files persist between sessions.

## Download

Expand Down Expand Up @@ -179,11 +178,9 @@ Because the format is plain JSON, files can be generated or edited by scripts an
</tr>
<tr>
<td width="50%"><img alt="Light mode" src="https://github.com/user-attachments/assets/7cbe0f3a-47f7-4e0a-9221-42b328ce9440"></td>
<td width="50%"><img alt="Labels beside nodes" src="https://github.com/user-attachments/assets/e501e873-f8ca-4104-8ab2-2d8eace6a696"></td>
</tr>
<tr>
<td align="center"><em>Light mode</em></td>
<td align="center"><em>Labels beside nodes</em></td>
</tr>
</table>

Expand Down
279 changes: 106 additions & 173 deletions main.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ <h3>Keyboard</h3>
<tr><td>F / Home</td><td>Focus on the clade / fit the whole tree</td></tr>
<tr><td>Ctrl+F</td><td>Search</td></tr>
<tr><td>1 2 3 4</td><td>Standard / Disks / Rectangular cladogram / Circular cladogram layout</td></tr>
<tr><td>L / Shift+D</td><td>Labels over or beside nodes / dark mode</td></tr>
<tr><td>Shift+D</td><td>Dark mode</td></tr>
<tr><td>+ - [ ] , .</td><td>Spread, shrink and curl (Standard layout)</td></tr>
<tr><td>Ctrl+O / Ctrl+S</td><td>Open / save a file</td></tr>
<tr><td>Ctrl+E</td><td>Export an image of the view or the whole tree</td></tr>
Expand Down
45 changes: 37 additions & 8 deletions web/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,39 @@ function showToast(s) {
}

// ---------------------------------------------------------------- hover notes
// Text as nodes, its web addresses made links that open in a new tab: anything
// after http:// or https://, anything starting www., and bare names like
// abc.xyz when they end in a common web suffix (so "e.g." or "life.json" stay
// text; email addresses too). Trailing punctuation stays outside a link, so a
// sentence can end with one; a closing bracket stays in when the link opened it,
// as in Wikipedia's.
const URL_RE = /(?:https?:\/\/|www\.)[^\s<>"]+|(?<![@\w.-])(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:com|org|net|edu|gov|mil|int|info|biz|io|co|ai|app|dev|xyz|me|tv|ly|site|online|tech|wiki|blog|news|museum|science|bio|eco|earth|club|page|uk|us|ca|au|nz|ie|de|fr|es|it|nl|be|at|ch|se|no|fi|dk|is|pl|cz|pt|gr|ru|ua|eu|jp|cn|kr|in|br|mx|ar|za)(?![\w-])(?:[/?#][^\s<>"]*)?/gi;
function trimUrl(u) {
for (;;) {
const last = u.slice(-1);
const open = { ')': '(', ']': '[', '}': '{' }[last];
if (open ? u.split(open).length <= u.split(last).length - 1 : /[.,;:!?'"]/.test(last)) u = u.slice(0, -1);
else return u;
}
}
function linkified(text) {
const out = [];
let at = 0;
for (const m of text.matchAll(URL_RE)) {
m[0] = trimUrl(m[0]);
if (m.index > at) out.push(document.createTextNode(text.slice(at, m.index)));
const a = document.createElement('a');
a.href = /^https?:\/\//i.test(m[0]) ? m[0] : 'https://' + m[0];
a.textContent = m[0];
a.target = '_blank';
a.rel = 'noopener noreferrer';
out.push(a);
at = m.index + m[0].length;
}
if (at < text.length) out.push(document.createTextNode(text.slice(at)));
return out;
}

// A clade's info appears beside the pointer after a short rest on it.
function hover(id, x, y) {
hideTip();
Expand Down Expand Up @@ -186,7 +219,8 @@ function renderPanel() {
}
if (!n.kids.length) { const li = document.createElement('li'); li.textContent = '(none)'; li.style.color = 'var(--dim)'; li.style.cursor = 'default'; kids.append(li); }
const info = $('info');
info.textContent = n.info || 'No info yet. Use Edit to add some.';
if (n.info) info.replaceChildren(...linkified(n.info));
else info.textContent = 'No info yet. Use Edit to add some.';
info.classList.toggle('none', !n.info);
const id = n.id;
renderPics($('pics'), () => doc.find(id)?.images || [], i => {
Expand Down Expand Up @@ -986,7 +1020,7 @@ if ('launchQueue' in window)
});

// ---------------------------------------------------------------- view settings
const prefs = Object.assign({ style: STANDARD, beside: false, theme: matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark' },
const prefs = Object.assign({ style: STANDARD, theme: matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark' },
store.get(PREFS_KEY) || {});
function savePrefs() { store.set(PREFS_KEY, prefs); }

Expand All @@ -1007,7 +1041,6 @@ function setStyle(st) {
savePrefs();
refresh();
}
function setBeside(on) { prefs.beside = on; view.labelsBeside = on; view.draw(); savePrefs(); }
function tune(f) {
f(view.lay);
if (view.lay.style === STANDARD) view.relayout(true);
Expand Down Expand Up @@ -1054,9 +1087,6 @@ const MENUS = {
View: () => [
...STYLES.map((st, i) => ({ label: STYLE_NAMES[st] + ' layout', key: String(i + 1), checked: view.lay.style === st, run: () => setStyle(st) })),
{ sep: true },
{ label: 'Labels over nodes', checked: !view.labelsBeside, run: () => setBeside(false) },
{ label: 'Labels beside nodes', key: 'L', checked: view.labelsBeside, run: () => setBeside(true) },
{ sep: true },
{ label: 'Dark mode', key: 'Shift+D', checked: prefs.theme === 'dark', run: () => applyTheme(prefs.theme === 'dark' ? 'light' : 'dark') },
{ sep: true },
{ label: 'Fit whole tree', key: 'Home', run: () => view.fitView() },
Expand Down Expand Up @@ -1320,7 +1350,6 @@ addEventListener('keydown', e => {
ArrowLeft: () => navigate('left'), ArrowRight: () => navigate('right'),
Home: () => view.fitView(),
1: () => setStyle(STYLES[0]), 2: () => setStyle(STYLES[1]), 3: () => setStyle(STYLES[2]), 4: () => setStyle(STYLES[3]),
l: () => setBeside(!view.labelsBeside),
d: e.shiftKey ? () => applyTheme(prefs.theme === 'dark' ? 'light' : 'dark') : actAdd, // D: add a daughter
'/': focusSearch, '?': () => $('helpDlg').showModal(),
'+': () => tune(l => { l.spread = Math.min(340 * DEG, l.spread + 10 * DEG); }),
Expand Down Expand Up @@ -1356,7 +1385,7 @@ if (CHANNEL) {
document.querySelector('.brand').after(b);
}
view.lay.style = STYLES.includes(prefs.style) ? prefs.style : STANDARD;
view.labelsBeside = !!prefs.beside;
delete prefs.beside, delete prefs.labels; // label settings of older versions
applyTheme(prefs.theme === 'light' ? 'light' : 'dark');

(async () => {
Expand Down
12 changes: 8 additions & 4 deletions web/js/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const POLY_GAP = 0.94; // fraction of its wedge a child fills (both styl
const POLY_ONLY_CHILD = 0.78; // radius of a single child
const POLY_EXP = 0.68; // wedge share = weight^POLY_EXP (both styles)
// standard style
export const STD_NODE = 0.05; // leaf / collapsed node radius, in its own units
export const STD_WIDTH = 0.09; // branch width, in units of the frame it leaves
// Labels inside nodes: every node is a disc of this radius, in its own units
// (1 = its outgoing branch), with its name fitted inside. Tune here; above
// about 0.5 a node meets its daughters.
export const INSIDE_NODE = 0.3;
const STD_FAN_STEP = 15 * DEG; // extra fan angle per child beyond two
const STD_MAX_FAN = 300 * DEG; // fan cap (unless the spread itself is wider)
const STD_WEIGHT_EXP = 0.35; // light sisters are scaled by (w / w_max)^this
Expand Down Expand Up @@ -51,7 +54,7 @@ export class Layout {
count() { return this.loc.size; }
extent(n) { return this.ext.get(n) ?? 1; }
weight(n) { return this.size.get(n) ?? 1; }
own(n) { return this.style === DISKS ? 1 : isClado(this.style) ? CLADO_NODE : hidesKids(n) ? STD_NODE : STD_WIDTH; }
own(n) { return this.style === DISKS ? 1 : isClado(this.style) ? CLADO_NODE : INSIDE_NODE; }

compute(root) {
this.loc.clear(); this.ext.clear(); this.size.clear(); this.leaves.clear(); this.reach.clear();
Expand Down Expand Up @@ -195,10 +198,11 @@ export class Layout {
// direction, and unit = its outgoing branch length. Returns the subtree's
// bounding circle (centre c on its x axis, radius R) and its pieces.
standard(n) {
if (hidesKids(n)) return { c: 0, R: STD_NODE, pieces: [[C(0, 0), STD_NODE]] };
const own = this.own(n);
if (hidesKids(n)) return { c: 0, R: own, pieces: [[C(0, 0), own]] };
const kids = this.ordered(n), m = kids.length;
const sub = kids.map(k => this.standard(k));
const circles = [[C(0, 0), STD_WIDTH]];
const circles = [[C(0, 0), own]];
const placeAt = (i, mid, s) => {
const u = polar(1, mid);
this.loc.set(kids[i], Local(u, scale(u, s), 0));
Expand Down
Loading
Loading