Με λίγα λόγια
Ένα κείμενο αίθουσας μουσείου με τις σημειώσεις μιας κριτού. Κάθε σημείωση σκιάζει τις λέξεις της και βάζει μια αριθμημένη ετικέτα στο δεξί περιθώριο, με διακεκομμένη γραμμή. Ένα πάτημα την ανοίγει κάτω από τις λέξεις.
Τι θα στοιχειοθετήσετε
Ένα κείμενο αίθουσας μουσείου για τον μηχανισμό των Αντικυθήρων, όπως το βλέπει η επιμελήτρια της έκθεσης με τις σημειώσεις της κριτού. Καθεμιά από τις τέσσερις σημειώσεις χρωματίζει τις λέξεις της και τραβά μια διακεκομμένη γραμμή ως μια αριθμημένη σημαία στο φαρδύ εξωτερικό περιθώριο. Η σημείωση 2 είναι ανοιχτή: ένα αναδυόμενο με το σχόλιο της κριτού κάθεται ακριβώς κάτω από τις λέξεις που αφορά, με τη μύτη του στην τελευταία. Πατήστε άλλη σημαία, ή τις χρωματισμένες λέξεις, και το αναδυόμενο μετακινείται εκεί· κυλήστε ή αλλάξτε μέγεθος και μένει με τις λέξεις του.
Αυτή η συνταγή απαντά
- Πώς τοποθετώ μια σημείωση ή ένα αναδυόμενο πλαίσιο δίπλα σε ένα χωρίο της σελιδοποιημένης σελίδας;
Η σύντομη απάντηση
const view = createViewer(document.getElementById('viewer'), {
document: doc, markdown, zoom: 'fit-width', maxZoom: 1, labels: L.viewer,
marks: { noteSide: 'end' }, // the flags in the outer margin, not the nearer one
annotations: notes.map(({ id, label, range }) => ({ id, kind: 'note', range, label })),
});
let open = null; // the note the popover shows
const place = () => {
if (!open) return;
// Under the last piece of the noted words: page px, turned into the window's px.
const last = rangeRects(doc, open.range).at(-1);
const at = last && view.pageToClient(last.pageIndex, last.rect.x, last.rect.y + last.rect.height);
pop.hidden = !at;
if (at) Object.assign(pop.style, { left: `${at.x}px`, top: `${at.y + 8}px` });
};
const show = (id) => {
open = notes.find((n) => n.id === id) ?? null;
if (open) fill(open);
place();
};
view.on('annotationclick', ({ id }) => show(id)); // a flag or the tinted words
view.on('zoomchange', place); // the pages moved under the popover
view.element.addEventListener('scroll', place, { capture: true, passive: true });
show('n2');
Υλικά
- Λειτουργίες
- Σημάδια σχολιασμούΓεωμετρία κειμένου (εύρη και σημεία)Πρόγραμμα προβολής σελίδων (postext-viewer)Γραμματοσειρές πριν από τη σελιδοποίηση
- Γραμματοσειρές
- Alegreya, Alegreya Sans (SIL OFL 1.1)
- Αρχεία
- Κανένα: κάθε εικόνα σχεδιάζεται με κώδικα
Προετοιμασία
#1 · Αφήστε περιθώριο για τις σημαίες
const config = {
locale: t({ en: 'en-gb', es: 'es' }),
colorPalette,
page: { sizePreset: 'custom', width: mm(160), height: mm(220), dpi: 150,
margins: { top: mm(20), bottom: mm(24), left: mm(18), right: mm(42) } },
layout: { layoutType: 'single' },
bodyText: { fontFamily: 'Alegreya', fontSize: pt(10.5), lineHeight: pt(15),
color: col('ink'), boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
textAlign: 'left', firstLineIndent: mm(5), indentAfterHeading: false,
hyphenation: { enabled: true }, avoidWidows: true, avoidOrphans: true, avoidRunts: true },
headings: { fontFamily: 'Alegreya Sans', color: col('accent'), fontWeight: 700, levels: [
{ level: 1, fontSize: pt(22), lineHeight: pt(26), marginTop: pt(4), marginBottom: pt(15) },
] },
header: { elements: [] },
footer: { elements: [
{ kind: 'text', id: 'folio', content: '{pageNumber}', fontFamily: 'Alegreya Sans',
fontSize: pt(8.5), fontWeight: 700, color: col('accent'), align: 'left',
placement: { anchor: { to: 'page', edge: 'bottom-left' },
offset: { x: mm(18), y: mm(-12) } } },
] },
};
Ένα εξωτερικό περιθώριο 42 mm χωρά τις σημαίες χωρίς να στριμώχνει το κείμενο. Η επιλογή marks της προβολής περνά το noteSide: 'end' στη διάταξη των σημαδιών, οπότε όλες οι σημαίες πάνε στο δεξί περιθώριο· χωρίς αυτό η καθεμιά παίρνει το περιθώριο που είναι πιο κοντά στη γραμμή της. Σημαίες στο ίδιο ύψος στοιβάζονται προς τα κάτω χωρίς να επικαλύπτονται, και μια πολύ μακριά ετικέτα κόβεται με αποσιωπητικά.
#2 · Κρατήστε τις σημειώσεις ως εύρη με ετικέτα
// A tool stores the offsets; the pen finds them from quotes so that both editions run.
const notes = t({
en: [['In the spring of 1901', 'The wreck was found in 1900; the mechanism came up in 1901.'],
['a small ball, half silver and half dark',
'Is the ball still there? If not, say it is reconstructed.'],
['nineteen-year calendar', 'Name it: the Metonic cycle. Readers will look it up.'],
['X-ray scans', 'Give the year of the scans, 2005.']],
es: [['En la primavera de 1901', 'El naufragio se halló en 1900; el mecanismo salió en 1901.'],
['una bolita, mitad plateada y mitad oscura',
'¿Se conserva la bolita? Si no, di que es una reconstrucción.'],
['calendario de diecinueve años', 'Dale su nombre: el ciclo metónico. El lector lo buscará.'],
['radiografías', 'Pon el año de las radiografías, 2005.']],
}).map(([quote, body], i) => {
const start = markdown.indexOf(quote);
const range = { start, end: start + quote.length };
return { id: `n${i + 1}`, label: String(i + 1), body, range };
});
Ένα σημάδι σημείωσης χρειάζεται ένα εύρος και μια σύντομη label για τη σημαία του, έναν αριθμό ή ένα αρχικό. Το σχόλιο, η συντάκτριά του και η συζήτηση μένουν στις εγγραφές του ιστότοπου· το σημάδι λέει μόνο πού ανήκουν.
#3 · Τοποθετήστε το αναδυόμενο από συντεταγμένες σελίδας
Ο κώδικας είναι η σύντομη απάντηση παραπάνω. Το rangeRects δίνει τα κομμάτια των σχολιασμένων λέξεων σε px σελίδας· η κάτω αριστερή γωνία του τελευταίου κομματιού, δοσμένη στο pageToClient, επιστρέφει σε px παραθύρου, έτοιμη για στοιχείο με position: fixed. Η προβολή αναφέρει το αρχικό ζουμ στο πρώτο της καρέ, οπότε ένας ακροατής του zoomchange ξανατοποθετεί το αρχικό αναδυόμενο αφού ζωγραφιστούν οι σελίδες, και ένας ακροατής κύλισης στο view.element το κρατά κάτω από τις λέξεις του.
Η πλήρης συνταγή
// ═══ Postext Cookbook · Nº 215 · Notes in the margin, with a popover ═════════════════
// https://postext.dev/en/cookbook/margin-notes-with-popovers
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Alegreya, Alegreya Sans (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// A museum's gallery text with a reviewer's notes on it: each note tints its words and
// puts a numbered flag in the outer margin. Pressing a flag opens the note in a popover,
// which the host places under the words from their page coordinates. Note 2 opens itself.
import { buildDocumentWithFonts, renderPageToCanvas, rangeRects } from 'https://esm.sh/postext';
import { createViewer } from 'https://esm.sh/postext-viewer';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'margin-notes-with-popovers';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
ink: '#211f1c', // text
accent: '#9a4b16', // bronze: title and folio
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
{ id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
// #endregion
// #region page: a wide outer margin, where the reviewer's flags go
const config = {
locale: t({ en: 'en-gb', es: 'es' }),
colorPalette,
page: { sizePreset: 'custom', width: mm(160), height: mm(220), dpi: 150,
margins: { top: mm(20), bottom: mm(24), left: mm(18), right: mm(42) } },
layout: { layoutType: 'single' },
bodyText: { fontFamily: 'Alegreya', fontSize: pt(10.5), lineHeight: pt(15),
color: col('ink'), boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
textAlign: 'left', firstLineIndent: mm(5), indentAfterHeading: false,
hyphenation: { enabled: true }, avoidWidows: true, avoidOrphans: true, avoidRunts: true },
headings: { fontFamily: 'Alegreya Sans', color: col('accent'), fontWeight: 700, levels: [
{ level: 1, fontSize: pt(22), lineHeight: pt(26), marginTop: pt(4), marginBottom: pt(15) },
] },
header: { elements: [] },
footer: { elements: [
{ kind: 'text', id: 'folio', content: '{pageNumber}', fontFamily: 'Alegreya Sans',
fontSize: pt(8.5), fontWeight: 700, color: col('accent'), align: 'left',
placement: { anchor: { to: 'page', edge: 'bottom-left' },
offset: { x: mm(18), y: mm(-12) } } },
] },
<main id="pages">
<div id="viewer"></div>
<section id="popover" role="dialog" aria-labelledby="popover-title" hidden>
<h3 id="popover-title"></h3>
<p class="by"></p>
<p class="body"></p>
<button type="button"></button>
</section>
</main>
/* The host's frame: the viewer fills the window, the popover floats over it.
The viewer fills its container, so the container gets a height. */
main#pages { display: block; height: max(560px, 100vh); padding: 0; }
#viewer { width: 100%; height: 100%; }
#viewer .postext-viewer { --pv-background: #e9e5df; --pv-focus: #9a4b16; }
/* Placed in window px from pageToClient, so fixed, not absolute. */
#popover {
position: fixed; z-index: 2; width: 300px; margin-left: -12px; padding: 14px 16px 12px;
background: #fff; border: 1px solid #cfc7bc; border-radius: 8px;
box-shadow: 0 10px 30px -8px rgb(0 0 0 / .35); font: 15px/1.45 system-ui, sans-serif;
color: #211f1c;
}
#popover::before {
content: ''; position: absolute; top: -7px; left: 20px; width: 12px; height: 12px;
background: #fff; border: solid #cfc7bc; border-width: 1px 0 0 1px; transform: rotate(45deg);
}
#popover h3 { margin: 0; font-size: 15px; color: #6d3fbf; }
#popover .by { margin: 0 0 8px; font-size: 13px; color: #5d5850; }
#popover .body { margin: 0 0 10px; }
#popover button {
min-height: 44px; padding: 0 14px; border: 1px solid #cfc7bc; border-radius: 6px;
background: #f6f3ee; font: inherit; color: inherit; cursor: pointer;
}
#popover button:focus-visible { outline: 2px solid #9a4b16; outline-offset: 2px; }
Το συντεθειμένο script.js εκτελείται ως έχει: επικολλήστε το στο module script οποιασδήποτε σελίδας ή ανοίξτε τη συνταγή στο CodePen. Φάκελος της συνταγής στο GitHub ↗ (ανοίγει σε νέα καρτέλα)
Παραλλαγές
#Άνοιγμα σημείωσης με το πέρασμα του δείκτη
Το συμβάν hover φέρνει τα id των σημαδιών κάτω από τον δείκτη, το πάνω πρώτο.
view.on('annotationclick', ({ id }) => show(id)); // a flag or the tinted words
+view.on('hover', ({ annotations }) => annotations[0] && show(annotations[0]));#Κάθε σημαία στο πιο κοντινό περιθώριο
Σε σελίδα με δύο στήλες, οι σημειώσεις της αριστερής στήλης θα πάνε τότε στο αριστερό περιθώριο.
- marks: { noteSide: 'end' }, // the flags in the outer margin, not the nearer one
+ marks: {}, // each flag in the margin nearer its wordsΕυχαριστίες
- Συνταγή
- Ignacio Ferro
- Κείμενο
- Πρωτότυπο κείμενο, CC BY 4.0
- Γραμματοσειρές
- Alegreya (SIL OFL 1.1) · Alegreya Sans (SIL OFL 1.1)
- Κώδικας
- MIT, όπως το Postext
Επεξεργασία αυτού του κειμένου ↗ (ανοίγει σε νέα καρτέλα)Φάκελος της συνταγής στο GitHub ↗ (ανοίγει σε νέα καρτέλα)



