oimp
Version:
A CLI tool for generating OI problem and packages
43 lines (36 loc) • 1.18 kB
JavaScript
const ta = document.getElementById('md-textarea');
const find = document.getElementById('find');
const repl = document.getElementById('replace');
const icase= document.getElementById('icase');
const word = document.getElementById('word');
const stat = document.getElementById('status');
// 初始化
const finder = new TextareaSearchReplace(ta);
// 公共选项
function opts() {
return {
caseSensitive : icase.checked,
wholeWord : word.checked
};
}
// 查找
function doFind() {
const ok = finder.find(find.value, opts());
stat.textContent = ok ? '' : '未找到';
}
// 替换
function doReplace() {
finder.replace(repl.value, opts());
doFind(); // 继续定位下一个
}
// 全部替换
function doReplaceAll() {
const n = finder.replaceAll(repl.value, opts());
stat.textContent = `共替换 ${n} 处`;
}
// 事件绑定
document.getElementById('btn-find').onclick = doFind;
document.getElementById('btn-replace').onclick = doReplace;
document.getElementById('btn-replace-all').onclick = doReplaceAll;
// 回车快速查找
find.addEventListener('keydown', e => e.key === 'Enter' && (e.preventDefault(), doFind()));