Skip to content

Commit a331c56

Browse files
committed
Support a few languages
1 parent 0ff8bcc commit a331c56

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

blocks/library/code-mirror/index.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
*/
44
import CodeMirror from 'react-codemirror';
55
import he from 'he';
6+
require( 'codemirror/mode/css/css' );
7+
require( 'codemirror/mode/diff/diff' );
68
require( 'codemirror/mode/javascript/javascript' );
9+
require( 'codemirror/mode/markdown/markdown' );
10+
require( 'codemirror/mode/pegjs/pegjs' );
11+
require( 'codemirror/mode/php/php' );
712
require( 'codemirror/lib/codemirror.css' );
813

914
/**
@@ -27,18 +32,31 @@ registerBlockType( 'core/code-mirror', {
2732

2833
edit( { attributes, setAttributes } ) {
2934
return (
30-
<CodeMirror
31-
value={ toEditor( attributes.html ) }
32-
mode="javascript"
33-
onChange={ value => setAttributes( { html: fromEditor( value ) } ) }
34-
options={ {
35-
lineNumbers: true,
36-
} }
37-
/>
35+
<div>
36+
<select onBlur={ ( { target: { value } } ) => setAttributes( { language: value } ) }>
37+
{ [ 'css', 'diff', 'javascript', 'markdown', 'pegjs', 'php' ].map( language => (
38+
<option
39+
key={ language }
40+
selected={ language === attributes.language }
41+
value={ language }
42+
>
43+
{ language }
44+
</option>
45+
) ) }
46+
</select>
47+
<CodeMirror
48+
value={ toEditor( attributes.html ) }
49+
onChange={ value => setAttributes( { html: fromEditor( value ) } ) }
50+
options={ {
51+
lineNumbers: true,
52+
mode: attributes.language,
53+
} }
54+
/>
55+
</div>
3856
);
3957
},
4058

4159
save( { attributes } ) {
4260
return attributes.html;
43-
}
61+
},
4462
} );

post-content.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ window._wpGutenbergPost = {
165165
'https://make.wordpress.org/core/2017/01/17/editor-technical-overview/',
166166
'<!-- /wp:core/embed -->',
167167

168-
'<!-- wp:core/code-mirror -->',
168+
'<!-- wp:core/code-mirror language="php" -->',
169169
'const add = ( a, b ) =&gt; a + b;<br>const sum = l =&gt; l.reduce( add, 0 );<br>',
170170
'<!-- /wp:core/code-mirror -->',
171171

0 commit comments

Comments
 (0)