-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
76 lines (59 loc) · 1.67 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var events = require('dom-events')
var tools = require('./tools')
var History = require('./history')
var deepcopy = require('deepcopy')
function passEvent(name, ev) {
if (this.tool && this.paths.editable) {
this.tool[name](ev, this.paths)
}
}
function PathClient(paths, opt) {
if (!(this instanceof PathClient))
return new PathClient(paths, opt)
opt = opt||{}
this.history = opt.history || History(40)
this.paths = paths
this._tool = null
paths.on('finish-change', function(e) {
this._pushHistory(e)
}.bind(this))
if (opt.element) {
;['mousedown', 'mouseup', 'mousemove'].forEach(function(name) {
events.on(opt.element, name, passEvent.bind(this, name))
}.bind(this))
}
this.resetHistory()
}
PathClient.prototype.clear = function() {
this.paths.emit('clear-path')
}
PathClient.prototype._pushHistory = function(e) {
var type = ''
if (e)
type = e.type
var state = deepcopy( this.paths.state() )
this.history.push( { state: state, type: type } )
// console.log(this.history.toString())
}
PathClient.prototype.resetHistory = function() {
this.history.reset()
this._pushHistory({ type: 'start' })
}
PathClient.prototype.undo = function() {
var info = this.history.undo()
this.paths.state(info.state)
}
PathClient.prototype.redo = function() {
var info = this.history.redo()
this.paths.state(info.state)
}
Object.defineProperty(PathClient.prototype, "tool", {
get: function() {
return this._tool
},
set: function(tool) {
this._tool = tool
this.tool.setup(this.paths)
}
})
module.exports = PathClient