Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support coloration schemes on non-chrome browsers #203

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ export function printBuffer(buffer, options) {
diffLogger(prevState, nextState, logger, isCollapsed);
}

try {
logger.groupEnd();
} catch (e) {
logger.log(`—— log end ——`);
}
logger.groupEnd();
});
}
4 changes: 3 additions & 1 deletion src/defaults.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as logger from './logger';

export default {
level: `log`,
logger: console,
logger: logger,
logErrors: true,
collapsed: undefined,
predicate: undefined,
Expand Down
65 changes: 65 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Browser support detection heavily borrowed from:
* https://github.com/evgenyrodionov/redux-logger/issues/170#issuecomment-240717892
*/
const browser = {
isFirefox: /firefox/i.test(navigator.userAgent),
isEdge: /\bEdge\b/.test(navigator.userAgent),
isIE: !!document.documentMode,
isChrome: !!window.chrome,
isSafari: !!window.safari,
};

const support = {
console: !!window.console,
consoleStyles: !(browser.isIE || browser.isEdge),
consoleGroupStyles: browser.isChrome || browser.isSafari,
};

/*
* By default colors are applied at the beginning of groupings strings
* This ensures that those are removed for browsers that do not support
*/
function truncateInitialColor(text) {
if (text && text.substring(0, 3) === `%c `) {
return text.substring(3);
}

return text;
}

export function log(text, css) {
if (!support.console) return;
if (css && support.consoleStyles) {
console.log(text, css);
} else {
console.log(truncateInitialColor(text));
}
}

export function group(text, css) {
if (!support.console) return;
if (css && support.consoleGroupStyles) {
console.group(text, css);
} else {
console.group(truncateInitialColor(text));
}
}

export function groupCollapsed(text, css) {
if (!support.console) return;
else if (css && support.consoleGroupStyles) {
console.groupCollapsed(text, css);
} else {
console.groupCollapsed(truncateInitialColor(text));
}
}

export function groupEnd() {
if (!support.console) return;
try {
console.groupEnd();
} catch (e) {
log(`—— log end ——`);
}
}