Skip to content
Merged
Show file tree
Hide file tree
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
45 changes: 22 additions & 23 deletions src/lib/input.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// 네이티브 키보드 입력 + IME 조합 처리 (PRD §4.1, §6).
// 글자 입력 + IME 조합 처리 (PRD §4.1, §6).
//
// 화면을 덮는 투명 input 대신 오프스크린 input 에 focus() 해서 키보드를 띄운다.
// 보이지 않는 input 에 focus 를 주는 방식은 모바일 브라우저가 키보드를
// 안정적으로 띄우지 않아서, 화면에 보이는 input 박스를 focus 대상으로 쓴다.
// 한글은 IME 조합이 끝난 시점(compositionend)에만 물체를 만들고,
// 조합 중(ㅎ → 하 → 한)에는 고스트 텍스트로만 보여준다.
// 조합 중(ㅎ → 하 → 한)은 input 박스 안에서 그대로 보인다.
//
// iOS Safari 와 안드로이드 크롬의 이벤트 순서가 다르다:
// - 조합 중 input 이벤트는 isComposing 으로 걸러서 중복 스폰을 막는다
Expand All @@ -12,24 +13,14 @@
export interface InputHooks {
/** 확정된 글자 하나 (공백·개행 제외) */
onChar: (char: string) => void;
/** 조합 중 텍스트. 빈 문자열이면 조합 종료(고스트 숨김) */
onComposing: (text: string) => void;
}

export function mountHiddenInput(container: HTMLElement, hooks: InputHooks): HTMLInputElement {
const input = document.createElement("input");
input.type = "text";
export function wireLetterInput(input: HTMLInputElement, hooks: InputHooks) {
input.autocomplete = "off";
input.autocapitalize = "off";
input.spellcheck = false;
input.setAttribute("autocorrect", "off");
input.setAttribute("aria-label", "글자 입력");
// 화면에는 안 보이되 focus 는 가능해야 한다 (display:none 이면 키보드가 안 뜬다).
// 상단에 두어야 iOS 가 focus 시 화면을 스크롤하지 않는다.
input.style.cssText =
"position:fixed;top:0;left:0;width:1px;height:1px;opacity:0;border:0;padding:0;" +
"background:transparent;color:transparent;caret-color:transparent;pointer-events:none;";
container.appendChild(input);
input.setAttribute("enterkeyhint", "done");

let composing = false;

Expand All @@ -46,13 +37,8 @@ export function mountHiddenInput(container: HTMLElement, hooks: InputHooks): HTM
composing = true;
});

input.addEventListener("compositionupdate", (e) => {
hooks.onComposing(e.data ?? "");
});

input.addEventListener("compositionend", (e) => {
composing = false;
hooks.onComposing("");
if (e.data) {
flushChars(e.data);
}
Expand All @@ -69,12 +55,25 @@ export function mountHiddenInput(container: HTMLElement, hooks: InputHooks): HTM
}
});

// 키보드가 닫혀도(blur) 조합 고스트가 남지 않게
input.addEventListener("blur", () => {
composing = false;
hooks.onComposing("");
input.value = "";
});
}

return input;
/** 데스크톱(마우스 환경)에서는 별도 탭 없이 바로 타이핑되도록 input 을 붙잡아 둔다 */
export function keepFocusForDesktop(input: HTMLInputElement) {
const desktop = window.matchMedia("(hover: hover) and (pointer: fine)").matches;
if (!desktop) {
return;
}
input.focus({ preventScroll: true });
window.addEventListener("keydown", (e) => {
if (e.isComposing || e.metaKey || e.ctrlKey || e.altKey) {
return;
}
if (document.activeElement !== input) {
input.focus({ preventScroll: true });
}
});
}
65 changes: 37 additions & 28 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,32 @@ const base = import.meta.env.BASE_URL.endsWith("/")
<body>
<main id="stage">
<canvas id="stage-canvas"></canvas>
<p id="ghost" hidden></p>
<p id="hint">탭하고 아무 글자나 쳐 보세요</p>
<p id="hint">아무 글자나 쳐 보세요</p>
<input id="letter-input" type="text" placeholder="여기에 입력" aria-label="글자 입력" />
</main>
</body>
</html>

<script>
import { mountHiddenInput } from "../lib/input";
import { keepFocusForDesktop, wireLetterInput } from "../lib/input";
import { mountStage } from "../lib/stage";

const canvas = document.querySelector<HTMLCanvasElement>("#stage-canvas");
const ghost = document.querySelector<HTMLParagraphElement>("#ghost");
const hint = document.querySelector<HTMLParagraphElement>("#hint");
if (!canvas) {
throw new Error("#stage-canvas 를 찾지 못했다");
const input = document.querySelector<HTMLInputElement>("#letter-input");
if (!canvas || !input) {
throw new Error("#stage-canvas / #letter-input 을 찾지 못했다");
}

const stage = mountStage(canvas, { onFirstSpawn: () => hint?.remove() });

const input = mountHiddenInput(document.body, {
onChar: (char) => stage.spawnLetter(char),
onComposing: (text) => {
if (ghost) {
ghost.textContent = text;
ghost.hidden = text === "";
}
},
});
wireLetterInput(input, { onChar: (char) => stage.spawnLetter(char) });

// 탭하면 네이티브 키보드를 소환한다
canvas.addEventListener("pointerdown", () => input.focus());
// 캔버스를 탭해도 입력 박스로 focus (click 은 신뢰된 제스처라 키보드가 뜬다)
canvas.addEventListener("click", () => input.focus({ preventScroll: true }));

// PC 에서는 별도 탭 없이 바로 타이핑
keepFocusForDesktop(input);
</script>

<style>
Expand All @@ -83,18 +78,32 @@ const base = import.meta.env.BASE_URL.endsWith("/")
touch-action: none;
}

/* 조합 중 고스트 — 스폰 지점(상단 중앙)에 흐리게 */
#ghost {
position: absolute;
top: calc(env(safe-area-inset-top, 0px) + 12px);
left: 0;
right: 0;
margin: 0;
/* 입력 박스 — safe-area 하단 중앙, 반투명 (PRD §5 하단 바 자리) */
#letter-input {
position: fixed;
left: 50%;
transform: translateX(-50%);
bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
width: min(70vw, 280px);
padding: 10px 16px;
/* 16px 미만이면 iOS 가 focus 시 화면을 확대한다 */
font-size: 18px;
text-align: center;
font-size: 56px;
color: rgba(232, 232, 239, 0.35);
user-select: none;
pointer-events: none;
color: #e8e8ef;
background: rgba(255, 255, 255, 0.08);
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 999px;
outline: none;
-webkit-backdrop-filter: blur(8px);
backdrop-filter: blur(8px);
}

#letter-input::placeholder {
color: #9a9aa8;
}

#letter-input:focus {
border-color: rgba(255, 255, 255, 0.35);
}

#hint {
Expand Down
Loading