File tree Expand file tree Collapse file tree 4 files changed +39
-1
lines changed
src/rsg-components/Playground Expand file tree Collapse file tree 4 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,10 @@ You can change settings in the `styleguide.config.js` file in your project’s r
108
108
Type: ` String ` , default: ` base16-light ` <br >
109
109
[ CodeMirror theme] ( http://codemirror.net/demo/theme.html ) name to use for syntax highlighting in examples.
110
110
111
+ * ** ` previewDelay ` ** <br >
112
+ Type: ` Number ` , default: 500<br >
113
+ Debounce time used before render the changes from the editor. While inserting code the preview will not be updated.
114
+
111
115
* ** ` getExampleFilename ` ** <br >
112
116
Type: ` Function ` , default: finds ` Readme.md ` in the component folder<br >
113
117
Function that returns examples file path for a given component path.
Original file line number Diff line number Diff line change @@ -141,6 +141,7 @@ module.exports.pitch = function() {
141
141
'title' ,
142
142
'highlightTheme' ,
143
143
'showCode' ,
144
+ 'previewDelay' ,
144
145
] ) ;
145
146
146
147
const code = toCode ( {
Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ const findup = require('findup');
8
8
const semverUtils = require ( 'semver-utils' ) ;
9
9
const prettyjson = require ( 'prettyjson' ) ;
10
10
const merge = require ( 'lodash/merge' ) ;
11
+ const isFinite = require ( 'lodash/isFinite' ) ;
12
+ const isUndefined = require ( 'lodash/isUndefined' ) ;
11
13
const utils = require ( './utils/utils' ) ;
12
14
const consts = require ( './consts' ) ;
13
15
const StyleguidistError = require ( './utils/error' ) ;
@@ -28,6 +30,7 @@ const DEFAULT_CONFIG = {
28
30
serverHost : 'localhost' ,
29
31
serverPort : 3000 ,
30
32
highlightTheme : 'base16-light' ,
33
+ previewDelay : 500 ,
31
34
verbose : false ,
32
35
getExampleFilename : componentpath => path . join ( path . dirname ( componentpath ) , 'Readme.md' ) ,
33
36
getComponentPathLine : componentpath => componentpath ,
@@ -170,6 +173,9 @@ function validateConfig(config) {
170
173
throw new StyleguidistError ( 'Styleguidist: "defaultExample" option must be either false, true, ' +
171
174
'or a string path to a markdown file.' ) ;
172
175
}
176
+ if ( ! isUndefined ( config . previewDelay ) && ! isFinite ( config . previewDelay ) ) {
177
+ throw new StyleguidistError ( 'Styleguidist: "previewDelay" option must be a positive number.' ) ;
178
+ }
173
179
}
174
180
175
181
/**
Original file line number Diff line number Diff line change 1
1
import React , { Component , PropTypes } from 'react' ;
2
+ import debounce from 'lodash/debounce' ;
2
3
import PlaygroundRenderer from 'rsg-components/Playground/PlaygroundRenderer' ;
3
4
4
5
export default class Playground extends Component {
@@ -17,6 +18,7 @@ export default class Playground extends Component {
17
18
super ( props , context ) ;
18
19
const { code } = props ;
19
20
const { showCode } = context . config ;
21
+
20
22
this . state = {
21
23
code,
22
24
showCode,
@@ -37,10 +39,35 @@ export default class Playground extends Component {
37
39
) ;
38
40
}
39
41
42
+ componentWillUnmount ( ) {
43
+ // clear pending changes before unmount
44
+ if ( this . queuedChange ) {
45
+ this . queuedChange . cancel ( ) ;
46
+ }
47
+ }
48
+
40
49
handleChange ( code ) {
41
- this . setState ( {
50
+ // clear pending changes before proceed
51
+ if ( this . queuedChange ) {
52
+ this . queuedChange . cancel ( ) ;
53
+ }
54
+
55
+ // stored update action
56
+ const queuedChange = ( ) => this . setState ( {
42
57
code,
43
58
} ) ;
59
+
60
+ const { previewDelay } = context . config ;
61
+
62
+ if ( previewDelay ) {
63
+ // if previewDelay is enabled debounce the code
64
+ this . queuedChange = debounce ( queuedChange , previewDelay ) ;
65
+ this . queuedChange ( ) ;
66
+ }
67
+ else {
68
+ // otherwise execute it
69
+ queuedChange ( ) ;
70
+ }
44
71
}
45
72
46
73
handleCodeToggle ( ) {
You can’t perform that action at this time.
0 commit comments