feat: not allow non ascii character in macro action text (#645)
This commit is contained in:
committed by
László Monda
parent
eb97dd844f
commit
091796d13c
@@ -1,5 +1,10 @@
|
|||||||
<div>
|
<div>
|
||||||
<h4>Type text</h4>
|
<h4>Type text</h4>
|
||||||
<textarea #macroTextInput name="macro-text" (change)="onTextChange()"
|
<textarea #macroTextInput
|
||||||
(keyup)="validate()" class="macro__text-input">{{ macroAction?.text }}</textarea>
|
name="macro-text"
|
||||||
|
(keydown)="onKeydown($event)"
|
||||||
|
(change)="onTextChange()"
|
||||||
|
(keyup)="validate()"
|
||||||
|
(paste)="onPaste($event)"
|
||||||
|
class="macro__text-input">{{ macroAction?.text }}</textarea>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import { TextMacroAction } from 'uhk-common';
|
|||||||
|
|
||||||
import { MacroBaseComponent } from '../macro-base.component';
|
import { MacroBaseComponent } from '../macro-base.component';
|
||||||
|
|
||||||
|
const NON_ASCII_REGEXP = /[^\x00-\x7F]/g;
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'macro-text-tab',
|
selector: 'macro-text-tab',
|
||||||
templateUrl: './macro-text.component.html',
|
templateUrl: './macro-text.component.html',
|
||||||
@@ -36,6 +38,41 @@ export class MacroTextTabComponent extends MacroBaseComponent implements OnInit,
|
|||||||
this.macroAction.text = this.input.nativeElement.value;
|
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;
|
isMacroValid = () => !!this.input.nativeElement.value;
|
||||||
|
|
||||||
private init = () => {
|
private init = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user