Repository navigation
Introduce new CodeMirror block for editing code. #665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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' ); | ||
|
|
||
| /** | ||
| * 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' ) }, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
import(/* webpackChunkName: "my-chunk-name" */ 'module');
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the comment.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can double check though too
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we miss something in Babel config ¯_(ツ)_/¯
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>; | ||
| }, | ||
| } ); | ||
There was a problem hiding this comment.
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:
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 :)There was a problem hiding this comment.
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.
importstatement.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