Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import {DomWatcher} from '../dom/dom-watcher';
import * as urlParams from '../dom/url-params';
export interface MarginOutlinerConfig {
/**
* A list of margin sizes you want to highlight.
* Example: [4,8,12,16,20,24]
*/
sizes: number[];
/**
* The name of the css class to attached to each generated spacer item.
*/
cssClassName: string;
/**
* The query selector of all elements you want to check for margins / paddings
* on the page.
*/
querySelector: string;
/**
* Specify an option url to enable outliner via url params.
*/
urlParamName?: string;
}
/**
* A class that will visibly highlight margins on your page.
*
*
* Setup:
* 1) Create your my-spacer css class.
*
* .my-spacer
* align-items: center
* background: rgba(#1a73e8, 0.15)
* box-shadow: none !important
* display: flex
* font-size: 12px
* height: var(--height)
* justify-content: center
* left: var(--left)
* margin: 0 !important
* pointer-events: none
* position: fixed !important
* top: var(--top)
* width: var(--width)
* z-index: 9
*
* &--padding
* background: rgba(255, 255, 0, .15)
*
*
* 2) Instantiate MarginOutliner. The query selector tells to inspect
* all divs on the page. Update it to scope the marginOutliner.
*
* new MarginOutliner({
* sizes: [4,8,12,16,20,24],
* cssClassName: 'my-spacer',
* querySelector: '.my-module > div'
* })
*
*
*
* Url param option:
* You can optionally, enable outliner via url params.
* Setting the urlParamName in outliner will ONLY enable
* outliner when the urlParam is in the url.
*
* new MarginOutliner({
* sizes: [4,8,12,16,20,24],
* cssClassName: 'my-spacer',
* querySelector: '.my-module > div'
* urlParamName: 'outliner'
* })
*
*
* Now do:
* ```
* xxx.com?outliner=true
* ```
*/
export class MarginOutliner {
private watcher: DomWatcher;
private config: MarginOutlinerConfig;
constructor(config: MarginOutlinerConfig) {
this.config = config;
this.watcher = new DomWatcher();
if (this.config.urlParamName) {
// Disable if url param is specified and the value is not true.
if (!urlParams.isTrue(this.config.urlParamName)) {
return;
}
}
this.watcher.add({
element: window,
on: ['click', 'resize', 'scroll'],
callback: this.run.bind(this),
});
this.run();
}
private createSpacer(
el: HTMLDivElement,
top: string,
left: number,
size: number,
isPadding: boolean
) {
const spacerEl = document.createElement('div');
spacerEl.classList.add(this.config.cssClassName);
spacerEl.style.setProperty('--left', `${left}px`);
spacerEl.style.setProperty('--top', top);
spacerEl.style.setProperty('--width', `${el.offsetWidth}px`);
spacerEl.style.setProperty('--height', `${size}px`);
if (isPadding) {
spacerEl.classList.add(`${this.config.cssClassName}--padding`);
}
spacerEl.innerText = `${size}px`;
el.parentElement!.appendChild(spacerEl);
}
public run() {
this.removeSpacers();
this.createSpacers();
}
private createSpacers() {
[].forEach.call(
document.querySelectorAll(this.config.querySelector),
(el: HTMLDivElement) => {
this.config.sizes.forEach((size) => {
// Element is hidden, don't highlight it.
if (!el.offsetWidth) {
return;
}
const style = window.getComputedStyle(el);
const rect = el.getBoundingClientRect();
if (style.marginTop === `${size}px`) {
this.createSpacer(
el,
`${rect.top - size}px`,
rect.left,
size,
false
);
}
if (style.marginBottom === `${size}px`) {
this.createSpacer(el, `${rect.bottom}px`, rect.left, size, false);
}
if (style.paddingTop === `${size}px`) {
this.createSpacer(el, `${rect.top}px`, rect.left, size, true);
}
if (style.paddingBottom === `${size}px`) {
this.createSpacer(
el,
`${rect.bottom - size}px`,
rect.left,
size,
true
);
}
});
}
);
}
private removeSpacers() {
[].forEach.call(
document.querySelectorAll(`.${this.config.cssClassName}`),
(el: HTMLDivElement) => {
el.parentNode!.removeChild(el);
}
);
}
public dispose() {
this.removeSpacers();
this.watcher && this.watcher.dispose();
}
}
|