2023-04-01 23:55:04 +02:00
import utils from "../services/utils.js" ;
import AttachmentActionsWidget from "./buttons/attachments_actions.js" ;
2023-03-30 23:48:26 +02:00
import BasicWidget from "./basic_widget.js" ;
2023-04-21 00:19:17 +02:00
import options from "../services/options.js" ;
2023-05-03 10:23:20 +02:00
import imageService from "../services/image.js" ;
2023-05-07 21:18:21 +02:00
import linkService from "../services/link.js" ;
2023-05-20 23:46:45 +02:00
import contentRenderer from "../services/content_renderer.js" ;
2023-05-29 00:19:54 +02:00
import toastService from "../services/toast.js" ;
2023-03-30 23:48:26 +02:00
const TPL = `
2023-05-21 18:14:17 +02:00
< div class = "attachment-detail-widget" >
2023-03-30 23:48:26 +02:00
< style >
2023-05-21 18:14:17 +02:00
. attachment - detail - widget {
height : 100 % ;
}
2023-03-30 23:48:26 +02:00
. attachment - detail - wrapper {
margin - bottom : 20 px ;
2023-05-21 18:14:17 +02:00
display : flex ;
flex - direction : column ;
2023-03-30 23:48:26 +02:00
}
. attachment - title - line {
display : flex ;
align - items : baseline ;
2023-05-07 21:18:21 +02:00
gap : 1 em ;
2023-03-30 23:48:26 +02:00
}
. attachment - details {
margin - left : 10 px ;
}
2023-05-21 18:14:17 +02:00
. attachment - content - wrapper {
flex - grow : 1 ;
}
. attachment - content - wrapper . rendered - content {
height : 100 % ;
}
. attachment - content - wrapper pre {
2023-03-30 23:48:26 +02:00
background : var ( -- accented - background - color ) ;
padding : 10 px ;
margin - top : 10 px ;
margin - bottom : 10 px ;
}
2023-05-21 18:14:17 +02:00
. attachment - detail - wrapper . list - view . attachment - content - wrapper {
max - height : 300 px ;
}
. attachment - detail - wrapper . full - detail {
height : 100 % ;
}
. attachment - detail - wrapper . full - detail . attachment - content - wrapper {
height : 100 % ;
}
. attachment - detail - wrapper . list - view . attachment - content - wrapper pre {
2023-04-11 17:45:51 +02:00
max - height : 400 px ;
}
2023-05-21 18:14:17 +02:00
. attachment - content - wrapper img {
2023-03-30 23:48:26 +02:00
margin : 10 px ;
2023-04-11 17:45:51 +02:00
}
2023-05-21 18:14:17 +02:00
. attachment - detail - wrapper . list - view . attachment - content - wrapper img {
2023-03-30 23:48:26 +02:00
max - height : 300 px ;
max - width : 90 % ;
object - fit : contain ;
}
2023-04-11 17:45:51 +02:00
2023-05-21 18:14:17 +02:00
. attachment - detail - wrapper . full - detail . attachment - content - wrapper img {
2023-04-11 17:45:51 +02:00
max - width : 90 % ;
object - fit : contain ;
}
2023-04-21 00:19:17 +02:00
2023-05-21 18:14:17 +02:00
. attachment - detail - wrapper . scheduled - for - deletion . attachment - content - wrapper img {
2023-04-21 00:19:17 +02:00
filter : contrast ( 10 % ) ;
}
2023-03-30 23:48:26 +02:00
< / s t y l e >
2023-04-01 13:58:53 +02:00
< div class = "attachment-detail-wrapper" >
< div class = "attachment-title-line" >
2023-05-07 21:18:21 +02:00
< div class = "attachment-actions-container" > < / d i v >
2023-04-11 17:45:51 +02:00
< h4 class = "attachment-title" > < / h 4 >
2023-04-01 13:58:53 +02:00
< div class = "attachment-details" > < / d i v >
2023-04-01 23:55:04 +02:00
< div style = "flex: 1 1;" > < / d i v >
2023-04-01 13:58:53 +02:00
< / d i v >
2023-04-21 00:19:17 +02:00
< div class = "attachment-deletion-warning alert alert-info" > < / d i v >
2023-05-21 18:14:17 +02:00
< div class = "attachment-content-wrapper" > < / d i v >
2023-04-01 13:58:53 +02:00
< / d i v >
2023-03-30 23:48:26 +02:00
< / d i v > ` ;
export default class AttachmentDetailWidget extends BasicWidget {
2023-05-07 10:43:51 +02:00
constructor ( attachment , isFullDetail ) {
2023-03-30 23:48:26 +02:00
super ( ) ;
2023-04-01 23:55:04 +02:00
this . contentSized ( ) ;
2023-03-30 23:48:26 +02:00
this . attachment = attachment ;
2023-05-07 10:43:51 +02:00
this . attachmentActionsWidget = new AttachmentActionsWidget ( attachment , isFullDetail ) ;
this . isFullDetail = isFullDetail ;
2023-04-01 13:58:53 +02:00
this . child ( this . attachmentActionsWidget ) ;
2023-03-30 23:48:26 +02:00
}
doRender ( ) {
this . $widget = $ ( TPL ) ;
2023-04-01 23:55:04 +02:00
this . refresh ( ) ;
super . doRender ( ) ;
}
2023-05-07 21:18:21 +02:00
async refresh ( ) {
2023-04-01 23:55:04 +02:00
this . $widget . find ( '.attachment-detail-wrapper' )
. empty ( )
. append (
$ ( TPL )
. find ( '.attachment-detail-wrapper' )
. html ( )
) ;
2023-03-30 23:48:26 +02:00
this . $wrapper = this . $widget . find ( '.attachment-detail-wrapper' ) ;
2023-04-11 17:45:51 +02:00
this . $wrapper . addClass ( this . isFullDetail ? "full-detail" : "list-view" ) ;
if ( ! this . isFullDetail ) {
this . $wrapper . find ( '.attachment-title' ) . append (
2023-05-29 00:19:54 +02:00
await linkService . createLink ( this . attachment . parentId , {
2023-05-07 21:18:21 +02:00
title : this . attachment . title ,
viewScope : {
viewMode : 'attachments' ,
attachmentId : this . attachment . attachmentId
}
} )
2023-04-11 17:45:51 +02:00
) ;
} else {
this . $wrapper . find ( '.attachment-title' )
. text ( this . attachment . title ) ;
}
2023-04-21 00:19:17 +02:00
const $deletionWarning = this . $wrapper . find ( '.attachment-deletion-warning' ) ;
const { utcDateScheduledForErasureSince } = this . attachment ;
if ( utcDateScheduledForErasureSince ) {
this . $wrapper . addClass ( "scheduled-for-deletion" ) ;
2023-04-20 00:11:09 +02:00
2023-04-21 00:19:17 +02:00
const scheduledSinceTimestamp = utils . parseDate ( utcDateScheduledForErasureSince ) ? . getTime ( ) ;
2023-05-29 13:02:25 +02:00
const intervalMs = options . getInt ( 'eraseUnusedAttachmentsAfterSeconds' ) * 1000 ;
2023-04-21 00:19:17 +02:00
const deletionTimestamp = scheduledSinceTimestamp + intervalMs ;
const willBeDeletedInMs = deletionTimestamp - Date . now ( ) ;
2023-04-20 00:11:09 +02:00
2023-04-21 00:19:17 +02:00
$deletionWarning . show ( ) ;
if ( willBeDeletedInMs >= 60000 ) {
$deletionWarning . text ( ` This attachment will be deleted in ${ utils . formatTimeInterval ( willBeDeletedInMs ) } ` ) ;
} else {
$deletionWarning . text ( ` This attachment will be deleted soon ` ) ;
}
2023-05-29 13:02:25 +02:00
$deletionWarning . append ( ", because the attachment is not linked in the note's content. To prevent deletion, add the attachment link back into the content." ) ;
2023-04-21 00:19:17 +02:00
} else {
this . $wrapper . removeClass ( "scheduled-for-deletion" ) ;
$deletionWarning . hide ( ) ;
2023-04-20 00:11:09 +02:00
}
2023-04-01 13:58:53 +02:00
this . $wrapper . find ( '.attachment-details' )
. text ( ` Role: ${ this . attachment . role } , Size: ${ utils . formatSize ( this . attachment . contentLength ) } ` ) ;
this . $wrapper . find ( '.attachment-actions-container' ) . append ( this . attachmentActionsWidget . render ( ) ) ;
2023-05-21 18:14:17 +02:00
this . $wrapper . find ( '.attachment-content-wrapper' ) . append ( ( await contentRenderer . getRenderedContent ( this . attachment ) ) . $renderedContent ) ;
2023-03-30 23:48:26 +02:00
}
2023-04-01 23:55:04 +02:00
2023-05-26 10:36:05 +02:00
async copyAttachmentLinkToClipboard ( ) {
if ( this . attachment . role === 'image' ) {
imageService . copyImageReferenceToClipboard ( this . $wrapper . find ( '.attachment-content-wrapper' ) ) ;
} else if ( this . attachment . role === 'file' ) {
2023-05-29 00:19:54 +02:00
const $link = await linkService . createLink ( this . attachment . parentId , {
2023-05-26 10:36:05 +02:00
referenceLink : true ,
viewScope : {
viewMode : 'attachments' ,
attachmentId : this . attachment . attachmentId
}
} ) ;
2023-05-29 00:19:54 +02:00
utils . copyHtmlToClipboard ( $link [ 0 ] . outerHTML ) ;
2023-05-26 10:36:05 +02:00
} else {
throw new Error ( ` Unrecognized attachment role ' ${ this . attachment . role } '. ` ) ;
}
2023-05-29 00:19:54 +02:00
toastService . showMessage ( "Attachment link copied to clipboard." ) ;
2023-05-03 10:23:20 +02:00
}
2023-04-03 23:47:24 +02:00
async entitiesReloadedEvent ( { loadResults } ) {
2023-06-05 16:26:05 +02:00
const attachmentRow = loadResults . getAttachmentRows ( ) . find ( att => att . attachmentId === this . attachment . attachmentId ) ;
2023-04-01 23:55:04 +02:00
2023-06-05 16:26:05 +02:00
if ( attachmentRow ) {
if ( attachmentRow . isDeleted ) {
2023-04-01 23:55:04 +02:00
this . toggleInt ( false ) ;
} else {
this . refresh ( ) ;
}
}
}
2023-03-30 23:48:26 +02:00
}