import { html } from 'lit'
import type { TFileComment, TQueueItemOperation, TQueueOperationContext } from './fileupload-types'
const COMMENTS_ATTRIBUTE = 'comments'
const formatTimestamp = (iso: string): string => {
const date = new Date(iso)
if (Number.isNaN(date.getTime())) return iso
const day = String(date.getDate()).padStart(2, '0')
const month = String(date.getMonth() + 1).padStart(2, '0')
const year = date.getFullYear()
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
return `${day}.${month}.${year} kl. ${hours}:${minutes}`
}
const handleRenameKeydown = (event: KeyboardEvent, save: () => void, cancel: () => void) => {
if (event.key === 'Enter') {
event.preventDefault()
event.stopPropagation()
save()
}
if (event.key === 'Escape') {
event.preventDefault()
event.stopPropagation()
cancel()
}
}
const handleCommentTextareaKeydown = (event: KeyboardEvent, save: () => void, cancel: () => void) => {
if (event.key === 'Escape') {
event.preventDefault()
event.stopPropagation()
cancel()
}
if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
event.preventDefault()
event.stopPropagation()
save()
}
}
export const createRemoveOperation = (removeFile: (fileId: string) => void): TQueueItemOperation => ({
id: 'remove',
title: 'Slett',
ariaLabel: 'Slett fil',
onClick: ({ file }) => removeFile(file.fileId),
})
export const createRenameOperation = (): TQueueItemOperation => ({
id: 'rename',
title: 'Rediger',
ariaLabel: 'Rediger filnavn',
renderInlineUI: (context: TQueueOperationContext) => {
const currentName =
context.getAttribute('targetFilename') ||
context.file.attributes?.targetFilename ||
context.file.file?.name ||
''
const inputId = `pkt-fileupload-rename-${context.file.fileId}`
const save = () => {
const input = document.getElementById(inputId) as HTMLInputElement | null
const nextName = input?.value?.trim()
if (nextName) {
context.setAttribute('targetFilename', nextName)
}
context.close()
}
return html`
handleRenameKeydown(event, save, context.close)}
/>
`
},
renderHidden: (context: TQueueOperationContext) => {
const targetFilename =
context.getAttribute('targetFilename') ||
context.file.attributes?.targetFilename ||
context.file.file?.name ||
''
return html``
},
})
export const createCommentOperation = (): TQueueItemOperation => ({
id: 'comment',
title: (file) => {
const comments = (file.attributes?.[COMMENTS_ATTRIBUTE] as TFileComment[] | undefined) ?? []
return comments.length > 0 ? '' : 'Legg til kommentar'
},
ariaLabel: 'Legg til kommentar',
renderExtendedUI: (context: TQueueOperationContext) => {
const comments = context.getAttribute(COMMENTS_ATTRIBUTE) ?? []
const existingComment = comments[0]
const textareaId = `pkt-fileupload-comment-${context.file.fileId}`
const save = () => {
const textarea = document.getElementById(textareaId) as HTMLTextAreaElement | null
const text = textarea?.value?.trim()
if (text) {
context.setAttribute(COMMENTS_ATTRIBUTE, [{ text, timestamp: new Date().toISOString() } satisfies TFileComment])
}
context.close()
}
return html`
`
},
renderContent: (context: TQueueOperationContext) => {
if (context.isActive) return null
const comments = context.getAttribute(COMMENTS_ATTRIBUTE) ?? []
if (comments.length === 0) return null
const comment = comments[0]
return html`
`
},
renderHidden: (context: TQueueOperationContext) => {
const comments = context.getAttribute(COMMENTS_ATTRIBUTE)
return html``
},
})