Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Undo Catcher</h1>
<p>Enter text in the box below. <span>Use any undo / redo command</span> with the content using whatever method you like to trigger the event. You'll need to reset the checkbox after the event has fired.</p>
<div class="container" contentEditable></div>
<span><label for="event">Event Triggered</label> <input id="event" type="checkbox"/></span>

              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Reem+Kufi');

*, *:before, *:after {
	box-sizing: border-box;
}

body {
	margin:48px;
	font-family: 'Reem Kufi';
	font-size: 1.2em;
	letter-spacing: 1px;
	background-color: #00bcd4;
	color: #fff;
}

span {
	background-color: #80deea;
	display: inline-block;
	padding: 0 8px;
}

.container {
	display: inline-block;
	width: 100%;
	border:1px solid #0097a7;
	background-color: #fff;
	color: #5677fc;
	white-space:pre-line;
	min-height: 200px;
	margin-bottom: 24px;
	padding: 8px;
}

              
            
!

JS

              
                function UndoListener(options) {
	if (!options.el instanceof HTMLElement) return;
	this.el = options.el;
	this.callback = options.callback || function() {};
	this.init();
}

UndoListener.prototype = {
	constructor: UndoListener,
	addListeners: function() {
		this.listeners = {
			keydown: e => (this.expectedChange = this.eventChecker(e)),
			cut: e => (this.expectedChange = true),
			paste: e => (this.expectedChange = true)
		};
		return this.listen(true);
	},
	addObserver: function() {
		this.observer = new MutationObserver(mt => {
			console.log("expectedChange", this.expectedChange);
			if (!this.expectedChange) {
				this.expectedChange = true;
				this.observer.disconnect();
				this.callback.call(this.el, {
					original: [...mt].shift().oldValue,
					current: this.el.innerText
				});
				this.addObserver();
			}
			this.expectedChange = false;
		});

		this.observer.observe(this.el, {
			characterData: true,
			subtree: true,
			characterDataOldValue: true
		});
		return this;
	},
	destroy: function() {
		this.observer.disconnect();
		return this.listen(false);
	},
	eventChecker: function(event) {
		return !(~['z', 'y'].indexOf(event.key) && (event.ctrlKey || event.metaKey));
	},
	init: function() {
		this.expectedChange = false;
		this.addListeners().addObserver();
		return this;
	},
	listen: function(bool) {
		Object.keys(this.listeners).forEach(type =>
			this.el[bool ? 'addEventListener' : 'removeEventListener'](type, this.listeners[type])
		);
		return this;
	}
};

// just a reference to the checkbox; not needed for the contstructor
var checkbox = document.getElementById("event");

// instantiate the listener on the element with a callback
var catcher = new UndoListener({
	// element to listen to
	el: document.querySelector(".container"),
	// callback to fire once undo/redo has fired, sends an object with original and current values
	callback: function(val) {
		console.log("callback fired", val);
		checkbox.checked = true;
	}
});

              
            
!
999px

Console