From 3269e1c58ada1a8a63617616093a954a0d813c41 Mon Sep 17 00:00:00 2001
From: Robert Anderson
Date: Mon, 8 Jan 2018 12:30:24 +1100
Subject: [PATCH 01/12] Use CodeMirror in Custom HTML block
Introduces a new component which wraps a CodeMirror
enhanced
' }
+ onChange={ value => console.log( value ) }
+ />
+ );
+}
+```
+
+## Props
+
+The component accepts the following props:
+
+### value
+
+The source code to load into the code editor.
+
+- Type: `string`
+- Required: Yes
+
+### onChange
+
+The function called when the user has modified the source code via the
+editor. It is passed the new value as an argument.
+
+- Type: `Function`
+- Required: No
diff --git a/components/code-editor/index.js b/components/code-editor/index.js
new file mode 100644
index 00000000000000..7f54f2f1278e1b
--- /dev/null
+++ b/components/code-editor/index.js
@@ -0,0 +1,44 @@
+/**
+ * WordPress dependencies
+ */
+import { Component } from '@wordpress/element';
+
+class CodeEditor extends Component {
+ constructor() {
+ super( ...arguments );
+
+ this.onBlur = this.onBlur.bind( this );
+ }
+
+ componentDidMount() {
+ const instance = wp.codeEditor.initialize( this.textarea );
+ this.editor = instance.codemirror;
+
+ this.editor.on( 'blur', this.onBlur );
+ }
+
+ componentWillReceiveProps( { value } ) {
+ if ( this.props.value !== value && this.editor.getValue() !== value ) {
+ this.editor.setValue( value );
+ }
+ }
+
+ componentWillUnmount() {
+ this.editor.off( 'blur', this.onBlur );
+
+ this.editor.toTextArea();
+ this.editor = null;
+ }
+
+ onBlur( editor ) {
+ if ( this.props.onChange ) {
+ this.props.onChange( editor.getValue() );
+ }
+ }
+
+ render() {
+ return