commandz 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 702744b575b0c129c8031ffb874531ccabc57686
4
- data.tar.gz: fcd2c78539f793ce8ca8d2724bcd00a03e234d95
3
+ metadata.gz: 331dea85970246369b095cc99b8660c3f9a93c04
4
+ data.tar.gz: f00a9e210a47a18b3458a6f88ea0d96ba58a331c
5
5
  SHA512:
6
- metadata.gz: 49f382728b5c447021de95d10e8b7ed1a6a6386592dfd1945fda902933962fd7c99149008a4332331235ff9ab03bb499c726952cd0a9dcc06c0b872b8ba0a596
7
- data.tar.gz: c349a981e728cb17c0b8fb885e4bec53d545367c9e1def72e37f833d30a112d4a949d40995df85542731a9f55d6887cbf44d8d992d932e35b3087c9a23adfbb8
6
+ metadata.gz: f30b30d62d585f39e024587ca28bf156493862829757104e3e23700f5ee02e55d3a2719db6449a5c5cc638f547e8654f8578da4ef3fa8962a050688a14e4d3ef
7
+ data.tar.gz: 410ba025d6b17e60f62f9db2a31bda7071e43401a710efa0880ace7f738b16be296bc093b7fa8cf6a9c3beb9c63e1b5a3b5d81e47a8b1f07b8c44473549fce31
data/README.md CHANGED
@@ -13,6 +13,21 @@
13
13
  <a href="http://badge.fury.io/rb/commandz"><img src="https://badge.fury.io/rb/commandz@2x.png" alt="Gem Version" height="18"></a>
14
14
  </p>
15
15
 
