mirror of
https://github.com/mjl-/mox.git
synced 2025-07-10 10:34:40 +03:00
add ability to include custom css & js in web interface (webmail, webaccount, webadmin), and use css variables in webmail for easier customization
if files {webmail,webaccount,webadmin}.{css,js} exist in the configdir (where the mox.conf file lives), their contents are included in the web apps. the webmail now uses css variables, mostly for colors. so you can write a custom webmail.css that changes the variables, e.g.: :root { --color: blue } you can also look at css class names and override their styles. in the future, we may want to make some css variables configurable in the per-user settings in the webmail. should reduce the number of variables first. any custom javascript is loaded first. if it defines a global function "moxBeforeDisplay", that is called each time a page loads (after authentication) with the DOM element of the page content as parameter. the webmail is a single persistent page. this can be used to make some changes to the DOM, e.g. inserting some elements. we'll have to see how well this works in practice. perhaps some patterns emerge (e.g. adding a logo), and we can make those use-cases easier to achieve. helps partially with issue #114, and based on questions from laura-lilly on matrix.
This commit is contained in:
158
webmail/msg.js
158
webmail/msg.js
@ -1055,10 +1055,10 @@ var api;
|
||||
// instances of a class.
|
||||
// We keep the default/regular styles and dark-mode styles in separate stylesheets.
|
||||
const cssStyle = dom.style(attr.type('text/css'));
|
||||
document.head.appendChild(cssStyle);
|
||||
document.head.prepend(cssStyle);
|
||||
const styleSheet = cssStyle.sheet;
|
||||
const cssStyleDark = dom.style(attr.type('text/css'));
|
||||
document.head.appendChild(cssStyleDark);
|
||||
document.head.prepend(cssStyleDark);
|
||||
const styleSheetDark = cssStyleDark.sheet;
|
||||
styleSheetDark.insertRule('@media (prefers-color-scheme: dark) {}');
|
||||
const darkModeRule = styleSheetDark.cssRules[0];
|
||||
@ -1085,8 +1085,11 @@ const ensureCSS = (selector, styles, important) => {
|
||||
let darkst;
|
||||
for (let [k, v] of Object.entries(styles)) {
|
||||
// We've kept the camel-case in our code which we had from when we did "st[prop] =
|
||||
// value". It is more convenient as object keys. So convert to kebab-case.
|
||||
k = k.replace(/[A-Z]/g, s => '-' + s.toLowerCase());
|
||||
// value". It is more convenient as object keys. So convert to kebab-case, but only
|
||||
// if this is not a css property.
|
||||
if (!k.startsWith('--')) {
|
||||
k = k.replace(/[A-Z]/g, s => '-' + s.toLowerCase());
|
||||
}
|
||||
if (Array.isArray(v)) {
|
||||
if (v.length !== 2) {
|
||||
throw new Error('2 elements required for light/dark mode style, got ' + v.length);
|
||||
@ -1112,54 +1115,105 @@ const css = (className, styles, important) => {
|
||||
};
|
||||
// todo: reduce number of colors. hopefully we can derive some colors from a few base colors (making them brighter/darker, or shifting hue, etc). then make them configurable through settings.
|
||||
// todo: add the standard padding and border-radius, perhaps more.
|
||||
// todo: could make some of these {prop: value} objects and pass them directly to css()
|
||||
const styles = {
|
||||
color: ['black', '#ddd'],
|
||||
colorMild: ['#555', '#bbb'],
|
||||
colorMilder: ['#666', '#aaa'],
|
||||
backgroundColor: ['white', '#222'],
|
||||
backgroundColorMild: ['#f8f8f8', '#080808'],
|
||||
backgroundColorMilder: ['#999', '#777'],
|
||||
borderColor: ['#ccc', '#333'],
|
||||
mailboxesTopBackgroundColor: ['#fdfdf1', 'rgb(26, 18, 0)'],
|
||||
msglistBackgroundColor: ['#f5ffff', 'rgb(4, 19, 13)'],
|
||||
boxShadow: ['0 0 20px rgba(0, 0, 0, 0.1)', '0px 0px 20px #000'],
|
||||
buttonBackground: ['#eee', '#222'],
|
||||
buttonBorderColor: ['#888', '#666'],
|
||||
buttonHoverBackground: ['#ddd', '#333'],
|
||||
overlayOpaqueBackgroundColor: ['#eee', '#011'],
|
||||
overlayBackgroundColor: ['rgba(0, 0, 0, 0.2)', 'rgba(0, 0, 0, 0.5)'],
|
||||
popupColor: ['black', 'white'],
|
||||
popupBackgroundColor: ['white', 'rgb(49, 50, 51)'],
|
||||
popupBorderColor: ['#ccc', '#555'],
|
||||
highlightBackground: ['gold', '#a70167'],
|
||||
highlightBorderColor: ['#8c7600', 'rgb(253, 31, 167)'],
|
||||
highlightBackgroundHover: ['#ffbd21', 'rgb(113, 4, 71)'],
|
||||
mailboxActiveBackground: ['linear-gradient(135deg, #ffc7ab 0%, #ffdeab 100%)', 'linear-gradient(135deg, rgb(182, 61, 0) 0%, rgb(140, 90, 13) 100%)'],
|
||||
mailboxHoverBackgroundColor: ['#eee', 'rgb(66, 31, 21)'],
|
||||
msgItemActiveBackground: ['linear-gradient(135deg, #8bc8ff 0%, #8ee5ff 100%)', 'linear-gradient(135deg, rgb(4, 92, 172) 0%, rgb(2, 123, 160) 100%)'],
|
||||
msgItemHoverBackgroundColor: ['#eee', 'rgb(7, 51, 72)'],
|
||||
msgItemFocusBorderColor: ['#2685ff', '#2685ff'],
|
||||
buttonTristateOnBackground: ['#c4ffa9', 'rgb(39, 126, 0)'],
|
||||
buttonTristateOffBackground: ['#ffb192', 'rgb(191, 65, 15)'],
|
||||
warningBackgroundColor: ['#ffca91', 'rgb(168, 87, 0)'],
|
||||
successBackground: ['#d2f791', '#1fa204'],
|
||||
emphasisBackground: ['#666', '#aaa'],
|
||||
// We define css variables, making them easy to override.
|
||||
ensureCSS(':root', {
|
||||
'--color': ['black', '#ddd'],
|
||||
'--colorMild': ['#555', '#bbb'],
|
||||
'--colorMilder': ['#666', '#aaa'],
|
||||
'--backgroundColor': ['white', '#222'],
|
||||
'--backgroundColorMild': ['#f8f8f8', '#080808'],
|
||||
'--backgroundColorMilder': ['#999', '#777'],
|
||||
'--borderColor': ['#ccc', '#333'],
|
||||
'--mailboxesTopBackgroundColor': ['#fdfdf1', '#1a1200'],
|
||||
'--msglistBackgroundColor': ['#f5ffff', '#04130d'],
|
||||
'--boxShadow': ['0 0 20px rgba(0, 0, 0, 0.1)', '0px 0px 20px #000'],
|
||||
'--buttonBackground': ['#eee', '#222'],
|
||||
'--buttonBorderColor': ['#888', '#666'],
|
||||
'--buttonHoverBackground': ['#ddd', '#333'],
|
||||
'--overlayOpaqueBackgroundColor': ['#eee', '#011'],
|
||||
'--overlayBackgroundColor': ['rgba(0, 0, 0, 0.2)', 'rgba(0, 0, 0, 0.5)'],
|
||||
'--popupColor': ['black', 'white'],
|
||||
'--popupBackgroundColor': ['white', '#313233'],
|
||||
'--popupBorderColor': ['#ccc', '#555'],
|
||||
'--highlightBackground': ['gold', '#a70167'],
|
||||
'--highlightBorderColor': ['#8c7600', '#fd1fa7'],
|
||||
'--highlightBackgroundHover': ['#ffbd21', '#710447'],
|
||||
'--mailboxActiveBackground': ['linear-gradient(135deg, #ffc7ab 0%, #ffdeab 100%)', 'linear-gradient(135deg, #b63d00 0%, #8c5a0d 100%)'],
|
||||
'--mailboxHoverBackgroundColor': ['#eee', '#421f15'],
|
||||
'--msgItemActiveBackground': ['linear-gradient(135deg, #8bc8ff 0%, #8ee5ff 100%)', 'linear-gradient(135deg, #045cac 0%, #027ba0 100%)'],
|
||||
'--msgItemHoverBackgroundColor': ['#eee', '#073348'],
|
||||
'--msgItemFocusBorderColor': ['#2685ff', '#2685ff'],
|
||||
'--buttonTristateOnBackground': ['#c4ffa9', '#277e00'],
|
||||
'--buttonTristateOffBackground': ['#ffb192', '#bf410f'],
|
||||
'--warningBackgroundColor': ['#ffca91', '#a85700'],
|
||||
'--successBackground': ['#d2f791', '#1fa204'],
|
||||
'--emphasisBackground': ['#666', '#aaa'],
|
||||
// For authentication/security results.
|
||||
underlineGreen: '#50c40f',
|
||||
underlineRed: '#e15d1c',
|
||||
underlineBlue: '#09f',
|
||||
underlineGrey: '#888',
|
||||
'--underlineGreen': '#50c40f',
|
||||
'--underlineRed': '#e15d1c',
|
||||
'--underlineBlue': '#09f',
|
||||
'--underlineGrey': '#888',
|
||||
'--quoted1Color': ['#03828f', '#71f2ff'],
|
||||
'--quoted2Color': ['#c7445c', '#ec4c4c'],
|
||||
'--quoted3Color': ['#417c10', '#73e614'],
|
||||
'--scriptSwitchUnderlineColor': ['#dca053', '#e88f1e'],
|
||||
'--linkColor': ['#096bc2', '#63b6ff'],
|
||||
'--linkVisitedColor': ['#0704c1', '#c763ff'],
|
||||
});
|
||||
// Typed way to reference a css variables. Kept from before used variables.
|
||||
const styles = {
|
||||
color: 'var(--color)',
|
||||
colorMild: 'var(--colorMild)',
|
||||
colorMilder: 'var(--colorMilder)',
|
||||
backgroundColor: 'var(--backgroundColor)',
|
||||
backgroundColorMild: 'var(--backgroundColorMild)',
|
||||
backgroundColorMilder: 'var(--backgroundColorMilder)',
|
||||
borderColor: 'var(--borderColor)',
|
||||
mailboxesTopBackgroundColor: 'var(--mailboxesTopBackgroundColor)',
|
||||
msglistBackgroundColor: 'var(--msglistBackgroundColor)',
|
||||
boxShadow: 'var(--boxShadow)',
|
||||
buttonBackground: 'var(--buttonBackground)',
|
||||
buttonBorderColor: 'var(--buttonBorderColor)',
|
||||
buttonHoverBackground: 'var(--buttonHoverBackground)',
|
||||
overlayOpaqueBackgroundColor: 'var(--overlayOpaqueBackgroundColor)',
|
||||
overlayBackgroundColor: 'var(--overlayBackgroundColor)',
|
||||
popupColor: 'var(--popupColor)',
|
||||
popupBackgroundColor: 'var(--popupBackgroundColor)',
|
||||
popupBorderColor: 'var(--popupBorderColor)',
|
||||
highlightBackground: 'var(--highlightBackground)',
|
||||
highlightBorderColor: 'var(--highlightBorderColor)',
|
||||
highlightBackgroundHover: 'var(--highlightBackgroundHover)',
|
||||
mailboxActiveBackground: 'var(--mailboxActiveBackground)',
|
||||
mailboxHoverBackgroundColor: 'var(--mailboxHoverBackgroundColor)',
|
||||
msgItemActiveBackground: 'var(--msgItemActiveBackground)',
|
||||
msgItemHoverBackgroundColor: 'var(--msgItemHoverBackgroundColor)',
|
||||
msgItemFocusBorderColor: 'var(--msgItemFocusBorderColor)',
|
||||
buttonTristateOnBackground: 'var(--buttonTristateOnBackground)',
|
||||
buttonTristateOffBackground: 'var(--buttonTristateOffBackground)',
|
||||
warningBackgroundColor: 'var(--warningBackgroundColor)',
|
||||
successBackground: 'var(--successBackground)',
|
||||
emphasisBackground: 'var(--emphasisBackground)',
|
||||
// For authentication/security results.
|
||||
underlineGreen: 'var(--underlineGreen)',
|
||||
underlineRed: 'var(--underlineRed)',
|
||||
underlineBlue: 'var(--underlineBlue)',
|
||||
underlineGrey: 'var(--underlineGrey)',
|
||||
quoted1Color: 'var(--quoted1Color)',
|
||||
quoted2Color: 'var(--quoted2Color)',
|
||||
quoted3Color: 'var(--quoted3Color)',
|
||||
scriptSwitchUnderlineColor: 'var(--scriptSwitchUnderlineColor)',
|
||||
linkColor: 'var(--linkColor)',
|
||||
linkVisitedColor: 'var(--linkVisitedColor)',
|
||||
};
|
||||
const styleClasses = {
|
||||
// For quoted text, with multiple levels of indentations.
|
||||
quoted: [
|
||||
css('quoted1', { color: ['#03828f', '#71f2ff'] }),
|
||||
css('quoted2', { color: ['#c7445c', 'rgb(236, 76, 76)'] }),
|
||||
css('quoted3', { color: ['#417c10', 'rgb(115, 230, 20)'] }), // blue
|
||||
css('quoted1', { color: styles.quoted1Color }),
|
||||
css('quoted2', { color: styles.quoted2Color }),
|
||||
css('quoted3', { color: styles.quoted3Color }),
|
||||
],
|
||||
// When text switches between unicode scripts.
|
||||
scriptswitch: css('scriptswitch', { textDecoration: 'underline 2px', textDecorationColor: ['#dca053', 'rgb(232, 143, 30)'] }),
|
||||
scriptswitch: css('scriptswitch', { textDecoration: 'underline 2px', textDecorationColor: styles.scriptSwitchUnderlineColor }),
|
||||
textMild: css('textMild', { color: styles.colorMild }),
|
||||
// For keywords (also known as flags/labels/tags) on messages.
|
||||
keyword: css('keyword', { padding: '0 .15em', borderRadius: '.15em', fontWeight: 'normal', fontSize: '.9em', margin: '0 .15em', whiteSpace: 'nowrap', background: styles.highlightBackground, color: styles.color, border: '1px solid', borderColor: styles.highlightBorderColor }),
|
||||
@ -1168,15 +1222,15 @@ const styleClasses = {
|
||||
ensureCSS('.msgHeaders td', { wordBreak: 'break-word' }); // Prevent horizontal scroll bar for long header values.
|
||||
ensureCSS('.keyword.keywordCollapsed', { opacity: .75 }),
|
||||
// Generic styling.
|
||||
ensureCSS('*', { fontSize: 'inherit', fontFamily: "'ubuntu', 'lato', sans-serif", margin: 0, padding: 0, boxSizing: 'border-box' });
|
||||
ensureCSS('html', { backgroundColor: 'var(--backgroundColor)', color: 'var(--color)' });
|
||||
ensureCSS('*', { fontSize: 'inherit', fontFamily: "'ubuntu', 'lato', sans-serif", margin: 0, padding: 0, boxSizing: 'border-box' });
|
||||
ensureCSS('.mono, .mono *', { fontFamily: "'ubuntu mono', monospace" });
|
||||
ensureCSS('table td, table th', { padding: '.15em .25em' });
|
||||
ensureCSS('.pad', { padding: '.5em' });
|
||||
ensureCSS('iframe', { border: 0 });
|
||||
ensureCSS('img, embed, video, iframe', { backgroundColor: 'white', color: 'black' });
|
||||
ensureCSS(':root', { backgroundColor: styles.backgroundColor, color: styles.color });
|
||||
ensureCSS('a', { color: ['rgb(9, 107, 194)', 'rgb(99, 182, 255)'] });
|
||||
ensureCSS('a:visited', { color: ['rgb(7, 4, 193)', 'rgb(199, 99, 255)'] });
|
||||
ensureCSS('a', { color: styles.linkColor });
|
||||
ensureCSS('a:visited', { color: styles.linkVisitedColor });
|
||||
// For message view with multiple inline elements (often a single text and multiple messages).
|
||||
ensureCSS('.textmulti > *:nth-child(even)', { backgroundColor: ['#f4f4f4', '#141414'] });
|
||||
ensureCSS('.textmulti > *', { padding: '2ex .5em', margin: '-.5em' /* compensate pad */ });
|
||||
@ -1417,13 +1471,17 @@ const init = () => {
|
||||
iframepath += '?sameorigin=true';
|
||||
let iframe;
|
||||
const page = document.getElementById('page');
|
||||
dom._kids(page, dom.div(css('msgMeta', { backgroundColor: styles.backgroundColorMild, borderBottom: '1px solid', borderBottomColor: styles.borderColor }), msgheaderview, msgattachmentview), iframe = dom.iframe(attr.title('Message body.'), attr.src(iframepath), css('msgIframe', { width: '100%', height: '100%' }), function load() {
|
||||
const root = dom.div(dom.div(css('msgMeta', { backgroundColor: styles.backgroundColorMild, borderBottom: '1px solid', borderBottomColor: styles.borderColor }), msgheaderview, msgattachmentview), iframe = dom.iframe(attr.title('Message body.'), attr.src(iframepath), css('msgIframe', { width: '100%', height: '100%' }), function load() {
|
||||
// Note: we load the iframe content specifically in a way that fires the load event only when the content is fully rendered.
|
||||
iframe.style.height = iframe.contentDocument.documentElement.scrollHeight + 'px';
|
||||
if (window.location.hash === '#print') {
|
||||
window.print();
|
||||
}
|
||||
}));
|
||||
if (typeof moxBeforeDisplay !== 'undefined') {
|
||||
moxBeforeDisplay(root);
|
||||
}
|
||||
dom._kids(page, root);
|
||||
};
|
||||
try {
|
||||
init();
|
||||
|
Reference in New Issue
Block a user