미디어위키:Gadget-dictionary.js: 두 판 사이의 차이
편집 요약 없음 |
편집 요약 없음 |
||
| 1번째 줄: | 1번째 줄: | ||
mw.loader.using( | mw.loader.using( | ||
['oojs-ui-core', 'oojs-ui.styles.icons-interactions', 'mediawiki.api'], | ['oojs-ui-core', 'oojs-ui.styles.icons-interactions', 'mediawiki.api'], | ||
function () { | function () { | ||
var jsonNode = document.getElementById('dictionary-json'); | var jsonNode = document.getElementById('dictionary-json'); | ||
if (!jsonNode) return; | if (!jsonNode) return; | ||
var dictRaw = JSON.parse(jsonNode.textContent); | var dictRaw = JSON.parse(jsonNode.textContent); | ||
var dict = {}; | var dict = {}; | ||
Object.keys(dictRaw).forEach(function (k) { | Object.keys(dictRaw).forEach(function (k) { | ||
const normKey = k.normalize('NFC').toLowerCase(); | const normKey = k.normalize('NFC').toLowerCase(); | ||
dict[normKey] = { raw: k, def: dictRaw[k] }; | dict[normKey] = { | ||
raw: k, | |||
def: dictRaw[k], | |||
rawNorm: k.normalize('NFC').toLowerCase(), | |||
defNorm: dictRaw[k].normalize('NFC').toLowerCase() | |||
}; | |||
}); | }); | ||
if (!document.getElementById('dict-card-style')) { | if (!document.getElementById('dict-card-style')) { | ||
mw.util.addCSS(` | mw.util.addCSS(` | ||
.dictionary-container .oo-ui-inputWidget-input, .dictionary-container .oo-ui-buttonElement-button { | .dictionary-container .oo-ui-inputWidget-input, .dictionary-container .oo-ui-buttonElement-button { | ||
background: var(--bg) !important; | background: var(--bg) !important; | ||
color: var(--text) !important; | color: var(--text) !important; | ||
border: 1px solid var(--border) !important; | border: 1px solid var(--border) !important; | ||
height: 36px !important; | height: 36px !important; | ||
} | } | ||
.oo-ui-inputWidget-input { | .oo-ui-inputWidget-input { | ||
border-radius: 0.5rem 0 0 0.5rem !important; | border-radius: 0.5rem 0 0 0.5rem !important; | ||
padding-left: 11px !important; | padding-left: 11px !important; | ||
} | } | ||
.oo-ui-buttonElement-button { | .oo-ui-buttonElement-button { | ||
border-radius: 0 0.5rem 0.5rem 0 !important; | border-radius: 0 0.5rem 0.5rem 0 !important; | ||
padding-top: 6px; | padding-top: 6px; | ||
padding-bottom: 6px; | padding-bottom: 6px; | ||
} | } | ||
.dict-card | .dict-card { | ||
padding: 20px 20px 8px !important; | |||
.dict-card .term{font-weight:600;font-size:1.5em;margin-right:.4em; padding:8px; padding-bottom: 3px; margin-bottom: -15px; padding-top: 4px; } | border:1px solid light-dark(#ccc, #555); | ||
.dict-card .def{padding-left:8px; padding-right:8px; margin-bottom: -3px;} | border-radius:0.8rem; | ||
.dict-none | margin: 1em 0 0.5em !important; | ||
background:var(--altbg); | |||
padding-top: | |||
} | |||
.dict-card .term { | |||
font-weight: 600; | |||
font-size: 1.5em; | |||
margin-right: .4em; | |||
padding: 8px; | |||
padding-bottom: 3px; | |||
margin-bottom: -15px; | |||
padding-top: 4px; | |||
} | |||
.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'; | `).id = 'dict-card-style'; | ||
} | } | ||
var api = new mw.Api(); | var api = new mw.Api(); | ||
function parseWikitext(wikitext) { | function parseWikitext(wikitext) { | ||
return api.get({ | return api.get({ | ||
action: | action: 'parse', | ||
format: | format: 'json', | ||
contentmodel: 'wikitext', | contentmodel: 'wikitext', | ||
prop: | prop: 'text', | ||
text: | text: wikitext, | ||
disablelimitreport: 1, | disablelimitreport: 1, | ||
disableeditsection: 1, | disableeditsection: 1, | ||
| 61번째 줄: | 81번째 줄: | ||
} | } | ||
/ | function matchLevel(entry, q) { | ||
if (entry.rawNorm === q) return 1; | |||
if (entry.rawNorm.startsWith(q)) return 2; | |||
if (entry.rawNorm.includes(q)) return 3; | |||
if (entry.defNorm.includes(q)) return 4; | |||
return 0; | |||
} | |||
function renderResults(results) { | |||
if (!results.length) { | |||
$result.html('<div class="dict-none">해당 단어가 없습니다.</div>'); | |||
return; | |||
} | |||
const html = results.map(r => | |||
'<div class="term">' + mw.html.escape(r.entry.raw) + '</div>' + | |||
'<div class="def">' + mw.html.escape(r.entry.def) + '</div>' | |||
).join(''); | |||
$result.html(html); | |||
} | |||
mw.hook('wikipage.content').add(function ($content) { | mw.hook('wikipage.content').add(function ($content) { | ||
$content.find('.dictionary-container').each(function () { | $content.find('.dictionary-container').each(function () { | ||
var $box = $(this); | var $box = $(this); | ||
if ($box.children().length) return; | if ($box.children().length) return; | ||
var input = new OO.ui.TextInputWidget({ placeholder: '단어 입력…', icons: ['search'] }); | |||
var input = new OO.ui.TextInputWidget({ placeholder: '단어 입력…', icons:['search'] }); | var button = new OO.ui.ButtonWidget({ label: '검색', icon: 'search', flags: ['progressive'] }); | ||
var button = new OO.ui.ButtonWidget({ label:'검색', icon:'search', flags:['progressive'] }); | |||
var field = new OO.ui.ActionFieldLayout(input, button, {align:'top'}) | var field = new OO.ui.ActionFieldLayout(input, button, {align:'top'}) | ||
.$element.css('margin-bottom','10px'); | .$element.css('margin-bottom','10px'); | ||
var $result = $('<div class="dict-result"></div>'); | var $result = $('<div class="dict-result"></div>'); | ||
$box.append(field, $result); | $box.append(field, $result); | ||
function run() { | function run() { | ||
const qRaw | const qRaw = input.getValue(); | ||
const q | const q = qRaw.trim().normalize('NFC').toLowerCase(); | ||
if (! | if (!q) { | ||
$result.empty(); | $result.empty(); | ||
return; | return; | ||
} | } | ||
const hits = []; | |||
Object.values(dict).forEach(entry => { | |||
const level = matchLevel(entry, q); | |||
if (level) { | |||
hits.push({ entry, level }); | |||
} | |||
}); | |||
hits.sort((a, b) => { | |||
if (a.level !== b.level) return a.level - b.level; | |||
return a.entry.rawNorm.localeCompare(b.entry.rawNorm); | |||
}); | |||
renderResults(hits.slice(0, 11)); | |||
} | } | ||
button.on('click', run); | button.on('click', run); | ||
input.on('enter', run); | input.on('enter', run); | ||
input.on('input', run); | |||
}); | }); | ||
}); | }); | ||
} | } | ||
); | ); | ||
2026년 3월 5일 (목) 22:20 판
mw.loader.using(
['oojs-ui-core', 'oojs-ui.styles.icons-interactions', 'mediawiki.api'],
function () {
var jsonNode = document.getElementById('dictionary-json');
if (!jsonNode) return;
var dictRaw = JSON.parse(jsonNode.textContent);
var dict = {};
Object.keys(dictRaw).forEach(function (k) {
const normKey = k.normalize('NFC').toLowerCase();
dict[normKey] = {
raw: k,
def: dictRaw[k],
rawNorm: k.normalize('NFC').toLowerCase(),
defNorm: dictRaw[k].normalize('NFC').toLowerCase()
};
});
if (!document.getElementById('dict-card-style')) {
mw.util.addCSS(`
.dictionary-container .oo-ui-inputWidget-input, .dictionary-container .oo-ui-buttonElement-button {
background: var(--bg) !important;
color: var(--text) !important;
border: 1px solid var(--border) !important;
height: 36px !important;
}
.oo-ui-inputWidget-input {
border-radius: 0.5rem 0 0 0.5rem !important;
padding-left: 11px !important;
}
.oo-ui-buttonElement-button {
border-radius: 0 0.5rem 0.5rem 0 !important;
padding-top: 6px;
padding-bottom: 6px;
}
.dict-card {
padding: 20px 20px 8px !important;
border:1px solid light-dark(#ccc, #555);
border-radius:0.8rem;
margin: 1em 0 0.5em !important;
background:var(--altbg);
padding-top:
}
.dict-card .term {
font-weight: 600;
font-size: 1.5em;
margin-right: .4em;
padding: 8px;
padding-bottom: 3px;
margin-bottom: -15px;
padding-top: 4px;
}
.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';
}
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);
});
}
function matchLevel(entry, q) {
if (entry.rawNorm === q) return 1;
if (entry.rawNorm.startsWith(q)) return 2;
if (entry.rawNorm.includes(q)) return 3;
if (entry.defNorm.includes(q)) return 4;
return 0;
}
function renderResults(results) {
if (!results.length) {
$result.html('<div class="dict-none">해당 단어가 없습니다.</div>');
return;
}
const html = results.map(r =>
'<div class="term">' + mw.html.escape(r.entry.raw) + '</div>' +
'<div class="def">' + mw.html.escape(r.entry.def) + '</div>'
).join('');
$result.html(html);
}
mw.hook('wikipage.content').add(function ($content) {
$content.find('.dictionary-container').each(function () {
var $box = $(this);
if ($box.children().length) return;
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');
var $result = $('<div class="dict-result"></div>');
$box.append(field, $result);
function run() {
const qRaw = input.getValue();
const q = qRaw.trim().normalize('NFC').toLowerCase();
if (!q) {
$result.empty();
return;
}
const hits = [];
Object.values(dict).forEach(entry => {
const level = matchLevel(entry, q);
if (level) {
hits.push({ entry, level });
}
});
hits.sort((a, b) => {
if (a.level !== b.level) return a.level - b.level;
return a.entry.rawNorm.localeCompare(b.entry.rawNorm);
});
renderResults(hits.slice(0, 11));
}
button.on('click', run);
input.on('enter', run);
input.on('input', run);
});
});
}
);