@phun-ky/speccer 3.5.2 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js CHANGED
@@ -1,825 +1,28 @@
1
- /* eslint no-console: 0 */
2
- import './lib/poly';
3
- import throttle from './lib/throttle';
4
- import { addStyleToElement, getCSSValue, getElementCSSStyle, normalizeCSSValue } from './lib/style';
5
- import './anatomy.styl';
6
- import './speccer.styl';
1
+ /* eslint no-console:0 */
2
+ 'use strict';
7
3
 
8
- const o = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'];
9
- const regions = [
10
- {
11
- type: 'button-center'
12
- },
13
- {
14
- type: 'button-outline'
15
- },
16
- {
17
- type: 'button-icon'
18
- }
19
- ];
20
- const avoidTheseTags = ['TR', 'TH', 'TD', 'TBODY', 'THEAD', 'TFOOT'];
21
- const createDissectionNode = (e = '', t, n = 'span') => {
22
- const r = document.createElement(n);
23
- const o = document.createTextNode(e);
24
- r.classList.add('dissection');
25
- if (t.indexOf('full') === -1 && t.indexOf('enclose') === -1) {
26
- r.appendChild(o);
27
- } else if (t.indexOf('full') !== -1 || t.indexOf('enclose') !== -1) {
28
- r.setAttribute('data-dissection-counter', e);
29
- }
30
- if (t) {
31
- if (t.indexOf(' ') !== -1) {
32
- var ts = t.split(' ');
33
- ts.forEach(v => {
34
- r.classList.add(v);
35
- });
36
- } else {
37
- r.classList.add(t);
38
- }
39
- }
40
- return r;
41
- };
42
- export const dissectElement = (elementToDissect, dissectIndex) => {
43
- const rectOfDissectedElement = elementToDissect.getBoundingClientRect();
44
- if (!elementToDissect.getAttribute('data-anatomy')) {
45
- regions.forEach((t, n) => {
46
- const dissectionNode = createDissectionNode(o[n], t.type);
47
- if (avoidTheseTags.indexOf(elementToDissect.nodeName) >= 0) {
48
- elementToDissect.closest('table').insertAdjacentElement('afterend', dissectionNode);
49
- } else {
50
- elementToDissect.insertAdjacentElement('afterend', dissectionNode);
51
- }
52
- const rectOfDissectionNode = dissectionNode.getBoundingClientRect();
53
- if ('button-center' == t.type) {
54
- addStyleToElement(dissectionNode, {
55
- left: elementToDissect.offsetLeft + rectOfDissectedElement.width / 2 - rectOfDissectionNode.width / 2 + 'px',
56
- top: elementToDissect.offsetTop - rectOfDissectionNode.height - 48 + rectOfDissectedElement.height / 2 + 'px'
57
- });
58
- } else if ('button-outline' == t.type) {
59
- addStyleToElement(dissectionNode, {
60
- left: elementToDissect.offsetLeft + rectOfDissectedElement.width + 48 + 'px',
61
- top: elementToDissect.offsetTop + rectOfDissectedElement.height / 2 - rectOfDissectionNode.height / 2 + 'px'
62
- });
63
- } else if ('button-icon' == t.type) {
64
- addStyleToElement(dissectionNode, {
65
- left: elementToDissect.offsetLeft - rectOfDissectionNode.width - 48 + 18 + 'px',
66
- top: elementToDissect.offsetTop + rectOfDissectedElement.height / 2 - rectOfDissectionNode.height / 2 + 'px'
67
- });
68
- }
69
- });
70
- } else {
71
- const dissectionArea = elementToDissect.getAttribute('data-anatomy');
72
- const dissectionNode = createDissectionNode(o[dissectIndex], dissectionArea);
73
- const rectOfDissectedElement = elementToDissect.getBoundingClientRect();
74
- let tableCorrectionTop = 0;
75
- let tableCorrectionLeft = 0;
76
- const tableCorrection = avoidTheseTags.indexOf(elementToDissect.nodeName) >= 0;
77
- if (tableCorrection) {
78
- const table = elementToDissect.closest('table');
79
- const tableStyle = window.getComputedStyle(table.parentElement);
80
- table.insertAdjacentElement('afterend', dissectionNode);
81
- tableCorrectionTop = table.getBoundingClientRect().top - parseInt(tableStyle.getPropertyValue('padding-top'), 10);
82
- tableCorrectionLeft =
83
- table.getBoundingClientRect().left - parseInt(tableStyle.getPropertyValue('padding-left'), 10);
84
- } else {
85
- elementToDissect.insertAdjacentElement('afterend', dissectionNode);
86
- }
87
- const rectOfDissectionNode = dissectionNode.getBoundingClientRect();
88
- let outlineLeftLeft =
89
- (tableCorrection ? rectOfDissectedElement.left - tableCorrectionLeft : elementToDissect.offsetLeft) -
90
- rectOfDissectionNode.width -
91
- 48 +
92
- 'px';
93
- let outlineLeftTop =
94
- (tableCorrection ? rectOfDissectedElement.top - tableCorrectionTop : elementToDissect.offsetTop) +
95
- rectOfDissectedElement.height / 2 -
96
- rectOfDissectionNode.height / 2 +
97
- 'px';
98
- let outlineRightLeft =
99
- (tableCorrection ? rectOfDissectedElement.left - tableCorrectionLeft : elementToDissect.offsetLeft) +
100
- rectOfDissectedElement.width +
101
- 48 +
102
- 'px';
103
- let outlineRightTop =
104
- (tableCorrection ? rectOfDissectedElement.top - tableCorrectionTop : elementToDissect.offsetTop) +
105
- rectOfDissectedElement.height / 2 -
106
- rectOfDissectionNode.height / 2 +
107
- 'px';
108
- let outlineTopLeft =
109
- (tableCorrection ? rectOfDissectedElement.left - tableCorrectionLeft : elementToDissect.offsetLeft) +
110
- rectOfDissectedElement.width / 2 -
111
- rectOfDissectionNode.width / 2 +
112
- 'px';
113
- let outlineTopTop =
114
- (tableCorrection ? rectOfDissectedElement.top - tableCorrectionTop : elementToDissect.offsetTop) -
115
- rectOfDissectionNode.height -
116
- 48 +
117
- 'px';
118
- let outlineBottomleft =
119
- (tableCorrection ? rectOfDissectedElement.left - tableCorrectionLeft : elementToDissect.offsetLeft) +
120
- rectOfDissectedElement.width / 2 -
121
- rectOfDissectionNode.width / 2 +
122
- 'px';
123
- let outlineBottomTop =
124
- (tableCorrection ? rectOfDissectedElement.top - tableCorrectionTop : elementToDissect.offsetTop) +
125
- rectOfDissectedElement.height +
126
- 48 +
127
- 'px';
128
- if (dissectionArea.indexOf('outline') !== -1) {
129
- if (dissectionArea.indexOf('left') !== -1) {
130
- if (dissectionArea.indexOf('full') !== -1) {
131
- addStyleToElement(dissectionNode, {
132
- left: elementToDissect.offsetLeft - 8 + 'px',
133
- top: elementToDissect.offsetTop + -1 + 'px',
134
- height: rectOfDissectedElement.height + 'px'
135
- });
136
- } else if (dissectionArea.indexOf('enclose') !== -1) {
137
- addStyleToElement(dissectionNode, {
138
- left: elementToDissect.offsetLeft - 1 + 'px',
139
- top: elementToDissect.offsetTop + -1 + 'px',
140
- height: rectOfDissectedElement.height + 'px',
141
- width: rectOfDissectedElement.width + 'px'
142
- });
143
- } else {
144
- addStyleToElement(dissectionNode, {
145
- left: outlineLeftLeft,
146
- top: outlineLeftTop
147
- });
148
- }
149
- } else if (dissectionArea.indexOf('right') !== -1) {
150
- if (dissectionArea.indexOf('full') !== -1) {
151
- addStyleToElement(dissectionNode, {
152
- left: elementToDissect.offsetLeft + rectOfDissectedElement.width + 'px',
153
- top: elementToDissect.offsetTop + -1 + 'px',
154
- height: rectOfDissectedElement.height + 'px'
155
- });
156
- } else if (dissectionArea.indexOf('enclose') !== -1) {
157
- addStyleToElement(dissectionNode, {
158
- left: elementToDissect.offsetLeft + -1 + 'px',
159
- top: elementToDissect.offsetTop + -1 + 'px',
160
- height: rectOfDissectedElement.height + 'px',
161
- width: rectOfDissectedElement.width + 'px'
162
- });
163
- } else {
164
- addStyleToElement(dissectionNode, {
165
- left: outlineRightLeft,
166
- top: outlineRightTop
167
- });
168
- }
169
- } else if (dissectionArea.indexOf('top') !== -1) {
170
- if (dissectionArea.indexOf('full') !== -1) {
171
- addStyleToElement(dissectionNode, {
172
- bottom: elementToDissect.offsetTop + rectOfDissectedElement.height + 'px',
173
- left: elementToDissect.offsetLeft + -1 + 'px',
174
- width: rectOfDissectedElement.width + 'px'
175
- });
176
- } else if (dissectionArea.indexOf('enclose') !== -1) {
177
- addStyleToElement(dissectionNode, {
178
- top: elementToDissect.offsetTop + -1 + 'px',
179
- left: elementToDissect.offsetLeft + -1 + 'px',
180
- height: rectOfDissectedElement.height + 'px',
181
- width: rectOfDissectedElement.width + 'px'
182
- });
183
- } else {
184
- addStyleToElement(dissectionNode, {
185
- left: outlineTopLeft,
186
- top: outlineTopTop
187
- });
188
- }
189
- } else if (dissectionArea.indexOf('bottom') !== -1) {
190
- if (dissectionArea.indexOf('full') !== -1) {
191
- addStyleToElement(dissectionNode, {
192
- top: elementToDissect.offsetTop + rectOfDissectedElement.height + 'px',
193
- left: elementToDissect.offsetLeft + -1 + 'px',
194
- width: rectOfDissectedElement.width + 'px'
195
- });
196
- } else if (dissectionArea.indexOf('enclose') !== -1) {
197
- addStyleToElement(dissectionNode, {
198
- top: elementToDissect.offsetTop + -1 + 'px',
199
- left: elementToDissect.offsetLeft + -1 + 'px',
200
- height: rectOfDissectedElement.height + 'px',
201
- width: rectOfDissectedElement.width + 'px'
202
- });
203
- } else {
204
- addStyleToElement(dissectionNode, {
205
- left: outlineBottomleft,
206
- top: outlineBottomTop
207
- });
208
- }
209
- } else {
210
- if (dissectionArea.indexOf('full') !== -1) {
211
- addStyleToElement(dissectionNode, {
212
- left: elementToDissect.offsetLeft + rectOfDissectedElement.width + 'px',
213
- top: elementToDissect.offsetTop + 'px',
214
- height: rectOfDissectedElement.height + 'px'
215
- });
216
- } else if (dissectionArea.indexOf('enclose') !== -1) {
217
- addStyleToElement(dissectionNode, {
218
- left: elementToDissect.offsetLeft + rectOfDissectedElement.width + 'px',
219
- top: elementToDissect.offsetTop + -1 + 'px',
220
- height: rectOfDissectedElement.height + 'px',
221
- width: rectOfDissectedElement.width + 'px'
222
- });
223
- } else {
224
- addStyleToElement(dissectionNode, {
225
- left: outlineLeftLeft,
226
- top: outlineLeftTop
227
- });
228
- }
229
- }
230
- } else {
231
- if (dissectionArea.indexOf('full') !== -1) {
232
- addStyleToElement(dissectionNode, {
233
- left: elementToDissect.offsetLeft - 8 + 'px',
234
- top: elementToDissect.offsetTop + -1 + 'px',
235
- height: rectOfDissectedElement.height + 'px'
236
- });
237
- } else if (dissectionArea.indexOf('enclose') !== -1) {
238
- addStyleToElement(dissectionNode, {
239
- left: elementToDissect.offsetLeft - 1 + 'px',
240
- top: elementToDissect.offsetTop + -1 + 'px',
241
- height: rectOfDissectedElement.height + 'px',
242
- width: rectOfDissectedElement.width + 'px'
243
- });
244
- } else {
245
- addStyleToElement(dissectionNode, {
246
- left: outlineLeftLeft,
247
- top: outlineLeftTop
248
- });
249
- }
250
- }
251
- }
252
- };
253
-
254
- export const anatomy = () => {
255
- console.info('[@phun-ky/speccer]: Running anatomy()');
256
- removeSpeccerElements('.dissection');
257
- document.querySelectorAll('[data-anatomy-section]').forEach(section => {
258
- const elementsToBeDissected = section.querySelectorAll('[data-anatomy]');
259
- elementsToBeDissected.forEach(dissectElement);
260
- });
261
- };
262
- const getDesiredCSSStyles = style => {
263
- const {
264
- marginTop,
265
- marginBottom,
266
- marginLeft,
267
- marginRight,
268
- paddingTop,
269
- paddingBottom,
270
- paddingLeft,
271
- paddingRight
272
- } = style;
273
- return {
274
- marginTop,
275
- marginBottom,
276
- marginLeft,
277
- marginRight,
278
- paddingTop,
279
- paddingBottom,
280
- paddingLeft,
281
- paddingRight
282
- };
283
- };
4
+ import * as node from './lib/node';
5
+ import * as spec from './spec';
6
+ import * as dissect from './dissect';
7
+ import * as measure from './measure';
8
+ import * as typography from './typography';
9
+ import * as browser from './browser';
284
10
 
