Skip to content

Commit 56d2c84

Browse files
authored
Merge pull request #835 from europeana/feat/MET-5782-Metis-Editor-Behaviour-Chrome
MET-5782 Codemirror Chrome / reactify config
2 parents 86541bc + bb4ed8d commit 56d2c84

File tree

8 files changed

+54
-35
lines changed

8 files changed

+54
-35
lines changed

projects/metis/cypress/e2e/preview.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ context('metis-ui', () => {
126126
const selIndicatorActiveBlack = '.editor-ctrl.black.active';
127127
const selIndicatorActiveWhite = '.editor-ctrl.active:not(.black)';
128128

129-
const selLinkThemeBlack = '[data-e2e=set-theme-white]';
129+
const selLinkThemeBlack = '[data-e2e=set-theme-black]';
130130
const selLinkThemeWhite = '[data-e2e=set-theme-white]';
131131

132132
cy.get(selEditorOps).should('have.length', 6);

projects/metis/src/app/_services/editor-pref.service.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Injectable } from '@angular/core';
22
import { EditorConfiguration } from 'codemirror';
3+
import { BehaviorSubject } from 'rxjs';
34

45
@Injectable({ providedIn: 'root' })
56
export class EditorPrefService {
67
altTheme: string;
7-
editorConfig: EditorConfiguration = {
8+
baseConf: EditorConfiguration = {
89
mode: 'application/xml',
910
lineNumbers: true,
1011
indentUnit: 2,
@@ -16,9 +17,11 @@ export class EditorPrefService {
1617
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
1718
theme: this.getEditorPref()
1819
};
20+
editorConfig: BehaviorSubject<EditorConfiguration>;
1921

2022
constructor() {
2123
this.altTheme = 'midnight';
24+
this.editorConfig = new BehaviorSubject<EditorConfiguration>(this.baseConf);
2225
}
2326

2427
/** getEditorPref
@@ -32,12 +35,16 @@ export class EditorPrefService {
3235
/** setEditorPref
3336
*
3437
* locally-stores the specified editor preference
35-
* updates the local editorConfig variable
38+
* updates the editorConfig subect
3639
* @param { string } theme - the theme to store
3740
**/
3841
setEditorPref(theme: string): void {
39-
this.editorConfig.theme = theme;
4042
localStorage.setItem('editor-pref', theme);
43+
this.editorConfig.next(
44+
Object.assign(this.baseConf, {
45+
theme: theme
46+
})
47+
);
4148
}
4249

4350
/** currentThemeIsDefault
@@ -58,12 +65,4 @@ export class EditorPrefService {
5865
this.setEditorPref(newTheme);
5966
return newTheme;
6067
}
61-
62-
/** getEditorConfig
63-
*
64-
* returns a configuration for the CodeMirror editor
65-
**/
66-
getEditorConfig(): EditorConfiguration {
67-
return this.editorConfig;
68-
}
6968
}

projects/metis/src/app/dataset/editor/editor.component.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="view-sample" [ngClass]="extraClasses">
22
<div class="view-sample-title">
3-
<div class="title-controls">
3+
<div *ngIf="editorConfig" class="title-controls">
44
<ng-template #defaultTitle>
55
<h3>
66
{{ title }}
@@ -37,7 +37,9 @@ <h3>
3737
><span>{{ step }}</span> <span *ngIf="stepCompare"> {{ stepCompare }}</span></span
3838
>
3939
</div>
40-
<ng-content></ng-content>
40+
<div [ngClass]="{ 'theme-midnight' : editorConfig.theme !== 'default' }">
41+
<ng-content></ng-content>
42+
</div>
4143
<div *ngIf="expandable" class="btns-set btns-set-right">
4244
<a class="load-more-btn" (click)="toggle()" *ngIf="!expanded">{{ 'expandInfo' | translate }}</a>
4345
</div>

projects/metis/src/app/dataset/editor/editor.component.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ describe('EditorComponent', () => {
3838
expect(component).toBeTruthy();
3939
});
4040

41+
it('should set the readOnly flag in the editorConfig', () => {
42+
component.ngOnInit();
43+
expect(component.editorConfig.readOnly).toBeTruthy();
44+
45+
component.isReadOnly = false;
46+
component.ngOnInit();
47+
expect(component.editorConfig.readOnly).toBeFalsy();
48+
});
49+
4150
it('should allow extra classes', () => {
4251
const testClass = 'myClass';
4352
const extraClasses = {} as ClassMap;

projects/metis/src/app/dataset/editor/editor.component.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { EditorConfiguration } from 'codemirror';
1616
import { CodemirrorComponent } from '@ctrl/ngx-codemirror';
1717

1818
// sonar-disable-next-statement (sonar doesn't read tsconfig paths entry)
19-
import { ClassMap } from 'shared';
19+
import { ClassMap, SubscriptionManager } from 'shared';
2020
import { XmlDownload } from '../../_models';
2121
import { EditorPrefService } from '../../_services';
2222

@@ -25,7 +25,7 @@ import { EditorPrefService } from '../../_services';
2525
templateUrl: './editor.component.html',
2626
styleUrls: ['./editor.component.scss']
2727
})
28-
export class EditorComponent implements AfterContentInit {
28+
export class EditorComponent extends SubscriptionManager implements AfterContentInit {
2929
private readonly editorPrefs = inject(EditorPrefService);
3030

3131
@ViewChildren(CodemirrorComponent) allEditors: QueryList<CodemirrorComponent>;
@@ -59,6 +59,7 @@ export class EditorComponent implements AfterContentInit {
5959
@Input() themeDisabled = false;
6060
@Input() title: string;
6161
@Input() isSearchEditor = false;
62+
@Input() isReadOnly = true;
6263
@Input() searchTerm: string;
6364

6465
_xmlDownloads?: Array<XmlDownload>;
@@ -80,21 +81,20 @@ export class EditorComponent implements AfterContentInit {
8081
@Output() onSearch = new EventEmitter<string>();
8182
@Output() onToggle = new EventEmitter<number>();
8283

83-
ngOnInit(): void {
84-
this.editorConfig = this.editorPrefs.getEditorConfig();
85-
}
86-
87-
/** getEditorConfig
88-
* returns this.editorConfig copied to incorporate readOnly overrides
89-
* @param { boolean } readOnly - override the default conf
84+
/**
85+
* subscribe to config / override readOnly
9086
**/
91-
getEditorConfig(readOnly = true): EditorConfiguration {
92-
const copyConfig = structuredClone(this.editorConfig);
93-
copyConfig.readOnly = readOnly;
94-
return copyConfig;
87+
ngOnInit(): void {
88+
this.subs.push(
89+
this.editorPrefs.editorConfig.subscribe((config: EditorConfiguration) => {
90+
config.readOnly = this.isReadOnly;
91+
this.editorConfig = config;
92+
})
93+
);
9594
}
9695

9796
/** ngAfterContentInit
97+
* animation utility for search
9898
* set initialised flag
9999
**/
100100
ngAfterContentInit(): void {

projects/metis/src/app/dataset/mapping/mapping.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@
3434
label: 'Custom'
3535
}
3636
]"
37+
[isReadOnly]="false"
3738
#editor
3839
>
3940
<div class="view-sample-editor">
4041
<div class="view-sample-editor-codemirror">
4142
<ngx-codemirror
4243
[ngModel]="xslt | beautifyXML"
4344
(ngModelChange)="xsltToSave = $event"
44-
[options]="editor.getEditorConfig(false)"
45+
[options]="editor.editorConfig"
4546
></ngx-codemirror>
4647
</div>
4748
</div>

projects/metis/src/app/dataset/preview/preview.component.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
<ngx-codemirror
128128
[ngModel]="searchedXMLSample.xmlRecord | beautifyXML"
129129
(ngModelChange)="searchedXMLSample = $event"
130-
[options]="editorSearch.getEditorConfig()"
130+
[options]="editorSearch.editorConfig"
131131
></ngx-codemirror>
132132
</div>
133133
<div
@@ -140,7 +140,7 @@
140140
<ngx-codemirror
141141
[ngModel]="searchedXMLSampleCompare.xmlRecord | beautifyXML"
142142
(ngModelChange)="searchedXMLSampleCompare.xmlRecord = $event"
143-
[options]="editorSearch.getEditorConfig()"
143+
[options]="editorSearch.editorConfig"
144144
></ngx-codemirror>
145145
</div>
146146
</div>
@@ -179,7 +179,7 @@
179179
<ngx-codemirror
180180
[ngModel]="sample.xmlRecord | beautifyXML"
181181
(ngModelChange)="sample.xmlRecord = $event"
182-
[options]="editorAll.getEditorConfig()"
182+
[options]="editorAll.editorConfig"
183183
></ngx-codemirror>
184184
</div>
185185
<div
@@ -192,7 +192,7 @@
192192
<ngx-codemirror
193193
[ngModel]="comparison.xmlRecord | beautifyXML"
194194
(ngModelChange)="comparison.xmlRecord = $event"
195-
[options]="editorAll.getEditorConfig()"
195+
[options]="editorAll.editorConfig"
196196
></ngx-codemirror>
197197
</div>
198198
</div>
@@ -216,7 +216,7 @@
216216
<ngx-codemirror
217217
[ngModel]="nosample | beautifyXML"
218218
(ngModelChange)="(nosample)"
219-
[options]="editorNone.getEditorConfig()"
219+
[options]="editorNone.editorConfig"
220220
></ngx-codemirror>
221221
</div>
222222
</app-editor>
@@ -269,7 +269,7 @@
269269
<ngx-codemirror
270270
[ngModel]="sample.xmlRecord | beautifyXML"
271271
(ngModelChange)="sample.xmlRecord = $event"
272-
[options]="editorTransformed.getEditorConfig()"
272+
[options]="editorTransformed.editorConfig"
273273
></ngx-codemirror>
274274
</div>
275275

@@ -283,7 +283,7 @@
283283
*ngIf="allTransformedSamples[i] && allTransformedSamples[i].xmlRecord"
284284
[ngModel]="allTransformedSamples[i].xmlRecord | beautifyXML"
285285
(ngModelChange)="allTransformedSamples[i].xmlRecord = $event"
286-
[options]="editorTransformed.getEditorConfig()"
286+
[options]="editorTransformed.editorConfig"
287287
></ngx-codemirror>
288288
</div>
289289

shared-styles/assets/sass/pandora/components/_viewxmlsamples.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ codemirror {
208208
}
209209
}
210210

211+
.view-sample.no-sample .theme-midnight .view-sample-editor,
212+
.view-sample .theme-midnight .view-sample-editor-codemirror {
213+
// colour taken from CodeMirror
214+
$midnight: #0F192A;
215+
background-color: $midnight;
216+
scrollbar-color: $eu-jade $midnight;
217+
}
218+
211219
.search-editor {
212220
$input-padding-left: 0.65rem;
213221
transition: margin-bottom 0.5s;

0 commit comments

Comments
 (0)