Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions blocks/library/code-mirror/index.js
Original file line number Diff line number Diff line change
@@ -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' );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All styles are loaded using the following syntax:

import './style.scss';

Webpack uses the following pattern test: /style\.s?css$/, to match styles. So it should work here, too. Unless this is the reason why it breaks :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I think it had to do with race conditions on the mounted components. I was comparing to async loading as the dynamic bits though vs. import statement.

mainly I meant to convey that this stylesheet seemed to need to load before the editor and I wasn't sure how best to do that


/**
* 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' ) },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.import is deprecated. You should use dynamic import instead`. See webpack 3 announcement post: https://medium.com/webpack/webpack-3-official-release-15fd2dd8f07b:

import(/* webpackChunkName: "my-chunk-name" */ 'module');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed we are on Webpack 2.7, but I think it still supports import, but without this magic comments.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment. import() was throwing errors as not supported

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can double check though too

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we miss something in Babel config ¯_(ツ)_/¯

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gziolo and @aduth I'm getting this when trying to use import()

ERROR in ./blocks/library/code-mirror/index.js
Module build failed: SyntaxError: Unexpected token (24:33)

Not seeing a source of that error either.

@dmsnell dmsnell Oct 4, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

babel/babel-loader#493

according to that ^^^ it seems like it's still experimental in babel so even though we have webpack support for it we're not going to get it unless we pull in a new dependency. I'm happy leaving this PR without additional dependencies.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/tc39/proposal-dynamic-import - it's stage 3. It learned yesterday that Gutenberg uses only features that are accepted by ECMA TC39. So this one needs to wait. It's good as it is :)

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 <div style={ { fontFamily: 'monospace', whiteSpace: 'pre' } }>{ attributes.content }</div>;
}

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 (
<div>
<CodeMirror
value={ attributes.content }
onChange={ value => setAttributes( { content: value } ) }
options={ {
lineNumbers: true,
mode,
readOnly: ! focus,
} }
/>
{ focus && (
<InspectorControls>
<label className="blocks-text-control__label" htmlFor="blocks-codemirror-language-select">{ __( 'Language' ) }</label>
<select // eslint-disable-line jsx-a11y/no-onchange
id="blocks-codemirror-language-select"
onChange={ ( { target: { value } } ) => setAttributes( { language: value } ) }
value={ attributes.language }
>
{ languageList.map( key => (
<option key={ key } value={ key }>
{ modes[ key ].label }
</option>
) ) }
</select>
</InspectorControls>
) }
</div>
);
},

save( { attributes } ) {
return <pre><code>{ attributes.content }</code></pre>;
},
} );
2 changes: 2 additions & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -39,6 +40,7 @@ export const registerCoreBlocks = () => {
button,
categories,
code,
codeMirror,
coverImage,
embed,
...embed.common,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions post-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,25 @@ window._wpGutenbergPost.content = {
'<p style="text-align:center"><img draggable="false" class="emoji" alt="👋" src="https://s.w.org/images/core/emoji/2.3/svg/1f44b.svg" /></p>',
'<!-- /wp:paragraph -->',

'<!-- wp:core/code-mirror {"language":"elm"} -->',
'<pre class="wp-block-code-mirror"><code>',
'parse : String -&gt; 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 &quot;&lt;!--&quot;)\n',
' |. whitespace\n',
' |. (symbol &quot;wp:&quot;)\n',
' |= blockName\n',
' |. whitespace\n',
' |= blockAttributes\n',
' |. (symbol &quot;--&gt;&quot;)\n',
'</code></pre>',
'<!-- /wp:core/code-mirror -->',

].join( '' ),
};
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const config = {
path: __dirname,
library: [ 'wp', '[name]' ],
libraryTarget: 'this',
publicPath: '/wp-content/plugins/gutenberg/',
},
externals,
resolve: {
Expand Down