| 1 |
const CUSTOM_CSS_ELEMENT_ID = 'media-views-important'; |
| 2 |
|
| 3 |
const processRules = (rules) => { |
| 4 |
const result = []; |
| 5 |
|
| 6 |
[...rules].forEach((rule) => { |
| 7 |
switch (rule.type) { |
| 8 |
case CSSRule.STYLE_RULE: |
| 9 |
result.push(addImportant(rule)); |
| 10 |
break; |
| 11 |
case CSSRule.MEDIA_RULE: |
| 12 |
result.push(`@media ${rule.conditionText} {`); |
| 13 |
processRules(rule.cssRules, result); |
| 14 |
result.push('}'); |
| 15 |
break; |
| 16 |
default: |
| 17 |
result.push(rule.cssText); |
| 18 |
break; |
| 19 |
} |
| 20 |
}); |
| 21 |
|
| 22 |
return result; |
| 23 |
}; |
| 24 |
|
| 25 |
const addImportant = (rule) => { |
| 26 |
const declarations = [...rule.style].map( |
| 27 |
(prop) => `${prop}: ${rule.style.getPropertyValue(prop)} !important`, |
| 28 |
); |
| 29 |
|
| 30 |
return `${rule.selectorText} { ${declarations.join('; ')}; }`; |
| 31 |
}; |
| 32 |
|
| 33 |
// A <style> tag resolves core's relative url() against the page, so they 404. |
| 34 |
const rebaseUrls = (css, base) => |
| 35 |
css.replace(/url\((["']?)([^"')]+)\1\)/g, (match, _quote, url) => { |
| 36 |
try { |
| 37 |
return `url("${new URL(url, base).href}")`; |
| 38 |
} catch { |
| 39 |
return match; |
| 40 |
} |
| 41 |
}); |
| 42 |
|
| 43 |
const getCustomMediaViewsCss = () => { |
| 44 |
const link = document.getElementById('media-views-css'); |
| 45 |
if (!link) return null; |
| 46 |
|
| 47 |
const processedRules = processRules(link.sheet?.cssRules); |
| 48 |
|
| 49 |
if (!processedRules?.length) return null; |
| 50 |
|
| 51 |
const css = rebaseUrls(processedRules.join('\n'), link.href); |
| 52 |
|
| 53 |
const additionalCSS = ` |
| 54 |
div:has(> .media-modal) {z-index: 999999 !important} |
| 55 |
|
| 56 |
/* A host page's <meta name="color-scheme"> otherwise dark-renders wp.media's inputs and scrollbars. */ |
| 57 |
.media-modal, |
| 58 |
.media-modal-backdrop { |
| 59 |
color-scheme: only light !important; |
| 60 |
} |
| 61 |
|
| 62 |
.media-frame { |
| 63 |
h1, h2, h3, h4, h5, h6 { |
| 64 |
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif !important; |
| 65 |
color: #1d2327 !important; |
| 66 |
} |
| 67 |
|
| 68 |
.uploader-inline-content { |
| 69 |
color: #1d2327 !important; |
| 70 |
} |
| 71 |
|
| 72 |
.media-sidebar { |
| 73 |
color: #646970 !important; |
| 74 |
} |
| 75 |
|
| 76 |
/* Core leaves these uncolored, so they take the theme's foreground. */ |
| 77 |
.media-search-input-label, |
| 78 |
.load-more-count, |
| 79 |
label[for="media-attachment-filters"], |
| 80 |
label[for="media-attachment-date-filters"] { |
| 81 |
color: #3c434a !important; |
| 82 |
} |
| 83 |
} |
| 84 |
`; |
| 85 |
|
| 86 |
return `${additionalCSS} ${css}`; |
| 87 |
}; |
| 88 |
|
| 89 |
export const addCustomMediaViewsCss = () => { |
| 90 |
if (document.getElementById(CUSTOM_CSS_ELEMENT_ID)) return; |
| 91 |
|
| 92 |
const css = getCustomMediaViewsCss(); |
| 93 |
|
| 94 |
if (!css) return; |
| 95 |
|
| 96 |
const style = document.createElement('style'); |
| 97 |
|
| 98 |
style.id = CUSTOM_CSS_ELEMENT_ID; |
| 99 |
style.textContent = css; |
| 100 |
|
| 101 |
document.head.appendChild(style); |
| 102 |
}; |
| 103 |
|
| 104 |
export const removeCustomMediaViewsCss = () => { |
| 105 |
const style = document.getElementById(CUSTOM_CSS_ELEMENT_ID); |
| 106 |
|
| 107 |
if (style) style.remove(); |
| 108 |
}; |
| 109 |
|