@@ -2,12 +2,11 @@ import React from "react";
22import BC0Editor from "./code_editor/bc0-editor" ;
33import C0EditorGroup from "./code_editor/c0-editor-group" ;
44
5- import { Segmented , Space , Tooltip , Upload } from "antd" ;
5+ import { Segmented , Space , Tooltip } from "antd" ;
66import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
77import { faCode , faLock } from "@fortawesome/free-solid-svg-icons" ;
88import { ConfigConsumer , ConfigConsumerProps } from "antd/es/config-provider" ;
99
10- import type { RcFile } from 'antd/lib/upload' ;
1110
1211export default class CodeEditor extends React . Component
1312< CodeEditorProps , CodeEditorState >
@@ -19,45 +18,6 @@ export default class CodeEditor extends React.Component
1918 mode : "c0" ,
2019 C0_nextKey : tabs . length === 0 ? 1 : Math . max ( ...tabs . map ( ( tab ) => tab . key ) ) + 1
2120 }
22- this . handle_import_folder = this . handle_import_folder . bind ( this ) ;
23- if ( DEBUG ) console . debug ( "handle_import_folder in CodeEditor is" , this . handle_import_folder )
24- }
25-
26- push_populated_tab ( tab : C0EditorTab ) {
27- // check if there's already file with this name and append _num if exists
28- let try_suffix = 1 ;
29- let is_conflict = false ;
30- const all_titles = this . props . app_state . C0Editors . map ( ( tab ) => tab . title ) ;
31- const extension = tab . title . slice ( tab . title . lastIndexOf ( "." ) ) ;
32- const file_name = tab . title . slice ( 0 , tab . title . lastIndexOf ( "." ) ) ;
33- const new_key = Math . max ( ...this . props . app_state . C0Editors . map ( ( tab ) => tab . key ) ) + 1 ;
34-
35- tab . key = new_key ;
36-
37- while ( all_titles . includes ( tab . title ) ) {
38- if ( ! all_titles . includes ( file_name + '_' + try_suffix + extension ) ) {
39- tab . title = file_name + '_' + try_suffix + extension ;
40- is_conflict = true ;
41- break ;
42- }
43- try_suffix ++
44- }
45-
46- if ( is_conflict ) {
47- globalThis . MSG_EMITTER . warn (
48- "Duplicated File Name" ,
49- `${ file_name + extension } already exists. It is renamed to ${ tab . title } to ensure all tabs have unique name.`
50- ) ;
51- }
52-
53- this . props . set_app_state ( ( S ) => {
54- const new_tabs = [ ...S . C0Editors , tab ]
55- for ( let i = 0 ; i < new_tabs . length ; i ++ ) {
56- new_tabs [ i ] . key = i ;
57- }
58- return { C0Editors : new_tabs , ActiveEditor : new_tabs [ 0 ] . key } ;
59- } ) ;
60- this . setState ( ( S ) => { return { C0_nextKey : S . C0_nextKey + 1 } } ) ;
6121 }
6222
6323 create_panel ( ) {
@@ -69,50 +29,13 @@ export default class CodeEditor extends React.Component
6929 breakpoints : [ ] ,
7030 } ) ;
7131 this . props . set_app_state ( { C0Editors : new_editors , ActiveEditor : this . state . C0_nextKey } ) ;
72- this . setState ( { C0_nextKey : this . state . C0_nextKey + 1 } ) ;
73- }
74-
75- // this function is called for every file in the uploaded directory, recursive.
76- // the function is called by ant design component "Upload"
77- handle_import_folder ( F : RcFile , FList : RcFile [ ] ) {
78- if ( DEBUG ) console . debug ( "received a folder upload, processing one of them" )
79-
80- if ( ! ( F . name . endsWith ( '.c0' ) || F . name . endsWith ( '.c1' ) || F . name . toLowerCase ( ) === "readme.txt" ) ) {
81- globalThis . MSG_EMITTER . warn (
82- "File is not Imported" ,
83- `${ F . name } is not a c0/c1 file and is thus ignored.`
84- ) ;
85- return Upload . LIST_IGNORE ;
86- }
87-
88- const reader = new FileReader ( ) ;
89-
90- reader . onload = e => {
91- if ( reader . result === null ) {
92- console . error ( "Failed to read input file" )
93- return Upload . LIST_IGNORE ;
94- }
95-
96- const res = reader . result . toString ( ) ;
97-
98- this . push_populated_tab ( {
99- title : F . name ,
100- key : - 1 ,
101- content : res ,
102- breakpoints : [ ] ,
103- } )
104- } ;
105- reader . readAsText ( F , "utf-8" ) ;
106-
107- // Prevent upload traffic
108- return false ;
32+ this . setState ( { C0_nextKey : this . state . C0_nextKey + 1 } )
10933 }
11034
111- remove_panel ( key : string ) {
112- const key_tbr = parseInt ( key ) ;
35+ remove_panel ( key : number ) {
11336 let new_editors : C0EditorTab [ ] = [ ...this . props . app_state . C0Editors ] ;
114- new_editors = new_editors . filter ( ( value ) => value . key !== key_tbr ) ;
115- const new_activeTab = this . props . app_state . ActiveEditor === key_tbr ? new_editors [ 0 ] . key : this . props . app_state . ActiveEditor ;
37+ new_editors = new_editors . filter ( ( value ) => value . key !== key ) ;
38+ const new_activeTab = this . props . app_state . ActiveEditor === key ? new_editors [ 0 ] . key : this . props . app_state . ActiveEditor ;
11639 this . props . set_app_state ( { C0Editors : new_editors , ActiveEditor : new_activeTab } ) ;
11740 }
11841
@@ -128,15 +51,13 @@ export default class CodeEditor extends React.Component
12851 return (
12952 < div className = "code-editor" data-lang = { this . state . mode } >
13053 < C0EditorGroup
131- currLine = { this . props . app_state . C0Runtime ?. state . CurrC0RefLine }
132- appState = { this . props . app_state }
54+ app_state = { this . props . app_state }
55+ set_app_state = { ( ns , cb ) => this . props . set_app_state ( ns , cb ) }
13356 selector = { selector }
134- set_app_state = { ( ns ) => this . props . set_app_state ( ns ) }
13557 set_group_state = { ( mode ) => this . setState ( { mode : mode } ) }
13658 newPanel = { ( ) => this . create_panel ( ) }
13759 removePanel = { ( key ) => this . remove_panel ( key ) }
138- updateContent = { ( key , s ) => this . update_content ( key , s ) }
139- handle_import_folder = { ( F : RcFile , FList : RcFile [ ] ) => this . handle_import_folder ( F , FList ) }
60+ set_content = { ( key , s ) => this . update_content ( key , s ) }
14061 />
14162 </ div > ) ;
14263 }
@@ -145,15 +66,13 @@ export default class CodeEditor extends React.Component
14566 let content = undefined ;
14667 if ( this . state . mode === "c0" ) {
14768 content = < C0EditorGroup
148- currLine = { this . props . app_state . C0Runtime ?. state . CurrC0RefLine }
149- appState = { this . props . app_state }
69+ app_state = { this . props . app_state }
70+ set_app_state = { ( ns , cb ) => this . props . set_app_state ( ns , cb ) }
15071 selector = { selector }
151- set_app_state = { ( ns ) => this . props . set_app_state ( ns ) }
15272 set_group_state = { ( mode ) => this . setState ( { mode : mode } ) }
15373 newPanel = { ( ) => this . create_panel ( ) }
15474 removePanel = { ( key ) => this . remove_panel ( key ) }
155- updateContent = { ( key , s ) => this . update_content ( key , s ) }
156- handle_import_folder = { ( F : RcFile , FList : RcFile [ ] ) => this . handle_import_folder ( F , FList ) }
75+ set_content = { ( key , s ) => this . update_content ( key , s ) }
15776 /> ;
15877 } else {
15978 const vm = this . props . app_state . C0Runtime ;
@@ -163,7 +82,6 @@ export default class CodeEditor extends React.Component
16382 { selector }
16483 </ div >
16584 < BC0Editor
166- updateContent = { s => this . props . set_app_state ( { BC0SourceCode : s } ) }
16785 editorValue = { this . props . app_state . BC0SourceCode }
16886 execLine = { vm === undefined ? 0 : vm . state . CurrLineNumber }
16987 breakpointVal = { this . props . app_state . BC0BreakPoints }
0 commit comments