Skip to content

Commit eff0d6c

Browse files
petrjasekCopilot
andauthored
use picture profile in media metadata editor modal (#5109)
* use picture profile in media metadata editor modal as a fallback for backward compatibility * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 48956a0 commit eff0d6c

2 files changed

Lines changed: 112 additions & 10 deletions

File tree

scripts/apps/authoring/media/media-fields-controller.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {sortBy, get} from 'lodash';
1+
import {sortBy} from 'lodash';
22
import {getLabelForFieldId} from 'apps/workspace/helpers/getLabelForFieldId';
33
import {appConfig} from 'appConfig';
44

5-
MediaFieldsController.$inject = ['$q', 'metadata', 'vocabularies'];
6-
export default function MediaFieldsController($q, metadata, vocabularies) {
5+
MediaFieldsController.$inject = ['$q', 'metadata', 'vocabularies', 'content'];
6+
export default function MediaFieldsController($q, metadata, vocabularies, content) {
77
function getCV(field) {
88
const cv = metadata.cvs.find((_cv) => _cv._id === field || _cv.schema_field === field);
99

@@ -33,9 +33,12 @@ export default function MediaFieldsController($q, metadata, vocabularies) {
3333
metadataInit: metadata.initialize(),
3434
getLabelForFieldId: vocabularies.getAllActiveVocabularies()
3535
.then((vocabulariesCollection) => (fieldId) => getLabelForFieldId(fieldId, vocabulariesCollection)),
36-
}).then(({getLabelForFieldId}) => {
37-
const editor = get(appConfig.editor, 'picture', {});
38-
const schema = get(appConfig.schema, 'picture', {});
36+
pictureType: content.getType('picture').catch(() => null),
37+
}).then(({getLabelForFieldId, pictureType}) => {
38+
const editorSource = appConfig.editor?.picture ?? pictureType?.editor ?? {};
39+
const schemaSource = appConfig.schema?.picture ?? pictureType?.schema ?? {};
40+
const editor = {...editorSource};
41+
const schema = {...schemaSource};
3942
const validator = appConfig.validator_media_metadata;
4043

4144
// get last order
@@ -58,7 +61,7 @@ export default function MediaFieldsController($q, metadata, vocabularies) {
5861
this.fields = sortBy(
5962
Object.keys(editor)
6063
.filter((key) => editor[key] != null)
61-
.filter((key) => get(editor[key], 'displayOnMediaEditor', true))
64+
.filter((key) => editor[key]?.displayOnMediaEditor ?? true)
6265
.map((field) => {
6366
const cv = getCV(field);
6467

scripts/apps/authoring/media/tests/MediaMetadataEditor.spec.ts

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ describe('media metadata editor', () => {
66
beforeEach(window.module('superdesk.apps.vocabularies'));
77
beforeEach(window.module('superdesk.apps.authoring.media'));
88
beforeEach(window.module('superdesk.apps.authoring.metadata'));
9+
beforeEach(window.module('superdesk.apps.workspace.content'));
910

10-
beforeEach(inject(($q, metadata, vocabularies, api) => {
11+
beforeEach(inject(($q, metadata, vocabularies) => {
1112
spyOn(metadata, 'initialize').and.returnValue($q.when({}));
1213
spyOn(vocabularies, 'getAllActiveVocabularies').and.returnValue($q.when([]));
1314
}));
1415

15-
it('displays all fields', inject(($controller, $rootScope) => {
16+
it('displays all fields', inject(($controller, $rootScope, $q, content) => {
17+
spyOn(content, 'getType').and.returnValue($q.when(null));
18+
1619
const testConfig: Partial<ISuperdeskGlobalConfig> = {
1720
editor: {
1821
picture: {
@@ -56,7 +59,103 @@ describe('media metadata editor', () => {
5659
expect(ctrl.fields.map((f) => f.field)).toEqual(['slugline', 'headline', 'category', 'genre']);
5760
}));
5861

59-
it('displays fields with dislayOnMediaEditor set', inject(($rootScope, $controller) => {
62+
it('uses appConfig when picture profile exists', inject(($controller, $rootScope, $q, content) => {
63+
const pictureProfile = {
64+
editor: {
65+
byline: {
66+
order: 1,
67+
required: true,
68+
displayOnMediaEditor: true,
69+
},
70+
alt_text: {
71+
order: 2,
72+
required: false,
73+
displayOnMediaEditor: true,
74+
},
75+
},
76+
schema: {
77+
byline: {type: 'string'},
78+
alt_text: {type: 'string'},
79+
},
80+
};
81+
82+
spyOn(content, 'getType').and.returnValue($q.when(pictureProfile));
83+
84+
const testConfig: Partial<ISuperdeskGlobalConfig> = {
85+
editor: {
86+
picture: {
87+
slugline: {
88+
order: 1,
89+
required: true,
90+
},
91+
headline: {
92+
order: 2,
93+
required: true,
94+
},
95+
},
96+
},
97+
schema: {
98+
picture: {
99+
slugline: {type: 'string'},
100+
headline: {type: 'string'},
101+
},
102+
},
103+
validator_media_metadata: {},
104+
};
105+
106+
Object.assign(appConfig, testConfig);
107+
108+
const ctrl = $controller('MediaFieldsController');
109+
110+
$rootScope.$apply();
111+
112+
expect(content.getType).toHaveBeenCalledWith('picture');
113+
expect(ctrl.fields.length).toBe(2);
114+
expect(ctrl.fields.map((f) => f.field)).toEqual(['slugline', 'headline']);
115+
}));
116+
117+
it('falls back to picture profile when appConfig is missing', inject(($controller, $rootScope, $q, content) => {
118+
const pictureProfile = {
119+
editor: {
120+
byline: {
121+
order: 1,
122+
required: true,
123+
displayOnMediaEditor: true,
124+
},
125+
alt_text: {
126+
order: 2,
127+
required: false,
128+
displayOnMediaEditor: true,
129+
},
130+
},
131+
schema: {
132+
byline: {type: 'string'},
133+
alt_text: {type: 'string'},
134+
},
135+
};
136+
137+
spyOn(content, 'getType').and.returnValue($q.when(pictureProfile));
138+
139+
const testConfig: Partial<ISuperdeskGlobalConfig> = {
140+
editor: {},
141+
schema: {},
142+
validator_media_metadata: {},
143+
};
144+
145+
Object.assign(appConfig, testConfig);
146+
147+
const ctrl = $controller('MediaFieldsController');
148+
149+
$rootScope.$apply();
150+
151+
expect(content.getType).toHaveBeenCalledWith('picture');
152+
expect(ctrl.fields.length).toBe(2);
153+
expect(ctrl.fields.map((f) => f.field)).toEqual(['byline', 'alt_text']);
154+
}));
155+
156+
it('displays fields with displayOnMediaEditor set', inject(($rootScope, $controller, $q, content) => {
157+
spyOn(content, 'getType').and.returnValue($q.when(null));
158+
60159
const testConfig: Partial<ISuperdeskGlobalConfig> = {
61160
schema: {
62161
picture: {

0 commit comments

Comments
 (0)