미디어위키:Gadget-dictionary.js: 두 판 사이의 차이

편집 요약 없음
편집 요약 없음
 
(같은 사용자의 중간 판 4개는 보이지 않습니다)
1번째 줄: 1번째 줄:
/* ▣ Dictionary “검색 전용” 가젯 ▣
/* ▣ Dictionary “검색 전용” 가젯 ▣
   위키문법을 서버 파서로 HTML 변환 후 출력
   - 표 없이: 검색 → 결과 카드 or “없음” 메시지
  - 단어(키)에 들어 있는 위키문법을 실제로 렌더링
-------------------------------------------------- */
-------------------------------------------------- */
mw.loader.using(
mw.loader.using(
6번째 줄: 7번째 줄:
   function () {
   function () {


     /* ─ 1. JSON → {소문자 단어: 정의(wikitext)} ─ */
     /* ────────── Ⅰ. 사전 JSON 로드 ────────── */
     var jsonNode = document.getElementById('dictionary-json');
     var jsonNode = document.getElementById('dictionary-json');
     if (!jsonNode) return;
     if (!jsonNode) return;                       // 사전 없는 페이지
     var raw = JSON.parse(jsonNode.textContent);
     var dictRaw = JSON.parse(jsonNode.textContent);
     var dict = {};
     var dict     = {};                           // 소문자 키 → {rawKey, def}
     Object.keys(raw).forEach(k => dict[k.toLowerCase()] = raw[k]);
     Object.keys(dictRaw).forEach(function (k) {
      dict[k.toLowerCase()] = { raw: k, def: dictRaw[k] };
    });


     /* ─ 2. 위키텍스트 → HTML 변환용 함수 (캐시 포함) */
     /* ────────── Ⅱ. CSS 삽입(1회) ────────── */
     const api  = new mw.Api();
     if (!document.getElementById('dict-card-style')) {
    const cache = Object.create(null);     // wt → html
      mw.util.addCSS(`
.oo-ui-inputWidget-input, .oo-ui-buttonElement-button {
background: light-dark(#fff, rgb(30,30,30)) !important;
color: var(--text) !important;
border: 1px solid light-dark(#ccc, #555) !important;
height: 36px !important;
}
.oo-ui-inputWidget-input {
border-radius: 5px 0 0 5px !important;
}
.oo-ui-buttonElement-button {
border-radius: 0 5px 5px 0 !important;
padding-top: 6px;
padding-bottom: 6px;
}
        .dict-card    {padding:0px 16px 8px; border:1px solid light-dark(#ccc, #555);border-radius:8px;
                      background:light-dark(#f9f9f9, HSL(200, 5%, 17%)); padding-top: }
        .dict-card .term{font-weight:600;font-size:1.2em;margin-right:.4em; padding:8px; padding-bottom: 3px; margin-bottom: -8px;}
.dict-card .def{padding-left:8px; padding-right:8px; margin-bottom: -3px;}
        .dict-none    {padding:8px;color:light-dark(#d33, HSL(0, 71%, 75%));}
      `).id = 'dict-card-style';
    }


     function parseWT(wt) {
     /* ────────── Ⅲ. 위키텍스트 → HTML 파서 헬퍼 ────────── */
      if (cache[wt]) return $.Deferred().resolve(cache[wt]).promise();
    var api = new mw.Api();
    function parseWikitext(wikitext) {
       return api.get({
       return api.get({
         action: 'parse',
         action: 'parse',
         format: 'json',
         format: 'json',
        prop:  'text',
        text:  wt,
         contentmodel: 'wikitext',
         contentmodel: 'wikitext',
         disablelimitreport: 1
        prop:    'text',
       }).then(data => {
        text:    wikitext,
         const html = data?.parse?.text['*'] || mw.html.escape(wt);
         disablelimitreport: 1,
        cache[wt] = html;
        disableeditsection: 1,
        return html;
        pst: 0, wrapshtml: 1          // 빠른 결과용 옵션들
       }, () => mw.html.escape(wt));         // 오류 시 그대로 출력
       }).then(function (data) {
         return (data.parse && data.parse.text) ? data.parse.text['*'] : mw.html.escape(wikitext);
       });
     }
     }


     /* ─ 3. 한 번만 결과·버튼 CSS 삽입 ─ */
     /* ────────── Ⅳ. 페이지 로드 때 UI 주입 ────────── */
     if (!document.getElementById('dict-card-style')) {
     mw.hook('wikipage.content').add(function ($content) {
       mw.util.addCSS(`
       $content.find('.dictionary-container').each(function () {
.oo-ui-inputWidget-input, .oo-ui-buttonElement-button{
        var $box = $(this);
  background:light-dark(#fff,rgb(30,30,30));color:var(--text);
        if ($box.children().length) return;       // 이미 UI가 있음
  border:1px solid light-dark(#ccc,#555);height:36px}
 
.oo-ui-inputWidget-input{border-radius:5px 0 0 5px}
        /* 1) 검색창 + 버튼 */
.oo-ui-buttonElement-button{border-radius:0 5px 5px 0;padding:6px 8px}
        var input  = new OO.ui.TextInputWidget({ placeholder: '단어 입력…', icons:['search'] });
.dict-card{border:1px solid light-dark(#ccc,#555);border-radius:8px;
        var button = new OO.ui.ButtonWidget({ label:'검색', icon:'search', flags:['progressive'] });
   background:light-dark(#f9f9f9,hsl(200,5%,17%));margin:8px 0;padding:12px 16px}
        var field  = new OO.ui.ActionFieldLayout(input, button, {align:'top'})
.dict-none{padding:8px;color:light-dark(#d33,hsl(0,71%,75%))}
                    .$element.css('margin-bottom','10px');
       `).id = 'dict-card-style';
 
    }
        /* 2) 결과 영역 */
        var $result = $('<div class="dict-result"></div>');
        $box.append(field, $result);
 
        /* 3) 검색 실행 */
/* 3) 검색 실행 – 단어·뜻 모두 위키텍스트 → HTML */
function run() {
  const q    = input.getValue().trim();
   const keyL  = q.toLowerCase();
 
  if ( !q ) {            // 검색어 비우면 결과 영역 초기화
    $result.empty();
    return;
  }
 
  if ( dict.hasOwnProperty( keyL ) ) {
    const entry = dict[ keyL ];          // { raw: 단어위키텍스트, def: 정의위키텍스트 }
 
    /* 단어·정의를 동시에 파싱하고 나서 카드 출력 */
    Promise.all( [
      parseWikitext( entry.raw ),       // ① 단어
      parseWikitext( entry.def )         // ② 정의
    ] ).then( function ( parts ) {
       const htmlKey = parts[0], htmlDef = parts[1];


    /* ─ 4. 검색 UI 주입 ─ */
      $result.html(
    mw.hook('wikipage.content').add($c => {
          '<div class="term">' + htmlKey + '</div>' +
       $c.find('.dictionary-container').each(function () {
          '<div class="def">'  + htmlDef + '</div>'
        const $box = $(this);
      );
        if ($box.children().length) return;   // 중복 방지
    } ).catch( function () {             // 어떤 이유로든 파싱 실패 → 안전 출력
       $result.html(
          '<div class="term">' + mw.html.escape( entry.raw ) + '</div>' +
          '<div class="def">'  + mw.html.escape( entry.def ) + '</div>'
      );
    } );


        /* 입력창 + 버튼 */
  } else {
        const input  = new OO.ui.TextInputWidget({placeholder:'단어 입력…',icons:['search']});
    $result.html( '<div class="dict-none">해당 단어가 없습니다.</div>' );
        const button = new OO.ui.ButtonWidget({label:'검색',icon:'search',flags:['progressive']});
  }
        const field  = new OO.ui.ActionFieldLayout(input,button,{align:'top'})
}
                        .$element.css('margin-bottom','10px');
        const $res  = $('<div class="dict-result"></div>');
        $box.append(field,$res);


        /* 검색 실행 */
        function run() {
          const q = input.getValue().trim();
          const key = q.toLowerCase();
          if (!q) return $res.empty();


          if (dict.hasOwnProperty(key)) {
         button.on('click', run);
            const wt = `'''${q}'''<br>${dict[key]}`;  // 카드 안에 단어+정의를 한 번에 파싱
         input.on('enter', run);
            parseWT(wt).then(html => {
              $res.html(`<div class="dict-card">${html}</div>`);
            });
          } else {
            $res.html('<div class="dict-none">해당 단어가 없습니다.</div>');
          }
        }
         button.on('click',run);
         input.on('enter',run);
       });
       });
     });
     });
   }
   }
);
);

