Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions content_scripts/ct.responsiveHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,58 @@
const ATTR_TAG = 'attr-tag';
const ATTR_VERSION = 'attr-version';

// Get all bootstrap4 breakpoints from :root style
function getBreakpoints4() {
const searchStr = '--breakpoint-';

// Get all computed :root style properties
const style = getComputedStyle( document.documentElement );

// Loop properties and filter breakpoints
// Return array of objects, eg: [{ key: 'xl', val: 1200 }]
let lastEmpty = false;
let bps = [...Array(1000).keys()].reduce( ( bps, i ) => {
if ( lastEmpty ) // Bail out if last entry was already empty.
return bps;

const key = style.item( i );

if ( ! key.length ) { // Bail out if entry is empty.
lastEmpty = true;
return bps;
}

if ( ! key.startsWith( searchStr ) ) { // Filter breakpoint properties
return bps;
}

return [
...bps,
{
key: key.replace( searchStr, '' ),
val: parseInt( style.getPropertyValue( key ).trim().replace( 'px', '' ) ),
}
]
}, [] );

// Sort breakpoints from large to small.
bps.sort( ( a, b ) => {
if ( a.val > b.val ) {
return -1;
}
if ( a.val < b.val ) {
return 1;
}
return 0;
} );

// Build the breakpoint config objects.
return [...bps].map( ( bp, i ) => bps.length === i + 1
? { tag: bp.key, version: '4', cls: 'd-none d-block' }
: { tag: bp.key, version: '4', cls: 'd-none d-' + bp.key + '-block' }
);
}

function removeOldDOM() {
const oldContainer = document.querySelector(CONTAINER_SELECTOR);
if (oldContainer) {
Expand All @@ -17,11 +69,7 @@
{ tag: 'md', version: '3', cls: 'visible-md-block' },
{ tag: 'sm', version: '3', cls: 'visible-sm-block' },
{ tag: 'xs', version: '3', cls: 'visible-xs-block' },
{ tag: 'xl', version: '4', cls: 'd-none d-xl-block' },
{ tag: 'lg', version: '4', cls: 'd-none d-lg-block' },
{ tag: 'md', version: '4', cls: 'd-none d-md-block' },
{ tag: 'sm', version: '4', cls: 'd-none d-sm-block' },
{ tag: 'xs', version: '4', cls: 'd-none d-block' },
...getBreakpoints4(),
];

var container = document.createElement('div');
Expand Down Expand Up @@ -81,7 +129,7 @@
}, 66);
}
}
removeOldDOM();
removeOldDOM();
insertDOM();
sendMessage();
}());