@@ -16,10 +16,14 @@ import 'codemirror/addon/hint/xml-hint'
1616import 'codemirror/addon/hint/show-hint'
1717import 'codemirror/addon/hint/show-hint.css'
1818
19+ import 'codemirror/addon/comment/comment'
20+
1921// import 'codemirror/addon/format/formatting'
2022
2123import Hello from './Hello'
2224
25+ import beautify from 'xml-beautifier' ;
26+
2327
2428export default class XML extends Component {
2529 constructor ( props ) {
@@ -29,6 +33,7 @@ export default class XML extends Component {
2933 code : '<Mode>\n\t<XML/>\n</Mode>\n<AutoComplete>\n\tCtrl+Space\n</AutoComplete>\n<keyBindings>\n\tSublime\n</KeyBindings> ' ,
3034 viewOpToggle : false ,
3135 } ;
36+
3237 }
3338
3439 autoComplete = cm => {
@@ -75,22 +80,22 @@ export default class XML extends Component {
7580
7681 } ;
7782
78- // getSelectedRange = () => {
79- // const codeMirror = this.refs['CodeMirror'].getCodeMirrorInstance();
80- // return { from: codeMirror.getCursor(true), to: codeMirror.getCursor(false) };
83+ toggleComment = function ( cm ) {
84+ cm . toggleComment ( { indent : true } ) ;
85+ }
86+
87+ // toggleSelection = cm => {
88+ // const {from, to} = this.getSelectedRange(cm);
89+ // cm.lineComment(from, to, { indent: true });
8190 // }
82-
83- // autoFormatSelection = () => {
84- // const range = this.getSelectedRange();
85- // const codeMirror = this.refs['CodeMirror'].getCodeMirrorInstance();
86- // codeMirror.autoFormatRange(range.from, range.to);
91+
92+ indentToggle = ( cm ) => {
93+ cm . execCommand ( 'indentAuto' ) ;
94+ }
95+
96+ // getSelectedRange = cm => {
97+ // return { from: cm.getCursor(true), to: cm.getCursor(false) };
8798 // }
88-
89- // commentSelection = (isComment) => {
90- // const range = this.getSelectedRange();
91- // const codeMirror = this.refs['CodeMirror'].getCodeMirrorInstance();
92- // codeMirror.commentRange(isComment, range.from, range.to);
93- // }
9499
95100 updateCode = ( newCode ) => {
96101 this . setState ( {
@@ -105,6 +110,7 @@ export default class XML extends Component {
105110 { viewOpToggle : ! this . state . viewOpToggle }
106111 )
107112 }
113+
108114 render ( ) {
109115 const options = {
110116 lineNumbers : true ,
@@ -116,26 +122,27 @@ export default class XML extends Component {
116122 matchTags : true ,
117123 extraKeys : {
118124 "Ctrl-Space" : this . autoComplete ,
119- // "Shift-Tab": this.autoFormatSelection
125+ "Shift-Alt" : this . indentToggle ,
126+ "Ctrl-Alt" : this . toggleComment
120127 } ,
121128 autoCloseBrackets : true ,
122129 keyMap : "sublime"
123130 } ;
124131 return (
125- < div >
126- < CodeMirror
127- ref = "CodeMirror"
128- value = { this . state . code }
129- options = { options }
130- onChange = { this . updateCode }
131- />
132-
133- < div className = "btn btn-primary p-2 my-3 toggler" onClick = { this . handleSubmit } > Toggle Code Display</ div >
134- { /* c */ }
135- { this . state . viewOpToggle &&
136- < Hello code = { this . state . code } />
137- }
138- </ div >
132+ < div >
133+ < CodeMirror
134+ ref = "CodeMirror"
135+ value = { this . state . code }
136+ options = { options }
137+ onChange = { this . updateCode }
138+ />
139+ < div className = "key-bindings" > To Indent, Select and Shift+Alt < br /> To Comment, Select and Ctrl-Alt </ div >
140+ < div className = "btn btn-primary p-2 my-3 toggler" onClick = { this . handleSubmit } > Toggle Code Display</ div >
141+ { /* <div className="btn btn-primary p-2 my-5 " onClick={this.toggleSelection}>Toggle indent</div> */ }
142+ { this . state . viewOpToggle &&
143+ < Hello code = { this . state . code } />
144+ }
145+ </ div >
139146 )
140147 }
141148}
0 commit comments