2025년 6월 29일 (일) 04:10 기준 최신판

/* ▣ Dictionary “검색 전용” 가젯 ▣
   - 표 없이: 검색 → 결과 카드 or “없음” 메시지
   - 단어(키)에 들어 있는 위키문법을 실제로 렌더링
-------------------------------------------------- */
mw.loader.using(
  ['oojs-ui-core', 'oojs-ui.styles.icons-interactions', 'mediawiki.api'],
  function () {

    /* ────────── Ⅰ. 사전 JSON 로드 ────────── */
    var jsonNode = document.getElementById('dictionary-json');
    if (!jsonNode) return;                        // 사전 없는 페이지
    var dictRaw  = JSON.parse(jsonNode.textContent);
    var dict     = {};                            // 소문자 키 → {rawKey, def}
    Object.keys(dictRaw).forEach(function (k) {
      dict[k.toLowerCase()] = { raw: k, def: dictRaw[k] };
    });

    /* ────────── Ⅱ. CSS 삽입(1회) ────────── */
    if (!document.getElementById('dict-card-style')) {
      mw.util.addCSS(`
.oo-ui-inputWidget-input, .oo-ui-buttonElement-button {
background: light-dark(#fff, rgb(30,30,30)) !important;
color: var(--text) !important;
border: 1px solid light-dark(#ccc, #555) !important;
height: 36px !important;
}
.oo-ui-inputWidget-input {
border-radius: 5px 0 0 5px !important;
}
.oo-ui-buttonElement-button {
border-radius: 0 5px 5px 0 !important;
padding-top: 6px;
padding-bottom: 6px;
}
        .dict-card    {padding:0px 16px 8px; border:1px solid light-dark(#ccc, #555);border-radius:8px;
                       background:light-dark(#f9f9f9, HSL(200, 5%, 17%)); padding-top: }
        .dict-card .term{font-weight:600;font-size:1.2em;margin-right:.4em; padding:8px; padding-bottom: 3px; margin-bottom: -8px;}
.dict-card .def{padding-left:8px; padding-right:8px; margin-bottom: -3px;}
        .dict-none    {padding:8px;color:light-dark(#d33, HSL(0, 71%, 75%));}
      `).id = 'dict-card-style';
    }

    /* ────────── Ⅲ. 위키텍스트 → HTML 파서 헬퍼 ────────── */
    var api = new mw.Api();
    function parseWikitext(wikitext) {
      return api.get({
        action:  'parse',
        format:  'json',
        contentmodel: 'wikitext',
        prop:    'text',
        text:    wikitext,
        disablelimitreport: 1,
        disableeditsection: 1,
        pst: 0, wrapshtml: 1          // 빠른 결과용 옵션들
      }).then(function (data) {
        return (data.parse && data.parse.text) ? data.parse.text['*'] : mw.html.escape(wikitext);
      });
    }

    /* ────────── Ⅳ. 페이지 로드 때 UI 주입 ────────── */
    mw.hook('wikipage.content').add(function ($content) {
      $content.find('.dictionary-container').each(function () {
        var $box = $(this);
        if ($box.children().length) return;       // 이미 UI가 있음

        /* 1) 검색창 + 버튼 */
        var input  = new OO.ui.TextInputWidget({ placeholder: '단어 입력…', icons:['search'] });
        var button = new OO.ui.ButtonWidget({ label:'검색', icon:'search', flags:['progressive'] });
        var field  = new OO.ui.ActionFieldLayout(input, button, {align:'top'})
                     .$element.css('margin-bottom','10px');

        /* 2) 결과 영역 */
        var $result = $('<div class="dict-result"></div>');
        $box.append(field, $result);

        /* 3) 검색 실행 */
/* 3) 검색 실행 – 단어·뜻 모두 위키텍스트 → HTML */
function run() {
  const q     = input.getValue().trim();
  const keyL  = q.toLowerCase();

  if ( !q ) {            // 검색어 비우면 결과 영역 초기화
    $result.empty();
    return;
  }

  if ( dict.hasOwnProperty( keyL ) ) {
    const entry = dict[ keyL ];          // { raw: 단어위키텍스트, def: 정의위키텍스트 }

    /* 단어·정의를 동시에 파싱하고 나서 카드 출력 */
    Promise.all( [
      parseWikitext( entry.raw ),        // ① 단어
      parseWikitext( entry.def )         // ② 정의
    ] ).then( function ( parts ) {
      const htmlKey = parts[0], htmlDef = parts[1];

      $result.html(
          '<div class="term">' + htmlKey + '</div>' +
          '<div class="def">'  + htmlDef + '</div>'
      );
    } ).catch( function () {             // 어떤 이유로든 파싱 실패 → 안전 출력
      $result.html(
          '<div class="term">' + mw.html.escape( entry.raw ) + '</div>' +
          '<div class="def">'  + mw.html.escape( entry.def ) + '</div>'
      );
    } );

  } else {
    $result.html( '<div class="dict-none">해당 단어가 없습니다.</div>' );
  }
}


        button.on('click', run);
        input.on('enter', run);
      });
    });
  }
);