Skip to content

Commit 126f3e2

Browse files
authored
Merge pull request #4682 from dpalou/MOBILE-4895
Mobile 4895
2 parents 840951c + 583f2d6 commit 126f3e2

File tree

4 files changed

+30
-55
lines changed

4 files changed

+30
-55
lines changed

UPGRADE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ This file describes API changes in the Moodle App that affect site plugins, info
22

33
For more information about upgrading, read the official documentation: https://moodledev.io/general/app/upgrading/
44

5+
5.2.0
6+
=====
7+
8+
- The core-files component now treats the `files` input array as immutable to simplify the change detection strategy and improve performance. To trigger an update, you must provide a new array reference (e.g. using `concat()` or the spread operator) rather than mutating the existing one with `push()` or `splice()`.
9+
510
5.1.0
611
=====
712

src/core/components/files/core-files.html

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
@if (showInline && contentText) {
2-
<core-format-text [text]="contentText" [filter]="false" />
1+
@if (showInline() && contentText()) {
2+
<core-format-text [text]="contentText()" [filter]="false" />
33
}
44

5-
<ng-container *ngFor="let file of files; let i = index">
5+
<ng-container *ngFor="let file of files(); let i = index">
66
@if (!file.embedType) {
77
@if (!file.name) {
88
<!-- Files already attached to the filearea. -->
9-
<core-file [file]="file" [component]="component" [componentId]="componentId" [alwaysDownload]="alwaysDownload"
10-
[canDownload]="canDownload" [showSize]="showSize" [showTime]="showTime" />
9+
<core-file [file]="file" [component]="component()" [componentId]="componentId()" [alwaysDownload]="alwaysDownload()"
10+
[canDownload]="canDownload()" [showSize]="showSize()" [showTime]="showTime()" />
1111
} @else {
1212
<!-- Files stored in offline to be sent later. -->
1313
<core-local-file [file]="file" />
1414
}
1515
}
1616

17-
@if (extraHtml && extraHtml[i]) {
17+
@let extraHtmlValue = extraHtml();
18+
@if (extraHtmlValue && extraHtmlValue[i]) {
1819
<ion-item class="ion-text-wrap">
1920
<ion-label>
20-
<core-format-text [component]="component" [componentId]="componentId" [text]="extraHtml[i]" />
21+
<core-format-text [component]="component()" [componentId]="componentId()" [text]="extraHtmlValue[i]" />
2122
</ion-label>
2223
</ion-item>
2324
}

src/core/components/files/files.ts

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import { toBoolean } from '@/core/transforms/boolean';
16-
import { Component, Input, OnInit, DoCheck, inject, IterableDiffer, IterableDiffers } from '@angular/core';
16+
import { Component, input, computed } from '@angular/core';
1717
import { CoreFileEntry } from '@services/file-helper';
1818

1919
import { CoreMimetype } from '@singletons/mimetype';
@@ -38,59 +38,28 @@ import { CoreFormatTextDirective } from '@directives/format-text';
3838
CoreFormatTextDirective,
3939
],
4040
})
41-
export class CoreFilesComponent implements OnInit, DoCheck {
41+
export class CoreFilesComponent {
4242

43-
@Input() files: CoreFileEntry[] = []; // List of files.
44-
@Input() component?: string; // Component the downloaded files will be linked to.
45-
@Input() componentId?: string | number; // Component ID.
46-
@Input({ transform: toBoolean }) alwaysDownload = false; // True to always display the refresh button when file is downloaded.
47-
@Input({ transform: toBoolean }) canDownload = true; // Whether file can be downloaded.
48-
@Input({ transform: toBoolean }) showSize = true; // Whether show filesize.
49-
@Input({ transform: toBoolean }) showTime = true; // Whether show file time modified.
50-
@Input({ transform: toBoolean }) showInline = false; // If true, it will reorder and try to show inline files first.
51-
@Input() extraHtml?: string[]; // Extra HTML for each attachment. Each HTML should be at the same position as the attachment.
43+
readonly files = input<CoreFileEntry[]>([]); // List of files.
44+
readonly component = input<string>(); // Component the downloaded files will be linked to.
45+
readonly componentId = input<string | number>(); // Component ID.
46+
readonly alwaysDownload = input(false, { transform: toBoolean }); // True to always display refresh button when is downloaded.
47+
readonly canDownload = input(true, { transform: toBoolean }); // Whether file can be downloaded.
48+
readonly showSize = input(true, { transform: toBoolean }); // Whether show filesize.
49+
readonly showTime = input(true, { transform: toBoolean }); // Whether show file time modified.
50+
readonly showInline = input(false, { transform: toBoolean }); // If true, it will reorder and try to show inline files first.
51+
readonly extraHtml = input<string[]>(); // Extra HTML for each attachment. Each HTML should be at the same position.
5252

53-
contentText?: string;
54-
55-
protected differ: IterableDiffer<CoreFileEntry>; // To detect changes in the data input.
56-
57-
constructor() {
58-
const differs = inject(IterableDiffers);
59-
60-
this.differ = differs.find([]).create();
61-
}
62-
63-
/**
64-
* @inheritdoc
65-
*/
66-
ngOnInit(): void {
67-
if (this.showInline && this.files) {
68-
this.renderInlineFiles();
69-
}
70-
}
71-
72-
/**
73-
* Detect and act upon changes that Angular can’t or won’t detect on its own (objects and arrays).
74-
*/
75-
ngDoCheck(): void {
76-
if (this.showInline && this.files) {
77-
// Check if there's any change in the files array.
78-
const changes = this.differ.diff(this.files);
79-
if (changes) {
80-
this.renderInlineFiles();
81-
}
53+
readonly contentText = computed(() => {
54+
if (!this.showInline()) {
55+
return '';
8256
}
83-
}
8457

85-
/**
86-
* Calculate contentText based on fils that can be rendered inline.
87-
*/
88-
protected renderInlineFiles(): void {
89-
this.contentText = this.files.reduce((previous, file) => {
58+
return this.files().reduce((previous, file) => {
9059
const text = CoreMimetype.getEmbeddedHtml(file);
9160

9261
return text ? `${previous}<br>${text}` : previous;
9362
}, '');
94-
}
63+
});
9564

9665
}

src/core/components/mod-icon/mod-icon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const enum IconVersion {
5454
host: {
5555
'[attr.role]': 'showAlt() ? "img" : "presentation"',
5656
'[attr.aria-label]': 'showAlt() ? modNameTranslated() : ""',
57-
'[class]': 'iconVersion() + " " + purposeClass() ?? ""',
57+
'[class]': 'iconVersion() + " " + purposeClass()',
5858
'[class.branded]': 'addBrandedClass()',
5959
'[class.colorize]': 'addColorizeClass()',
6060
},

0 commit comments

Comments
 (0)