1
1
import state from './state.js'
2
+ import { loadSetting , saveSetting } from './common/utilities.js' ;
2
3
3
4
const btnModeEditor = document . getElementById ( 'btn-mode-editor' ) ;
4
5
const btnModeSerial = document . getElementById ( 'btn-mode-serial' ) ;
@@ -8,28 +9,57 @@ const editorPage = document.getElementById('editor-page');
8
9
const serialPage = document . getElementById ( 'serial-page' ) ;
9
10
const pageSeparator = document . getElementById ( 'page-separator' ) ;
10
11
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 ) ;
15
31
}
16
- btnModeEditor . classList . toggle ( 'active' ) ;
17
- editorPage . classList . toggle ( 'active' )
18
- updatePageLayout ( true , false ) ;
19
- } ) ;
32
+ }
20
33
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 ) ;
25
39
}
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 ) ;
30
47
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 ( ) ) {
33
63
pageSeparator . classList . add ( 'active' ) ;
34
64
} else {
35
65
pageSeparator . classList . remove ( 'active' ) ;
@@ -40,39 +70,41 @@ function updatePageLayout(editor = false, serial = false) {
40
70
return ;
41
71
}
42
72
73
+ // Mobile layout, so only show one or the other
43
74
if ( mainContent . offsetWidth < 768 ) {
44
- if ( editor ) {
45
- btnModeSerial . classList . remove ( 'active' ) ;
75
+ if ( updateType == UPDATE_TYPE_EDITOR && isEditorVisible ( ) ) {
46
76
serialPage . classList . remove ( 'active' ) ;
47
- } else if ( serial ) {
48
- btnModeEditor . classList . remove ( 'active' ) ;
77
+ } else if ( updateType == UPDATE_TYPE_SERIAL && isSerialVisible ( ) ) {
49
78
editorPage . classList . remove ( 'active' ) ;
50
79
}
80
+
81
+ // Make sure the separator is hidden for mobile
51
82
pageSeparator . classList . remove ( 'active' ) ;
52
83
} else {
53
84
let w = mainContent . offsetWidth ;
54
85
let s = pageSeparator . offsetWidth ;
55
86
editorPage . style . width = ( ( w - s ) / 2 ) + 'px' ;
56
- editorPage . style . flex = '0 0 auto ' ;
87
+ editorPage . style . flex = 'none ' ;
57
88
serialPage . style . width = ( ( w - s ) / 2 ) + 'px' ;
58
- serialPage . style . flex = '0 0 auto ' ;
89
+ serialPage . style . flex = 'none ' ;
59
90
}
60
91
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' ) ;
63
97
}
64
- }
65
98
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
+ }
71
104
72
- export function showSerial ( ) {
73
- btnModeSerial . classList . add ( 'active' ) ;
74
- serialPage . classList . add ( 'active' ) ;
75
- updatePageLayout ( false , true ) ;
105
+ if ( isSerialVisible ( ) ) {
106
+ refitTerminal ( ) ;
107
+ }
76
108
}
77
109
78
110
function refitTerminal ( ) {
@@ -94,12 +126,12 @@ function refitTerminal() {
94
126
function fixViewportHeight ( e ) {
95
127
let vh = window . innerHeight * 0.01 ;
96
128
document . documentElement . style . setProperty ( '--vh' , `${ vh } px` ) ;
97
- updatePageLayout ( ) ;
129
+ updatePageLayout ( UPDATE_TYPE_EDITOR ) ;
98
130
}
99
- fixViewportHeight ( ) ;
100
- window . addEventListener ( "resize" , fixViewportHeight ) ;
101
131
132
+ // Resize the panes when the separator is moved
102
133
function resize ( e ) {
134
+ console . log ( "Resized" ) ;
103
135
const w = mainContent . offsetWidth ;
104
136
const gap = pageSeparator . offsetWidth ;
105
137
const ratio = e . clientX / w ;
@@ -124,6 +156,31 @@ function resize(e) {
124
156
serialPage . style . width = ( w - e . clientX - gap / 2 ) + 'px' ;
125
157
}
126
158
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
+
127
184
function stopResize ( e ) {
128
185
window . removeEventListener ( 'mousemove' , resize , false ) ;
129
186
window . removeEventListener ( 'mouseup' , stopResize , false ) ;
@@ -133,3 +190,7 @@ pageSeparator.addEventListener('mousedown', async function (e) {
133
190
window . addEventListener ( 'mousemove' , resize , false ) ;
134
191
window . addEventListener ( 'mouseup' , stopResize , false ) ;
135
192
} ) ;
193
+
194
+ fixViewportHeight ( ) ;
195
+ window . addEventListener ( "resize" , fixViewportHeight ) ;
196
+ loadPanelSettings ( ) ;
0 commit comments