Skip to content

Commit 47b8709

Browse files
committed
added top float support
1 parent 12996f9 commit 47b8709

File tree

3 files changed

+87
-46
lines changed

3 files changed

+87
-46
lines changed

book.js

+85-44
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@
103103
* You should always set this to something much larger than what you will ever
104104
* expect that you book will need.
105105
*
106+
* topfloatSelector: '.pagination-topfloat' -- This is the CSS selector used
107+
* for finding top floats within the HTML code. Top floats are placed on the
108+
* page were they were in the text at the time of document loading, and they
109+
* are not moved later on.In editing environments, the top float should be
110+
* inserted into to additional elements, like this:
111+
*
112+
* <span class='pagination-topfloat'><span><span>This is the top float contents
113+
* </span></span></span>
114+
*
115+
* footnoteSelector: '.pagination-footnote' -- This is the CSS selector used
116+
* for finding footnotes within the HTML code. Footnotes are automatically
117+
* moved if the page of their reference changes. In editing environments, the
118+
* footnote should be inserted into to additional elements, like this:
119+
*
120+
* <span class='pagination-footnote'><span><span>This is a footnote</span>
121+
* </span></span>.
122+
*
106123
* Page style options
107124
*
108125
* These settings provide a way to do simple styling of the page. These
@@ -184,6 +201,8 @@
184201
'autoStart': true,
185202
'numberPages': true,
186203
'divideContents': true,
204+
'footnoteSelector': '.pagination-footnote',
205+
'topfloatSelector': '.pagination-topfloat',
187206
'maxPageNumber': 10000,
188207
'outerMargin': .5,
189208
'innerMargin': .8,
@@ -267,7 +286,8 @@
267286
"\n.pagination-contents {display: -webkit-flex; -webkit-flex: 1;}"
268287
/* There seems to be a bug in the new flexbox model code which requires the
269288
* height to be set to an arbitrary value (which is ignored).
270-
*/ + "\n.pagination-contents {height: 0px;}" +
289+
*/
290+
+ "\n.pagination-contents {height: 0px;}" +
271291
"\n.pagination-contents-column {-webkit-flex: 1;}" + "\nbody {" +
272292
"counter-reset: pagination-footnote pagination-footnote-reference;}" +
273293
"\n.pagination-footnote::before {" +
@@ -350,7 +370,8 @@
350370
/* This seems to be a bug in Webkit. But unless we set the width of the
351371
* original element that is being flown, some elements extend beyond the
352372
* contentsContainer's width.
353-
*/ + "\n.pagination-contents-item {width:" + columnWidth + ";}" +
373+
*/
374+
+ "\n.pagination-contents-item {width:" + columnWidth + ";}" +
354375
"\n.pagination-frontmatter-contents {width:" + contentsWidth + ";}"
355376
// Footnotes in non-CSS Regions browsers will render as right margin notes.
356377
+ "\n.pagination-simple .pagination-footnote > span {" +
@@ -459,7 +480,7 @@
459480
contentsContainer.classList.add('pagination-contents-container');
460481

461482
topFloats = document.createElement('div');
462-
topFloats.classList.add('pagination-top-floats');
483+
topFloats.classList.add('pagination-topfloats');
463484

464485
contents = document.createElement('div');
465486
contents.classList.add('pagination-contents');
@@ -849,10 +870,11 @@
849870

850871
this.escapes = {}
851872
this.escapes.footnote = [];
873+
this.escapes.topfloat = [];
852874

853875
this.escapeStylesheets = {}
854876
this.escapeStylesheets.footnote = document.createElement('style');
855-
877+
this.escapeStylesheets.topfloat = document.createElement('style');
856878
};
857879

858880
flowObject.prototype.redoPages = false;
@@ -879,7 +901,9 @@
879901
this.namedFlow = document.webkitGetNamedFlows()[this.name];
880902
this.addOrRemovePages();
881903
this.setupReflow();
904+
this.findAllTopfloats();
882905
this.findAllFootnotes();
906+
this.layoutTopfloats();
883907
this.layoutFootnotes();
884908
this.setupFootnoteReflow();
885909
if (pagination.config('numberPages')) {
@@ -937,7 +961,7 @@
937961
}
938962
};
939963

940-
// Footnote handling
964+
// Footnote and top float handling
941965

942966
flowObject.prototype.findFootnoteReferencePage = function (
943967
footnoteReference) {
@@ -1073,8 +1097,13 @@
10731097
}
10741098
};
10751099

