메뉴 여닫기
환경 설정 메뉴 여닫기
개인 메뉴 여닫기
로그인하지 않음
만약 지금 편집한다면 당신의 IP 주소가 공개될 수 있습니다.

미디어위키:Common.js: 두 판 사이의 차이

novawiki
편집 요약 없음
편집 요약 없음
 
3번째 줄: 3번째 줄:
/*Autofocus on when clicking search button*/
/*Autofocus on when clicking search button*/
(function () {
(function () {
   const DETAILS = document.getElementById('citizen-search-details');
   const DETAILS_ID = 'citizen-search-details';
   const TOGGLE_SEL = '#citizen-search-details > summary.citizen-dropdown-summary';
   const TOGGLE_SEL = '#citizen-search-details > summary.citizen-dropdown-summary';
   const CARD_ID = 'citizen-search__card';
   const REAL_INPUT_ID = 'searchInput';
  const INPUT_ID = 'searchInput';


   if (!DETAILS) return;
   function ensureProxy() {
    let p = document.getElementById('mw-search-proxy');
    if (p) return p;


  function isVisible(el) {
    p = document.createElement('input');
     if (!el) return false;
     p.id = 'mw-search-proxy';
     const r = el.getBoundingClientRect();
     p.type = 'search';
     return r.width > 0 && r.height > 0;
    p.autocomplete = 'off';
  }
     p.setAttribute('aria-hidden', 'true');


  function focusInput(tag) {
    // "안 보이지만" 포커스는 가능한 상태로 (display:none / visibility:hidden 쓰면 포커스 자체가 막힘)
     const input = document.getElementById(INPUT_ID);
     p.style.position = 'fixed';
     if (!input) return false;
    p.style.left = '-9999px';
     if (!isVisible(input)) return false;
    p.style.top = '0';
    p.style.width = '1px';
    p.style.height = '1px';
     p.style.opacity = '0';
     p.style.pointerEvents = 'none';


     // iOS-friendly focus
     document.documentElement.appendChild(p);
    input.focus({ preventScroll: true });
     return p;
    input.click();
 
     return document.activeElement === input;
   }
   }


   function focusWhenShown() {
   function focusRealSoon() {
     const card = document.getElementById(CARD_ID);
     const details = document.getElementById(DETAILS_ID);
     const input = document.getElementById(INPUT_ID);
     const real = document.getElementById(REAL_INPUT_ID);
     if (!card || !input) return;
     if (!details || !real) return;
 
    // 1) 이미 보이면 즉시
    if (focusInput('immediate')) return;
 
    // 2) Citizen이 열릴 때 CSS transition/animation 타면 그 끝에서 포커스
    const onDone = () => {
      if (focusInput('transitionend')) cleanup();
    };


     const cleanup = () => {
     // 열기 보장
      card.removeEventListener('transitionend', onDone, true);
    if (!details.open) details.open = true;
      card.removeEventListener('animationend', onDone, true);
      clearInterval(poll);
    };


     card.addEventListener('transitionend', onDone, true);
     // 레이아웃 한 번 강제 (iOS에 은근 도움 됨)
     card.addEventListener('animationend', onDone, true);
     void real.offsetHeight;


     // 3) 그래도 안 잡히면 짧은 폴링 (최대 1초)
     // 키보드가 이미 떠있으면 real focus가 더 잘 붙는 편
     const start = Date.now();
     real.focus({ preventScroll: true });
     const poll = setInterval(() => {
     real.click();
      if (focusInput('poll')) return cleanup();
      if (Date.now() - start > 1000) cleanup();
    }, 50);
   }
   }


  // 핵심: "사용자 클릭" 이벤트 안에서 열고, 그 다음 '보이는 순간'에 포커스
   function onToggleGesture(e) {
   function openAndQueueFocus(e) {
     const hit = e.target.closest(TOGGLE_SEL);
     const hit = e.target.closest(TOGGLE_SEL);
     if (!hit) return;
     if (!hit) return;


     // 여기서 preventDefault를 걸면 Citizen 토글 로직이 꼬일 수 있어서
     // 1) 유저 제스처 안에서 "input focus"를 먼저 한 번 만들어줌
     // 일단은 open만 보장하고, Citizen이 열어주는 흐름도 같이 허용.
    const proxy = ensureProxy();
     if (!DETAILS.open) DETAILS.open = true;
 
    try {
      proxy.focus({ preventScroll: true });
      proxy.click();
    } catch (_) {}
 
     // 2) 같은 제스처 안에서 real focus도 바로 시도
     focusRealSoon();


     focusWhenShown();
     // 3) 그래도 Citizen이 애니메이션/상태 적용을 늦게 하면 다음 프레임에 한 번 더
    requestAnimationFrame(() => {
      focusRealSoon();
      // proxy는 필요 없으니 정리
      try { proxy.blur(); } catch (_) {}
    });
   }
   }


   // iOS: touchstart가 제일 안정적
   // iOS에서 제일 잘 먹는 건 touchstart
   document.addEventListener('touchstart', openAndQueueFocus, { capture: true, passive: true });
   document.addEventListener('touchstart', onToggleGesture, { capture: true, passive: true });
   document.addEventListener('click', openAndQueueFocus, true);
   document.addEventListener('click', onToggleGesture, true);
})();
})();

