json diff patch repatch, undo / redo
npm install oj-diff-patchtypescript
import { DiffPatch } from "oj-diff-patch"
`
$3
`typescript
const dp = new DiffPatch()
`
$3
`typescript
let state = dp.add({ packages: [{ name: "oj-diff-patch", version: "1.0.0" }] })
`
`typescript
state = dp.add({ packages: [{ name: "oj-diff-patch", version: "1.0.1" }, { name: "oj-store", version: "1.0.0" }] })
`
$3
`typescript
undoBtn.classList.toggle("is-disabled", !dp.canUndo())
redoBtn.classList.toggle("is-disabled", !dp.canRedo())
`
`typescript
state = dp.undo()
// state is { packages: [{ name: "oj-diff-patch", version: "1.0.0" }] }
`
`typescript
state = dp.redo()
// state is { packages: [{ name: "oj-diff-patch", version: "1.0.1" }, { name: "oj-store", version: "1.0.0" }] }
`
$3
Removes all recoded patches, undo/redo wont be possible anymore until a new change is added.
It won't change the current state.
`typescript
dp.reset()
`
$3
Listen to changes, get the deltas, from and to states.
`typescript
state = dp.listen = (deltas, from, to) => {
storage.set("deltas", deltas)
}
`
$3
Apply delta array.
This will override the current state.
`typescript
state = dp.load(storage.get("deltas")) // [{"packages":[{"name":"oj-diff-patch","version":"1.0.0"}]},{"packages":{"0":{"version":["1.0.0","1.0.1"]},"1":[{"name":"oj-store","version":"1.0.0"}],"_t":"a"}}]
// state is { packages: [{ name: "oj-diff-patch", version: "1.0.1" }, { name: "oj-store", version: "1.0.0" }] }
``