11import state from './state.js'
2+ import { loadSetting , saveSetting } from './common/utilities.js' ;
23
34const btnModeEditor = document . getElementById ( 'btn-mode-editor' ) ;
45const btnModeSerial = document . getElementById ( 'btn-mode-serial' ) ;
@@ -8,28 +9,57 @@ const editorPage = document.getElementById('editor-page');
89const serialPage = document . getElementById ( 'serial-page' ) ;
910const pageSeparator = document . getElementById ( 'page-separator' ) ;
1011
11- btnModeEditor . addEventListener ( 'click' , async function ( e ) {
12- if ( btnModeEditor . classList . contains ( 'active' ) && ! btnModeSerial . classList . contains ( 'active' ) ) {
13- // this would cause both editor & serial pages to disappear
14- return ;
12+ const SETTING_EDITOR_VISIBLE = "editor-visible" ;
13+ const SETTING_TERMINAL_VISIBLE = "terminal-visible" ;
14+
15+ const UPDATE_TYPE_EDITOR = 1 ;
16+ const UPDATE_TYPE_SERIAL = 2 ;
17+
18+ function isEditorVisible ( ) {
19+ return editorPage . classList . contains ( 'active' ) ;
20+ }
21+
22+ function isSerialVisible ( ) {
23+ return serialPage . classList . contains ( 'active' ) ;
24+ }
25+
26+ async function toggleEditor ( ) {
27+ if ( isSerialVisible ( ) ) {
28+ editorPage . classList . toggle ( 'active' ) ;
29+ saveSetting ( SETTING_EDITOR_VISIBLE , isEditorVisible ( ) ) ;
30+ updatePageLayout ( UPDATE_TYPE_EDITOR ) ;
1531 }
16- btnModeEditor . classList . toggle ( 'active' ) ;
17- editorPage . classList . toggle ( 'active' )
18- updatePageLayout ( true , false ) ;
19- } ) ;
32+ }
2033
21- btnModeSerial . addEventListener ( 'click' , async function ( e ) {
22- if ( btnModeSerial . classList . contains ( 'active' ) && ! btnModeEditor . classList . contains ( 'active' ) ) {
23- // this would cause both editor & serial pages to disappear
24- return ;
34+ async function toggleSerial ( ) {
35+ if ( isEditorVisible ( ) ) {
36+ serialPage . classList . toggle ( 'active' ) ;
37+ saveSetting ( SETTING_TERMINAL_VISIBLE , isSerialVisible ( ) ) ;
38+ updatePageLayout ( UPDATE_TYPE_SERIAL ) ;
2539 }
26- btnModeSerial . classList . toggle ( 'active' ) ;
27- serialPage . classList . toggle ( 'active' )
28- updatePageLayout ( false , true ) ;
29- } ) ;
40+ }
41+
42+ btnModeEditor . removeEventListener ( 'click' , toggleEditor ) ;
43+ btnModeEditor . addEventListener ( 'click' , toggleEditor ) ;
44+
45+ btnModeSerial . removeEventListener ( 'click' , toggleSerial ) ;
46+ btnModeSerial . addEventListener ( 'click' , toggleSerial ) ;
3047
31- function updatePageLayout ( editor = false , serial = false ) {
32- if ( editorPage . classList . contains ( 'active' ) && serialPage . classList . contains ( 'active' ) ) {
48+ // Show the editor panel if hidden
49+ export function showEditor ( ) {
50+ editorPage . classList . add ( 'active' ) ;
51+ updatePageLayout ( UPDATE_TYPE_EDITOR ) ;
52+ }
53+
54+ // Show the serial panel if hidden
55+ export function showSerial ( ) {
56+ serialPage . classList . add ( 'active' ) ;
57+ updatePageLayout ( UPDATE_TYPE_SERIAL ) ;
58+ }
59+
60+ // update type is used to indicate which button was clicked
61+ function updatePageLayout ( updateType ) {
62+ if ( isEditorVisible ( ) && isSerialVisible ( ) ) {
3363 pageSeparator . classList . add ( 'active' ) ;
3464 } else {
3565 pageSeparator . classList . remove ( 'active' ) ;
@@ -40,39 +70,41 @@ function updatePageLayout(editor = false, serial = false) {
4070 return ;
4171 }
4272
73+ // Mobile layout, so only show one or the other
4374 if ( mainContent . offsetWidth < 768 ) {
44- if ( editor ) {
45- btnModeSerial . classList . remove ( 'active' ) ;
75+ if ( updateType == UPDATE_TYPE_EDITOR && isEditorVisible ( ) ) {
4676 serialPage . classList . remove ( 'active' ) ;
47- } else if ( serial ) {
48- btnModeEditor . classList . remove ( 'active' ) ;
77+ } else if ( updateType == UPDATE_TYPE_SERIAL && isSerialVisible ( ) ) {
4978 editorPage . classList . remove ( 'active' ) ;
5079 }
80+
81+ // Make sure the separator is hidden for mobile
5182 pageSeparator . classList . remove ( 'active' ) ;
5283 } else {
5384 let w = mainContent . offsetWidth ;
5485 let s = pageSeparator . offsetWidth ;
5586 editorPage . style . width = ( ( w - s ) / 2 ) + 'px' ;
56- editorPage . style . flex = '0 0 auto ' ;
87+ editorPage . style . flex = 'none ' ;
5788 serialPage . style . width = ( ( w - s ) / 2 ) + 'px' ;
58- serialPage . style . flex = '0 0 auto ' ;
89+ serialPage . style . flex = 'none ' ;
5990 }
6091
61- if ( serial ) {
62- refitTerminal ( ) ;
92+ // Match the button state to the panel state to avoid getting out of sync
93+ if ( isEditorVisible ( ) ) {
94+ btnModeEditor . classList . add ( 'active' ) ;
95+ } else {
96+ btnModeEditor . classList . remove ( 'active' ) ;
6397 }
64- }
6598
66- export function showEditor ( ) {
67- btnModeEditor . classList . add ( 'active' ) ;
68- editorPage . classList . add ( 'active' ) ;
69- updatePageLayout ( true , false ) ;
70- }
99+ if ( isSerialVisible ( ) ) {
100+ btnModeSerial . classList . add ( 'active' ) ;
101+ } else {
102+ btnModeSerial . classList . remove ( 'active' ) ;
103+ }
71104
72- export function showSerial ( ) {
73- btnModeSerial . classList . add ( 'active' ) ;
74- serialPage . classList . add ( 'active' ) ;
75- updatePageLayout ( false , true ) ;
105+ if ( isSerialVisible ( ) ) {
106+ refitTerminal ( ) ;
107+ }
76108}
77109
78110function refitTerminal ( ) {
@@ -94,12 +126,12 @@ function refitTerminal() {
94126function fixViewportHeight ( e ) {
95127 let vh = window . innerHeight * 0.01 ;
96128 document . documentElement . style . setProperty ( '--vh' , `${ vh } px` ) ;
97- updatePageLayout ( ) ;
129+ updatePageLayout ( UPDATE_TYPE_EDITOR ) ;
98130}
99- fixViewportHeight ( ) ;
100- window . addEventListener ( "resize" , fixViewportHeight ) ;
101131
132+ // Resize the panes when the separator is moved
102133function resize ( e ) {
134+ console . log ( "Resized" ) ;
103135 const w = mainContent . offsetWidth ;
104136 const gap = pageSeparator . offsetWidth ;
105137 const ratio = e . clientX / w ;
@@ -124,6 +156,31 @@ function resize(e) {
124156 serialPage . style . width = ( w - e . clientX - gap / 2 ) + 'px' ;
125157}
126158
159+ // For the moment, we're going to just use this to keep track of the shown and hidden states
160+ // of the terminal and editor (possibly plotter)
161+ function loadPanelSettings ( ) {
162+ // Load all saved settings or defaults
163+ // Update the terminal first
164+ if ( loadSetting ( SETTING_EDITOR_VISIBLE , true ) ) {
165+ editorPage . classList . add ( 'active' ) ;
166+ } else {
167+ editorPage . classList . remove ( 'active' ) ;
168+ }
169+
170+ if ( loadSetting ( SETTING_TERMINAL_VISIBLE , false ) ) {
171+ serialPage . classList . add ( 'active' ) ;
172+ } else {
173+ serialPage . classList . remove ( 'active' ) ;
174+ }
175+
176+ // Make sure at lest one is visible
177+ if ( ! isEditorVisible ( ) && ! isSerialVisible ( ) ) {
178+ editorPage . classList . add ( 'active' ) ;
179+ }
180+
181+ updatePageLayout ( UPDATE_TYPE_SERIAL ) ;
182+ }
183+
127184function stopResize ( e ) {
128185 window . removeEventListener ( 'mousemove' , resize , false ) ;
129186 window . removeEventListener ( 'mouseup' , stopResize , false ) ;
@@ -133,3 +190,7 @@ pageSeparator.addEventListener('mousedown', async function (e) {
133190 window . addEventListener ( 'mousemove' , resize , false ) ;
134191 window . addEventListener ( 'mouseup' , stopResize , false ) ;
135192} ) ;
193+
194+ fixViewportHeight ( ) ;
195+ window . addEventListener ( "resize" , fixViewportHeight ) ;
196+ loadPanelSettings ( ) ;
0 commit comments