Skip to content

Commit

Permalink
Feat: Add paste functionality to the input component
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberDex committed Dec 2, 2024
1 parent 66b4eb8 commit c21a18a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class Input extends Container
protected onKeyUpBinding = this.onKeyUp.bind(this);
protected stopEditingBinding = this.stopEditing.bind(this);
protected onInputBinding = this.onInput.bind(this);
protected onPasteBinding = this.onPaste.bind(this);

/** Fires when input loses focus. */
onEnter: Signal<(text: string) => void>;
Expand Down Expand Up @@ -162,6 +163,9 @@ export class Input extends Container

if (keysToSkip.includes(key)) return;

if (e.metaKey) return;
if (e.ctrlKey) return;

if (key === 'Backspace')
{
this._delete();
Expand Down Expand Up @@ -341,6 +345,7 @@ export class Input extends Container
this.input.removeEventListener('blur', this.stopEditingBinding);
this.input.removeEventListener('keydown', this.onKeyUpBinding);
this.input.removeEventListener('input', this.onInputBinding as EventListener);
this.input.removeEventListener('paste', this.onPasteBinding);

this.input?.blur();
this.input?.remove();
Expand Down Expand Up @@ -379,6 +384,7 @@ export class Input extends Container
input.addEventListener('blur', this.stopEditingBinding);
input.addEventListener('keydown', this.onKeyUpBinding);
input.addEventListener('input', this.onInputBinding as EventListener);
input.addEventListener('paste', this.onPasteBinding);

this.input = input;

Expand Down Expand Up @@ -713,4 +719,14 @@ export class Input extends Container

this.inputMask.position.set(this.paddingLeft, this.paddingTop);
}

protected onPaste(e: any) {

Check warning on line 723 in src/Input.ts

View workflow job for this annotation

GitHub Actions / build

Opening curly brace appears on the same line as controlling statement
e.preventDefault();

const text = (e.clipboardData || (window as any).clipboardData).getData("text");

Check warning on line 726 in src/Input.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

if (!text) return;

this._add(text);
}
}

0 comments on commit c21a18a

Please sign in to comment.