GithubHelp home page GithubHelp logo

atom.undo's People

Contributors

kurtmilam avatar polytypic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kurtmilam sdawood

atom.undo's Issues

Mark this library as obsolete and introduce new Partial Lenses History library

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))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.