Cross-platform Quill Delta rich-text operations in MoonBit
let document = Delta::parse_json("{\"ops\":[{\"insert\":\"Hello world\\n\"}]}")
let change = Delta::new().retain_plain(6).delete(5).insert_plain("MoonBit")
let updated = document.apply(change)
println(updated.stringify())
// {"ops":[{"insert":"Hello MoonBit\n"}]}let history = History::new(document, 100)
history.apply(change)
ignore(history.undo())
ignore(history.redo())fn counter_value(value : Json) -> Double {
match value {
Number(number, ..) => number
_ => 0.0
}
}
let handlers = EmbedHandlers::new()
handlers.register(
"counter",
EmbedHandler::new(
(base, update, _) =>
Json::number(counter_value(base) + counter_value(update)),
(update, _) => Json::number(-counter_value(update)),
(_, other, _) => other,
),
)
let base = Delta::new().insert_embed(
Json::object({ "counter": Json::number(5) }),
empty_attributes(),
)
let increment = Delta::new().retain_embed(
Json::object({ "counter": Json::number(2) }),
empty_attributes(),
)
let changed = base.apply_with_handlers(increment, handlers)moon add xiaobai718-ab/moondeltamoon check --target all --deny-warn
moon build --target all --deny-warn
moon test --target all --deny-warn
moon run --target js cmd/mainpowershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\verify.ps1powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\test-short-path.ps1powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\update-interface.ps1pub(all) struct CursorRange {
index : Int
length : Int
}fn Delta::apply_with_handlers(self : Delta, change : Delta, handlers : EmbedHandlers) -> Delta raise DeltaErrorfn Delta::compose_with_handlers(self : Delta, next : Delta, handlers : EmbedHandlers) -> Delta raise DeltaErrorfn Delta::diff_with_cursor_info(self : Delta, target : Delta, cursor : CursorInfo) -> Delta raise DeltaErrorfn Delta::each_line(self : Delta, visit : (Delta, Json, Int) -> Bool, newline? : String) -> Unit raise DeltaErrorfn Delta::invert_with_handlers(self : Delta, base : Delta, handlers : EmbedHandlers) -> Delta raise DeltaErrorfn Delta::transform_with_handlers(self : Delta, other : Delta, priority : Bool, handlers : EmbedHandlers) -> Delta raise DeltaErrorpub struct EmbedHandler {
compose_value : (Json, Json, Bool) -> Json raise DeltaError
invert_value : (Json, Json) -> Json raise DeltaError
transform_value : (Json, Json, Bool) -> Json raise DeltaError
}fn EmbedHandler::new(compose : (Json, Json, Bool) -> Json raise DeltaError, invert : (Json, Json) -> Json raise DeltaError, transform : (Json, Json, Bool) -> Json raise DeltaError) -> EmbedHandlerfn EmbedHandlers::register(self : EmbedHandlers, embed_type : String, handler : EmbedHandler) -> Unit raise DeltaErrorInstall
Download zipCross-platform Quill Delta rich-text operations in MoonBit