@@ -286,9 +286,9 @@ abstract class AddonModGlossaryFormHandler {
286286 * Upload attachments online.
287287 *
288288 * @param glossary Glossary.
289- * @returns Uploaded attachments item id.
289+ * @returns Uploaded attachments item id, undefined if nothing to upload or change .
290290 */
291- protected async uploadAttachments ( glossary : AddonModGlossaryGlossary ) : Promise < number > {
291+ protected async uploadAttachments ( glossary : AddonModGlossaryGlossary ) : Promise < number | undefined > {
292292 const data = this . page . data ;
293293 const itemId = await CoreFileUploader . uploadOrReuploadFiles (
294294 data . attachments ,
@@ -643,10 +643,7 @@ class AddonModGlossaryOnlineFormHandler extends AddonModGlossaryFormHandler {
643643 data . fullmatch = this . entry . fullmatch ;
644644 }
645645
646- // Treat offline attachments if any.
647- if ( this . entry . attachments ) {
648- data . attachments = this . entry . attachments ;
649- }
646+ data . attachments = ( this . entry . attachments ?? [ ] ) . slice ( ) ;
650647
651648 this . page . originalData = {
652649 concept : data . concept ,
@@ -677,11 +674,7 @@ class AddonModGlossaryOnlineFormHandler extends AddonModGlossaryFormHandler {
677674 const definition = CoreText . formatHtmlLines ( data . definition ) ;
678675
679676 // Upload attachments, if any.
680- let attachmentsId : number | undefined = undefined ;
681-
682- if ( data . attachments . length ) {
683- attachmentsId = await this . uploadAttachments ( glossary ) ;
684- }
677+ const attachmentsId = await this . uploadAttachments ( ) ;
685678
686679 // Save entry data.
687680 await AddonModGlossary . updateEntry ( glossary . id , this . entry . id , data . concept , definition , options , attachmentsId ) ;
@@ -694,6 +687,31 @@ class AddonModGlossaryOnlineFormHandler extends AddonModGlossaryFormHandler {
694687 return true ;
695688 }
696689
690+ /**
691+ * Upload attachments online.
692+ *
693+ * @returns Uploaded attachments item id, undefined if nothing to upload or change.
694+ */
695+ protected async uploadAttachments ( ) : Promise < number | undefined > {
696+ const data = this . page . data ;
697+
698+ if ( ! CoreFileUploader . areFileListDifferent ( data . attachments , this . entry . attachments ?? [ ] ) ) {
699+ return ;
700+ }
701+
702+ const { attachmentsid : attachmentsId } = await AddonModGlossary . prepareEntryForEdition ( this . entry . id ) ;
703+
704+ const removedFiles = CoreFileUploader . getFilesToDelete ( this . entry . attachments ?? [ ] , data . attachments ) ;
705+
706+ if ( removedFiles . length ) {
707+ await CoreFileUploader . deleteDraftFiles ( attachmentsId , removedFiles ) ;
708+ }
709+
710+ await CoreFileUploader . uploadFiles ( attachmentsId , data . attachments ) ;
711+
712+ return attachmentsId ;
713+ }
714+
697715}
698716
699717/**
0 commit comments