@@ -31,7 +31,7 @@ function updatePath(newPath, referencePath=null){
3131//Call in codemirror to initialize path in layerViewer
3232function updateLayer ( radius , nbPointsInLayer , pos = [ 0 , 0 , 0 ] ) {
3333 let iframe = document . getElementById ( "layerVieweriFrame" ) ;
34- if ( radius != null && nbPointsInLayer != null ) {
34+ if ( iframe && radius != null && nbPointsInLayer != null ) {
3535 iframe . contentWindow . state . radius = radius ;
3636 iframe . contentWindow . state . nbPointsInLayer = nbPointsInLayer ;
3737 }
@@ -41,7 +41,7 @@ function updateLayer(radius, nbPointsInLayer, pos=[0, 0, 0]){
4141//Call in codemirror to initialize path in profileViewer
4242function updateProfile ( layerHeight , nbLayers , pos = [ 0 , 0 , 0 ] ) {
4343 let iframe = document . getElementById ( "profileVieweriFrame" ) ;
44- if ( nbLayers != null && layerHeight != null ) {
44+ if ( iframe && nbLayers != null && layerHeight != null ) {
4545 iframe . contentWindow . state . layerHeight = layerHeight ;
4646 iframe . contentWindow . state . nbLayers = nbLayers ;
4747 }
@@ -58,6 +58,18 @@ function setBedDimensions(printerType){
5858 }
5959}
6060
61+ function getCoilCAMExample ( ) {
62+ // Check URL parameters for example vessel to load in
63+ // since there are no buttons in the simple viewer
64+ // Structure should be (ex: folder=some-folder&example=some-example (ex: folder=tutorial-functions&example=functions_sine)
65+ const currentUrl = window . location . href ;
66+ const url = new URL ( currentUrl ) ;
67+ const params = url . searchParams ;
68+ var folder = params . get ( 'folder' ) ; //specifies folder
69+ var vesselName = params . get ( 'example' ) ; //specifies name of file
70+ return [ folder , vesselName ] ;
71+ }
72+
6173function setUpCodeMirror ( ) {
6274 let textArea , textArea2 ;
6375 let editorCodeMirror ;
@@ -73,15 +85,8 @@ function setUpCodeMirror(){
7385 getExampleVessel ( defaultPathToVessel )
7486 . then ( text => { editorCodeMirror . setValue ( text ) } ) ;
7587
76- // Check URL parameters for example vessel to load in
77- // since there are no buttons in the simple viewer
78- // Structure should be (ex: folder=some-folder&example=some-example (ex: folder=tutorial-functions&example=functions_sine)
79- const currentUrl = window . location . href ;
80- const url = new URL ( currentUrl ) ;
81- const params = url . searchParams ;
82- var folder = params . get ( 'folder' ) ; //specifies folder
83- var vesselName = params . get ( 'example' ) ; //specifies name of file
8488
89+ var [ folder , vesselName ] = getCoilCAMExample ( ) ;
8590 if ( vesselName !== null ) {
8691 if ( folder == null ) { folder = "" ; }
8792 var pathToVessel = "examples/" + folder + '/' + vesselName + '.js' ; //from URL parameters
@@ -97,17 +102,32 @@ function setUpCodeMirror(){
97102 mode : 'javascript' ,
98103 lineWrapping : true ,
99104 } ) ;
100- window . onload = function ( ) { //shortcut to comment out line
101- editorCodeMirror . setOption ( "extraKeys" , { "Cmd-/" :function ( cm ) {
102- let cursor = cm . getCursor ( ) ;
103- let lineNumber = cursor . line ;
104- let currentLine = cm . getLine ( lineNumber ) ;
105- if ( currentLine . startsWith ( "//" ) ) {
106- editorCodeMirror . replaceRange ( currentLine . slice ( 2 ) , CodeMirror . Pos ( lineNumber , 0 ) , CodeMirror . Pos ( lineNumber ) , 0 ) ;
107- } else {
108- editorCodeMirror . replaceRange ( "//" + currentLine , CodeMirror . Pos ( lineNumber , 0 ) , CodeMirror . Pos ( lineNumber ) , 0 ) ;
105+ window . onload = function ( ) { //shortcut to comment out single line
106+ editorCodeMirror . setOption ( "extraKeys" , {
107+ "Cmd-/" :function ( cm ) {
108+ let cursor = cm . getCursor ( ) ;
109+ let lineNumber = cursor . line ;
110+ let currentLine = cm . getLine ( lineNumber ) ;
111+ if ( currentLine . startsWith ( "//" ) ) {
112+ editorCodeMirror . replaceRange ( currentLine . slice ( 2 ) , CodeMirror . Pos ( lineNumber , 0 ) , CodeMirror . Pos ( lineNumber ) , 0 ) ;
113+ } else {
114+ editorCodeMirror . replaceRange ( "//" + currentLine , CodeMirror . Pos ( lineNumber , 0 ) , CodeMirror . Pos ( lineNumber ) , 0 ) ;
115+ }
116+ } ,
117+ "Cmd-Enter" :function ( cm ) {
118+ runCode ( ) ;
119+ } ,
120+ "Shift-Enter" :function ( cm ) {
121+ runCode ( ) ;
109122 }
110- } } , ) ; }
123+ } ) ;
124+ }
125+
126+ editorCodeMirror . setOption ( "extraKeys" , { "Cmd-Enter" :function ( cm ) {
127+ console . log ( "working" ) ;
128+ runCode ( ) ;
129+ } } ) ;
130+
111131
112132 editorCodeMirror . setSize ( "100%" , "100%" ) ;
113133
@@ -119,14 +139,13 @@ function setUpCodeMirror(){
119139 textArea2 . style . width = 140 + '%' ;
120140 textArea2 . style . height = 200 + 'px' ;
121141
122-
123142 // configs
124143 consoleCodeMirror = CodeMirror . fromTextArea ( textArea2 , {
125144 lineNumbers : true ,
126145 mode : 'javascript' ,
127146 lineWrapping : true ,
128147
129- //extraKeys: {"Ctrl-Space":"autocomplete"}
148+ // extraKeys: {"Ctrl-Space":"autocomplete"}
130149 } ) ;
131150 consoleCodeMirror . setSize ( "100%" , "100%" ) ;
132151
@@ -191,10 +210,17 @@ function setUpCodeMirror(){
191210 } ) ;
192211 }
193212
194-
195-
196-
197-
213+ var webEditorButton = document . getElementById ( "b_editor" ) ;
214+ if ( webEditorButton ) {
215+ webEditorButton . addEventListener ( "click" , function ( ) {
216+ var [ folder , vesselName ] = getCoilCAMExample ( ) ;
217+ if ( vesselName !== null || folder !== null ) {
218+ window . location . href = "https://sambourgault.github.io/coilCAM-js/?folder=" + folder + "&example=" + vesselName ;
219+ } else {
220+ window . location . href = "https://sambourgault.github.io/coilCAM-js" ;
221+ }
222+ } ) ;
223+ }
198224
199225 document . getElementById ( "b_run" ) . addEventListener ( "click" , runCode ) ;
200226 function runCode ( ) {
@@ -220,17 +246,6 @@ function setUpCodeMirror(){
220246 anchor . click ( ) ;
221247 }
222248
223- const b_docs = document . getElementById ( "b_docs" )
224- if ( b_docs ) {
225- b_docs . addEventListener ( "click" , newTab , { capture : true } ) ;
226- function newTab ( ) {
227- let newTab = document . createElement ( 'a' ) ;
228- newTab . href = "https://github.com/sambourgault/coilCAM-docs" ;
229- newTab . target = "_blank" ;
230- newTab . click ( ) ;
231- }
232- }
233-
234249 document . getElementById ( "baby_pb" ) ?. addEventListener ( "click" , function ( ) { setBedDimensions ( "baby" ) } ) ;
235250 document . getElementById ( "super_pb" ) ?. addEventListener ( "click" , function ( ) { setBedDimensions ( "super" ) } ) ;
236251
0 commit comments