@@ -5,111 +5,13 @@ import * as actions from '../actions';
55import { useAppDispatch } from '../configureStore' ;
66
77import AceEditor from './AceEditor' ;
8+ import SimpleEditor from './SimpleEditor' ;
89import MonacoEditor from './MonacoEditor' ;
9- import { CommonEditorProps , Editor as EditorType , Position , Selection } from '../types' ;
10+ import { Editor as EditorType } from '../types' ;
1011import { State } from '../reducers' ;
1112
1213import styles from './Editor.module.css' ;
1314
14- class CodeByteOffsets {
15- readonly code : string ;
16- readonly lines : string [ ] ;
17-
18- constructor ( code : string ) {
19- this . code = code ;
20- this . lines = code . split ( '\n' ) ;
21- }
22-
23- public lineToOffsets ( line : number ) {
24- const precedingBytes = this . bytesBeforeLine ( line ) ;
25-
26- const highlightedLine = this . lines [ line ] ;
27- const highlightedBytes = highlightedLine . length ;
28-
29- return [ precedingBytes , precedingBytes + highlightedBytes ] ;
30- }
31-
32- public rangeToOffsets ( start : Position , end : Position ) {
33- const startBytes = this . positionToBytes ( start ) ;
34- const endBytes = this . positionToBytes ( end ) ;
35- return [ startBytes , endBytes ] ;
36- }
37-
38- private positionToBytes ( position : Position ) {
39- // Subtract one as this logic is zero-based and the columns are one-based
40- return this . bytesBeforeLine ( position . line ) + position . column - 1 ;
41- }
42-
43- private bytesBeforeLine ( line : number ) {
44- // Subtract one as this logic is zero-based and the lines are one-based
45- line -= 1 ;
46-
47- const precedingLines = this . lines . slice ( 0 , line ) ;
48-
49- // Add one to account for the newline we split on and removed
50- return precedingLines . map ( l => l . length + 1 ) . reduce ( ( a , b ) => a + b ) ;
51- }
52- }
53-
54- class SimpleEditor extends React . PureComponent < CommonEditorProps > {
55- private _editor : HTMLTextAreaElement | null = null ;
56-
57- private onChange : React . ChangeEventHandler < HTMLTextAreaElement > = e => this . props . onEditCode ( e . target . value ) ;
58- private trackEditor : React . RefCallback < HTMLTextAreaElement > = component => this . _editor = component ;
59- private onKeyDown : React . KeyboardEventHandler < HTMLTextAreaElement > = e => {
60- if ( e . key === 'Enter' && ( e . ctrlKey || e . metaKey ) ) {
61- this . props . execute ( ) ;
62- }
63- }
64-
65- public render ( ) {
66- return (
67- < textarea
68- ref = { this . trackEditor }
69- className = { styles . simple }
70- name = "editor-simple"
71- autoCapitalize = "none"
72- autoComplete = "off"
73- autoCorrect = "off"
74- spellCheck = { false }
75- value = { this . props . code }
76- onChange = { this . onChange }
77- onKeyDown = { this . onKeyDown } />
78- ) ;
79- }
80-
81- public componentDidUpdate ( prevProps : CommonEditorProps ) {
82- this . gotoPosition ( prevProps . position , this . props . position ) ;
83- this . setSelection ( prevProps . selection , this . props . selection ) ;
84- }
85-
86- private gotoPosition ( oldPosition : Position , newPosition : Position ) {
87- const editor = this . _editor ;
88-
89- if ( ! newPosition || ! editor ) { return ; }
90- if ( newPosition === oldPosition ) { return ; }
91-
92- const offsets = new CodeByteOffsets ( this . props . code ) ;
93- const [ startBytes , endBytes ] = offsets . lineToOffsets ( newPosition . line ) ;
94-
95- editor . focus ( ) ;
96- editor . setSelectionRange ( startBytes , endBytes ) ;
97- }
98-
99- private setSelection ( oldSelection : Selection , newSelection : Selection ) {
100- const editor = this . _editor ;
101-
102- if ( ! newSelection || ! newSelection . start || ! newSelection . end || ! editor ) { return ; }
103- if ( newSelection === oldSelection ) { return ; }
104-
105- const offsets = new CodeByteOffsets ( this . props . code ) ;
106- const [ startBytes , endBytes ] = offsets . rangeToOffsets ( newSelection . start , newSelection . end ) ;
107-
108- editor . focus ( ) ;
109- editor . setSelectionRange ( startBytes , endBytes ) ;
110- }
111- }
112-
11315const editorMap = {
11416 [ EditorType . Simple ] : SimpleEditor ,
11517 [ EditorType . Ace ] : AceEditor ,
0 commit comments