Skip to content

Commit 3ba27e9

Browse files
authored
Feat: Config option to debounce preview rendering while live editing
PR styleguidist#227
2 parents 899471e + ce80545 commit 3ba27e9

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

docs/Configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ You can change settings in the `styleguide.config.js` file in your project’s r
108108
Type: `String`, default: `base16-light`<br>
109109
[CodeMirror theme](http://codemirror.net/demo/theme.html) name to use for syntax highlighting in examples.
110110

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+
111115
* **`getExampleFilename`**<br>
112116
Type: `Function`, default: finds `Readme.md` in the component folder<br>
113117
Function that returns examples file path for a given component path.

loaders/styleguide.loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ module.exports.pitch = function() {
141141
'title',
142142
'highlightTheme',
143143
'showCode',
144+
'previewDelay',
144145
]);
145146

146147
const code = toCode({

scripts/config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const findup = require('findup');
88
const semverUtils = require('semver-utils');
99
const prettyjson = require('prettyjson');
1010
const merge = require('lodash/merge');
11+
const isFinite = require('lodash/isFinite');
12+
const isUndefined = require('lodash/isUndefined');
1113
const utils = require('./utils/utils');
1214
const consts = require('./consts');
1315
const StyleguidistError = require('./utils/error');
@@ -28,6 +30,7 @@ const DEFAULT_CONFIG = {
2830
serverHost: 'localhost',
2931
serverPort: 3000,
3032
highlightTheme: 'base16-light',
33+
previewDelay: 500,
3134
verbose: false,
3235
getExampleFilename: componentpath => path.join(path.dirname(componentpath), 'Readme.md'),
3336
getComponentPathLine: componentpath => componentpath,
@@ -170,6 +173,9 @@ function validateConfig(config) {
170173
throw new StyleguidistError('Styleguidist: "defaultExample" option must be either false, true, ' +
171174
'or a string path to a markdown file.');
172175
}
176+
if (!isUndefined(config.previewDelay) && !isFinite(config.previewDelay)) {
177+
throw new StyleguidistError('Styleguidist: "previewDelay" option must be a positive number.');
178+
}
173179
}
174180

175181
/**

src/rsg-components/Playground/Playground.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component, PropTypes } from 'react';
2+
import debounce from 'lodash/debounce';
23
import PlaygroundRenderer from 'rsg-components/Playground/PlaygroundRenderer';
34

45
export default class Playground extends Component {
@@ -17,6 +18,7 @@ export default class Playground extends Component {
1718
super(props, context);
1819
const { code } = props;
1920
const { showCode } = context.config;
21+
2022
this.state = {
2123
code,
2224
showCode,
@@ -37,10 +39,35 @@ export default class Playground extends Component {
3739
);
3840
}
3941

42+
componentWillUnmount() {
43+
// clear pending changes before unmount
44+
if (this.queuedChange) {
45+
this.queuedChange.cancel();
46+
}
47+
}
48+
4049
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({
4257
code,
4358
});
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+
}
4471
}
4572

4673
handleCodeToggle() {

0 commit comments

Comments
 (0)