diff --git a/components/code-editor/README.md b/components/code-editor/README.md deleted file mode 100644 index c48c0545aa0b43..00000000000000 --- a/components/code-editor/README.md +++ /dev/null @@ -1,88 +0,0 @@ -CodeEditor -======= - -CodeEditor is a React component that provides the user with a code editor -that has syntax highlighting and linting. - -The components acts as a drop-in replacement for a
' } - onChange={ value => console.log( value ) } - /> - ); -} -``` - -## Props - -The component accepts the following props: - -### value - -The source code to load into the code editor. - -- Type: `string` -- Required: Yes - -### focus - -Whether or not the code editor should be focused. - -- Type: `boolean` -- Required: No - -### onFocus - -The function called when the editor is focused. - -- Type: `Function` -- Required: No - -### onChange - -The function called when the user has modified the source code via the -editor. It is passed the new value as an argument. - -- Type: `Function` -- Required: No - -### settings - -The settings object used to initialize the WordPress code editor. The object contains all of the settings for the editor, including specific settings for CodeMirror. This object is passed into `wp.codeEditor.initialize()`. If you do not specify a settings object, `window._wpGutenbergCodeEditorSettings` will be used instead. - -If you are extending `window._wpGutenbergCodeEditorSettings` make sure to clone the object using `Object.assign` or something similar instead of modifying it directly so the default settings remain the same. - -``` -const settings = Object.assign( { - codemirror: { - mode: css, - lint: false, - } }, - window._wpGutenbergCodeEditorSetting -); -``` - -- Type: `Object` -- Required: No - -### editorRef - -A reference to the instance of CodeMirror initialized when the editor is loaded so that it can be dynamically updated from a parent component. - -`editorRef={ ( ref ) => this.editorInstance = ref }` - -`this.editorInstance` will contain a full instance of `CodeMirror` which can then be modified or updated from the component it is being reference from using the [CodeMirror API](https://codemirror.net/doc/manual.html#api). For example, to dynamically change the language mode of CodeMirror to CSS you can call: - -`this.editorInstance.setOption( 'mode', 'css' );` - -- Type `Function` -- Required: No diff --git a/components/code-editor/editor.js b/components/code-editor/editor.js deleted file mode 100644 index 6d0f52b6427f2e..00000000000000 --- a/components/code-editor/editor.js +++ /dev/null @@ -1,106 +0,0 @@ -/** - * WordPress dependencies - */ -import { Component } from '@wordpress/element'; -import { UP, DOWN } from '@wordpress/keycodes'; - -class CodeEditor extends Component { - constructor() { - super( ...arguments ); - - this.onFocus = this.onFocus.bind( this ); - this.onBlur = this.onBlur.bind( this ); - this.onCursorActivity = this.onCursorActivity.bind( this ); - this.onKeyHandled = this.onKeyHandled.bind( this ); - } - - componentDidMount() { - const settings = this.props.settings || window._wpGutenbergCodeEditorSettings; - const instance = wp.codeEditor.initialize( this.textarea, settings ); - this.editor = instance.codemirror; - - this.editor.on( 'focus', this.onFocus ); - this.editor.on( 'blur', this.onBlur ); - this.editor.on( 'cursorActivity', this.onCursorActivity ); - this.editor.on( 'keyHandled', this.onKeyHandled ); - - // Pass a reference to the editor back up. - if ( this.props.editorRef ) { - this.props.editorRef( this.editor ); - } - - this.updateFocus(); - } - - componentDidUpdate( prevProps ) { - if ( this.props.value !== prevProps.value && this.editor.getValue() !== this.props.value ) { - this.editor.setValue( this.props.value ); - } - - if ( this.props.focus !== prevProps.focus ) { - this.updateFocus(); - } - } - - componentWillUnmount() { - this.editor.on( 'focus', this.onFocus ); - this.editor.off( 'blur', this.onBlur ); - this.editor.off( 'cursorActivity', this.onCursorActivity ); - this.editor.off( 'keyHandled', this.onKeyHandled ); - - this.editor.toTextArea(); - this.editor = null; - } - - onFocus() { - if ( this.props.onFocus ) { - this.props.onFocus(); - } - } - - onBlur( editor ) { - if ( this.props.onChange ) { - this.props.onChange( editor.getValue() ); - } - } - - onCursorActivity( editor ) { - this.lastCursor = editor.getCursor(); - } - - onKeyHandled( editor, name, event ) { - /* - * Pressing UP/DOWN should only move focus to another block if the cursor is - * at the start or end of the editor. - * - * We do this by stopping UP/DOWN from propagating if: - * - We know what the cursor was before this event; AND - * - This event caused the cursor to move - */ - if ( event.keyCode === UP || event.keyCode === DOWN ) { - const areCursorsEqual = ( a, b ) => a.line === b.line && a.ch === b.ch; - if ( this.lastCursor && ! areCursorsEqual( editor.getCursor(), this.lastCursor ) ) { - event.stopImmediatePropagation(); - } - } - } - - updateFocus() { - if ( this.props.focus && ! this.editor.hasFocus() ) { - // Need to wait for the next frame to be painted before we can focus the editor - window.requestAnimationFrame( () => { - this.editor.focus(); - } ); - } - - if ( ! this.props.focus && this.editor.hasFocus() ) { - document.activeElement.blur(); - } - } - - render() { - return