1076-
1077-
flowObject.prototype.findAllFootnotes = function() {
1100+
flowObject.prototype.findAllTopfloats = function () {
1101+
// Find all the topfloats in the text and prepare them for flow.
1102+
this.findAllEscapes('topfloat');
1103+
}
1104+
1105+
1106+
flowObject.prototype.findAllFootnotes = function () {
10781107
// Find all the footnotes in the text and prepare them for flow.
10791108
this.findAllEscapes('footnote');
10801109
}
@@ -1084,16 +1113,16 @@
10841113
// Find all the escapes (footnotes, topfloats) in the text and prepare them for flow.
10851114

10861115
if (this.escapeStylesheets[escapeType].parentNode === document.head) {
1087-
// Remove all previous footnote stylesheet rules.
1116+
// Remove all previous stylesheet rules of the same escape type.
10881117
document.head.removeChild(this.escapeStylesheets[escapeType]);
10891118
this.escapeStylesheets[escapeType].innerHTML = '';
10901119
}
10911120

10921121
/* Look for all the items that have "pagination-"+escapeType in their class list. These
10931122
* will be treated as escapes from the normal text flow.
10941123
*/
1095-
var allEscapes = this.rawdiv.getElementsByClassName(
1096-
'pagination-' + escapeType);
1124+
var allEscapes = this.rawdiv.querySelectorAll(
1125+
pagination.config(escapeType + 'Selector'));
10971126

10981127
for (var i = 0; i < allEscapes.length; i++) {
10991128

@@ -1102,12 +1131,12 @@
11021131
* using CSS rules.
11031132
*/
11041133
allEscapes[i].id = pagination.createRandomId(
1105-
'pagination-'+escapeType+'-');
1134+
'pagination-' + escapeType + '-');
11061135
}
11071136

11081137
var escapeId = allEscapes[i].id;
11091138

1110-
this.escapeStylesheets.footnote.innerHTML +=
1139+
this.escapeStylesheets[escapeType].innerHTML +=
11111140
'\n#' + escapeId + ' > * {-webkit-flow-into: ' + escapeId +
11121141
';}' + '\n#' + escapeId + '-flow-into {-webkit-flow-from: ' +
11131142
escapeId + ';}';
@@ -1124,7 +1153,7 @@
11241153

11251154
escapeFlowTo.id = escapeId + '-flow-into';
11261155

1127-
escapeFlowTo.classList.add('pagination-'+escapeType+'-item');
1156+
escapeFlowTo.classList.add('pagination-' + escapeType + '-item');
11281157

11291158
escapeObject['item'] = escapeFlowTo;
11301159

@@ -1134,58 +1163,73 @@
11341163

11351164

11361165
}
1137-
if (this.escapeStylesheets.footnote.innerHTML !== '') {
1138-
document.head.appendChild(this.escapeStylesheets.footnote);
1166+
if (this.escapeStylesheets[escapeType].innerHTML !== '') {
1167+
document.head.appendChild(this.escapeStylesheets[escapeType]);
11391168
}
11401169

11411170
};
11421171

1172+
flowObject.prototype.layoutTopfloats = function () {
1173+
// Layout all top floats
1174+
this.layoutTopBottomEscapes('topfloat');
1175+
};
1176+
11431177
flowObject.prototype.layoutFootnotes = function () {
11441178
// Layout all footnotes
1179+
this.layoutTopBottomEscapes('footnote');
1180+
}
11451181