285
- const createTypographySpeccerNode = (html, area) => {
286
- const newTag = document.createElement('div');
287
- newTag.innerHTML = html;
288
- newTag.classList.add('speccer');
289
- newTag.classList.add('typography');
290
- if (area !== '') {
291
- if (area.indexOf(' ') !== -1) {
292
- var ts = area.split(' ');
293
- ts.forEach(v => {
294
- newTag.classList.add(v);
295
- });
296
- } else {
297
- newTag.classList.add(area);
298
- }
299
- }
300
- return newTag;
301
- };
11
+ const speccer = () => {
12
+ node.removeAll('.speccer');
13
+ node.removeAll('.dissection');
302
14
 
303
- const createSpeccerNode = (text = '', tag = 'span') => {
304
- const newTag = document.createElement(tag);
305
- const textContent = document.createTextNode(text);
306
- newTag.appendChild(textContent);
307
- newTag.setAttribute('title', text + 'px');
308
- newTag.classList.add('speccer');
309
- return newTag;
310
- };
311
- const createMeasureNode = (text = '', area = '', tag = 'span') => {
312
- const newTag = document.createElement(tag);
313
- // const textContent = document.createTextNode(text);
314
- // newTag.appendChild(textContent);
315
- newTag.setAttribute('title', text + 'px');
316
- newTag.setAttribute('data-measure', parseInt(text, 10) + 'px');
317
- newTag.classList.add('speccer');
318
- newTag.classList.add('measure');
319
- if (area !== '') {
320
- if (area.indexOf(' ') !== -1) {
321
- var ts = area.split(' ');
322
- ts.forEach(v => {
323
- newTag.classList.add(v);
324
- });
325
- } else {
326
- newTag.classList.add(area);
327
- }
328
- }
329
- return newTag;
330
- };
331
- export const measureElement = elementToBeMeasured => {
332
- const rectOfMeasuredElement = elementToBeMeasured.getBoundingClientRect();
333
- const measureArea = elementToBeMeasured.getAttribute('data-speccer-measure');
334
- if (measureArea === '') {
335
- return;
336
- } else {
337
- if (measureArea.indexOf('width') !== -1) {
338
- if (measureArea.indexOf('bottom') !== -1) {
339
- const measureNode = createMeasureNode(rectOfMeasuredElement.width, 'width bottom');
340
- if (avoidTheseTags.indexOf(elementToBeMeasured.nodeName) >= 0) {
341
- elementToBeMeasured.closest('table').insertAdjacentElement('afterend', measureNode);
342
- } else {
343
- elementToBeMeasured.insertAdjacentElement('afterend', measureNode);
344
- }
345
- addStyleToElement(measureNode, {
346
- left: elementToBeMeasured.offsetLeft + 'px',
347
- top: elementToBeMeasured.offsetTop + rectOfMeasuredElement.height + 1 + 'px',
348
- width: rectOfMeasuredElement.width + 'px'
349
- });
350
- } else {
351
- const measureNode = createMeasureNode(rectOfMeasuredElement.width, 'width top');
352
- if (avoidTheseTags.indexOf(elementToBeMeasured.nodeName) >= 0) {
353
- elementToBeMeasured.closest('table').insertAdjacentElement('afterend', measureNode);
354
- } else {
355
- elementToBeMeasured.insertAdjacentElement('afterend', measureNode);
356
- }
357
- const rectOfMeasureNode = measureNode.getBoundingClientRect();
358
- addStyleToElement(measureNode, {
359
- left: elementToBeMeasured.offsetLeft + 'px',
360
- top: elementToBeMeasured.offsetTop - rectOfMeasureNode.height + 1 + 'px',
361
- width: rectOfMeasuredElement.width + 'px'
362
- });
363
- }
364
- } else if (measureArea.indexOf('height') !== -1) {
365
- if (measureArea.indexOf('right') !== -1) {
366
- const measureNode = createMeasureNode(rectOfMeasuredElement.height, 'height right');
367
- if (avoidTheseTags.indexOf(elementToBeMeasured.nodeName) >= 0) {
368
- elementToBeMeasured.closest('table').insertAdjacentElement('afterend', measureNode);
369
- } else {
370
- elementToBeMeasured.insertAdjacentElement('afterend', measureNode);
371
- }
372
- addStyleToElement(measureNode, {
373
- left: elementToBeMeasured.offsetLeft + rectOfMeasuredElement.width + 'px',
374
- top: elementToBeMeasured.offsetTop + 'px',
375
- height: rectOfMeasuredElement.height + 'px'
376
- });
377
- } else {
378
- const measureNode = createMeasureNode(rectOfMeasuredElement.height, 'height top');
379
- if (avoidTheseTags.indexOf(elementToBeMeasured.nodeName) >= 0) {
380
- elementToBeMeasured.closest('table').insertAdjacentElement('afterend', measureNode);
381
- } else {
382
- elementToBeMeasured.insertAdjacentElement('afterend', measureNode);
383
- }
384
- const rectOfMeasureNode = measureNode.getBoundingClientRect();
385
- addStyleToElement(measureNode, {
386
- left: elementToBeMeasured.offsetLeft - rectOfMeasureNode.width + 'px',
387
- top: elementToBeMeasured.offsetTop + 'px',
388
- height: rectOfMeasuredElement.height + 'px'
389
- });
390
- }
391
- }
392
- }
393
- };
15
+ const _els_to_be_specced = document.querySelectorAll('[data-speccer],[data-speccer] *:not(td)');
16
+ const _els_to_be_measured = document.querySelectorAll('[data-speccer-measure]');
17
+ const _els_to_be_typography_specced = document.querySelectorAll('[data-speccer-typography]');
18
+ const _els_to_be_dissected = document.querySelectorAll('[data-anatomy-section] [data-anatomy]');
394
19
 
395
- const getDesiredTypographyCSSStyles = style => {
396
- const { lineHeight, letterSpacing, fontFamily, fontSize, fontStyle, fontVariationSettings, fontWeight } = style;
397
- return {
398
- lineHeight,
399
- letterSpacing,
400
- fontFamily,
401
- fontSize,
402
- fontStyle,
403
- fontVariationSettings,
404
- fontWeight
405
- };
20
+ _els_to_be_specced.forEach(spec.element);
21
+ _els_to_be_measured.forEach(measure.element);
22
+ _els_to_be_typography_specced.forEach(typography.element);
23
+ _els_to_be_dissected.forEach(dissect.element);
406
24
  };
407
25
 
408
- export const specTypographyElement = elementToBeSpecced => {
409
- const typographySpecArea = elementToBeSpecced.getAttribute('data-speccer-typography');
410
- const elementCSSStyle = getElementCSSStyle(elementToBeSpecced);
411
- if (elementCSSStyle.display === 'none' || elementCSSStyle.visibility === 'hidden') {
412
- return;
413
- }
414
- elementToBeSpecced.classList.add('speccer-isSpecced');
415
- const parentElementCSSStyle = getElementCSSStyle(elementToBeSpecced.parentElement);
416
- if (parentElementCSSStyle.position === 'static') {
417
- elementToBeSpecced.parentElement.style.position = 'relative';
418
- }
419
- const desiredStyles = getDesiredTypographyCSSStyles(elementCSSStyle);
420
- const rectOfSpeccedElement = elementToBeSpecced.getBoundingClientRect();
421
-
422
- const html =
423
- `
424
- ` +
425
- `font-styles: {` +
426
- `<ul class="speccer-styles">` +
427
- ` <li><span class="property">font-family:</span> ${desiredStyles['fontFamily']};</li>` +
428
- ` <li><span class="property">font-size:</span> ${desiredStyles['fontSize']} / ${parseInt(
429
- desiredStyles['fontSize'],
430
- 10
431
- ) / 16}rem;</li>` +
432
- ` <li><span class="property">font-weight:</span> ${desiredStyles['fontWeight']};</li>` +
433
- ` <li><span class="property">font-variation-settings:</span> ${desiredStyles['fontVariationSettings']};</li>` +
434
- ` <li><span class="property">line-height:</span> ${desiredStyles['lineHeight']} / ${parseInt(
435
- desiredStyles['lineHeight'],
436
- 10
437
- ) / 16}rem;</li>` +
438
- ` <li><span class="property">letter-spacing:</span> ${desiredStyles['letterSpacing']};</li>` +
439
- ` <li><span class="property">font-style:</span> ${desiredStyles['fontStyle']};</li>` +
440
- `</ul>` +
441
- `}`;
442
- const speccerNode = createTypographySpeccerNode(html, typographySpecArea);
443
-
444
- let tableCorrectionTop = 0;
445
- let tableCorrectionLeft = 0;
446
- const tableCorrection = avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0;
447
- if (tableCorrection) {
448
- const table = elementToBeSpecced.parentElement;
449
- const tableStyle = window.getComputedStyle(table.parentElement);
450
- table.insertAdjacentElement('afterend', speccerNode);
451
- tableCorrectionTop = table.getBoundingClientRect().top - parseInt(tableStyle.getPropertyValue('padding-top'), 10);
452
- tableCorrectionLeft =
453
- table.getBoundingClientRect().left - parseInt(tableStyle.getPropertyValue('padding-left'), 10);
454
- } else {
455
- elementToBeSpecced.insertAdjacentElement('afterend', speccerNode);
456
- }
457
-
458
- const rectOfSpeccerNode = speccerNode.getBoundingClientRect();
459
-
460
- let outlineLeftLeft =
461
- (tableCorrection ? rectOfSpeccerNode.left - tableCorrectionLeft : elementToBeSpecced.offsetLeft) -
462
- rectOfSpeccerNode.width -
463
- 48 +
464
- 'px';
465
-
466
- let outlineLeftTop =
467
- parseFloat(
468
- (tableCorrection ? rectOfSpeccerNode.top - tableCorrectionTop : elementToBeSpecced.offsetTop) -
469
- rectOfSpeccerNode.height / 2 +
470
- rectOfSpeccedElement.height / 2
471
- ).toFixed(3) + 'px';
472
- let outlineRightLeft =
473
- (tableCorrection ? rectOfSpeccerNode.left - tableCorrectionLeft : elementToBeSpecced.offsetLeft) +
474
- rectOfSpeccedElement.width +
475
- 48 +
476
- 'px';
477
-
478
- let outlineRightTop =
479
- parseFloat(
480
- (tableCorrection ? rectOfSpeccerNode.top - tableCorrectionTop : elementToBeSpecced.offsetTop) -
481
- rectOfSpeccerNode.height / 2 +
482
- rectOfSpeccedElement.height / 2
483
- ).toFixed(3) + 'px';
484
- let outlineTopLeft =
485
- parseFloat(
486
- (tableCorrection ? rectOfSpeccerNode.left - tableCorrectionLeft : elementToBeSpecced.offsetLeft) -
487
- rectOfSpeccerNode.width / 2 +
488
- rectOfSpeccedElement.width / 2
489
- ).toFixed(3) + 'px';
490
- let outlineTopTop =
491
- (tableCorrection ? rectOfSpeccerNode.top - tableCorrectionTop : elementToBeSpecced.offsetTop) -
492
- rectOfSpeccerNode.height -
493
- 48 +
494
- 'px';
495
- let outlineBottomleft =
496
- parseFloat(
497
- (tableCorrection ? rectOfSpeccerNode.left - tableCorrectionLeft : elementToBeSpecced.offsetLeft) -
498
- rectOfSpeccerNode.width / 2 +
499
- rectOfSpeccedElement.width / 2
500
- ).toFixed(3) + 'px';
501
- let outlineBottomTop =
502
- (tableCorrection ? rectOfSpeccerNode.top - tableCorrectionTop : elementToBeSpecced.offsetTop) +
503
- rectOfSpeccedElement.height +
504
- 48 +
505
- 'px';
506
-
507
- let position = {
508
- left: outlineLeftLeft,
509
- top: outlineLeftTop
510
- };
511
- if (typographySpecArea.indexOf('right') !== -1) {
512
- position = {
513
- left: outlineRightLeft,
514
- top: outlineRightTop
515
- };
516
- } else if (typographySpecArea.indexOf('top') !== -1) {
517
- position = {
518
- left: outlineTopLeft,
519
- top: outlineTopTop
520
- };
521
- } else if (typographySpecArea.indexOf('bottom') !== -1) {
522
- position = {
523
- left: outlineBottomleft,
524
- top: outlineBottomTop
525
- };
526
- }
527
-
528
- addStyleToElement(speccerNode, position);
529
- };
530
-
531
- export const specElement = elementToBeSpecced => {
532
- const speccerElement = {};
533
- const elementCSSStyle = getElementCSSStyle(elementToBeSpecced);
534
- if (elementCSSStyle.display === 'none' || elementCSSStyle.visibility === 'hidden') {
535
- return;
536
- }
537
- elementToBeSpecced.classList.add('speccer-isSpecced');
538
- const parentElementCSSStyle = getElementCSSStyle(elementToBeSpecced.parentElement);
539
- if (parentElementCSSStyle.position === 'static') {
540
- elementToBeSpecced.parentElement.style.position = 'relative';
541
- }
542
- speccerElement.style = getDesiredCSSStyles(elementCSSStyle);
543
- speccerElement.rect = elementToBeSpecced.getBoundingClientRect();
544
- if (speccerElement.style['marginTop'] !== '0px') {
545
- const speccerMarginTopElement = createSpeccerNode(getCSSValue(speccerElement.style.marginTop));
546
- speccerMarginTopElement.classList.add('margin');
547
- speccerMarginTopElement.classList.add('marginTop');
548
- addStyleToElement(speccerMarginTopElement, {
549
- height: speccerElement.style.marginTop,
550
- width: speccerElement.rect.width + 'px',
551
- left:
552
- normalizeCSSValue(speccerElement.rect.x - elementToBeSpecced.parentElement.getBoundingClientRect().x) + 'px',
553
- top:
554
- normalizeCSSValue(
555
- speccerElement.rect.y -
556
- elementToBeSpecced.parentElement.getBoundingClientRect().y -
557
- parseInt(speccerElement.style.marginTop, 10)
558
- ) + 'px'
559
- });
560
- if (avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0) {
561
- elementToBeSpecced.closest('table').insertAdjacentElement('afterend', speccerMarginTopElement);
562
- } else {
563
- elementToBeSpecced.insertAdjacentElement('afterend', speccerMarginTopElement);
564
- }
565
- }
566
- if (speccerElement.style['marginRight'] !== '0px') {
567
- const speccerMarginRightElement = createSpeccerNode(getCSSValue(speccerElement.style.marginRight));
568
- speccerMarginRightElement.classList.add('margin');
569
- speccerMarginRightElement.classList.add('marginRight');
570
- addStyleToElement(speccerMarginRightElement, {
571
- height: speccerElement.rect.height + 'px',
572
- width: speccerElement.style.marginRight,
573
- left:
574
- normalizeCSSValue(
575
- speccerElement.rect.x -
576
- elementToBeSpecced.parentElement.getBoundingClientRect().x +
577
- parseInt(speccerElement.rect.width, 10)
578
- ) + 'px',
579
- top: normalizeCSSValue(speccerElement.rect.y - elementToBeSpecced.parentElement.getBoundingClientRect().y) + 'px'
580
- });
581
- if (avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0) {
582
- elementToBeSpecced.closest('table').insertAdjacentElement('afterend', speccerMarginRightElement);
583
- } else {
584
- elementToBeSpecced.insertAdjacentElement('afterend', speccerMarginRightElement);
585
- }
586
- }
587
- if (speccerElement.style['marginBottom'] !== '0px') {
588
- const speccerMarginBottomElement = createSpeccerNode(getCSSValue(speccerElement.style.marginBottom));
589
- speccerMarginBottomElement.classList.add('margin');
590
- speccerMarginBottomElement.classList.add('marginBottom');
591
- addStyleToElement(speccerMarginBottomElement, {
592
- height: speccerElement.style.marginBottom,
593
- width: speccerElement.rect.width + 'px',
594
- left:
595
- normalizeCSSValue(speccerElement.rect.x - elementToBeSpecced.parentElement.getBoundingClientRect().x) + 'px',
596
- top:
597
- normalizeCSSValue(
598
- speccerElement.rect.y -
599
- elementToBeSpecced.parentElement.getBoundingClientRect().y +
600
- parseInt(speccerElement.rect.height, 10)
601
- ) + 'px'
602
- });
603
- if (avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0) {
604
- elementToBeSpecced.closest('table').insertAdjacentElement('afterend', speccerMarginBottomElement);
605
- } else {
606
- elementToBeSpecced.insertAdjacentElement('afterend', speccerMarginBottomElement);
607
- }
608
- }
609
- if (speccerElement.style['marginLeft'] !== '0px') {
610
- const speccerMarginLeftElement = createSpeccerNode(getCSSValue(speccerElement.style.marginLeft));
611
- speccerMarginLeftElement.classList.add('margin');
612
- speccerMarginLeftElement.classList.add('marginLeft');
613
- addStyleToElement(speccerMarginLeftElement, {
614
- height: speccerElement.rect.height + 'px',
615
- width: speccerElement.style.marginLeft,
616
- left:
617
- normalizeCSSValue(
618
- speccerElement.rect.x -
619
- elementToBeSpecced.parentElement.getBoundingClientRect().x -
620
- parseInt(speccerElement.style.marginLeft, 10)
621
- ) + 'px',
622
- top: normalizeCSSValue(speccerElement.rect.y - elementToBeSpecced.parentElement.getBoundingClientRect().y) + 'px'
623
- });
624
- if (avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0) {
625
- elementToBeSpecced.closest('table').insertAdjacentElement('afterend', speccerMarginLeftElement);
626
- } else {
627
- elementToBeSpecced.insertAdjacentElement('afterend', speccerMarginLeftElement);
628
- }
629
- }
630
- if (speccerElement.style['paddingTop'] !== '0px') {
631
- const speccerPaddingBottomElement = createSpeccerNode(getCSSValue(speccerElement.style.paddingTop));
632
- speccerPaddingBottomElement.classList.add('padding');
633
- speccerPaddingBottomElement.classList.add('paddingTop');
634
- addStyleToElement(speccerPaddingBottomElement, {
635
- height: speccerElement.style.paddingTop,
636
- width: speccerElement.rect.width + 'px',
637
- left:
638
- normalizeCSSValue(speccerElement.rect.x - elementToBeSpecced.parentElement.getBoundingClientRect().x) + 'px',
639
- top: normalizeCSSValue(speccerElement.rect.y - elementToBeSpecced.parentElement.getBoundingClientRect().y) + 'px'
640
- });
641
- if (avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0) {
642
- elementToBeSpecced.closest('table').insertAdjacentElement('afterend', speccerPaddingBottomElement);
643
- } else {
644
- elementToBeSpecced.insertAdjacentElement('afterend', speccerPaddingBottomElement);
645
- }
646
- }
647
- if (speccerElement.style['paddingBottom'] !== '0px') {
648
- const speccerPaddingBottomElement = createSpeccerNode(getCSSValue(speccerElement.style.paddingBottom));
649
- speccerPaddingBottomElement.classList.add('padding');
650
- speccerPaddingBottomElement.classList.add('paddingBottom');
651
- addStyleToElement(speccerPaddingBottomElement, {
652
- height: speccerElement.style.paddingBottom,
653
- width: speccerElement.rect.width + 'px',
654
- left:
655
- normalizeCSSValue(speccerElement.rect.x - elementToBeSpecced.parentElement.getBoundingClientRect().x) + 'px',
656
- top:
657
- normalizeCSSValue(
658
- speccerElement.rect.y -
659
- elementToBeSpecced.parentElement.getBoundingClientRect().y +
660
- (parseInt(speccerElement.rect.height, 10) - parseInt(speccerElement.style.paddingBottom, 10))
661
- ) + 'px'
662
- });
663
- if (avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0) {
664
- elementToBeSpecced.closest('table').insertAdjacentElement('afterend', speccerPaddingBottomElement);
665
- } else {
666
- elementToBeSpecced.insertAdjacentElement('afterend', speccerPaddingBottomElement);
667
- }
668
- }
669
- if (speccerElement.style['paddingRight'] !== '0px') {
670
- const speccerPaddingRightElement = createSpeccerNode(getCSSValue(speccerElement.style.paddingRight));
671
- speccerPaddingRightElement.classList.add('padding');
672
- speccerPaddingRightElement.classList.add('paddingRight');
673
- addStyleToElement(speccerPaddingRightElement, {
674
- height: speccerElement.rect.height + 'px',
675
- width: speccerElement.style.paddingRight,
676
- left:
677
- normalizeCSSValue(
678
- speccerElement.rect.x -
679
- elementToBeSpecced.parentElement.getBoundingClientRect().x +
680
- (parseInt(speccerElement.rect.width, 10) - parseInt(speccerElement.style.paddingRight, 10))
681
- ) + 'px',
682
- top: normalizeCSSValue(speccerElement.rect.y - elementToBeSpecced.parentElement.getBoundingClientRect().y) + 'px'
683
- });
684
- if (avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0) {
685
- elementToBeSpecced.closest('table').insertAdjacentElement('afterend', speccerPaddingRightElement);
686
- } else {
687
- elementToBeSpecced.insertAdjacentElement('afterend', speccerPaddingRightElement);
688
- }
689
- }
690
- if (speccerElement.style['paddingLeft'] !== '0px') {
691
- const speccerPaddingLeftElement = createSpeccerNode(getCSSValue(speccerElement.style.paddingLeft));
692
- speccerPaddingLeftElement.classList.add('padding');
693
- speccerPaddingLeftElement.classList.add('paddingLeft');
694
- addStyleToElement(speccerPaddingLeftElement, {
695
- height: speccerElement.rect.height + 'px',
696
- width: speccerElement.style.paddingLeft,
697
- left:
698
- normalizeCSSValue(speccerElement.rect.x - elementToBeSpecced.parentElement.getBoundingClientRect().x) + 'px',
699
- top: normalizeCSSValue(speccerElement.rect.y - elementToBeSpecced.parentElement.getBoundingClientRect().y) + 'px'
700
- });
701
- if (avoidTheseTags.indexOf(elementToBeSpecced.nodeName) >= 0) {
702
- elementToBeSpecced.closest('table').insertAdjacentElement('afterend', speccerPaddingLeftElement);
703
- } else {
704
- elementToBeSpecced.insertAdjacentElement('afterend', speccerPaddingLeftElement);
705
- }
706
- }
707
- };
708
- export const speccer = () => {
709
- console.info('[@phun-ky/speccer]: Running speccer()');
710
- removeSpeccerElements('.speccer');
711
- const elementsToBeSpecced = document.querySelectorAll('[data-speccer],[data-speccer] *:not(td)');
712
- elementsToBeSpecced.forEach(specElement);
713
- const elementsToBeMeasured = document.querySelectorAll('[data-speccer-measure]');
714
- elementsToBeMeasured.forEach(measureElement);
715
- const elementsToBeTypographySpecced = document.querySelectorAll('[data-speccer-typography]');
716
- elementsToBeTypographySpecced.forEach(specTypographyElement);
717
- };
718
-
719
- export const activateOnResize = () => {
720
- throttle('resize', 'speccer-onResize');
721
- throttle('resize', 'anatomy-onResize');
722
-
723
- const speccerEventFunc = () => {
724
- console.info('[@phun-ky/speccer]: Event speccer-onResize triggered');
725
- speccer();
726
- };
727
-
728
- const anatomyEventFunc = () => {
729
- console.info('[@phun-ky/speccer]: Event anatomy-onResize triggered');
730
- anatomy();
731
- };
732
- window.removeEventListener('speccer-onResize', speccerEventFunc);
733
- window.removeEventListener('anatomy-onResize', anatomyEventFunc);
734
- window.addEventListener('speccer-onResize', speccerEventFunc);
735
- window.addEventListener('anatomy-onResize', anatomyEventFunc);
736
- };
737
-
738
- export const removeSpeccerElements = (selector, el = document) => {
739
- [].forEach.call(el.querySelectorAll(selector), function(e) {
740
- e.parentNode.removeChild(e);
741
- });
742
- };
743
-
744
- const speccerScript = document.currentScript;
745
- console.log(speccerScript);
746
-
747
- if (speccerScript) {
748
- if (
749
- speccerScript.getAttribute('src').indexOf('speccer.js') !== -1 ||
750
- // for codepen
751
- speccerScript.getAttribute('src').indexOf('JaXpOK.js') !== -1
752
- ) {
753
- if (speccerScript.hasAttribute('data-manual')) {
754
- console.info('[@phun-ky/speccer]: Initialized with: data-manual');
755
- window.speccer = speccer;
756
- window.anatomy = anatomy;
757
- } else if (speccerScript.hasAttribute('data-instant')) {
758
- anatomy();
759
- speccer();
760
- } else if (speccerScript.hasAttribute('data-dom')) {
761
- console.info('[@phun-ky/speccer]: Initialized with: data-dom');
762
- if (document.readyState === 'loading') {
763
- document.addEventListener('DOMContentLoaded', anatomy);
764
- document.addEventListener('DOMContentLoaded', speccer);
765
- } else {
766
- // `DOMContentLoaded` already fired
767
- anatomy();
768
- speccer();
769
- }
770
- } else if (speccerScript.hasAttribute('data-lazy')) {
771
- console.info('[@phun-ky/speccer]: Initialized with: data-lazy');
772
- let specElementObserver = new IntersectionObserver((entries, observer) => {
773
- entries.forEach(entry => {
774
- if (entry.intersectionRatio > 0) {
775
- specElement(entry.target);
776
- observer.unobserve(entry.target);
777
- }
778
- });
779
- });
780
- document.querySelectorAll('[data-speccer],[data-speccer] *:not(td)').forEach(el => {
781
- specElementObserver.observe(el);
782
- });
783
- let measureElementObserver = new IntersectionObserver((entries, observer) => {
784
- entries.forEach(entry => {
785
- if (entry.intersectionRatio > 0) {
786
- measureElement(entry.target);
787
- observer.unobserve(entry.target);
788
- }
789
- });
790
- });
791
- document.querySelectorAll('[data-speccer-measure]').forEach(el => {
792
- measureElementObserver.observe(el);
793
- });
794
- let dissectElementObserver = new IntersectionObserver((entries, observer) => {
795
- entries.forEach(entry => {
796
- const targets = entry.target.querySelectorAll('[data-anatomy]');
797
- if (entry.intersectionRatio > 0) {
798
- targets.forEach(dissectElement);
799
- observer.unobserve(entry.target);
800
- }
801
- });
802
- });
803
-
804
- const observeAnatomySections = section => {
805
- dissectElementObserver.observe(section);
806
- };
807
-
808
- document.querySelectorAll('[data-anatomy-section]').forEach(observeAnatomySections);
809
- } else {
810
- console.info('[@phun-ky/speccer]: Initialized with nothing, falling back to data-dom');
811
- if (document.readyState === 'loading') {
812
- document.addEventListener('DOMContentLoaded', anatomy);
813
- document.addEventListener('DOMContentLoaded', speccer);
814
- } else {
815
- // `DOMContentLoaded` already fired
816
- anatomy();
817
- speccer();
818
- }
819
- }
26
+ export default speccer;
820
27
 
821
- if (!speccerScript.hasAttribute('data-manual') && !speccerScript.hasAttribute('data-lazy')) {
822
- activateOnResize();
823
- }
824
- }
825
- }
28
+ browser.activate(speccer);