<html>
<head>
<meta name="description" content="Undo/Redo using Mutation Observers" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">
<title>Undo/Redo using Mutation Observers</title>
<script src="https://rawgit.com/jzaefferer/undo/master/undo.js"></script>
</head>
<body>
<button class="undo btn" href="#">Undo</button>
<button class="redo btn" href="#">Redo</button>
<section id="todoapp">
<header id="header">
<h1>todos</h1>
<form id="addTodos">
<input id="new-todo" placeholder="What needs to be done?" autofocus>
</form>
</header>
<section id="main">
<input id="toggle-all" type="checkbox">
<ul id="todo-list">
<template id="my-template">
<li>
<div class="view">
<input class="toggle" type="checkbox">
<label class="item" contenteditable="true"></label>
</div>
</li>
</template>
</ul>
</section>
</body>
</html>
html,
body {
margin: 0;
padding: 0;
}
button {
margin: 0;
padding: 0;
border: 0;
background: none;
font-size: 100%;
vertical-align: baseline;
font-family: inherit;
font-weight: inherit;
color: inherit;
appearance: none;
appearance: none;
appearance: none;
font-smoothing: antialiased;
font-smoothing: antialiased;
font-smoothing: antialiased;
font-smoothing: antialiased;
}
body {
font: 14px 'Helvetica Neue', Helvetica, Arial, sans-serif;
line-height: 1.4em;
background: #f5f5f5;
color: #4d4d4d;
width: 550px;
margin: 0 auto;
font-smoothing: antialiased;
font-smoothing: antialiased;
font-smoothing: antialiased;
font-smoothing: antialiased;
font-weight: 300;
padding-top: 40px;
}
button,
input[type="checkbox"] {
outline: none;
}
.hidden {
display: none;
}
#todoapp {
background: #fff;
margin: 130px 0 40px 0;
position: relative;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2),
0 25px 50px 0 rgba(0, 0, 0, 0.1);
}
#todoapp input::input-placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
#todoapp input::placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
#todoapp input::input-placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
#todoapp h1 {
position: absolute;
top: -110px;
width: 100%;
font-size: 50px;
font-weight: 300;
text-align: center;
text-decoration: underline;
text-transform: uppercase;
color: rgba(255, 255, 255, 0.2);
text-shadow: -1px -1px rgba(0, 0, 0, 0.2);
text-rendering: optimizeLegibility;
text-rendering: optimizeLegibility;
text-rendering: optimizeLegibility;
text-rendering: optimizeLegibility;
}
#new-todo,
.edit {
position: relative;
margin: 0;
width: 100%;
font-size: 24px;
font-family: inherit;
font-weight: inherit;
line-height: 1.4em;
border: 0;
outline: none;
color: inherit;
padding: 6px;
border: 1px solid #999;
box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2);
box-sizing: border-box;
box-sizing: border-box;
box-sizing: border-box;
font-smoothing: antialiased;
font-smoothing: antialiased;
font-smoothing: antialiased;
font-smoothing: antialiased;
}
#new-todo {
padding: 16px 16px 16px 60px;
border: none;
background: rgba(0, 0, 0, 0.003);
box-shadow: inset 0 -2px 1px rgba(0,0,0,0.03);
}
#main {
position: relative;
z-index: 2;
border-top: 1px solid #e6e6e6;
}
label[for='toggle-all'] {
display: none;
}
#toggle-all {
position: absolute;
top: -55px;
left: -15px;
width: 60px;
height: 34px;
text-align: center;
border: none; /* Mobile Safari */
}
#toggle-all:before {
content: '❯';
font-size: 22px;
color: #e6e6e6;
padding: 10px 27px 10px 27px;
}
#toggle-all:checked:before {
color: #737373;
}
#todo-list {
margin: 0;
padding: 0;
list-style: none;
}
#todo-list li {
position: relative;
font-size: 24px;
border-bottom: 1px solid #ededed;
}
#todo-list li:last-child {
border-bottom: none;
}
#todo-list li.editing {
border-bottom: none;
padding: 0;
}
#todo-list li.editing .edit {
display: block;
width: 506px;
padding: 13px 17px 12px 17px;
margin: 0 0 0 43px;
}
#todo-list li.editing .view {
display: none;
}
#todo-list li .toggle {
text-align: center;
width: 40px;
/* auto, since non-WebKit browsers doesn't support input styling */
height: auto;
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
border: none; /* Mobile Safari */
appearance: none;
appearance: none;
appearance: none;
}
#todo-list li .toggle:after {
content: '✔';
line-height: 43px; /* 40 + a couple of pixels visual adjustment */
font-size: 20px;
color: #d9d9d9;
text-shadow: 0 -1px 0 #bfbfbf;
}
#todo-list li .toggle:checked:after {
color: #aedad2;
text-shadow: 0 1px 0 #abd1ca;
bottom: 1px;
position: relative;
opacity: 0.7;
}
#todo-list li label {
white-space: pre;
word-break: break-word;
padding: 15px 60px 15px 15px;
margin-left: 45px;
display: block;
line-height: 1.2;
transition: color 0.4s;
transition: color 0.4s;
}
#todo-list li.completed label {
color: #d9d9d9;
text-decoration: line-through;
}
#todo-list li .destroy {
display: none;
position: absolute;
top: 0;
right: 10px;
bottom: 0;
width: 40px;
height: 40px;
margin: auto 0;
font-size: 30px;
color: #cc9a9a;
margin-bottom: 11px;
transition: color 0.2s ease-out;
transition: colo 0.2s ease-out;
}
#todo-list li .destroy:hover {
color: #af5b5e;
}
#todo-list li .destroy:after {
content: '×';
}
#todo-list li:hover .destroy {
display: block;
}
#todo-list li .edit {
display: none;
}
#todo-list li.editing:last-child {
margin-bottom: -1px;
}
#footer {
color: #777;
padding: 10px 15px;
height: 20px;
text-align: center;
border-top: 1px solid #e6e6e6;
}
#footer:before {
content: '';
position: absolute;
right: 0;
bottom: 0;
left: 0;
height: 50px;
overflow: hidden;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2),
0 8px 0 -3px #f6f6f6,
0 9px 1px -3px rgba(0, 0, 0, 0.2),
0 16px 0 -6px #f6f6f6,
0 17px 2px -6px rgba(0, 0, 0, 0.2);
}
#todo-count {
float: left;
text-align: left;
}
#todo-count strong {
font-weight: 300;
}
#filters {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
right: 0;
left: 0;
}
#filters li {
display: inline;
}
#filters li a {
color: inherit;
margin: 3px;
text-decoration: none;
}
#filters li a.selected {
text-shadow: 0 0 20px rgba(175, 47, 47, 0.7);
}
#clear-completed,
html #clear-completed:active {
float: right;
position: relative;
line-height: 20px;
text-decoration: none;
cursor: pointer;
visibility: hidden;
position: relative;
}
#clear-completed::after {
visibility: visible;
content: 'Clear completed';
position: absolute;
right: 0;
}
#filters li a,
#clear-completed {
transition: text-shadow 0.2s ease-in-out;
transition: text-shadow 0.2s ease-in-out;
}
#filters li a:hover,
#clear-completed:hover {
text-shadow: 0 0 20px rgba(175, 47, 47, 0.3);
}
#info {
margin: 65px auto 0;
color: #ccc;
font-size: 10px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
text-align: center;
}
#info a {
color: inherit;
text-decoration: none;
font-weight: 400;
}
#info a:hover {
text-decoration: underline;
}
.btn {
text-decoration: underline;
font-weight: bold;
}
.btn[disabled] {
color: #ccc;
}
/*
Hack to remove background from Mobile Safari.
Can't use it globally since it destroys checkboxes in Firefox and Opera
*/
@media screen and (min-device-pixel-ratio:0) {
#toggle-all,
#todo-list li .toggle {
background: none;
}
#todo-list li .toggle {
height: 40px;
}
#toggle-all {
transform: rotate(90deg);
transform: rotate(90deg);
appearance: none;
appearance: none;
}
}
/*@addyosmani*/
document.addEventListener("DOMContentLoaded", function () {
var x = document.querySelector.bind(document);
var el = x("#todo-list");
var startValue = el.innerHTML;
var blocked = false;
var undo = x(".undo");
var redo = x(".redo");
var input = x("#new-todo");
var form = x("#addTodos");
var stack = new Undo.Stack();
var list = [];
var lastItem = null;
var newValue = "";
var t = x("#my-template");
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function (mutations) {
if(blocked){
blocked = false;
return;
}
newValue = el.innerHTML;
stack.execute(new EditCommand(el, startValue, newValue));
startValue = newValue;
});
observer.observe(el, {
attributes: true,
childList: true,
characterData: true,
characterDataOldValue: true,
subtree: true
});
var EditCommand = Undo.Command.extend({
constructor: function (textarea, oldValue, newValue) {
this.el = el;
this.oldValue = oldValue;
this.newValue = newValue;
},
execute: function () {},
undo: function () {
blocked = true;
lastItem = list[list.length-1];
list.pop();
this.el.innerHTML = this.oldValue;
},
redo: function () {
blocked = true;
if(lastItem){
list.push(lastItem);
}
this.el.innerHTML = this.newValue;
}
});
function todosUI(){
el.innerHTML = "";
for (var i=0; i < list.length; i++){
var todo = list[i];
var tmpl = t.content.cloneNode(true);
tmpl.querySelector('.item').innerHTML = todo.label;
el.appendChild(tmpl);
}
}
function stackUI() {
redo.disabled = !stack.canRedo();
undo.disabled = !stack.canUndo();
}
stackUI();
undo.addEventListener("click", function () {
stack.undo();
});
redo.addEventListener("click", function () {
stack.redo();
});
stack.changed = function () {
stackUI();
};
form.onsubmit = function (e) {
e.preventDefault();
list.push({ label: input.value });
todosUI();
input.value = '';
};
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |