better code: isomorphic
Massimo Melina committed
Jun 11, 2024 at 12:28 UTC
b6cc47b997611581328deca9eb4675220539a62d
4 files changed
+10
-9
frontend/src/dialog.ts
+5
-5
@@ -14,14 +14,14 @@ export { toast } from './toasts'
14
_.merge(dialogsDefaults, { closableProps: { 'aria-label': t`Close` } })
15
16
interface PromptOptions extends Partial<DialogOptions> {
17
- def?: string,
17
+ value?: string,
18
type?: string,
19
trim?: boolean,
20
helperText?: ReactNode,
21
inputProps?: Partial<InputHTMLAttributes<string>>
22
onSubmit?: (v: string) => Promisable<string>
23
}
24
-export async function promptDialog(msg: string, { def, type, helperText, trim=true, inputProps, onSubmit, ...rest }:PromptOptions={}) : Promise<string | undefined> {
24
+export async function promptDialog(msg: string, { value, type, helperText, trim=true, inputProps, onSubmit, ...rest }:PromptOptions={}) : Promise<string | undefined> {
25
const textarea = type === 'textarea' && type
26
return new Promise(resolve => newDialog({
27
className: 'dialog-prompt',
@@ -38,8 +38,8 @@ export async function promptDialog(msg: string, { def, type, helperText, trim=tr
38
if (!e) return
39
const inp = e as HTMLInputElement
40
setTimeout(()=> inp.focus(),100)
41
- if (def)
42
- inp.value = def
41
+ if (value)
42
+ inp.value = value
43
if (textarea) {
44
function resize() {
45
inp.style.height = 'auto'
@@ -56,7 +56,7 @@ export async function promptDialog(msg: string, { def, type, helperText, trim=tr
56
type,
57
name: 'input',
58
style: {
59
- width: def ? (def.length / 2) + 'em' : 'auto',
59
+ width: value ? (value.length / 2) + 'em' : 'auto',
60
minWidth: '100%', maxWidth: '100%', boxSizing: 'border-box',
61
...textarea && { width: '30em', maxHeight: '70vh' },
62
},
frontend/src/fileMenu.ts
+1
-1
@@ -127,7 +127,7 @@ export function openFileMenu(entry: DirEntry, ev: MouseEvent, addToMenu: (FileMe
127
}
128
129
async function rename(entry: DirEntry) {
130
- const dest = await promptDialog(t`Name`, { def: entry.name, title: t`Rename` })
130
+ const dest = await promptDialog(t`Name`, { value: entry.name, title: t`Rename` })
131
if (!dest) return
132
try {
133
const { n, uri } = entry
frontend/src/upload.ts
+3
-3
@@ -163,7 +163,7 @@ export function showUpload() {
163
rec.comment = s || undefined
164
},
165
async edit(rec) {
166
- const s = await promptDialog(t('upload_name', "Upload with new name"), { def: rec.file.name })
166
+ const s = await promptDialog(t('upload_name', "Upload with new name"), { value: rec.file.name })
167
if (!s) return
168
rec.name = s
169
},
@@ -499,6 +499,6 @@ async function createFolder() {
499
}
500
}
501
502
-export function inputComment(filename: string, def?: string) {
503
- return promptDialog(t('enter_comment', { name: filename }, "Comment for {name}"), { def, type: 'textarea' })
502
+export function inputComment(filename: string, value?: string) {
503
+ return promptDialog(t('enter_comment', { name: filename }, "Comment for {name}"), { value, type: 'textarea' })
504
}
\ No newline at end of file
shared/index.ts
+1
@@ -48,6 +48,7 @@ export function buildUrlQueryString(params: Dict) { // not using URLSearchParams
48
}
49
50
export function domOn<K extends keyof WindowEventMap>(eventName: K, cb: (ev: WindowEventMap[K]) => void, { target=window }={}) {
51
+ if (!target) return
52
target.addEventListener(eventName, cb)
53
return () => target.removeEventListener(eventName, cb)
54
}