-
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 9 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
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
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> |
134 changes: 134 additions & 0 deletions
134
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,134 @@ | ||
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 (this.isUndo(event)) { | ||
event.preventDefault(); | ||
this.undo(); | ||
return; | ||
} else if (this.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); | ||
} | ||
|
||
private isUndo(event: KeyboardEvent) { | ||
// prevents undo from firing when ctrl shift z is pressed | ||
if (navigator.platform.indexOf('Mac') === 0) { | ||
return event.metaKey && event.code === 'KeyZ' && !event.shiftKey; | ||
} | ||
return event.ctrlKey && event.code === 'KeyZ' && !event.shiftKey; | ||
} | ||
|
||
private isRedo(event: KeyboardEvent) { | ||
if (navigator.platform.indexOf('Mac') === 0) { | ||
return event.metaKey && event.shiftKey && event.code === 'KeyZ'; | ||
} | ||
return (event.ctrlKey && event.shiftKey && event.code === 'KeyZ') || (event.ctrlKey && event.code === 'KeyY'); | ||
} | ||
} | ||
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
76 changes: 76 additions & 0 deletions
76
tests/app/shared/title-editor/title-editor.component.spec.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,76 @@ | ||
import { CUSTOM_ELEMENTS_SCHEMA, DebugElement } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { FormControl, FormGroup, FormsModule } from '@angular/forms'; | ||
import { By } from '@angular/platform-browser'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
|
||
import { TitleEditorComponent } from '../../../../src/app/shared/issue/title-editor/title-editor.component'; | ||
import { SharedModule } from '../../../../src/app/shared/shared.module'; | ||
|
||
describe('CommentEditor', () => { | ||
let fixture: ComponentFixture<TitleEditorComponent>; | ||
let debugElement: DebugElement; | ||
let component: TitleEditorComponent; | ||
|
||
const TEST_INITIAL_TITLE = 'abc'; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [TitleEditorComponent], | ||
imports: [FormsModule, SharedModule, BrowserAnimationsModule], | ||
schemas: [CUSTOM_ELEMENTS_SCHEMA] | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(TitleEditorComponent); | ||
debugElement = fixture.debugElement; | ||
component = fixture.componentInstance; | ||
|
||
// initialize compulsory inputs | ||
const titleField: FormControl = new FormControl(''); | ||
const titleForm: FormGroup = new FormGroup({ | ||
title: titleField | ||
}); | ||
const id = 'title'; | ||
|
||
// manually inject inputs into the component | ||
component.titleField = titleField; | ||
component.titleForm = titleForm; | ||
component.id = id; | ||
}); | ||
|
||
describe('text input box', () => { | ||
it('should render', () => { | ||
fixture.detectChanges(); | ||
|
||
const textBoxDe: DebugElement = debugElement.query(By.css('input')); | ||
expect(textBoxDe).toBeTruthy(); | ||
}); | ||
|
||
it('should contain an empty string if no initial description is provided', () => { | ||
fixture.detectChanges(); | ||
|
||
const textBox: any = debugElement.query(By.css('input')).nativeElement; | ||
expect(textBox.value).toEqual(''); | ||
}); | ||
|
||
it('should contain an initial description if one is provided', () => { | ||
component.initialTitle = TEST_INITIAL_TITLE; | ||
fixture.detectChanges(); | ||
|
||
const textBox: any = debugElement.query(By.css('input')).nativeElement; | ||
expect(textBox.value).toEqual(TEST_INITIAL_TITLE); | ||
}); | ||
|
||
it('should allow users to input text', async () => { | ||
fixture.detectChanges(); | ||
|
||
const textBox: any = debugElement.query(By.css('input')).nativeElement; | ||
textBox.value = '123'; | ||
textBox.dispatchEvent(new Event('input')); | ||
|
||
fixture.whenStable().then(() => { | ||
expect(textBox.value).toEqual('123'); | ||
}); | ||
}); | ||
}); | ||
chia-yh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
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.
I think this code is repeated from
comment-editor.component.ts
. Perhaps these functions can be inundoredo.model.ts
?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.
Good suggestion, I think
isUndo
andisRedo
can definitely be moved intoundoredo.model.ts
, perhaps as a static method. However, I'm not sure if the same should be done for theundo
andredo
methods, as I personally feel that the handling of values fromUndoRedo::undo
/UndoRedo::redo
should be done in the components using theUndoRedo
model, in the interests of keepingUndoRedo
more general. Could I perhaps ask for your thoughts on this?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.
Leaving the undo and redo methods as it is is okay too