@@ -31,6 +31,13 @@ const styles = {
31
31
32
32
export const pad = ( num , maxLength ) => `${ num } ` . padStart ( 0 , maxLength ) ;
33
33
34
+ function omit ( keys , object ) {
35
+ return Object . entries ( object ) . reduce ( ( acc , [ key , value ] ) => {
36
+ acc [ key ] = keys . includes ( key ) ? '_ignored_' : value ;
37
+ return acc ;
38
+ } , { } ) ;
39
+ }
40
+
34
41
function formatTime ( time ) {
35
42
return `${ pad ( time . getHours ( ) , 2 ) } :${ pad ( time . getMinutes ( ) , 2 ) } :${ pad (
36
43
time . getSeconds ( ) ,
@@ -60,10 +67,16 @@ function renderDiff(diff) {
60
67
}
61
68
}
62
69
70
+ // debug can be `false`, `true` or `diff`. On production, logging is disabled
71
+ // when debug is `undefined` and can be enabled by setting it to either `true`
72
+ // or `diff`. On develop, it's enabled when `undefined`, and can be disabled
73
+ // by setting it to `false`.
74
+ const debug = localStorage . getItem ( 'debug' ) ;
75
+ const logLevel = debug === 'false' ? false : debug === 'true' ? true : debug ;
63
76
const isLoggingEnabled =
64
- process . env . NODE_ENV === 'development'
65
- ? localStorage . getItem ( 'debug' ) !== false
66
- : localStorage . getItem ( 'debug' ) === true ;
77
+ process . env . NODE_ENV === 'development' ? logLevel !== false : ! ! logLevel ;
78
+ const isDiffEnabled = logLevel === 'diff' ;
79
+ const diffIgnoreKeys = [ 'markupEditor' , 'queryEditor' , 'sandbox' ] ;
67
80
68
81
export function withLogging ( reducerFn ) {
69
82
if ( ! isLoggingEnabled ) {
@@ -79,7 +92,6 @@ export function withLogging(reducerFn) {
79
92
const newState = reducerFn ( prevState , action ) ;
80
93
81
94
const took = timer . now ( ) - started ;
82
- const diff = differ ( prevState , newState ) ;
83
95
84
96
const header = [
85
97
[
@@ -94,7 +106,7 @@ export function withLogging(reducerFn) {
94
106
] ;
95
107
96
108
if ( supportsGroups ) {
97
- console . group ( ...header ) ;
109
+ console . groupCollapsed ( ...header ) ;
98
110
} else {
99
111
console . log ( ...header ) ;
100
112
}
@@ -103,29 +115,38 @@ export function withLogging(reducerFn) {
103
115
console . log ( '%c action %O' , styles . action , action ) ;
104
116
console . log ( '%c new state %O' , styles . nextState , newState ) ;
105
117
106
- if ( ! diff ) {
107
- console . log (
108
- '%c diff %cno state change!' ,
109
- styles . prevState ,
110
- styles . error ,
111
- ) ;
118
+ if ( ! isDiffEnabled ) {
119
+ console . log ( '%c diff %c _disabled_' , styles . prevState , styles . note ) ;
112
120
} else {
113
- if ( supportsGroups ) {
114
- console . groupCollapsed ( ' diff' ) ;
115
- } else {
116
- console . log ( 'diff' ) ;
117
- }
121
+ const diff = differ (
122
+ omit ( diffIgnoreKeys , prevState ) ,
123
+ omit ( diffIgnoreKeys , newState ) ,
124
+ ) ;
118
125
119
- diff . forEach ( ( elem ) => {
126
+ if ( ! diff ) {
120
127
console . log (
121
- ` %c ${ styles . diff [ elem . kind ] . text } ` ,
122
- styles . diff [ elem . kind ] . style ,
123
- ... renderDiff ( elem ) ,
128
+ ' %c diff %cno state change!' ,
129
+ styles . prevState ,
130
+ styles . error ,
124
131
) ;
125
- } ) ;
126
-
127
- if ( supportsGroups ) {
128
- console . groupEnd ( ) ;
132
+ } else {
133
+ if ( supportsGroups ) {
134
+ console . groupCollapsed ( ' diff' ) ;
135
+ } else {
136
+ console . log ( 'diff' ) ;
137
+ }
138
+
139
+ diff . forEach ( ( elem ) => {
140
+ console . log (
141
+ `%c ${ styles . diff [ elem . kind ] . text } ` ,
142
+ styles . diff [ elem . kind ] . style ,
143
+ ...renderDiff ( elem ) ,
144
+ ) ;
145
+ } ) ;
146
+
147
+ if ( supportsGroups ) {
148
+ console . groupEnd ( ) ;
149
+ }
129
150
}
130
151
}
131
152
0 commit comments