From 9f57c2baf55a4e4b0f3e7b0d591b44e330e5e45d Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Wed, 4 Oct 2017 12:02:51 -0400 Subject: [PATCH 1/2] Introduce new CodeMirror block for editing code. While demonstrating Gutenberg to my team the question was asked about editing code blocks: "it's so terribly difficult in the WordPress editor." Wanting to demonstrate some of the flexibility that the block system provides I created a new block type which turns a normal block into an instance of the CodeMirror editor. This should eventually replace the standard `code` block once it's ready for production. At the moment it lazy-loads the `CodeMirror` editor _and_ the mode files, so thankfully this keeps the initial builds small. --- blocks/library/code-mirror/index.js | 128 ++++++++++++++++++++++++++++ blocks/library/index.js | 2 + package.json | 1 + post-content.js | 20 +++++ webpack.config.js | 1 + 5 files changed, 152 insertions(+) create mode 100644 blocks/library/code-mirror/index.js diff --git a/blocks/library/code-mirror/index.js b/blocks/library/code-mirror/index.js new file mode 100644 index 00000000000000..1fc93e4ec8296e --- /dev/null +++ b/blocks/library/code-mirror/index.js @@ -0,0 +1,128 @@ +/** + * 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 'i18n'; +import InspectorControls from '../../inspector-controls'; +import { registerBlockType, source } from '../../api'; +const { prop } = source; + +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: wp.i18n.__( 'Code Editor' ), + icon: 'text', + category: 'formatting', + + attributes: { + content: { type: 'string', source: prop( 'code', 'textContent' ) }, + language: { type: 'string' }, + }, + + edit( { attributes, focus, setAttributes, setFocus } ) { + if ( ! initialized ) { + require.ensure( [], require => { + CodeMirror = require( 'react-codemirror' ); + initialized = true; + setFocus(); + } ); + + return
{ attributes.content }
; + } + + const language = attributes.language; + const modeData = modes[ language ]; + + // make sure we load in the mode settings + if ( language && modeData && ! modeData.hasLoaded ) { + modeData.req().then( () => { + modes[ language ].hasLoaded = true; + setFocus(); + } ); + } + + // we have to force a refresh of the editor, so + // don't actually load the language setting until + // the mode file has loaded + const mode = modeData && modeData.hasLoaded + ? language + : undefined; + + return ( +
+ setAttributes( { content: value } ) } + options={ { + lineNumbers: true, + mode, + } } + /> + { focus && ( + + + + + ) } +
+ ); + }, + + save( { attributes } ) { + 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: { From 94abcdc03d9754ec15797bbc937173bb07af1634 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Sat, 2 Dec 2017 16:36:29 -0600 Subject: [PATCH 2/2] Update with project --- blocks/library/code-mirror/index.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/blocks/library/code-mirror/index.js b/blocks/library/code-mirror/index.js index 1fc93e4ec8296e..ce5347d752be90 100644 --- a/blocks/library/code-mirror/index.js +++ b/blocks/library/code-mirror/index.js @@ -9,10 +9,9 @@ require( 'codemirror/lib/codemirror.css' ); /** * Internal dependencies */ -import { __ } from 'i18n'; +import { __ } from '@wordpress/i18n'; import InspectorControls from '../../inspector-controls'; -import { registerBlockType, source } from '../../api'; -const { prop } = source; +import { registerBlockType } from '../../api'; let initialized = false; let CodeMirror; // loaded on-demand below @@ -54,12 +53,17 @@ const modes = { const languageList = Object.keys( modes ).sort( ( a, b ) => modes[ a ].label.localeCompare( modes[ b ].label ) ); registerBlockType( 'core/code-mirror', { - title: wp.i18n.__( 'Code Editor' ), + title: __( 'Code Editor' ), icon: 'text', category: 'formatting', attributes: { - content: { type: 'string', source: prop( 'code', 'textContent' ) }, + content: { + type: 'string', + source: 'property', + selector: 'code', + property: 'textContent', + }, language: { type: 'string' }, }, @@ -100,6 +104,7 @@ registerBlockType( 'core/code-mirror', { options={ { lineNumbers: true, mode, + readOnly: ! focus, } } /> { focus && (