feat: not allow non ascii character in macro action text (#645)

This commit is contained in:
Róbert Kiss
2018-05-17 23:56:36 +02:00
committed by László Monda
parent eb97dd844f
commit 091796d13c
2 changed files with 44 additions and 2 deletions

View File

@@ -1,5 +1,10 @@
<div>
<h4>Type text</h4>
<textarea #macroTextInput name="macro-text" (change)="onTextChange()"
(keyup)="validate()" class="macro__text-input">{{ macroAction?.text }}</textarea>
<textarea #macroTextInput
name="macro-text"
(keydown)="onKeydown($event)"
(change)="onTextChange()"
(keyup)="validate()"
(paste)="onPaste($event)"
class="macro__text-input">{{ macroAction?.text }}</textarea>
</div>

View File

@@ -11,6 +11,8 @@ import { TextMacroAction } from 'uhk-common';
import { MacroBaseComponent } from '../macro-base.component';
const NON_ASCII_REGEXP = /[^\x00-\x7F]/g;
@Component({
selector: 'macro-text-tab',
templateUrl: './macro-text.component.html',
@@ -36,6 +38,41 @@ export class MacroTextTabComponent extends MacroBaseComponent implements OnInit,
this.macroAction.text = this.input.nativeElement.value;
}
/**
* Not allow non ascii character
* @param $event
*/
onKeydown($event: KeyboardEvent): void {
if (new RegExp(NON_ASCII_REGEXP).test($event.key)) {
$event.preventDefault();
$event.stopPropagation();
}
}
/**
* Remove non ascii character from clipboard data
* @param $event
*/
onPaste($event: ClipboardEvent): void {
$event.preventDefault();
const textarea: HTMLTextAreaElement = this.input.nativeElement;
const data = $event.clipboardData.getData('text/plain');
const text = data && data.replace(NON_ASCII_REGEXP, '') || '';
if (text.length === 0) {
return;
}
const value = textarea.value || '';
const prefix = value.substr(0, textarea.selectionStart);
const end = textarea.selectionEnd;
const suffix = value.substr(textarea.selectionEnd);
textarea.value = prefix + text + suffix;
const correction = end === 0 ? 0 : 1;
textarea.selectionStart = textarea.selectionEnd = end + text.length - correction;
this.macroAction.text = textarea.value;
}
isMacroValid = () => !!this.input.nativeElement.value;
private init = () => {