calmm-js / atom.undo Goto Github PK
View Code? Open in Web Editor NEWReactive variables with Undo-Redo
License: MIT License
Reactive variables with Undo-Redo
License: MIT License
This library should be marked obsolete and replaced with a new Partial Lenses History library that should allow for far more flexible handling of history.
Below is untested draft code for the new undo-redo (or history) manipulation approach. The idea is that you store "history" somewhere. This module provides operations on the plain history data. I was thinking of publishing this as Partial Lenses History library. (I'm still thinking about some aspects of the below API, but it should be workable.)
import {acyclicEqualsU, curry} from "infestines"
import {lens} from "partial.lenses"
//
const viewGetter = ({index, values}) => values[index]
const viewSetter = curry((toAct, next, {time, index, values}) => {
const prev = values[index]
const {delta, time: t} = toAct({time, prev, next})
if (void 0 === delta)
return {time: t, index, values}
const i = index + delta
const nextValues = values.slice(0, i)
nextValues.push(next)
return {time: t, index: i, values: nextValues}
})
//
export const init = ({time = Date.now(), value}) => ({
time,
index: 0,
values: [value]
})
export const initial = history => history.values[0]
export const count = history => history.values.length
export const index = history => history.index
export const setIndex = curry((index, {time, values}) =>
({time, index: Math.max(0, Math.min(index, values.length-1)), values}))
export const undoCount = index
export const redoCount = history => count(history) - 1 - index(history)
export const shift = curry((delta, history) =>
setIndex(index(history) + delta, history))
export const undo = shift(-1)
export const redo = shift(+1)
export const undoForget = ({time, index, values}) =>
({time, index: 0, values: values.slice(index)})
export const redoForget = ({time, index, values}) =>
({time, index, values: values.slice(0, index + 1)})
export const Push = time => ({delta: 1, time})
export const Replace = time => ({delta: 0, time})
export const Ignore = time => ({time})
export const ignoreEquals = ({time, prev, next}) =>
acyclicEqualsU(prev, next) ? Ignore(time) : Push(time)
export const replaceYounger = curry((ms, {time, prev, next}) => {
const now = Date.now()
if (acyclicEqualsU(prev, next))
return Ignore(now)
return now - time < ms ? Replace(now) : Push(now)
})
export const viewAnd = toAct => lens(viewGetter, viewSetter(toAct))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.