Skip to content

Commit

Permalink
feat: remove clipboard input experimental feature - fix #403
Browse files Browse the repository at this point in the history
  • Loading branch information
pagoru committed Dec 12, 2024
1 parent c992c90 commit e1ed0fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/example/src/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { container, ContainerComponent } from "@tu/tulip";
import { dataComponent } from "data.component";
// import { testComponent } from "test.component";
// import { textsComponent } from "texts.component";
// import { inputsComponent } from "inputs.component";
import { inputsComponent } from "inputs.component";
// import { playerComponent } from "player.component";
// import { dragComponent } from "drag.component";

Expand All @@ -14,7 +14,7 @@ export const appComponent: ContainerComponent<Props, Mutable> = () => {

// $container.add(textsComponent());
// $container.add(playerComponent());
// $container.add(inputsComponent());
$container.add(inputsComponent());
// $container.add(dragComponent());

const a = dataComponent({ test: "Abc12312311333" });
Expand Down
20 changes: 13 additions & 7 deletions src/components/prefabs/input-text-sprite.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
closeKeyboard,
combineAccentAndChar,
getAccentCode,
getInputElement,
isNotNullish,
openKeyboard,
} from "../../utils";
Expand Down Expand Up @@ -285,13 +286,7 @@ export const inputTextSprite: ContainerComponent<
shiftKey,
}: KeyboardEvent) => {
if (key === "Tab") return;
if ((metaKey || ctrlKey) && key.toLowerCase() === "v") {
// @ts-ignore
await navigator.permissions.query({ name: "clipboard-read" });
const text = await navigator.clipboard.readText();
setText(`${$text}${text}`);
return;
}
if ((metaKey || ctrlKey) && key.toLowerCase() === "v") return;

const accentCode = getAccentCode(code, shiftKey);
if (accentCode) {
Expand Down Expand Up @@ -368,13 +363,22 @@ export const inputTextSprite: ContainerComponent<
$startCursorBlink();
};

const onPaste = (event: ClipboardEvent) => {
const clipboardData = event.clipboardData;
const pastedText = clipboardData.getData("text");

setText(`${$text}${pastedText}`);
};

let removeOnKeyDown: () => void;
let removeOnKeyUp: () => void;

$container.on(DisplayObjectEvent.CONTEXT_ENTER, () => {
removeOnKeyDown = global.events.on(Event.KEY_DOWN, onKeyDown, $textSprite);
removeOnKeyUp = global.events.on(Event.KEY_UP, onKeyUp, $textSprite);

getInputElement().addEventListener("paste", onPaste);

//Move cursor to end
$cursorIndex = $text.length;
$cursor.setPositionX($textSprite.$getTextBounds().width + 1);
Expand All @@ -394,6 +398,8 @@ export const inputTextSprite: ContainerComponent<
removeOnKeyDown();
removeOnKeyUp();

getInputElement().removeEventListener("paste", onPaste);

$placeHolderTextSprite.setVisible($text.length === 0);

$selectionComponent.setVisible(false);
Expand Down
2 changes: 1 addition & 1 deletion src/consts/events.consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Event } from "../enums";

//eventName, Event, preventDefault
export const EVENT_MAP: [string, Event, boolean][] = [
["keydown", Event.KEY_DOWN, true],
["keydown", Event.KEY_DOWN, false],
["keyup", Event.KEY_UP, true],
["contextmenu", Event.RIGHT_CLICK, true],
["mousemove", Event.POINTER_MOVE, true],
Expand Down
8 changes: 1 addition & 7 deletions src/utils/keyboard.utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { isMobile } from "./os.utils";

const getInputElement = (): HTMLInputElement =>
export const getInputElement = (): HTMLInputElement =>
document.getElementsByTagName("input")[0] || document.createElement("input");

export const openKeyboard = () => {
if (!isMobile()) return;

const target = getInputElement();
target.style.position = "absolute";
target.style.left = "-20px";
Expand All @@ -18,8 +14,6 @@ export const openKeyboard = () => {
};

export const closeKeyboard = () => {
if (!isMobile()) return;

const target = getInputElement();
target.blur();
target.value = "";
Expand Down

0 comments on commit e1ed0fb

Please sign in to comment.