1
1
import { KeyboardKey } from '@/services/ioService' ;
2
- import { STRING_EDIT_LOCKED_ATTEMPT } from '@/strings' ;
3
2
import { WebApplication } from '@/ui_models/application' ;
4
3
import { AppState } from '@/ui_models/app_state' ;
5
4
import {
6
5
MENU_MARGIN_FROM_APP_BORDER ,
7
6
MAX_MENU_SIZE_MULTIPLIER ,
8
7
} from '@/views/constants' ;
9
- import {
10
- reloadFont ,
11
- transactionForAssociateComponentWithCurrentNote ,
12
- transactionForDisassociateComponentWithCurrentNote ,
13
- } from '@/components/NoteView/NoteView' ;
14
8
import {
15
9
Disclosure ,
16
10
DisclosureButton ,
@@ -19,18 +13,14 @@ import {
19
13
import {
20
14
ComponentArea ,
21
15
IconType ,
22
- ItemMutator ,
23
- NoteMutator ,
24
- PrefKey ,
25
16
SNComponent ,
26
17
SNNote ,
27
- TransactionalMutation ,
28
18
} from '@standardnotes/snjs' ;
29
19
import { FunctionComponent } from 'preact' ;
30
20
import { useEffect , useRef , useState } from 'preact/hooks' ;
31
21
import { Icon } from '../Icon' ;
32
22
import { createEditorMenuGroups } from './changeEditor/createEditorMenuGroups' ;
33
- import { EditorAccordionMenu } from './changeEditor/EditorAccordionMenu ' ;
23
+ import { ChangeEditorMenu } from './changeEditor/ChangeEditorMenu ' ;
34
24
35
25
type ChangeEditorOptionProps = {
36
26
appState : AppState ;
@@ -59,6 +49,7 @@ type MenuPositionStyle = {
59
49
right ?: number | 'auto' ;
60
50
bottom : number | 'auto' ;
61
51
left ?: number | 'auto' ;
52
+ visibility ?: 'hidden' | 'visible' ;
62
53
} ;
63
54
64
55
const calculateMenuPosition = (
@@ -102,11 +93,13 @@ const calculateMenuPosition = (
102
93
position = {
103
94
bottom : positionBottom ,
104
95
right : clientWidth - buttonRect . left ,
96
+ visibility : 'hidden' ,
105
97
} ;
106
98
} else {
107
99
position = {
108
100
bottom : positionBottom ,
109
101
left : buttonRect . right ,
102
+ visibility : 'hidden' ,
110
103
} ;
111
104
}
112
105
}
@@ -121,12 +114,14 @@ const calculateMenuPosition = (
121
114
...position ,
122
115
top : MENU_MARGIN_FROM_APP_BORDER + buttonRect . top - buttonRect . height ,
123
116
bottom : 'auto' ,
117
+ visibility : 'visible' ,
124
118
} ;
125
119
} else {
126
120
return {
127
121
...position ,
128
122
top : MENU_MARGIN_FROM_APP_BORDER ,
129
123
bottom : 'auto' ,
124
+ visibility : 'visible' ,
130
125
} ;
131
126
}
132
127
}
@@ -135,15 +130,16 @@ const calculateMenuPosition = (
135
130
return position ;
136
131
} ;
137
132
138
- const TIME_IN_MS_TO_WAIT_BEFORE_REPAINT = 1 ;
139
-
140
133
export const ChangeEditorOption : FunctionComponent < ChangeEditorOptionProps > = ( {
141
134
application,
142
- appState,
143
135
closeOnBlur,
144
136
note,
145
137
} ) => {
146
138
const [ changeEditorMenuOpen , setChangeEditorMenuOpen ] = useState ( false ) ;
139
+ const [ changeEditorMenuVisible , setChangeEditorMenuVisible ] = useState ( false ) ;
140
+ const [ changeEditorMenuMaxHeight , setChangeEditorMenuMaxHeight ] = useState <
141
+ number | 'auto'
142
+ > ( 'auto' ) ;
147
143
const [ changeEditorMenuPosition , setChangeEditorMenuPosition ] =
148
144
useState < MenuPositionStyle > ( {
149
145
right : 0 ,
@@ -193,84 +189,29 @@ export const ChangeEditorOption: FunctionComponent<ChangeEditorOptionProps> = ({
193
189
) ;
194
190
195
191
if ( newMenuPosition ) {
192
+ const { clientHeight } = document . documentElement ;
193
+ const footerElementRect = document
194
+ . getElementById ( 'footer-bar' )
195
+ ?. getBoundingClientRect ( ) ;
196
+ const footerHeightInPx = footerElementRect ?. height ;
197
+
198
+ if (
199
+ footerHeightInPx &&
200
+ newMenuPosition . top &&
201
+ newMenuPosition . top !== 'auto'
202
+ ) {
203
+ setChangeEditorMenuMaxHeight (
204
+ clientHeight - newMenuPosition . top - footerHeightInPx - 2
205
+ ) ;
206
+ }
207
+
196
208
setChangeEditorMenuPosition ( newMenuPosition ) ;
209
+ setChangeEditorMenuVisible ( true ) ;
197
210
}
198
- } , TIME_IN_MS_TO_WAIT_BEFORE_REPAINT ) ;
211
+ } ) ;
199
212
}
200
213
} , [ changeEditorMenuOpen ] ) ;
201
214
202
- const selectComponent = async ( component : SNComponent | null ) => {
203
- if ( component ) {
204
- if ( component . conflictOf ) {
205
- application . changeAndSaveItem ( component . uuid , ( mutator ) => {
206
- mutator . conflictOf = undefined ;
207
- } ) ;
208
- }
209
- }
210
-
211
- const transactions : TransactionalMutation [ ] = [ ] ;
212
-
213
- if ( appState . getActiveNoteController ( ) ?. isTemplateNote ) {
214
- await appState . getActiveNoteController ( ) . insertTemplatedNote ( ) ;
215
- }
216
-
217
- if ( note . locked ) {
218
- application . alertService . alert ( STRING_EDIT_LOCKED_ATTEMPT ) ;
219
- return ;
220
- }
221
-
222
- if ( ! component ) {
223
- if ( ! note . prefersPlainEditor ) {
224
- transactions . push ( {
225
- itemUuid : note . uuid ,
226
- mutate : ( m : ItemMutator ) => {
227
- const noteMutator = m as NoteMutator ;
228
- noteMutator . prefersPlainEditor = true ;
229
- } ,
230
- } ) ;
231
- }
232
- const currentEditor = application . componentManager . editorForNote ( note ) ;
233
- if ( currentEditor ?. isExplicitlyEnabledForItem ( note . uuid ) ) {
234
- transactions . push (
235
- transactionForDisassociateComponentWithCurrentNote (
236
- currentEditor ,
237
- note
238
- )
239
- ) ;
240
- }
241
- reloadFont ( application . getPreference ( PrefKey . EditorMonospaceEnabled ) ) ;
242
- } else if ( component . area === ComponentArea . Editor ) {
243
- const currentEditor = application . componentManager . editorForNote ( note ) ;
244
- if ( currentEditor && component . uuid !== currentEditor . uuid ) {
245
- transactions . push (
246
- transactionForDisassociateComponentWithCurrentNote (
247
- currentEditor ,
248
- note
249
- )
250
- ) ;
251
- }
252
- const prefersPlain = note . prefersPlainEditor ;
253
- if ( prefersPlain ) {
254
- transactions . push ( {
255
- itemUuid : note . uuid ,
256
- mutate : ( m : ItemMutator ) => {
257
- const noteMutator = m as NoteMutator ;
258
- noteMutator . prefersPlainEditor = false ;
259
- } ,
260
- } ) ;
261
- }
262
- transactions . push (
263
- transactionForAssociateComponentWithCurrentNote ( component , note )
264
- ) ;
265
- }
266
-
267
- await application . runTransactionalMutations ( transactions ) ;
268
- /** Dirtying can happen above */
269
- application . sync ( ) ;
270
-
271
- setSelectedEditor ( application . componentManager . editorForNote ( note ) ) ;
272
- } ;
273
-
274
215
return (
275
216
< Disclosure open = { changeEditorMenuOpen } onChange = { toggleChangeEditorMenu } >
276
217
< DisclosureButton
@@ -299,17 +240,19 @@ export const ChangeEditorOption: FunctionComponent<ChangeEditorOptionProps> = ({
299
240
} }
300
241
style = { {
301
242
...changeEditorMenuPosition ,
243
+ maxHeight : changeEditorMenuMaxHeight ,
302
244
position : 'fixed' ,
303
245
} }
304
246
className = "sn-dropdown flex flex-col max-h-120 min-w-68 fixed overflow-y-auto"
305
247
>
306
- < EditorAccordionMenu
248
+ < ChangeEditorMenu
307
249
application = { application }
308
250
closeOnBlur = { closeOnBlur }
309
251
currentEditor = { selectedEditor }
252
+ setSelectedEditor = { setSelectedEditor }
253
+ note = { note }
310
254
groups = { editorMenuGroups }
311
- isOpen = { changeEditorMenuOpen }
312
- selectComponent = { selectComponent }
255
+ isOpen = { changeEditorMenuVisible }
313
256
/>
314
257
</ DisclosurePanel >
315
258
</ Disclosure >
0 commit comments