2026년 1월 17일 (토) 19:01 기준 최신판

/* 이 자바스크립트 설정은 모든 문서, 모든 사용자에게 적용됩니다. */

/*Autofocus on when clicking search button*/
(function () {
  const DETAILS_ID = 'citizen-search-details';
  const TOGGLE_SEL = '#citizen-search-details > summary.citizen-dropdown-summary';
  const REAL_INPUT_ID = 'searchInput';

  function ensureProxy() {
    let p = document.getElementById('mw-search-proxy');
    if (p) return p;

    p = document.createElement('input');
    p.id = 'mw-search-proxy';
    p.type = 'search';
    p.autocomplete = 'off';
    p.setAttribute('aria-hidden', 'true');

    // "안 보이지만" 포커스는 가능한 상태로 (display:none / visibility:hidden 쓰면 포커스 자체가 막힘)
    p.style.position = 'fixed';
    p.style.left = '-9999px';
    p.style.top = '0';
    p.style.width = '1px';
    p.style.height = '1px';
    p.style.opacity = '0';
    p.style.pointerEvents = 'none';

    document.documentElement.appendChild(p);
    return p;
  }

  function focusRealSoon() {
    const details = document.getElementById(DETAILS_ID);
    const real = document.getElementById(REAL_INPUT_ID);
    if (!details || !real) return;

    // 열기 보장
    if (!details.open) details.open = true;

    // 레이아웃 한 번 강제 (iOS에 은근 도움 됨)
    void real.offsetHeight;

    // 키보드가 이미 떠있으면 real focus가 더 잘 붙는 편
    real.focus({ preventScroll: true });
    real.click();
  }

  function onToggleGesture(e) {
    const hit = e.target.closest(TOGGLE_SEL);
    if (!hit) return;

    // 1) 유저 제스처 안에서 "input focus"를 먼저 한 번 만들어줌
    const proxy = ensureProxy();

    try {
      proxy.focus({ preventScroll: true });
      proxy.click();
    } catch (_) {}

    // 2) 같은 제스처 안에서 real focus도 바로 시도
    focusRealSoon();

    // 3) 그래도 Citizen이 애니메이션/상태 적용을 늦게 하면 다음 프레임에 한 번 더
    requestAnimationFrame(() => {
      focusRealSoon();
      // proxy는 필요 없으니 정리
      try { proxy.blur(); } catch (_) {}
    });
  }

  // iOS에서 제일 잘 먹는 건 touchstart
  document.addEventListener('touchstart', onToggleGesture, { capture: true, passive: true });
  document.addEventListener('click', onToggleGesture, true);
})();