diff --git a/blocks/library/code-mirror/index.js b/blocks/library/code-mirror/index.js new file mode 100644 index 00000000000000..ce5347d752be90 --- /dev/null +++ b/blocks/library/code-mirror/index.js @@ -0,0 +1,133 @@ +/** + * External dependencies + */ +// when this CSS was imported dynamically +// it was causing major visual glitches to +// the editor which completely broke it +require( 'codemirror/lib/codemirror.css' ); + +/** + * Internal dependencies + */ +import { __ } from '@wordpress/i18n'; +import InspectorControls from '../../inspector-controls'; +import { registerBlockType } from '../../api'; + +let initialized = false; +let CodeMirror; // loaded on-demand below + +// there are more +// @see node_modules/codemirror/modes +// when supported we can probably replace with standard `import()` syntax +const modes = { + apl: { label: 'APL', req: () => System.import( 'codemirror/mode/apl/apl' ) }, + clike: { label: 'C/C++/C#/Java', req: () => System.import( 'codemirror/mode/clike/clike' ) }, + clojure: { label: 'Clojure', req: () => System.import( 'codemirror/mode/clojure/clojure' ) }, + commonlisp: { label: 'Common Lisp', req: () => System.import( 'codemirror/mode/commonlisp/commonlisp' ) }, + css: { label: 'CSS', req: () => System.import( 'codemirror/mode/css/css' ) }, + diff: { label: 'diff', req: () => System.import( 'codemirror/mode/diff/diff' ) }, + ebnf: { label: 'EBNF', req: () => System.import( 'codemirror/mode/ebnf/ebnf' ) }, + elm: { label: 'Elm', req: () => System.import( 'codemirror/mode/elm/elm' ) }, + erlang: { label: 'Erlang', req: () => System.import( 'codemirror/mode/erlang/erlang' ) }, + gfm: { label: 'Markdown', req: () => System.import( 'codemirror/mode/gfm/gfm' ) }, + go: { label: 'go', req: () => System.import( 'codemirror/mode/go/go' ) }, + haskell: { label: 'Haskell', req: () => System.import( 'codemirror/mode/haskell/haskell' ) }, + htmlmixed: { label: 'HTML', req: () => System.import( 'codemirror/mode/htmlmixed/htmlmixed' ) }, + http: { label: 'HTTP', req: () => System.import( 'codemirror/mode/http/http' ) }, + javascript: { label: 'Javascript', req: () => System.import( 'codemirror/mode/javascript/javascript' ) }, + pegjs: { label: 'PEGjs', req: () => System.import( 'codemirror/mode/pegjs/pegjs' ) }, + php: { label: 'PHP', req: () => System.import( 'codemirror/mode/php/php' ) }, + python: { label: 'Python', req: () => System.import( 'codemirror/mode/python/python' ) }, + ruby: { label: 'Ruby', req: () => System.import( 'codemirror/mode/ruby/ruby' ) }, + rust: { label: 'Rust', req: () => System.import( 'codemirror/mode/rust/rust' ) }, + sass: { label: 'SASS', req: () => System.import( 'codemirror/mode/sass/sass' ) }, + shell: { label: 'Shell', req: () => System.import( 'codemirror/mode/shell/shell' ) }, + sql: { label: 'SQL', req: () => System.import( 'codemirror/mode/sql/sql' ) }, + swift: { label: 'Swift', req: () => System.import( 'codemirror/mode/swift/swift' ) }, + vue: { label: 'Vue', req: () => System.import( 'codemirror/mode/vue/vue' ) }, + xml: { label: 'XML', req: () => System.import( 'codemirror/mode/xml/xml' ) }, + yaml: { label: 'YAML', req: () => System.import( 'codemirror/mode/yaml/yaml' ) }, +}; + +// just a sorted list for the UI +const languageList = Object.keys( modes ).sort( ( a, b ) => modes[ a ].label.localeCompare( modes[ b ].label ) ); + +registerBlockType( 'core/code-mirror', { + title: __( 'Code Editor' ), + icon: 'text', + category: 'formatting', + + attributes: { + content: { + type: 'string', + source: 'property', + selector: 'code', + property: 'textContent', + }, + language: { type: 'string' }, + }, + + edit( { attributes, focus, setAttributes, setFocus } ) { + if ( ! initialized ) { + require.ensure( [], require => { + CodeMirror = require( 'react-codemirror' ); + initialized = true; + setFocus(); + } ); + + return
{ attributes.content };
+ },
+} );
diff --git a/blocks/library/index.js b/blocks/library/index.js
index ac6c0b2c20d20e..7793ed63126d32 100644
--- a/blocks/library/index.js
+++ b/blocks/library/index.js
@@ -10,6 +10,7 @@ import * as audio from './audio';
import * as button from './button';
import * as categories from './categories';
import * as code from './code';
+import * as codeMirror from './code-mirror';
import * as coverImage from './cover-image';
import * as embed from './embed';
import * as freeform from './freeform';
@@ -39,6 +40,7 @@ export const registerCoreBlocks = () => {
button,
categories,
code,
+ codeMirror,
coverImage,
embed,
...embed.common,
diff --git a/package.json b/package.json
index 9423b0abe8b2db..afe52efc9200bc 100644
--- a/package.json
+++ b/package.json
@@ -41,6 +41,7 @@
"react": "16.2.0",
"react-autosize-textarea": "2.0.0",
"react-click-outside": "2.3.1",
+ "react-codemirror": "1.0.0",
"react-color": "2.13.4",
"react-datepicker": "0.61.0",
"react-dom": "16.2.0",
diff --git a/post-content.js b/post-content.js
index 35e29f5935a631..2612a8eaddeffd 100644
--- a/post-content.js
+++ b/post-content.js
@@ -158,5 +158,25 @@ window._wpGutenbergPost.content = {
'',
+ 'parse : String -> Result String (List Block)\n',
+ 'parse s = runParser blockParser s\n',
+ '\n',
+ 'type BlockOpener = BlockOpener String (List (String, String))\n',
+ '\n',
+ 'blockOpener : Parser BlockOpener\n',
+ 'blockOpener =\n',
+ ' succeed BlockOpener\n',
+ ' |. (symbol "<!--")\n',
+ ' |. whitespace\n',
+ ' |. (symbol "wp:")\n',
+ ' |= blockName\n',
+ ' |. whitespace\n',
+ ' |= blockAttributes\n',
+ ' |. (symbol "-->")\n',
+ '',
+ '',
+
].join( '' ),
};
diff --git a/webpack.config.js b/webpack.config.js
index 04b7172e1496e0..a3074b13357476 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -90,6 +90,7 @@ const config = {
path: __dirname,
library: [ 'wp', '[name]' ],
libraryTarget: 'this',
+ publicPath: '/wp-content/plugins/gutenberg/',
},
externals,
resolve: {