@@ -17,6 +17,8 @@ import {
1717 newBuf
1818} from './editor' ;
1919
20+ import { genericFetch } from './tools'
21+
2022// parsing: copied from the iodide project
2123// https://github.com/iodide-project/iodide/blob/master/src/editor/iomd-tools/iomd-parser.js
2224import {
@@ -26,6 +28,7 @@ import {
2628// processing: execute/render editor's content
2729import {
2830 runPython ,
31+ runJS ,
2932 addCSS ,
3033 checkCssStatus ,
3134 renderMarkdown ,
@@ -56,46 +59,49 @@ const notebook = document.getElementById('rp-notebook');
5659// By default only the primary is visible.
5760// On click of split view, secondary editor is visible
5861// Each editor can display multiple documents and doc types.
59- // the created ones are main/tab1 /css
60- // user has the option to add their own document .
62+ // the created ones are main/python/js /css
63+ // user has the option to add their own documents .
6164// all new documents are python docs
6265// adapted/inspired from https://codemirror.net/demo/buffers.html
63- const primaryEditor = CodeMirror ( document . getElementById ( " primary-editor" ) , {
64- theme : " ayu-mirage" ,
66+ const primaryEditor = CodeMirror ( document . getElementById ( ' primary-editor' ) , {
67+ theme : ' ayu-mirage' ,
6568 lineNumbers : true ,
6669 lineWrapping : true ,
6770} ) ;
6871
69- const secondaryEditor = CodeMirror ( document . getElementById ( " secondary-editor" ) , {
72+ const secondaryEditor = CodeMirror ( document . getElementById ( ' secondary-editor' ) , {
7073 lineNumbers : true ,
7174 lineWrapping : true
7275} ) ;
7376
7477const buffers = { } ;
7578
7679// list of buffers (displayed on UI as inline list item next to run)
77- const buffersList = document . getElementById ( " buffers-list" ) ;
80+ const buffersList = document . getElementById ( ' buffers-list' ) ;
7881
7982// dropdown of buffers (visible on click of split view)
80- const buffersDropDown = document . getElementById ( " buffers-selection" ) ;
83+ const buffersDropDown = document . getElementById ( ' buffers-selection' ) ;
8184
8285// By default open 3 buffers, main, tab1 and css
8386// TODO: add a JS option
8487// Params for OpenBuffer (buffers object, name of buffer to create, default content, type, link in UI 1, link in UI 2)
85- openBuffer ( buffers , " main" ,
86- " # python code or code blocks that start with %%py, %%md %%math." , " notebook" ,
88+ openBuffer ( buffers , ' main' ,
89+ ' # python code or code blocks that start with %%py, %%md %%math.' , ' notebook' ,
8790 buffersDropDown , buffersList ) ;
8891
89- openBuffer ( buffers , "tab1" , " # Python code" , " python" ,
92+ openBuffer ( buffers , 'python' , ' # Python code' , ' python' ,
9093 buffersDropDown , buffersList ) ;
9194
92- // openBuffer(buffers, "js", "// Javascript code go here", "javascript", buffersDropDown, buffersList);
93- openBuffer ( buffers , "css" , "/* CSS */" , "css" , buffersDropDown ,
95+ openBuffer ( buffers , 'js' , '// Javascript code go here' , 'javascript' ,
96+ buffersDropDown , buffersList ) ;
97+
98+ openBuffer ( buffers , 'css' , '/* CSS goes here */' , 'css' , buffersDropDown ,
9499 buffersList ) ;
95100
96- // select main buffer by default
97- selectBuffer ( primaryEditor , buffers , "main" ) ;
98- selectBuffer ( secondaryEditor , buffers , "main" ) ;
101+ // select main buffer by default and set the main tab to active
102+ selectBuffer ( primaryEditor , buffers , 'main' ) ;
103+ selectBuffer ( secondaryEditor , buffers , 'main' ) ;
104+ document . querySelector ( 'ul#buffers-list li:first-child' ) . classList . add ( 'active' ) ;
99105
100106function onReady ( ) {
101107 /* By default the notebook has the keyword "loading"
@@ -124,7 +130,7 @@ function readEditors() {
124130 // get the content of the css editor
125131 // and add the css to the head
126132 // use dataset.status for a flag to know when to update
127- let cssCode = buffers [ " css" ] . getValue ( ) ;
133+ let cssCode = buffers [ ' css' ] . getValue ( ) ;
128134 let cssStatus = checkCssStatus ( ) ;
129135 switch ( cssStatus ) {
130136 case 'none' :
@@ -139,13 +145,18 @@ function readEditors() {
139145 // do nothing
140146 }
141147
142- // get all the buffers, except css and main
148+ //
149+ let jsCode = buffers [ 'js' ] . getValue ( ) ;
150+ runJS ( jsCode ) ;
151+
152+ // get all the buffers, except css, js and main
143153 // css is auto executed at the start
144154 // main is parsed then executed at the end
145155 // main can have md, math and python function calls
146156 let {
147157 css,
148158 main,
159+ js,
149160 ...pythonBuffers
150161 } = buffers ;
151162
@@ -169,7 +180,7 @@ function parseCodeFromMainEditor() {
169180 // runJS(js_code);
170181
171182 // gets code from main editor
172- let mainCode = buffers [ " main" ] . getValue ( ) ;
183+ let mainCode = buffers [ ' main' ] . getValue ( ) ;
173184 /*
174185 Split code into chunks.
175186 Uses %%keyword or %% keyword as separator
@@ -190,9 +201,9 @@ function parseCodeFromMainEditor() {
190201 runPython ( content , notebook , error ) ;
191202 break ;
192203 // TODO: fix how js is injected and ran
193- // case 'js':
194- // runJS(content);
195- // break;
204+ case 'js' :
205+ runJS ( content ) ;
206+ break ;
196207 case 'md' :
197208 notebook . innerHTML += renderMarkdown ( content ) ;
198209 break ;
@@ -205,49 +216,53 @@ function parseCodeFromMainEditor() {
205216 } ) ;
206217}
207218
219+ function updatePopup ( type , message ) {
220+ document . getElementById ( 'popup' ) . dataset . type = type ;
221+ document . getElementById ( 'popup-header' ) . textContent = message ;
222+ }
223+
208224// import button
209225// show a url input + fetch button
210226// takes a url where there is raw code
211- // document.getElementById('popup-import').addEventListener('click', async function () {
212- // const url = document.getElementById('popup-url').value;
213- // const type = document.getElementById('popup').dataset.type;
214- // const code = await genericFetch(url, type);
215- // switch (type) {
216- // case '':
217- // case 'py':
218- // pyEditor.setValue(code);
219- // break;
220- // case 'js':
221- // jsEditor.setValue(code);
222- // break;
223- // default:
224- // //do nothing
225- // }
226- // });
227- // document.getElementById('import-code').addEventListener('click' , function() {
228- // updatePopup('python', 'URL (raw text format)');
229- // });
227+ document . getElementById ( 'popup-import' ) . addEventListener ( 'click' , async function ( ) {
228+ let url = document . getElementById ( 'popup-url' ) . value ;
229+ let type = document . getElementById ( 'popup' ) . dataset . type ;
230+ let code = await genericFetch ( url , type ) ;
231+ primaryEditor . setValue ( code ) ;
232+ } ) ;
233+
234+ document . getElementById ( 'import-code' ) . addEventListener ( 'click' , function ( ) {
235+ updatePopup ( 'python' , 'URL (raw text format)' ) ;
236+ } ) ;
230237
231238// click on an item in the list
232- CodeMirror . on ( buffersList , " click" , function ( e ) {
239+ CodeMirror . on ( buffersList , ' click' , function ( e ) {
233240 selectBuffer ( primaryEditor , buffers , e . target . dataset . language ) ;
234241} ) ;
235242
236243// select an item in the dropdown
237- CodeMirror . on ( buffersDropDown , " change" , function ( ) {
244+ CodeMirror . on ( buffersDropDown , ' change' , function ( ) {
238245 selectBuffer ( secondaryEditor , buffers , buffersDropDown . options [
239246 buffersDropDown . selectedIndex ] . value ) ;
240247} ) ;
241248
242249// when css code editor changes
243250// update data attribute flag to modified
244- CodeMirror . on ( buffers [ " css" ] , " change" , function ( ) {
251+ CodeMirror . on ( buffers [ ' css' ] , ' change' , function ( ) {
245252 let style = document . getElementsByTagName ( 'style' ) [ 0 ] ;
246253 if ( style ) {
247- style . dataset . status = " modified" ;
254+ style . dataset . status = ' modified' ;
248255 }
249256} ) ;
250257
258+ document . getElementById ( 'buffers-list' ) . addEventListener ( 'click' , function ( event ) {
259+ let elem = document . querySelector ( '.active' ) ;
260+ if ( elem ) {
261+ elem . classList . remove ( 'active' ) ;
262+ }
263+ event . target . classList . add ( 'active' ) ;
264+ } ) ;
265+
251266// new tab, new buffer
252267document . getElementById ( 'new-tab' )
253268 . addEventListener ( 'click' , function ( ) {
0 commit comments