Skip to content
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

Feat: Add paste functionality to the input component #213

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

if (keysToSkip.includes(key)) return;

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

Comment on lines +166 to +168
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes #196

if (key === 'Backspace')
{
this._delete();
Expand All @@ -171,7 +175,7 @@
this.stopEditing();
}
else if (key.length === 1)
{

Check warning on line 178 in src/Input.ts

View workflow job for this annotation

GitHub Actions / build

Block must not be padded by blank lines

this._add(key);
}
Expand All @@ -180,7 +184,7 @@
this._add(this.lastInputData);
}

if (this.input) {

Check warning on line 187 in src/Input.ts

View workflow job for this annotation

GitHub Actions / build

Opening curly brace appears on the same line as controlling statement
this.input.value = '';
}
}
Expand Down Expand Up @@ -341,6 +345,7 @@
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 @@
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 @@

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);
}
}
Loading