Structured input editing for MoonBit: masks, exact decimal text, selections and caret mapping
moon add YeeHh2004/mooninput@0.1.0import {
"YeeHh2004/mooninput" @input,
}let mask = @input.pattern("### #### ####").unwrap()
let editor = @input.Editor::new(mask).unwrap()
let filled = editor.insert("13800138000").unwrap()
println(filled.display()) // 138 0013 8000
println(filled.value().unwrap()) // 13800138000
let edited = filled.select(4, 8).unwrap().insert("9999").unwrap()
println(edited.display()) // 138 9999 8000
println(filled.display()) // 原快照仍为 138 0013 8000moon run examples/basic --target wasm-gc| 入口 | 行为 |
|---|---|
| pattern("AA-####[/**]") | 数字、字母、可打印字符、分隔符、单个可选后缀 |
| decimal(precision=2, signed=true) | 精确小数字符串、千位分组、符号与中间状态 |
| iso_date() | YYYY-MM-DD 格式与 0001–9999 年公历检查 |
| Editor::select / insert | 选区替换、插入、格式化粘贴 |
| Editor::backspace / delete_forward | 删除选区或相邻可编辑字符 |
| Editor::display / raw / value / status | 显示值、原始值、完整值与状态 |
| Editor::reconcile | 组合输入/自动填充后的归一化与光标恢复 |
git clone https://github.com/YeeHh2004/mooninput.git
cd mooninput
node scripts/build.mjs
node scripts/serve.mjsimport {bindInput} from './adapter.mjs';
const binding = bindInput(document.querySelector('#amount'),
{kind:'decimal', precision:2}, {
onChange(state) { console.log(state.value, state.status); },
onError(error) { console.log(error.message); }
});
// binding.setValue('1234.50'); binding.undo(); binding.redo();
// 卸载时调用 binding.destroy()。node cli/mooninput.mjs --date 2026-10-02
node cli/mooninput.mjs --decimal 9007199254740993.10
node cli/mooninput.mjs --pattern "AA-####" ab1234
node cli/mooninput.mjs --replay examples/events.jsonmoon fmt --check
moon check --target js
moon check --target wasm-gc
moon test --target js
moon test --target wasm-gc
node scripts/build.mjs
node --test tests/*.test.mjs
node scripts/check-consumer.mjs
node scripts/check-examples.mjs
python scripts/check-mooncake.pypub struct Editor {
// private fields
}fn Editor::reconcile(self : Editor, text : String, start : Int, end : Int) -> Result[Editor, InputError]pub struct Mask {
// private fields
}Install
Download zipStructured input editing for MoonBit: masks, exact decimal text, selections and caret mapping