1146-
for (var i = 0; i < this.escapes.footnote.length; i++) {
1147-
/* Go through all footnotes, delete the spacer blocks if they have any
1148-
* and remove the footnote itself from the DOM.
1182+
flowObject.prototype.layoutTopBottomEscapes = function (escapeType) {
1183+
// Layout all footnotes and top floats
1184+
1185+
for (var i = 0; i < this.escapes[escapeType].length; i++) {
1186+
/* Go through all escapes, delete the spacer blocks if they have any
1187+
* in the case of footnotes and remove the escape node itself from the DOM.
11491188
*/
11501189

1151-
if ('hidden' in this.escapes.footnote[i]) {
1152-
this.escapes.footnote[i]['hidden'].parentNode.removeChild(
1153-
this.escapes.footnote[i]['hidden']);
1154-
delete this.escapes.footnote[i]['hidden'];
1190+
if (escapeType === 'footnote' && 'hidden' in this.escapes[
1191+
escapeType][i]) {
1192+
this.escapes[escapeType][i]['hidden'].parentNode.removeChild(
1193+
this.escapes[escapeType][i]['hidden']);
1194+
delete this.escapes[escapeType][i]['hidden'];
11551195
}
11561196

1157-
if (this.escapes.footnote[i]['item'].parentNode !== null) {
1158-
this.escapes.footnote[i]['item'].parentNode.removeChild(
1159-
this.escapes.footnote[i]['item']);
1197+
if (this.escapes[escapeType][i]['item'].parentNode !== null) {
1198+
this.escapes[escapeType][i]['item'].parentNode.removeChild(
1199+
this.escapes[escapeType][i]['item']);
11601200
}
11611201
}
11621202

1163-
for (var i = 0; i < this.escapes.footnote.length; i++) {
1164-
/* Go through the footnotes again, this time with the purpose of
1203+
for (var i = 0; i < this.escapes[escapeType].length; i++) {
1204+
/* Go through the escapes again, this time with the purpose of
11651205
* placing them correctly.
11661206
*/
11671207

1168-
var footnoteReferencePage = this.findFootnoteReferencePage(
1169-
document.getElementById(this.escapes.footnote[i]['id']));
1170-
// We find the page where the footnote is referenced from.
1171-
var firstFootnoteContainer = footnoteReferencePage.querySelector(
1172-
'.pagination-footnotes');
1173-
firstFootnoteContainer.appendChild(this.escapes.footnote[i]['item']);
1174-
// We insert the footnote in the footnote container of that page.
1208+
var escapeReferencePage = this.findFootnoteReferencePage(
1209+
document.getElementById(this.escapes[escapeType][i]['id']));
1210+
// We find the page where the escape is referenced from.
1211+
var firstEscapeContainer = escapeReferencePage.querySelector(
1212+
'.pagination-' + escapeType + 's');
1213+
firstEscapeContainer.appendChild(this.escapes[escapeType][i]['item']);
1214+
// We insert the escape in the footnote or topfloat container of that page.
11751215

1176-
if (!(this.compareReferenceAndFootnotePage(this.escapes.footnote[i]))) {
1216+
if (escapeType === 'footnote' && !(this.compareReferenceAndFootnotePage(
1217+
this.escapes.footnote[i]))) {
11771218
/* If the footnote reference has been moved from one page to
11781219
* another through the insertion procedure, we move the footnote to
11791220
* where it is referenced from now and create an empty div
11801221
* ('hidden') and set it in it's place.
11811222
*/
11821223

1183-
this.escapes.footnote[i]['hidden'] = document.createElement('div');
1224+
this.escapes.footnote[i]['hidden'] = document.createElement(
1225+
'div');
11841226

11851227
this.escapes.footnote[i]['hidden'].style.height = (
11861228
this.escapes.footnote[i]['item'].clientHeight) + 'px';
11871229

1188-
this.escapes.footnote[i]['hidden'].id = this.escapes.footnote[i]['id'] +
1230+
this.escapes.footnote[i]['hidden'].id = this.escapes.footnote[i][
1231+
'id'
1232+
] +
11891233
'hidden';
11901234

11911235
var newFootnoteReferencePage = this.findFootnoteReferencePage(
@@ -1199,10 +1243,12 @@
11991243
/* We then insert the hidden element into the container where the
12001244
* footnote was previously so that the body text doesn't flow back.
12011245
*/
1202-
firstFootnoteContainer.replaceChild(
1246+
firstEscapeContainer.replaceChild(
12031247
this.escapes.footnote[i]['hidden'],
12041248
this.escapes.footnote[i]['item']);
1205-
newFootnoteContainer.appendChild(this.escapes.footnote[i]['item']);
1249+
newFootnoteContainer.appendChild(this.escapes.footnote[i][
1250+
'item'
1251+
]);
12061252

12071253
var checkSpacerSize = function () {
12081254
if (this.escapes.footnote[i]['item'].clientHeight <
@@ -1216,8 +1262,6 @@
12161262
}
12171263
};
12181264

1219-
1220-
12211265
var observer = new MutationObserver(function (mutations) {
12221266
checkSpacerSize();
12231267
});
@@ -1229,9 +1273,6 @@
12291273

12301274
});
12311275

1232-
1233-
1234-
12351276
}
12361277
}
12371278
};

test.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h1>Some text part</h1>
1414

1515
<div>1 Lorem ipsum dolor sit amet, consectetuer adipiscing
1616
elit<span class="pagination-footnote"><span><span>This is a footnote. Footnotes can be quite long and go over several lines. As you will notice, footnotes span the entire width of the page, even in multi column layouts.</span></span></span>, sed diam nonummy nibh euismod tincidunt ut laoreet dolore
17-
magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud
17+
magna aliquam erat volutpat. <span class="pagination-topfloat"><span><span><h1>THIS IS A TOP FLOAT!</h1><p>Different from footnotes, it doesn't move when text is moved around.</p></span></span></span>Ut wisi enim ad minim veniam, quis nostrud
1818
exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo
1919
consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit
2020
esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at

test2.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h1>Some text part</h1>
1919
exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo
2020
consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit
2121
esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at
22-
vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum
22+
vero eros et accumsan et iusto <span class="pagination-topfloat"><span><span><h1>THIS IS A TOP FLOAT</h1></span></span></span>odio dignissim qui blandit praesent luptatum
2323
zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor
2424
cum soluta nobis eleifend option congue nihil imperdiet doming id quod
2525
mazim placerat facer possim assum.<span class="pagination-footnote"><span><span>This is another footnote.</span></span></span> Typi non habent claritatem insitam;

0 commit comments

Comments
 (0)