Coder Social home page Coder Social logo

kevinnayar / temporis Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 0.0 1.93 MB

An intuitive and lightweight approach to constructing timelines. Allows you to capture a history of app state as immutable snapshots and implement undo and redo with predictability and ease.

TypeScript 70.81% CSS 8.84% JavaScript 18.05% HTML 2.30%

temporis's Introduction

⏰ temporis

License: MIT

An intuitive and lightweight approach to constructing timelines. Allows you to capture a history of app state as immutable snapshots and implement undo and redo with predictability and ease.

Under 8KB minified / Under 3KB minified + gzipped.

Undo/Redo

Undo/Redo

Visualizing the history of actions

History

Installation

npm install temporis   or   yarn add temporis

Usage

Vanilla JS

import Temporis from 'temporis';

const temporis = Temporis();

// Push complete state with each action
temporis.pushOne({ id: 'foo', name: 'Foo', color: 'red', size: 28 }); // 1st
temporis.pushOne({ id: 'foo', name: 'Foo', color: 'green', size: 36 }); // 2nd
temporis.pushOne({ id: 'foo', name: 'Foo', color: 'blue', size: 44 }); // 3rd

let currentItem = temporis.getCurrentItem(); // returns 3rd item

temporis.undo();
temporis.undo();

currentItem = temporis.getCurrentItem(); // returns 1st item

temporis.redo();

currentItem = temporis.getCurrentItem(); // returns 2nd item

React

import * as React from 'react';
import Temporis, { useTemporis } from 'temporis'; 

const temporis = Temporis();
temporis.pushOne({ name: 'Hello, World!' });

export default function App() {
  const { items, pushOne, undo, redo } = useTemporis(temporis, initialState);

  return (
    <div className="app">
      <button className="action-btn" onClick={undo}>Undo</button>
      <button className="action-btn" onClick={redo}>Redo</button>

      <input
        className="name-input"
        name="name"
        value={items.name}
        onChange={(e) => pushOne({ name: e.target.value })}
      />

      <div className="preview">{items.name}</div>
    </div>
  );
}

Check out these examples

API

Overview

temporis is sequential and synchronous by design. No promises, no asynchronous behavior, just pure sequential method calls. Each action that is passed into the temporis internal history should be given a full state object at that point in time. These actions construct the timeline of actions and then temporis provides the API to traverse over that timeline.

It's great for apps where users are performing several actions and you want to provide them the ability to undo and redo their actions. It's small, simple, and can be wired in with a few lines of code.

Instantiate

const temporis = Temporis(50);

This is not a class, so don't use the new keyword when instantiating it. The function takes an optional argument limit which is of type number. This represents the number of actions stored in history and it defaults to a 100. There is no upper limit but increasing this will increase the memory footprint of your app, so caveat emptor 😊

Push a single action

temporis.pushOne({ color: 'red', name: 'foo' });

Push a single action into history which becomes the current state.

Push many actions sequentially

const actions = [
  { color: 'red', name: 'foo' },
  { color: 'green', name: 'foo' },
  { color: 'blue', name: 'foo' },
];
temporis.pushMany(actions);

Push many actions into history in sequence, the last of which becomes the current state.

Undo

temporis.undo();

Go back one action and make that the current state. If there is no previous action, do nothing.

Redo

temporis.redo();

Go forward one action and makes that the current state. If there is no future action, do nothing.

Get current item

const currentItem = temporis.getCurrentItem();

Returns the current state. If there is no currrent action in history (i.e, you haven't pushed anything as yet), returns undefined.

Get history

const history = temporis.getHistory();

Returns the history of actions stored within the temporis instance. Mostly for internal purposes, but can be helpful in debugging.

temporis's People

Contributors

kevinnayar avatar

Stargazers

Tjikal Jedy avatar Craig Regnier avatar Caleb Taylor avatar Aaron Dancer 傅子威 avatar Alex Kern avatar Primož Kerin avatar Jose Albizures avatar Casper Engelmann avatar Ali Moezzi avatar Soumyajit Pathak avatar Sai Krishna avatar

Watchers

James Cloos avatar  avatar

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.