-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #396 from fujaba/feat/edit-assignment-diff
View Diff after editing Assignment
- Loading branch information
Showing
12 changed files
with
116 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
frontend/src/app/assignment/modules/edit-assignment/diff-modal/diff-modal.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<ngbx-modal [back]="['..']" size="lg" #modal> | ||
<ng-container modal-title> | ||
Differences | ||
</ng-container> | ||
<ng-container modal-body> | ||
<h5>Metadata</h5> | ||
<pre [innerHTML]="diffMetadata | safeHtml"></pre> | ||
<h5>Tasks</h5> | ||
<pre [innerHTML]="diffTasks | safeHtml"></pre> | ||
</ng-container> | ||
<ng-container modal-footer> | ||
<button type="button" class="btn btn-secondary" (click)="modal.close()"> | ||
Close | ||
</button> | ||
</ng-container> | ||
</ngbx-modal> |
Empty file.
51 changes: 51 additions & 0 deletions
51
frontend/src/app/assignment/modules/edit-assignment/diff-modal/diff-modal.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Component, OnInit} from '@angular/core'; | ||
import {AssignmentService} from "../../../services/assignment.service"; | ||
import * as jsdiff from "diff"; | ||
import {ActivatedRoute} from "@angular/router"; | ||
import {AssignmentContext} from "../../../services/assignment.context"; | ||
import {switchMap} from "rxjs/operators"; | ||
import {TaskMarkdownService} from "../task-markdown.service"; | ||
|
||
@Component({ | ||
selector: 'app-diff-modal', | ||
templateUrl: './diff-modal.component.html', | ||
styleUrl: './diff-modal.component.scss' | ||
}) | ||
export class DiffModalComponent implements OnInit { | ||
diffMetadata = ''; | ||
diffTasks = ''; | ||
|
||
constructor( | ||
private route: ActivatedRoute, | ||
private context: AssignmentContext, | ||
private assignmentService: AssignmentService, | ||
private taskMarkdownService: TaskMarkdownService, | ||
) { | ||
} | ||
|
||
ngOnInit() { | ||
this.route.params.pipe( | ||
switchMap(({aid}) => this.assignmentService.get(aid)), | ||
).subscribe(oldAssignment => { | ||
const {tasks: oldTasks, ...old} = oldAssignment; | ||
const {tasks: newTasks, ...current} = this.context.assignment; | ||
this.diffMetadata = renderDiff(jsdiff.diffJson(old, current)); | ||
const oldTasksMd = this.taskMarkdownService.renderTasks(oldTasks); | ||
const newTasksMd = this.taskMarkdownService.renderTasks(newTasks); | ||
this.diffTasks = renderDiff(jsdiff.diffWords(oldTasksMd, newTasksMd)); | ||
}); | ||
} | ||
} | ||
|
||
function escapeHtml(text: string) { | ||
return text.replace(/[&<>"'`=\/]/g, function (s) { | ||
return `&#${s.charCodeAt(0)};`; | ||
}); | ||
} | ||
|
||
function renderDiff(diff: jsdiff.Change[]) { | ||
return diff.map(part => { | ||
const color = part.added ? 'text-success' : part.removed ? 'text-danger' : ''; | ||
return `<span class="${color}">${escapeHtml(part.value)}</span>`; | ||
}).join(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 1 addition & 6 deletions
7
frontend/src/app/assignment/modules/edit-assignment/preview/preview.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
frontend/src/app/assignment/modules/edit-assignment/preview/preview.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
frontend/src/app/assignment/services/assignment.context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
import {Injectable} from '@angular/core'; | ||
import Assignment, {CreateAssignmentDto} from '../model/assignment'; | ||
import Task from "../model/task"; | ||
|
||
@Injectable() | ||
export class AssignmentContext { | ||
assignment: Assignment | CreateAssignmentDto; | ||
saveDraft: () => void; | ||
|
||
getAssignment(): Assignment | CreateAssignmentDto { | ||
return { | ||
...this.assignment, | ||
tasks: this.getTasks(this.assignment.tasks), | ||
}; | ||
} | ||
|
||
private getTasks(tasks: Task[]): Task[] { | ||
return tasks.filter(t => !t.deleted).map(({deleted, collapsed, children, ...rest}) => ({ | ||
...rest, | ||
children: this.getTasks(children), | ||
})); | ||
} | ||
} |