-
-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathChangeEditorMenu.tsx
More file actions
349 lines (304 loc) · 11.5 KB
/
ChangeEditorMenu.tsx
File metadata and controls
349 lines (304 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import Icon from '@/Components/Icon/Icon'
import Menu from '@/Components/Menu/Menu'
import { usePremiumModal } from '@/Hooks/usePremiumModal'
import { STRING_EDIT_LOCKED_ATTEMPT } from '@/Constants/Strings'
import { WebApplication } from '@/Application/WebApplication'
import {
UIFeature,
EditorFeatureDescription,
NativeFeatureIdentifier,
IframeComponentFeatureDescription,
NoteMutator,
NoteType,
SNNote,
ContentType,
LocalPrefKey,
PreferencePaneId,
} from '@standardnotes/snjs'
import { FunctionComponent, useCallback, useEffect, useState } from 'react'
import { EditorMenuGroup } from '@/Components/NotesOptions/EditorMenuGroup'
import { EditorMenuItem } from '@/Components/NotesOptions/EditorMenuItem'
import { createEditorMenuGroups } from '../../Utils/createEditorMenuGroups'
import { reloadFont } from '../NoteView/FontFunctions'
import { PremiumFeatureIconClass, PremiumFeatureIconName } from '../Icon/PremiumFeatureIcon'
import { SuperNoteImporter } from '../SuperEditor/SuperNoteImporter'
import MenuRadioButtonItem from '../Menu/MenuRadioButtonItem'
import { Pill } from '../Preferences/PreferencesComponents/Content'
import ModalOverlay from '../Modal/ModalOverlay'
import SuperNoteConverter from '../SuperEditor/SuperNoteConverter'
import MenuSection from '../Menu/MenuSection'
type ChangeEditorMenuProps = {
application: WebApplication
closeMenu: () => void
note: SNNote | undefined
onSelect?: (component: UIFeature<EditorFeatureDescription | IframeComponentFeatureDescription>) => void
setDisableClickOutside?: (value: boolean) => void
}
const getGroupId = (group: EditorMenuGroup) => group.title.toLowerCase().replace(/\s/, '-')
const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
application,
closeMenu,
note,
onSelect,
setDisableClickOutside,
}) => {
const [groups, setGroups] = useState<EditorMenuGroup[]>([])
const [unableToFindEditor, setUnableToFindEditor] = useState(false)
const reloadGroups = useCallback(() => {
const groups = createEditorMenuGroups(application)
setGroups(groups)
if (note && note.editorIdentifier) {
let didFindEditor = false
for (const group of groups) {
for (const item of group.items) {
if (item.uiFeature.featureIdentifier === note.editorIdentifier) {
didFindEditor = true
break
}
}
}
setUnableToFindEditor(!didFindEditor)
}
}, [application, note])
useEffect(() => {
application.items.streamItems([ContentType.TYPES.Component], reloadGroups)
}, [application, reloadGroups])
useEffect(() => {
reloadGroups()
}, [reloadGroups])
const [currentFeature, setCurrentFeature] =
useState<UIFeature<EditorFeatureDescription | IframeComponentFeatureDescription>>()
const [pendingConversionItem, setPendingConversionItem] = useState<EditorMenuItem | null>(null)
const showSuperNoteImporter =
!!pendingConversionItem &&
note?.noteType !== NoteType.Super &&
!!note?.text.length &&
pendingConversionItem.uiFeature.noteType === NoteType.Super
const showSuperNoteConverter =
!!pendingConversionItem &&
note?.noteType === NoteType.Super &&
pendingConversionItem.uiFeature.noteType !== NoteType.Super
useEffect(() => {
if (note) {
setCurrentFeature(application.componentManager.editorForNote(note))
}
}, [application, note])
const premiumModal = usePremiumModal()
const isSelected = useCallback(
(item: EditorMenuItem) => {
if (currentFeature) {
return item.uiFeature.featureIdentifier === currentFeature.featureIdentifier
}
const itemNoteTypeIsSameAsCurrentNoteType = item.uiFeature.noteType === note?.noteType
const noteDoesntHaveTypeAndItemIsPlain = !note?.noteType && item.uiFeature.noteType === NoteType.Plain
const unknownNoteTypeAndItemIsPlain =
note?.noteType === NoteType.Unknown && item.uiFeature.noteType === NoteType.Plain
return itemNoteTypeIsSameAsCurrentNoteType || noteDoesntHaveTypeAndItemIsPlain || unknownNoteTypeAndItemIsPlain
},
[currentFeature, note],
)
const selectComponent = useCallback(
async (uiFeature: UIFeature<EditorFeatureDescription | IframeComponentFeatureDescription>, note: SNNote) => {
if (uiFeature.isComponent && uiFeature.asComponent.conflictOf) {
void application.changeAndSaveItem.execute(uiFeature.asComponent, (mutator) => {
mutator.conflictOf = undefined
})
}
await application.itemListController.insertCurrentIfTemplate()
await application.changeAndSaveItem.execute(note, (mutator) => {
const noteMutator = mutator as NoteMutator
noteMutator.noteType = uiFeature.noteType
noteMutator.editorIdentifier = uiFeature.featureIdentifier
})
setCurrentFeature(application.componentManager.editorForNote(note))
if (uiFeature.featureIdentifier === NativeFeatureIdentifier.TYPES.PlainEditor) {
reloadFont(application.preferences.getLocalValue(LocalPrefKey.EditorMonospaceEnabled))
}
},
[application],
)
const handleConversionCompletion = useCallback(
(item?: EditorMenuItem) => {
const conversionItem = item || pendingConversionItem
if (!conversionItem || !note) {
return
}
selectComponent(conversionItem.uiFeature, note).catch(console.error)
closeMenu()
},
[pendingConversionItem, note, closeMenu, selectComponent],
)
const handleMenuSelection = useCallback(
async (menuItem: EditorMenuItem) => {
if (!menuItem.isEntitled) {
if (menuItem.uiFeature.featureIdentifier === NativeFeatureIdentifier.TYPES.SuperEditor) {
premiumModal.showSuperDemo()
return
}
premiumModal.activate(menuItem.uiFeature.displayName)
return
}
if (!note) {
return
}
if (note.locked) {
application.alerts.alert(STRING_EDIT_LOCKED_ATTEMPT).catch(console.error)
return
}
if (menuItem.uiFeature.noteType === NoteType.Super) {
if (note.text.length === 0) {
handleConversionCompletion(menuItem)
return
}
if (note.noteType === NoteType.Super) {
return
}
setPendingConversionItem(menuItem)
setDisableClickOutside?.(true)
return
}
if (note.noteType === NoteType.Super && note.text.length > 0) {
setPendingConversionItem(menuItem)
setDisableClickOutside?.(true)
return
}
let shouldMakeSelection = true
if (menuItem.uiFeature) {
const changeRequiresAlert = application.componentManager.doesEditorChangeRequireAlert(
currentFeature,
menuItem.uiFeature,
)
if (changeRequiresAlert) {
shouldMakeSelection = await application.componentManager.showEditorChangeAlert()
}
}
if (shouldMakeSelection) {
selectComponent(menuItem.uiFeature, note).catch(console.error)
}
closeMenu()
if (onSelect) {
onSelect(menuItem.uiFeature)
}
},
[
note,
closeMenu,
onSelect,
premiumModal,
application.alerts,
application.componentManager,
setDisableClickOutside,
handleConversionCompletion,
currentFeature,
selectComponent,
],
)
const recommendSuper =
!note ||
(note.noteType &&
[NoteType.Plain, NoteType.Markdown, NoteType.RichText, NoteType.Task, NoteType.Code, NoteType.Unknown].includes(
note.noteType,
))
const closeSuperNoteImporter = () => {
setPendingConversionItem(null)
setDisableClickOutside?.(false)
}
const closeSuperNoteConverter = () => {
setPendingConversionItem(null)
setDisableClickOutside?.(false)
}
const managePlugins = useCallback(() => {
application.openPreferences(PreferencePaneId.Plugins)
}, [application])
return (
<>
<Menu className="pb-1 pt-0.5" a11yLabel="Change note type menu">
<MenuSection>
<div className="flex items-center justify-between py-3 pr-4 md:pb-1 md:pt-0">
<div className="px-3">
<h2 className="text-base font-bold">Choose a note type</h2>
{unableToFindEditor && (
<p className="mr-2 pt-1 text-xs text-warning">
Unable to find system editor for this note. Select Manage Plugins to reinstall this editor.
</p>
)}
</div>
<button className="cursor-pointer whitespace-nowrap text-right text-xs text-info" onClick={managePlugins}>
Manage Plugins
</button>
</div>
</MenuSection>
{groups
.filter((group) => group.items && group.items.length)
.map((group) => {
const groupId = getGroupId(group)
return (
<MenuSection key={groupId}>
{group.items.map((menuItem) => {
const onClickEditorItem = () => {
handleMenuSelection(menuItem).catch(console.error)
}
return (
<MenuRadioButtonItem
key={menuItem.uiFeature.uniqueIdentifier.value}
onClick={onClickEditorItem}
className={'flex-row-reversed py-2'}
checked={isSelected(menuItem)}
info={menuItem.uiFeature.description}
>
<div className="flex flex-grow items-center justify-between">
<div className={`flex items-center ${group.featured ? 'font-bold' : ''}`}>
{group.icon && <Icon type={group.icon} className={`mr-2 ${group.iconClassName}`} />}
{menuItem.uiFeature.displayName}
{menuItem.isLabs && (
<Pill className="px-1.5 py-0.5" style="success">
Labs
</Pill>
)}
{menuItem.uiFeature.featureIdentifier === NativeFeatureIdentifier.TYPES.SuperEditor &&
!isSelected(menuItem) &&
recommendSuper && (
<Pill className="px-1.5 py-0.5 text-[9px]" style="info">
Recommended
</Pill>
)}
</div>
{!menuItem.isEntitled && (
<Icon type={PremiumFeatureIconName} className={PremiumFeatureIconClass} />
)}
</div>
</MenuRadioButtonItem>
)
})}
</MenuSection>
)
})}
</Menu>
<ModalOverlay isOpen={showSuperNoteImporter} close={closeSuperNoteImporter}>
{note && (
<SuperNoteImporter
note={note}
application={application}
onComplete={handleConversionCompletion}
closeDialog={closeSuperNoteImporter}
/>
)}
</ModalOverlay>
<ModalOverlay
isOpen={showSuperNoteConverter}
close={closeSuperNoteConverter}
className="md:h-full md:max-h-[90%]"
>
{note && pendingConversionItem && (
<SuperNoteConverter
note={note}
convertTo={pendingConversionItem}
closeDialog={closeSuperNoteConverter}
onComplete={handleConversionCompletion}
/>
)}
</ModalOverlay>
</>
)
}
export default ChangeEditorMenu