Skip to content

Commit 0505a73

Browse files
committed
feature(wysiwyg): moving function arrayMoveByIndex to helpers ang linting
1 parent 1d12d01 commit 0505a73

File tree

8 files changed

+70
-55
lines changed

8 files changed

+70
-55
lines changed

apps/codelab/src/app/admin/content/presentation-editor/reducer.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { moveItemInArray } from '@angular/cdk/drag-drop';
22
import { ContentPresentation } from './types';
3+
import { arrayMoveByIndex } from '../../../shared/helpers/helpers';
34

45
export function reducer(
56
presentations: ContentPresentation[],
@@ -46,30 +47,9 @@ export function reducer(
4647

4748
const slides = getPresentation().slides;
4849

49-
const normalizeIndexes = (indexes, array) =>
50-
indexes.filter(index => index >= 0 && index < array.length);
50+
const reorderedSlides = arrayMoveByIndex(slides, selections, toIndex);
5151

52-
const normalizeToIndex = (toIndex, array, moveIndexes) =>
53-
Math.min(Math.max(0, toIndex), array.length - moveIndexes.length);
54-
55-
const arrayMoveByIndex = (array, index, toIndex) => {
56-
const indexes = Array.isArray(index) ? index : [index];
57-
const normalizedIndexes = normalizeIndexes(indexes, array);
58-
const normalizedToIndex = normalizeToIndex(toIndex, array, indexes);
59-
60-
const moveValues = normalizedIndexes.map(moveIndex => array[moveIndex]);
61-
62-
const dontMoveValues = array.filter(
63-
(item, index) => normalizedIndexes.indexOf(index) === -1
64-
);
65-
66-
dontMoveValues.splice(normalizedToIndex, 0, ...moveValues);
67-
return dontMoveValues;
68-
};
69-
70-
getPresentation().slides = [
71-
...arrayMoveByIndex(slides, selections, toIndex)
72-
];
52+
getPresentation().slides = [...reorderedSlides];
7353

7454
return presentations;
7555

apps/codelab/src/app/admin/content/presentation-editor/side-panel/side-panel.component.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ import { ContentSlide } from '../types';
1818
import { MultiselectModel } from '../../../../multiselect/multiselect-model';
1919

2020
function normalizeSelectionIndexes(indexes: number[]): number[] {
21-
return indexes.filter((index) => index >= 0);
21+
return indexes.filter(index => index >= 0);
2222
}
2323

24-
function selectionModelToIndexes<T>(items: T[], model: MultiselectModel<T>): number[] {
25-
return normalizeSelectionIndexes(model.selected.map((item) => items.indexOf(item)));
24+
function selectionModelToIndexes<T>(
25+
items: T[],
26+
model: MultiselectModel<T>
27+
): number[] {
28+
return normalizeSelectionIndexes(
29+
model.selected.map(item => items.indexOf(item))
30+
);
2631
}
2732

2833
function slideIdsMapper(slides: ContentSlide[]): string[] {
@@ -128,7 +133,10 @@ export class SidePanelComponent implements OnInit, OnChanges {
128133
}
129134

130135
droppedIntoList(event: CdkDragDrop<any, any>) {
131-
const selectedSlideIndexes = selectionModelToIndexes(this.slideIds, this.selectionModel);
136+
const selectedSlideIndexes = selectionModelToIndexes(
137+
this.slideIds,
138+
this.selectionModel
139+
);
132140

133141
this.contentService.reorderSlides(
134142
this.presentationId,
@@ -150,5 +158,4 @@ export class SidePanelComponent implements OnInit, OnChanges {
150158
this.navigationService.goToSlide(this.presentationId, index);
151159
}
152160
}
153-
154161
}

apps/codelab/src/app/admin/content/presentation-editor/side-panel/side-panel.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import { MultiselectModule } from '../../../../multiselect/multiselect.module';
1010
@NgModule({
1111
declarations: [SidePanelComponent],
1212
exports: [SidePanelComponent],
13-
imports: [CommonModule, RouterModule, DragDropModule, PreviewModule, MultiselectModule]
13+
imports: [
14+
CommonModule,
15+
RouterModule,
16+
DragDropModule,
17+
PreviewModule,
18+
MultiselectModule
19+
]
1420
})
1521
export class SidePanelModule {}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { Directive, HostListener, Inject, Input } from '@angular/core';
2-
import { MULTISELECT_LIST, MultiselectListDirective } from './multiselect-list.directive';
2+
import {
3+
MULTISELECT_LIST,
4+
MultiselectListDirective
5+
} from './multiselect-list.directive';
36

47
@Directive({
58
selector: '[multiselectItem]'
69
})
710
export class MultiselectItemDirective<T> {
8-
911
@Input() msItem: T;
1012

1113
constructor(
1214
@Inject(MULTISELECT_LIST) public parentList: MultiselectListDirective<T>
13-
) {
14-
15-
}
15+
) {}
1616

1717
@HostListener('click', ['$event'])
1818
private handleClickEvent(event: MouseEvent) {
@@ -24,5 +24,4 @@ export class MultiselectItemDirective<T> {
2424
this.parentList.msModel.selectSingle(this.msItem);
2525
}
2626
}
27-
2827
}

apps/codelab/src/app/multiselect/multiselect-list.directive.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
import { Directive, ElementRef, HostListener, InjectionToken, Input } from '@angular/core';
1+
import {
2+
Directive,
3+
ElementRef,
4+
HostListener,
5+
InjectionToken,
6+
Input
7+
} from '@angular/core';
28

39
import { MultiselectModel } from './multiselect-model';
410

5-
export const MULTISELECT_LIST = new InjectionToken<MultiselectListDirective<any>>('MultiselectList');
11+
export const MULTISELECT_LIST = new InjectionToken<
12+
MultiselectListDirective<any>
13+
>('MultiselectList');
614

715
@Directive({
816
selector: '[multiselectList]',
917
providers: [
1018
{
1119
provide: MULTISELECT_LIST,
1220
useExisting: MultiselectListDirective
13-
},
21+
}
1422
]
1523
})
1624
export class MultiselectListDirective<T> {
1725
elementHasFocus = false;
1826

1927
@Input() msModel: MultiselectModel<T>;
2028

21-
constructor(
22-
private readonly el: ElementRef
23-
) {
24-
25-
}
29+
constructor(private readonly el: ElementRef) {}
2630

2731
@HostListener('document:click', ['$event'])
2832
private handleClickEvent(event: MouseEvent) {
@@ -41,5 +45,4 @@ export class MultiselectListDirective<T> {
4145
event.preventDefault();
4246
}
4347
}
44-
4548
}

apps/codelab/src/app/multiselect/multiselect-model.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Subject } from 'rxjs';
22

33
export class MultiselectModel<T> {
4-
54
private selection = new Set<T>();
65

76
private stack: T[] = [];
@@ -86,5 +85,4 @@ export class MultiselectModel<T> {
8685
emitChangeEvent(): void {
8786
this.changed.next(this.selected);
8887
}
89-
9088
}

apps/codelab/src/app/multiselect/multiselect.module.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ import { MultiselectItemDirective } from './multiselect-item.directive';
44
import { MultiselectListDirective } from './multiselect-list.directive';
55

66
@NgModule({
7-
declarations: [
8-
MultiselectListDirective,
9-
MultiselectItemDirective
10-
],
11-
exports: [
12-
MultiselectListDirective,
13-
MultiselectItemDirective
14-
],
7+
declarations: [MultiselectListDirective, MultiselectItemDirective],
8+
exports: [MultiselectListDirective, MultiselectItemDirective]
159
})
16-
export class MultiselectModule { }
10+
export class MultiselectModule {}

apps/codelab/src/app/shared/helpers/helpers.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,31 @@ export function solve(exerciseConfig) {
366366
}))
367367
};
368368
}
369+
370+
const normalizeIndexes = (indexes: number[], array: any[]) =>
371+
indexes.filter(index => index >= 0 && index < array.length);
372+
373+
const normalizeToIndex = (
374+
toIndex: number,
375+
array: any[],
376+
moveIndexes: number[]
377+
) => Math.min(Math.max(0, toIndex), array.length - moveIndexes.length);
378+
379+
export function arrayMoveByIndex<T>(
380+
array: T[],
381+
index: number[] | number,
382+
toIndex: number
383+
): T[] {
384+
const indexes = Array.isArray(index) ? index : [index];
385+
const normalizedIndexes = normalizeIndexes(indexes, array);
386+
const normalizedToIndex = normalizeToIndex(toIndex, array, indexes);
387+
388+
const moveValues = normalizedIndexes.map(moveIndex => array[moveIndex]);
389+
390+
const remainingValues = array.filter(
391+
(item, index) => !normalizedIndexes.includes(index)
392+
);
393+
394+
remainingValues.splice(normalizedToIndex, 0, ...moveValues);
395+
return remainingValues;
396+
}

0 commit comments

Comments
 (0)