-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmarginbox.html
More file actions
338 lines (323 loc) · 11.5 KB
/
Copy pathmarginbox.html
File metadata and controls
338 lines (323 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Paged Horizontal Margin</title>
<script type="module" src="/lib/PagedPreview.js"></script>
<style>
body style {
display: block;
white-space: pre;
font-family: monospace;
max-width: 80ch;
background-color: rgb(243, 243, 243);
outline: 1px dotted grey;
}
paged-horizontal-margin {
outline: 1px dotted red;
height: 10mm;
margin: 10mm 0;
overflow: hidden;
}
paged-margin-box[slot="left"] {
background: honeydew;
}
paged-margin-box[slot="center"] {
background: lavender;
}
paged-margin-box[slot="right"] {
background: papayawhip;
}
</style>
</head>
<body>
<h1>
<code><paged-horizontal-margin></code>
</h1>
<p>
Tests and example usage.
</p>
<h2>
Margin boxes, content inserted with slots, optionally sized with javascript.
</h2>
<p>
Two components <code><paged-horizontal-margin></code> & <code><paged-margin-box></code>.
</p>
<p>
<code>paged-horizontal-margin</code> offers three slots, <code>left</code>, <code>center</code>, <code>right</code>.
It is assumed that only <code>paged-margin-box</code> elements are slotted in.
</p>
<p>
<code>paged-margin-box</code> offers one unnamed slot, for the content of the margin box.
</p>
<code>
<pre>
<paged-horizontal-margin>
<paged-margin-box slot="left">
left
</paged-margin-box>
<paged-margin-box slot="center">
center
</paged-margin-box>
<paged-margin-box slot="right">
right
</paged-margin-box>
</paged-horizontal-margin>
</pre>
</code>
<paged-horizontal-margin>
<paged-margin-box slot="left">
left
</paged-margin-box>
<paged-margin-box slot="center">
center
</paged-margin-box>
<paged-margin-box slot="right">
right
</paged-margin-box>
</paged-horizontal-margin>
<h3>Styling</h3>
<p>
Margin can be styled through an element selector. Specific boxes van be targeted with an
attribute selector.
</p>
<style>
paged-margin-box[slot="left"] {
color: green;
}
</style>
<paged-horizontal-margin>
<paged-margin-box slot="left">
left
</paged-margin-box>
<paged-margin-box slot="center">
center
</paged-margin-box>
<paged-margin-box slot="right">
right
</paged-margin-box>
</paged-horizontal-margin>
<h3>Overwrite alignment</h3>
<p>Overwrite the alignment of a single margin box</p>
<style>
paged-margin-box.left-aligned {
justify-content: start;
color: brown;
font-weight: bold;
}
</style>
<paged-horizontal-margin>
<paged-margin-box slot="left">
left
</paged-margin-box>
<paged-margin-box slot="center">
center
</paged-margin-box>
<paged-margin-box slot="right" class="left-aligned">
right
</paged-margin-box>
</paged-horizontal-margin>
<h3>Overwrite alignment with an hierarchical selector</h3>
<p>Overwrite the alignment of all margin boxes within a <code>paged-horizontal-margin</code>.</p>
<style>
paged-horizontal-margin.center-aligned paged-margin-box {
justify-content: center;
color: purple;
font-weight: bold;
}
</style>
<paged-horizontal-margin class="center-aligned">
<paged-margin-box slot="left">
left
</paged-margin-box>
<paged-margin-box slot="center">
center
</paged-margin-box>
<paged-margin-box slot="right">
right
</paged-margin-box>
</paged-horizontal-margin>
<h3>Empty slots</h3>
<p>Not all slots have to be filled.</p>
<paged-horizontal-margin>
<paged-margin-box slot="left">
left
</paged-margin-box>
<paged-margin-box slot="right">
right
</paged-margin-box>
</paged-horizontal-margin>
<h3>Left & center uneven</h3>
<p>
On this page, a simplified version of
the <a href="https://github.com/pagedjs/pagedjs/blob/ba1e3c5e4b8d4404b577fbdb0f2b9356a67107e6/src/modules/paged-media/atpage.js#L1771-L1907">existing sizing algorithm</a>
calculates and adjust the widths of the grid columns.
</p>
<paged-horizontal-margin>
<paged-margin-box slot="left">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero. Phasellus pharetra ante a purus ornare, eget facilisis sem ultricies.
</paged-margin-box>
<paged-margin-box slot="center">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.
</paged-margin-box>
</paged-horizontal-margin>
<h3>Center & Right uneven</h3>
<paged-horizontal-margin>
<paged-margin-box slot="center">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.
</paged-margin-box>
<paged-margin-box slot="right">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero. Phasellus pharetra ante a purus ornare, eget facilisis sem ultricies.
</paged-margin-box>
</paged-horizontal-margin>
<h3>Left & right uneven content</h3>
<paged-horizontal-margin>
<paged-margin-box slot="left">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero. Phasellus pharetra ante a purus ornare, eget facilisis sem ultricies.
</paged-margin-box>
<paged-margin-box slot="right">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.
</paged-margin-box>
</paged-horizontal-margin>
<h3>Left & right even content</h3>
<paged-horizontal-margin>
<paged-margin-box slot="left">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.
</paged-margin-box>
<paged-margin-box slot="right">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.
</paged-margin-box>
</paged-horizontal-margin>
<h3>Left</h3>
<paged-horizontal-margin>
<paged-margin-box slot="left">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero. Phasellus pharetra ante a purus ornare, eget facilisis sem ultricies.
</paged-margin-box>
</paged-horizontal-margin>
<h3>Center</h3>
<paged-horizontal-margin>
<paged-margin-box slot="center">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.
</paged-margin-box>
</paged-horizontal-margin>
<h3>Right</h3>
<paged-horizontal-margin>
<paged-margin-box slot="right">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.
</paged-margin-box>
</paged-horizontal-margin>
<h3>Left & right uneven content, with tags</h3>
<p>
Content slotted into a <code>paged-margin-box</code> can be tags.
</p>
<paged-horizontal-margin>
<paged-margin-box slot="left">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero. Phasellus pharetra ante a purus ornare, eget facilisis sem ultricies.
</paged-margin-box>
<paged-margin-box slot="right">
<h2>Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.</h2>
</paged-margin-box>
</paged-horizontal-margin>
<h3>Filling a slot twice</h3>
<p>
Filling a slot twice leads to weird results, as an extra row is created in the grid.
</p>
<paged-horizontal-margin>
<paged-margin-box slot="left">
Maecenas arcu massa, egestas vel est nec, mollis venenatis tortor. Ut eu finibus libero.
</paged-margin-box>
<paged-margin-box slot="left">
Phasellus pharetra ante a purus ornare, eget facilisis sem ultricies.
</paged-margin-box>
<paged-margin-box slot="right">
Ut eu finibus libero.
</paged-margin-box>
</paged-horizontal-margin>
<script>
/**
* Measure intrinsic width of content in the slot.
*
* @param {Array} nodes Nodes to measure
* @returns int
*/
function _measureIntrinsicContentWidth (nodes) {
return Array.from(nodes).reduce((width, node) => {
node.style.whiteSpace = 'nowrap';
const nodeWidth = node.offsetWidth;
node.style.removeProperty('white-space');
return width + nodeWidth;
}, 0);
}
Array.from(document.querySelectorAll('paged-horizontal-margin')).forEach((marginBox) => {
/**
* Simplified reimplementation of the sizing algorithm.
* Does, at the moment not consider whether the margin boxes have a `max-width` value
* other than `none` or `auto`.
*/
// Find slotted content per box
const contentNodesLeft = marginBox.querySelectorAll('[slot="left"]');
const contentNodesCenter = marginBox.querySelectorAll('[slot="center"]');
const contentNodesRight = marginBox.querySelectorAll('[slot="right"]');
// Measure instrinsic size
const contentWidthLeft = this._measureIntrinsicContentWidth(contentNodesLeft);
const contentWidthCenter = this._measureIntrinsicContentWidth(contentNodesCenter);
const contentWidthRight = this._measureIntrinsicContentWidth(contentNodesRight);
let gridTemplateColumnsString = '';
// Decide on proportions of the grid column.
// Based on which boxes have content. And then their 'inherent width'
// found by not allowing whitespace and then measuring the size.
if (contentNodesCenter.length) {
if (contentNodesLeft.length == 0 && contentNodesRight.length == 0) {
// only center
gridTemplateColumnsString = '0 1fr 0';
}
else {
if (contentNodesLeft.length) {
if (contentNodesRight.length) {
const outerwidths = contentWidthLeft + contentWidthCenter + contentWidthRight;
const newcenterWidth = contentWidthCenter * 100 / outerwidths;
if (newcenterWidth > 40) {
// left center right
gridTemplateColumnsString = "minmax(16.66%, 1fr) minmax(33%, " + newcenterWidth + "%) minmax(16.66%, 1fr)";
} else {
// left center right
gridTemplateColumnsString = "repeat(3, 1fr)";
}
}
else {
// left & center
let outerwidths = contentWidthLeft + contentWidthCenter;
let newcenterWidth = contentWidthCenter * 100 / outerwidths;
gridTemplateColumnsString = "minmax(16.66%, 1fr) minmax(33%, " + newcenterWidth + "%) minmax(16.66%, 1fr)";
}
}
else {
// center & right
const outerwidths = contentWidthRight + contentWidthCenter;
const newcenterWidth = contentWidthCenter * 100 / outerwidths;
gridTemplateColumnsString = "minmax(16.66%, 1fr) minmax(33%, " + newcenterWidth + "%) minmax(16.66%, 1fr)";
}
}
}
else if (contentNodesLeft.length) {
if (contentNodesRight.length) {
// left & right
const outerwidths = contentWidthLeft + contentWidthRight;
const newLeftWidth = contentWidthLeft * 100 / outerwidths;
gridTemplateColumnsString = "minmax(16.66%, " + newLeftWidth + "%) 0 1fr";
}
else {
// only left
gridTemplateColumnsString = '1fr 0 0';
}
}
else if (contentNodesRight.length) {
// only right
gridTemplateColumnsString = '0 0 1fr'
}
marginBox.style.gridTemplateColumns = gridTemplateColumnsString;
});
</script>
</body>
</html>