16
+ ## Table of contents
17
+ - [API](#api)
18
+ - [CommandZ.execute](#execute)
19
+ - [CommandZ.undo](#undo)
20
+ - [CommandZ.redo](#redo)
21
+ - [CommandZ.status](#status)
22
+ - [CommandZ.onChange](#onchange)
23
+ - [CommandZ.clear](#clear)
24
+ - [CommandZ.keyboardShortcuts](#keyboardshortcuts)
25
+ - [DOM Example](#dom-example)
26
+ - [Setup](#setup)
27
+ - [Rails](#rails)
28
+ - [Other](#other)
29
+ - [Tests](#tests)
30
+
16
31
  ## API
17
32
  #### Glossary
18
33
  ```js
@@ -110,7 +125,7 @@ console.log(CommandZ.index) // => 0
110
125
  ```
111
126
 
112
127
  ### status
113
- Return the current `COMMAND`.
128
+ Return the current status.
114
129
 
115
130
  ```js
116
131
  CommandZ.execute({
@@ -123,11 +138,33 @@ CommandZ.execute({
123
138
  down: function() { console.log('down 2') }
124
139
  }) // => up 2
125
140
 
126
- console.log(CommandZ.status()) // => { up: function() { console.log('up 2') }, down: function() { console.log('down 2') } }
141
+ console.log(CommandZ.status()) // => { canUndo: true, canRedo: false }
127
142
  console.log(CommandZ.commands.length) // => 2
128
143
  console.log(CommandZ.index) // => 1
129
144
  ```
130
145
 
146
+ ### onChange
147
+ Register a callback that will be called with the `status` every time there’s a change to the history.
148
+
149
+ ```js
150
+ CommandZ.onChange(function(status) {
151
+ console.log(status)
152
+ })
153
+
154
+ CommandZ.execute({
155
+ up: function() { 'up 1' },
156
+ down: function() { 'down 1' }
157
+ }) // => { canUndo: true, canRedo: false }
158
+
159
+ CommandZ.execute({
160
+ up: function() { 'up 2' },
161
+ down: function() { 'down 2' }
162
+ }) // => { canUndo: true, canRedo: false }
163
+
164
+ CommandZ.undo() // => { canUndo: true, canRedo: true }
165
+ CommandZ.undo() // => { canUndo: false, canRedo: true }
166
+ ```
167
+
131
168
  ### clear
132
169
  Clear history.
133
170
 
@@ -149,6 +186,15 @@ console.log(CommandZ.commands.length) // => 0
149
186
  console.log(CommandZ.index) // => -1
150
187
  ```
151
188
 
189
+ ### keyboardShortcuts
190
+ Enable or disable `CMD+Z` & `CMD+SHIFT+Z` keyboard shortcuts. These shortcuts are enabled by default.<br>
191
+ Will only `undo()` & `redo()` if the current selected element is not an input so that it doesn’t prevent your OS default behavior.
192
+
193
+ ```js
194
+ CommandZ.keyboardShortcuts(true) // default
195
+ CommandZ.keyboardShortcuts(false)
196
+ ```
197
+
152
198
  ## DOM Example
153
199
  ```js
154
200
  // This example requires jQuery or Zepto
@@ -1,10 +1,10 @@
1
1
  /*
2
- * CommandZ v0.0.1
2
+ * CommandZ v0.0.2
3
3
  * https://github.com/EtienneLem/commandz
4
4
  *
5
5
  * Copyright 2013, Etienne Lemay http://heliom.ca
6
6
  * Released under the MIT license
7
7
  *
8
- * Date: 2013-08-28 23:28:40 -0400
8
+ * Date: 2013-08-29 03:07:13 -0400
9
9
  */
10
- (function(){var t;t=function(){function t(){this.VERSION="0.0.1",this.clear()}return t.prototype.clear=function(){return this.commands=[],this.index=-1},t.prototype.execute=function(t){var n;return this.up(t),this.index<this.commands.length-1&&(n=this.commands.length-this.index-1,this.commands.splice(-n)),this.commands.push(t),this.index=this.commands.length-1},t.prototype.undo=function(t){var n,i;if(null==t&&(t=1),!(this.index<0))for(n=i=1;t>=1?t>=i:i>=t;n=t>=1?++i:--i){if(!this.commands[this.index])return;this.down(this.commands[this.index]),this.index--}},t.prototype.redo=function(t){var n,i;if(null==t&&(t=1),!(this.index>=this.commands.length-1))for(n=i=1;t>=1?t>=i:i>=t;n=t>=1?++i:--i){if(!this.commands[this.index+1])return;this.index++,this.up(this.commands[this.index])}},t.prototype.exec=function(t,n){var i,s,e,o;if(!(n instanceof Array))return n[t]();for(o=[],s=0,e=n.length;e>s;s++)i=n[s],o.push(i[t]());return o},t.prototype.up=function(t){return this.exec("up",t)},t.prototype.down=function(t){return this.exec("down",t)},t.prototype.status=function(){return this.commands[this.index]},t}(),this.CommandZ=new t}).call(this);
10
+ (function(){var t,n=function(t,n){return function(){return t.apply(n,arguments)}};t=function(){function t(){this.handleKeyboard=n(this.handleKeyboard,this),this.VERSION="0.0.2",this.changeCallback=null,this.clear(),this.keyboardShortcuts(!0)}return t.prototype.clear=function(){return this.commands=[],this.index=-1},t.prototype.keyboardShortcuts=function(t){var n;return null==t&&(t=!0),n=t?"addEventListener":"removeEventListener",document[n]("keypress",this.handleKeyboard)},t.prototype.handleKeyboard=function(t){return"INPUT"!==document.activeElement.nodeName&&122===t.keyCode&&t.metaKey===!0?(t.preventDefault(),t.shiftKey?this.redo():this.undo()):void 0},t.prototype.execute=function(t){var n;return this.up(t),this.index<this.commands.length-1&&(n=this.commands.length-this.index-1,this.commands.splice(-n)),this.commands.push(t),this.index=this.commands.length-1},t.prototype.undo=function(t){var n,e;if(null==t&&(t=1),this.status().canUndo)for(n=e=1;t>=1?t>=e:e>=t;n=t>=1?++e:--e){if(!this.commands[this.index])return;this.down(this.commands[this.index]),this.index--,this.handleChange()}},t.prototype.redo=function(t){var n,e;if(null==t&&(t=1),this.status().canRedo)for(n=e=1;t>=1?t>=e:e>=t;n=t>=1?++e:--e){if(!this.commands[this.index+1])return;this.index++,this.up(this.commands[this.index]),this.handleChange()}},t.prototype.exec=function(t,n){var e,i,o,s;if(!(n instanceof Array))return n[t]();for(s=[],i=0,o=n.length;o>i;i++)e=n[i],s.push(e[t]());return s},t.prototype.up=function(t){return this.exec("up",t)},t.prototype.down=function(t){return this.exec("down",t)},t.prototype.onChange=function(t){return this.changeCallback=t,this.handleChange()},t.prototype.handleChange=function(){return this.changeCallback?this.changeCallback(this.status()):void 0},t.prototype.status=function(){return{canUndo:this.index>-1,canRedo:this.index<this.commands.length-1}},t}(),this.CommandZ=new t}).call(this);
@@ -1,13 +1,27 @@
1
1
  class CommandZ
2
2
 
3
3
  constructor: ->
4
- @VERSION = '0.0.1'
4
+ @VERSION = '0.0.2'
5
+ @changeCallback = null
6
+
5
7
  this.clear()
6
-
8
+ this.keyboardShortcuts(true)
9
+
7
10
  clear: ->
8
11
  @commands = []
9
12
  @index = -1
10
13
 
14
+ keyboardShortcuts: (enable=true) ->
15
+ addOrRemove = if enable then 'addEventListener' else 'removeEventListener'
16
+ document[addOrRemove]('keypress', this.handleKeyboard)
17
+
18
+ handleKeyboard: (e) =>
19
+ return if document.activeElement.nodeName is 'INPUT'
20
+ return unless e.keyCode is 122 and e.metaKey is true
21
+
22
+ e.preventDefault()
23
+ if e.shiftKey then this.redo() else this.undo()
24
+
11
25
  execute: (command) ->
12
26
  this.up(command)
13
27
 
@@ -21,19 +35,27 @@ class CommandZ
21
35
  @index = @commands.length - 1
22
36
 
23
37
  undo: (times=1) ->
24
- return if @index < 0
38
+ return unless this.status().canUndo
39
+
25
40
  for i in [1..times]
26
41
  return unless @commands[@index]
42
+
27
43
  this.down(@commands[@index])
28
44
  @index--
29
45
 
46
+ this.handleChange()
47
+
30
48
  redo: (times=1) ->
31
- return if @index >= @commands.length - 1
49
+ return unless this.status().canRedo
50
+
32
51
  for i in [1..times]
33
52
  return unless @commands[@index + 1]
53
+
34
54
  @index++
35
55
  this.up(@commands[@index])
36
56
 
57
+ this.handleChange()
58
+
37
59
  # Execute up/down on a command
38
60
  # command can be a group of commands or a single command
39
61
  exec: (action, command) ->
@@ -43,8 +65,19 @@ class CommandZ
43
65
  up: (command) -> this.exec('up', command)
44
66
  down: (command) -> this.exec('down', command)
45
67
 
46
- # Return current command
47
- status: -> @commands[@index]
68
+ # Register onChange callback
69
+ onChange: (callback) ->
70
+ @changeCallback = callback
71
+ this.handleChange()
72
+
73
+ handleChange: ->
74
+ return unless @changeCallback
75
+ @changeCallback(this.status())
76
+
77
+ # Return current status
78
+ status: ->
79
+ canUndo: @index > -1
80
+ canRedo: @index < @commands.length - 1
48
81
 
49
82
  # Singleton
50
83
  @CommandZ = new CommandZ
@@ -1,3 +1,3 @@
1
1
  module CommandZ
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -36,9 +36,11 @@ describe 'CommandZ', ->
36
36
  expect(CommandZ.commands.length).toBe(10)
37
37
  expect(CommandZ.index).toBe(9)
38
38
 
39
- it 'returns current command', ->
40
- currentCommand = CommandZ.status()
41
- expect(currentCommand.up()).toBe(9)
39
+ it 'returns current status', ->
40
+ status = CommandZ.status()
41
+
42
+ expect(status.canUndo).toBe(true)
43
+ expect(status.canRedo).toBe(false)
42
44
 
43
45
  it 'overwrites upcoming commands', ->
44
46
  CommandZ.undo(3)
@@ -63,6 +65,19 @@ describe 'CommandZ', ->
63
65
 
64
66
  expect(CommandZ.index).toBe(0)
65
67
 
68
+ it 'registers onChange callback', ->
69
+ CommandZ.clear()
70
+ [0..2].forEach (i) -> CommandZ.execute({up: (-> i), down: (-> i)})
71
+
72
+ onChangeCallback = jasmine.createSpy('onChangeCallback')
73
+ CommandZ.onChange (status) -> onChangeCallback('test')
74
+
75
+ CommandZ.undo(3)
76
+ CommandZ.redo(2)
77
+
78
+ expect(onChangeCallback.calls.length).toBe(6)
79
+ CommandZ.onChange(null)
80
+
66
81
  describe 'integration', ->
67
82
  $container = null
68
83
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commandz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Etienne Lemay