Skip to content

Commit

Permalink
Feat: Add paste functionality to the input component (#213) (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberDex authored Dec 2, 2024
1 parent 7230af8 commit 1c20bd6
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 @@ -62,6 +62,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 @@ -147,6 +148,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 @@ -315,6 +319,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 @@ -353,6 +358,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 @@ -659,4 +665,14 @@ export class Input extends Container

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

protected onPaste(e: any) {
e.preventDefault();

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

if (!text) return;

this._add(text);
}
}

0 comments on commit 1c20bd6

Please sign in to comment.