2023-04-12 11:14:17 +02:00
|
|
|
import m, { Children, Component, Vnode, VnodeDOM } from "mithril"
|
2023-04-12 16:51:08 +02:00
|
|
|
import { Attachment } from "../mail/editor/SendMailModel.js"
|
|
|
|
import { Button, ButtonType } from "./base/Button.js"
|
|
|
|
import { Icons } from "./base/icons/Icons.js"
|
|
|
|
import { formatStorageSize } from "../misc/Formatter.js"
|
2023-10-17 12:50:51 +02:00
|
|
|
import { defer, DeferredObject, NBSP, noOp, Thunk } from "@tutao/tutanota-utils"
|
2023-04-12 16:51:08 +02:00
|
|
|
import { modal, ModalComponent } from "./base/Modal.js"
|
2023-04-24 15:02:34 +02:00
|
|
|
import { focusNext, focusPrevious, Shortcut } from "../misc/KeyManager.js"
|
2023-04-12 16:51:08 +02:00
|
|
|
import { PosRect } from "./base/Dropdown.js"
|
|
|
|
import { Keys } from "../api/common/TutanotaConstants.js"
|
|
|
|
import { px } from "./size.js"
|
|
|
|
import { Icon } from "./base/Icon.js"
|
|
|
|
import { theme } from "./theme.js"
|
|
|
|
import { animations, height, opacity, transform, TransformEnum, width } from "./animation/Animations.js"
|
|
|
|
import { ease } from "./animation/Easing.js"
|
2023-10-17 12:50:51 +02:00
|
|
|
import { getFileBaseName, getFileExtension, isTutanotaFile } from "../api/common/utils/FileUtils.js"
|
2023-04-12 16:51:08 +02:00
|
|
|
import { getSafeAreaInsetBottom } from "./HtmlUtils.js"
|
2023-10-17 12:50:51 +02:00
|
|
|
import { hasError } from "../api/common/utils/ErrorCheckUtils.js"
|
2023-04-12 11:14:17 +02:00
|
|
|
|
|
|
|
export type AttachmentBubbleAttrs = {
|
|
|
|
attachment: Attachment
|
|
|
|
download: Thunk | null
|
|
|
|
open: Thunk | null
|
2023-04-12 15:05:11 +02:00
|
|
|
remove: Thunk | null
|
2023-04-12 11:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class AttachmentBubble implements Component<AttachmentBubbleAttrs> {
|
|
|
|
private dom: HTMLElement | null = null
|
|
|
|
|
|
|
|
view(vnode: Vnode<AttachmentBubbleAttrs>): Children {
|
|
|
|
const { attachment } = vnode.attrs
|
2023-10-17 12:50:51 +02:00
|
|
|
if (isTutanotaFile(attachment) && hasError(attachment)) {
|
|
|
|
return m(Button, {
|
|
|
|
label: "emptyString_msg",
|
|
|
|
title: "corrupted_msg",
|
|
|
|
icon: () => Icons.Warning,
|
|
|
|
type: ButtonType.Bubble,
|
|
|
|
click: noOp,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
const extension = getFileExtension(attachment.name)
|
|
|
|
const rest = getFileBaseName(attachment.name)
|
|
|
|
return m(Button, {
|
|
|
|
label: () => rest,
|
|
|
|
title: () => attachment.name,
|
|
|
|
icon: () => Icons.Attachment,
|
|
|
|
type: ButtonType.Bubble,
|
|
|
|
staticRightText: `${extension}, ${formatStorageSize(Number(attachment.size))}`,
|
|
|
|
click: async () => {
|
|
|
|
await showAttachmentDetailsPopup(this.dom!, vnode.attrs)
|
|
|
|
this.dom?.focus()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2023-04-12 11:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
oncreate(vnode: VnodeDOM<AttachmentBubbleAttrs>): void {
|
|
|
|
this.dom = vnode.dom as HTMLElement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function showAttachmentDetailsPopup(dom: HTMLElement, attrs: AttachmentBubbleAttrs): Promise<void> {
|
|
|
|
const parentRect = dom.getBoundingClientRect()
|
|
|
|
const panel = new AttachmentDetailsPopup(parentRect, parentRect.width, attrs)
|
|
|
|
panel.show()
|
2023-04-24 15:02:34 +02:00
|
|
|
return panel.deferAfterClose
|
2023-04-12 11:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class AttachmentDetailsPopup implements ModalComponent {
|
|
|
|
private readonly _shortcuts: Array<Shortcut> = []
|
|
|
|
private domContent: HTMLElement | null = null
|
|
|
|
private domPanel: HTMLElement | null = null
|
2023-04-24 15:02:34 +02:00
|
|
|
private closeDefer: DeferredObject<void> = defer()
|
|
|
|
|
|
|
|
get deferAfterClose(): Promise<void> {
|
|
|
|
return this.closeDefer.promise
|
|
|
|
}
|
2023-04-12 11:14:17 +02:00
|
|
|
|
|
|
|
constructor(private readonly targetRect: PosRect, private readonly targetWidth: number, private readonly attrs: AttachmentBubbleAttrs) {
|
|
|
|
this._shortcuts.push({
|
|
|
|
key: Keys.ESC,
|
|
|
|
exec: () => this.onClose(),
|
|
|
|
help: "close_alt",
|
|
|
|
})
|
2023-04-24 15:02:34 +02:00
|
|
|
this._shortcuts.push({
|
|
|
|
key: Keys.TAB,
|
|
|
|
shift: true,
|
|
|
|
exec: () => (this.domContent ? focusPrevious(this.domContent) : false),
|
|
|
|
help: "selectPrevious_action",
|
|
|
|
})
|
|
|
|
this._shortcuts.push({
|
|
|
|
key: Keys.TAB,
|
|
|
|
shift: false,
|
|
|
|
exec: () => (this.domContent ? focusNext(this.domContent) : false),
|
|
|
|
help: "selectNext_action",
|
|
|
|
})
|
2023-04-12 11:14:17 +02:00
|
|
|
if (attrs.open) {
|
|
|
|
this._shortcuts.push({
|
|
|
|
key: Keys.O,
|
2023-04-12 15:22:45 +02:00
|
|
|
exec: () => this.thenClose(attrs.open),
|
2023-04-12 11:14:17 +02:00
|
|
|
help: "open_action",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (attrs.download) {
|
|
|
|
this._shortcuts.push({
|
|
|
|
key: Keys.D,
|
2023-04-12 15:22:45 +02:00
|
|
|
exec: () => this.thenClose(attrs.download),
|
2023-04-12 11:14:17 +02:00
|
|
|
help: "download_action",
|
|
|
|
})
|
|
|
|
}
|
2023-04-12 15:05:11 +02:00
|
|
|
if (attrs.remove) {
|
|
|
|
this._shortcuts.push({
|
|
|
|
key: Keys.DELETE,
|
2023-04-12 15:22:45 +02:00
|
|
|
exec: () => this.thenClose(attrs.remove),
|
2023-04-12 15:05:11 +02:00
|
|
|
help: "remove_action",
|
|
|
|
})
|
|
|
|
}
|
2023-04-12 11:14:17 +02:00
|
|
|
this.view = this.view.bind(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
view(): Children {
|
|
|
|
return m(
|
|
|
|
".abs.bubble-color.plr-button.border-radius.overflow-hidden.flex.flex-column",
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
width: px(this.targetWidth),
|
2023-07-25 18:30:02 +02:00
|
|
|
// see hack description below. if #5587 persists, we might try visibility: hidden instead?
|
2023-04-12 11:14:17 +02:00
|
|
|
opacity: "0",
|
|
|
|
},
|
|
|
|
oncreate: (vnode) => {
|
|
|
|
this.domPanel = vnode.dom as HTMLElement
|
2023-07-25 18:30:02 +02:00
|
|
|
// This is a hack to get "natural" view size but render it invisibly first and then show the panel with inferred size.
|
2023-04-24 15:02:34 +02:00
|
|
|
// also focus the first tabbable element in the content after the panel opens.
|
2023-07-25 18:30:02 +02:00
|
|
|
deferToNextFrame(() => this.animatePanel().then(() => this.domContent && focusNext(this.domContent)))
|
2023-04-12 11:14:17 +02:00
|
|
|
},
|
|
|
|
onclick: () => this.onClose(),
|
|
|
|
},
|
|
|
|
this.renderContent(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private renderContent(): Children {
|
2023-04-12 15:05:11 +02:00
|
|
|
const { remove, open, download, attachment } = this.attrs
|
2023-04-12 11:14:17 +02:00
|
|
|
return m(
|
|
|
|
".flex.row.mb-s.pr",
|
|
|
|
{
|
|
|
|
oncreate: (vnode) => (this.domContent = vnode.dom as HTMLElement),
|
|
|
|
},
|
|
|
|
[
|
|
|
|
m(Icon, {
|
|
|
|
icon: Icons.Attachment,
|
|
|
|
class: "pr-s",
|
|
|
|
style: {
|
|
|
|
fill: theme.button_bubble_fg,
|
|
|
|
"background-color": "initial",
|
2023-04-12 15:22:45 +02:00
|
|
|
marginTop: "6px",
|
2023-04-12 11:14:17 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
m(".flex.col.flex-grow", [
|
2023-04-12 15:22:45 +02:00
|
|
|
m(
|
|
|
|
".mb.break-all.smaller",
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
marginTop: "5px",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
attachment.name,
|
|
|
|
),
|
2023-04-12 11:14:17 +02:00
|
|
|
m(".flex.row.justify-between.items-center.flex-grow", [
|
2023-04-12 15:22:45 +02:00
|
|
|
m("span.smaller", `${formatStorageSize(Number(attachment.size))}`),
|
2023-04-12 11:14:17 +02:00
|
|
|
m(".no-wrap", [
|
2023-04-12 15:22:45 +02:00
|
|
|
remove ? m(Button, { type: ButtonType.Secondary, label: "remove_action", click: () => this.thenClose(remove) }) : null,
|
|
|
|
open ? m(Button, { type: ButtonType.Secondary, label: "open_action", click: () => this.thenClose(open) }) : null,
|
|
|
|
download ? m(Button, { type: ButtonType.Secondary, label: "download_action", click: () => this.thenClose(download) }) : null,
|
2023-04-12 11:14:17 +02:00
|
|
|
]),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-12 15:22:45 +02:00
|
|
|
private thenClose(action: Thunk | null): void {
|
|
|
|
action?.()
|
|
|
|
this.onClose()
|
|
|
|
}
|
|
|
|
|
2023-04-12 11:14:17 +02:00
|
|
|
private async animatePanel(): Promise<void> {
|
|
|
|
const { targetRect, domPanel, domContent } = this
|
|
|
|
if (domPanel == null || domContent == null) return
|
|
|
|
// from .bubble class
|
|
|
|
const initialHeight = 30
|
2023-07-25 18:30:02 +02:00
|
|
|
// there is a possibility that we get 0 here in some circumstances, but it's unclear when.
|
|
|
|
// might have something to do with the opacity: 0 hack above or because the original 24ms
|
|
|
|
// delay was not enough. https://github.com/tutao/tutanota/issues/5587
|
|
|
|
// 85 is a value that fits for a single line of attachment name, but looks weird for more while
|
|
|
|
// keeping the buttons accessible.
|
|
|
|
// if the reports continue, we can ask for logs.
|
|
|
|
const targetHeight = domContent.offsetHeight === 0 ? 85 : domContent.offsetHeight
|
|
|
|
if (domContent.offsetHeight === 0) {
|
|
|
|
console.log(
|
|
|
|
"got offsetHeight 0, panel contains content:",
|
|
|
|
domPanel.contains(domContent),
|
|
|
|
"content style:",
|
|
|
|
domContent.style,
|
|
|
|
"panel style:",
|
|
|
|
domPanel.style,
|
|
|
|
)
|
|
|
|
}
|
2023-04-12 11:14:17 +02:00
|
|
|
// for very short attachment bubbles, we need to set a min width so the buttons fit.
|
2023-04-24 15:02:34 +02:00
|
|
|
const targetWidth = Math.max(targetRect.width, 300)
|
2023-04-12 11:14:17 +02:00
|
|
|
domPanel.style.width = px(targetRect.width)
|
|
|
|
domPanel.style.height = px(initialHeight)
|
|
|
|
// add half the difference between .button height of 44px and 30px for pixel-perfect positioning
|
|
|
|
domPanel.style.top = px(targetRect.top + 7)
|
2023-10-13 16:27:31 +02:00
|
|
|
|
|
|
|
//Verify if the attachment bubble is going to overflow the screen
|
|
|
|
//if yes, invert the side of the margin and discount the bubble width
|
|
|
|
if (targetRect.left + targetWidth > window.innerWidth) {
|
|
|
|
domPanel.style.right = px(targetRect.right - targetWidth)
|
|
|
|
} else {
|
|
|
|
domPanel.style.left = px(targetRect.left)
|
|
|
|
}
|
2023-04-12 11:14:17 +02:00
|
|
|
|
|
|
|
const mutations = [opacity(0, 1, true), height(initialHeight, targetHeight)]
|
|
|
|
if (targetRect.width !== targetWidth) {
|
|
|
|
mutations.push(width(targetRect.width, targetWidth))
|
|
|
|
}
|
|
|
|
// space below the panel after it fully extends minus a bit.
|
|
|
|
const spaceBelow = window.innerHeight - getSafeAreaInsetBottom() - targetRect.top - targetHeight - initialHeight
|
|
|
|
if (spaceBelow < 0) {
|
|
|
|
mutations.push(transform(TransformEnum.TranslateY, 0, spaceBelow))
|
|
|
|
}
|
|
|
|
|
|
|
|
await animations.add(domPanel, mutations, {
|
|
|
|
easing: ease.out,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
2023-04-24 15:02:34 +02:00
|
|
|
modal.displayUnique(this, true)
|
2023-04-12 11:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
backgroundClick(e: MouseEvent): void {
|
|
|
|
modal.remove(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
async hideAnimation(): Promise<void> {
|
|
|
|
if (this.domPanel == null) return
|
|
|
|
const startHeight = this.domPanel.offsetHeight
|
|
|
|
const startWidth = this.domPanel.offsetWidth
|
|
|
|
await animations.add(this.domPanel, [height(startHeight, 30), width(startWidth, this.targetWidth), opacity(1, 0, false)], {
|
|
|
|
easing: ease.out,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
onClose(): void {
|
|
|
|
modal.remove(this)
|
2023-04-24 15:02:34 +02:00
|
|
|
this.closeDefer.resolve()
|
2023-04-12 11:14:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shortcuts(): Shortcut[] {
|
|
|
|
return this._shortcuts
|
|
|
|
}
|
|
|
|
|
|
|
|
popState(e: Event): boolean {
|
|
|
|
modal.remove(this)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2023-07-25 18:30:02 +02:00
|
|
|
|
|
|
|
/** try and execute stuff after the next rendering frame */
|
|
|
|
const deferToNextFrame = (fn: Thunk) => {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
window.requestAnimationFrame(fn)
|
|
|
|
})
|
|
|
|
}
|