-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve undo/redo for Title component #1197
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3951d6d
Add title-editor.component, title-editor.module
chia-yh 9429a7e
Modify existing titles to use title-editor
chia-yh e09cca0
Add title-editor.component.spec.ts for testing
chia-yh 514f8eb
Fix lint error
chia-yh b3c173a
Remove unused classes
chia-yh 615632e
Remove Apollo
chia-yh 3af1542
Shift `title-editor` component under shared/issue
chia-yh ad3c2e3
Center `title-button` text
chia-yh 88a94ea
Fix lint errors
chia-yh cc4dc79
Extract isUndo/isRedo methods into undoredo.model
chia-yh 8efeafb
Move title-editor.component.spec.ts
chia-yh b6f4b04
Add additional tests for title-editor.component
chia-yh df664f9
Modify tests to account for macos
chia-yh 269c52b
Fix macos redo test
chia-yh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
margin: 0 auto; | ||
} | ||
|
||
mat-form-field { | ||
app-title-editor { | ||
width: 100%; | ||
} | ||
|
||
|
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
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,3 @@ | ||
mat-form-field { | ||
width: 100%; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/app/shared/issue/title-editor/title-editor.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,25 @@ | ||
<form [formGroup]="titleForm"> | ||
<mat-form-field> | ||
<input | ||
(keydown)="onKeyPress($event)" | ||
(beforeinput)="handleBeforeInputChange($event)" | ||
(input)="handleInputChange($event)" | ||
#titleInput | ||
id="{{ this.id }}" | ||
formControlName="{{ this.id }}" | ||
matInput | ||
required | ||
placeholder="{{ this.placeholderText }}" | ||
maxlength="{{ this.maxLength }}" | ||
/> | ||
<mat-error *ngIf="titleField.errors && titleField.errors['required'] && (titleField.touched || titleField.dirty)"> | ||
Title required. | ||
</mat-error> | ||
<mat-error *ngIf="titleField.errors && titleField.errors['maxlength']"> | ||
Title cannot exceed {{ maxLength }} characters. | ||
</mat-error> | ||
<mat-hint *ngIf="titleField.value?.length >= maxLength - 50"> | ||
{{ maxLength - titleField.value?.length }} characters remaining. | ||
</mat-hint> | ||
</mat-form-field> | ||
</form> |
119 changes: 119 additions & 0 deletions
119
src/app/shared/issue/title-editor/title-editor.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,119 @@ | ||
import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core'; | ||
import { AbstractControl, FormGroup, Validators } from '@angular/forms'; | ||
import { UndoRedo } from '../../../core/models/undoredo.model'; | ||
|
||
const ISSUE_BODY_SIZE_LIMIT = 256; | ||
|
||
type textEntry = { | ||
text: string; | ||
selectStart: number; | ||
selectEnd: number; | ||
}; | ||
|
||
@Component({ | ||
selector: 'app-title-editor', | ||
templateUrl: './title-editor.component.html', | ||
styleUrls: ['./title-editor.component.css'] | ||
}) | ||
export class TitleEditorComponent implements OnInit { | ||
|
||
constructor() {} | ||
|
||
@Input() titleField: AbstractControl; // Compulsory Input | ||
@Input() titleForm: FormGroup; // Compulsory Input | ||
@Input() id: string; // Compulsory Input | ||
|
||
@Input() initialTitle?: string; | ||
placeholderText = 'Title'; | ||
|
||
@ViewChild('titleInput', { static: true }) titleInput: ElementRef<HTMLInputElement>; | ||
|
||
maxLength = ISSUE_BODY_SIZE_LIMIT; | ||
|
||
history: UndoRedo<textEntry>; | ||
|
||
ngOnInit() { | ||
if (this.initialTitle !== undefined) { | ||
this.titleField.setValue(this.initialTitle); | ||
} | ||
|
||
if (this.titleField === undefined || this.titleForm === undefined || this.id === undefined) { | ||
throw new Error("Title Editor's compulsory properties are not defined."); | ||
} | ||
|
||
this.titleField.setValidators([Validators.required, Validators.maxLength(this.maxLength)]); | ||
this.history = new UndoRedo<textEntry>( | ||
75, | ||
() => { | ||
return { | ||
text: this.titleInput.nativeElement.value, | ||
selectStart: this.titleInput.nativeElement.selectionStart, | ||
selectEnd: this.titleInput.nativeElement.selectionEnd | ||
}; | ||
}, | ||
500 | ||
); | ||
} | ||
|
||
onKeyPress(event: KeyboardEvent) { | ||
if (UndoRedo.isUndo(event)) { | ||
event.preventDefault(); | ||
this.undo(); | ||
return; | ||
} else if (UndoRedo.isRedo(event)) { | ||
this.redo(); | ||
event.preventDefault(); | ||
return; | ||
} | ||
} | ||
|
||
handleBeforeInputChange(event: InputEvent): void { | ||
switch (event.inputType) { | ||
case 'historyUndo': | ||
case 'historyRedo': | ||
// ignore these events that doesn't modify the text | ||
event.preventDefault(); | ||
break; | ||
case 'insertFromPaste': | ||
this.history.forceSave(null, true, false); | ||
break; | ||
|
||
default: | ||
this.history.updateBeforeChange(); | ||
} | ||
} | ||
|
||
handleInputChange(event: InputEvent): void { | ||
switch (event.inputType) { | ||
case 'historyUndo': | ||
case 'historyRedo': | ||
// ignore these events that doesn't modify the text | ||
event.preventDefault(); | ||
break; | ||
case 'insertFromPaste': | ||
// paste events will be handled exclusively by handleBeforeInputChange | ||
break; | ||
|
||
default: | ||
this.history.createDelayedSave(); | ||
} | ||
} | ||
|
||
private undo(): void { | ||
const entry = this.history.undo(); | ||
if (entry === null) { | ||
return; | ||
} | ||
this.titleField.setValue(entry.text); | ||
this.titleInput.nativeElement.setSelectionRange(entry.selectStart, entry.selectEnd); | ||
} | ||
|
||
private redo(): void { | ||
const entry = this.history.redo(); | ||
if (entry === null) { | ||
return; | ||
} | ||
this.titleInput.nativeElement.value = entry.text; | ||
this.titleInput.nativeElement.setSelectionRange(entry.selectStart, entry.selectEnd); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This refactoring is good to have