22import React from 'react' ;
33import CodeMirror from 'codemirror' ;
44import styled from 'styled-components' ;
5+ import { debounce } from 'lodash' ;
56
67import { getCodeMirror } from 'app/utils/codemirror' ;
78
@@ -70,6 +71,12 @@ const handleError = (cm, currentError, nextError, nextCode, nextId) => {
7071export default class CodeEditor extends React . PureComponent {
7172 props : Props ;
7273
74+ constructor ( props ) {
75+ super ( props ) ;
76+
77+ this . handleChange = debounce ( this . handleChange , 200 ) ;
78+ }
79+
7380 shouldComponentUpdate ( nextProps : Props ) {
7481 return nextProps . id !== this . props . id ||
7582 nextProps . error !== this . props . error ||
@@ -242,13 +249,20 @@ export default class CodeEditor extends React.PureComponent {
242249 }
243250 } ;
244251
252+ getCode = ( ) => {
253+ return this . codemirror . getValue ( ) ;
254+ } ;
255+
245256 prettify = async ( ) => {
246- const { id, code } = this . props ;
257+ const { id } = this . props ;
258+ const code = this . getCode ( ) ;
247259 try {
248260 const prettier = await System . import ( 'prettier' ) ;
249261 const newCode = prettier . format ( code ) ;
250- this . props . changeCode ( id , newCode ) ;
251- this . updateCodeMirrorCode ( newCode ) ;
262+ if ( newCode !== code ) {
263+ this . props . changeCode ( id , newCode ) ;
264+ this . updateCodeMirrorCode ( newCode ) ;
265+ }
252266 } catch ( e ) { }
253267 } ;
254268
@@ -257,7 +271,8 @@ export default class CodeEditor extends React.PureComponent {
257271 if ( preferences . prettifyOnSaveEnabled ) {
258272 await this . prettify ( ) ;
259273 }
260-
274+ const { id } = this . props ;
275+ this . props . changeCode ( id , this . getCode ( ) ) ;
261276 saveCode ( ) ;
262277 } ;
263278
0 commit comments