-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
533 lines (460 loc) · 14.4 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
var hyperglue = require('hyperglue')
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var EventStream = require('./event_stream')
var copy = require('shallow-copy')
var xtend = require('xtend')
var fs = require('fs')
var insertCss = require('insert-css')
var Color = require('color')
var path = require('path')
var validEvent = EventStream.validEvent
var idSequence = 0
module.exports = Svg
inherits(Svg, EventEmitter)
function Svg (opt) {
if (!(this instanceof Svg)) return new Svg(opt)
this.idSequence = ++idSequence
insertCss(fs.readFileSync(path.join(__dirname, '/style.css'), 'utf-8'))
this.html = fs.readFileSync(path.join(__dirname, '/index.html'), 'utf-8')
this.DEFAULT_STYLE = {
fill: 'rgba(0, 0, 0, 0)',
stroke: 'rgba(0, 0, 0, 1)'
}
this.opt = opt || {}
this._resetStyle()
this.fonts = {
'default': {
'font-family': 'Arial',
'letter-spacing': 0,
'font-weight': 'normal'
}
}
this.fonts.Normal = xtend(this.fonts.default, {
'font-size': 11,
'letter-spacing': 0
})
this.fonts.Heading = xtend(this.fonts.default, {
'font-size': 22,
'font-weight': 'bold',
'letter-spacing': 0
})
this.font = copy(this.fonts.default)
this.deleted = []
this.listeners = {
'mousedown': this._down.bind(this),
'touchstart': this._down.bind(this),
'mouseup': this._up.bind(this),
'touchend': this._up.bind(this),
'mousemove': this._move.bind(this),
'touchmove': this._move.bind(this),
'anchorselect': this._anchorSelect.bind(this)
}
this.eventStream = new EventStream()
}
Svg.prototype.appendTo = function appendTo (el) {
if (this.el) return this
this.el = el.appendChild(hyperglue(this.html, {
'#grid': {
'id': 'grid-' + this.idSequence
},
'.grid': {
'fill': 'url(#grid-' + this.idSequence + ')'
},
'#smallGrid': {
'id': 'smallGrid-' + this.idSequence
},
'.smallGrid': {
'fill': 'url(#smallGrid-' + this.idSequence + ')'
}
})).children[0]
this.controls = {}
this._eventListeners('addEventListener')
return this
}
Svg.prototype.registerControl = function (name, instance) {
var self = this
if (!instance || typeof instance.emit === 'undefined') return
self.controls[name] = instance
var control = self.controls[name]
if (!control.on) return
control.on('createEvent', createEvent)
control.on('updateEvent', updateEvent)
control.on('deletePath', deletePath)
control.on('closeToPath', self._pathSelected.bind(self))
function createEvent (event) {
self.eventStream.push(event)
self._redraw([event])
}
function updateEvent (event) {
self._redraw([event])
}
function deletePath (opt) {
self.eventStream.push({
type: 'delete',
target: opt.target,
args: {},
path: opt.path
})
self._redraw(self.eventStream.events.slice(-1))
}
}
Svg.prototype.remove = function remove () {
if (!this.el) return
var el = this.el.parentNode
this._eventListeners('removeEventListener')
el.parentNode.removeChild(el)
this.el = null
}
Svg.prototype._triggerEventsChanged = function trigger () {
if (trigger.timer) {
clearTimeout(trigger.timer)
trigger.timer = null
}
trigger.timer = setTimeout(function () {
this.emit('eventStream', this.eventStream.toJSON())
}.bind(this), 300)
}
Svg.prototype.setEvents = function setEvents (events) {
this._resetStyle()
while (this.eventStream.events.length) this._deleteEvent()
this.eventStream.events = events
this._redraw()
this._init(events)
}
Svg.prototype._deleteEvent = function deleteEvent () {
var event = this.eventStream.events.pop()
if (event.el && event.el.parentNode) {
event.el.parentNode.removeChild(event.el)
event.el = null
}
}
Svg.prototype._resetEvents = function resetEvents () {
this._resetStyle()
this._redraw()
this._init(this.eventStream.events)
}
Svg.prototype.setControl = function setControl (control) {
if (control === 'grid') return this._grid()
if (control === 'undo') return this.undo()
if (control === 'redo') return this.redo()
this.el.setAttribute('data-control', control)
this.control = this.controls[control]
this.emit('changeControl', control)
if (!this.control) throw new Error('control ' + control + ' not supported')
if (this.opt.anchor) {
if (!(this.opt.anchor.exclude || /undo|redo|grid/).test(control)) this._removeAnchorElements()
if ((this.opt.anchor.include || /rubber|move|text/).test(control)) this._addAnchorElements(control)
}
}
Svg.prototype._init = function init (events) {
var self = this
var color, style, controlName
this.silent = true
events.forEach(setDefaults)
this.emit('changeStyle', style || self.style)
this.emit('changeColor', color || self.DEFAULT_STYLE.stroke)
if (controlName) self.setControl(controlName)
this.silent = false
function setDefaults (event) {
if (event.type === 'style') {
style = event.args
if (event.args.stroke) {
color = event.args.stroke
}
return
}
controls().forEach(setControl)
function setControl (opt) {
var control = opt.control
if (control.matchEvent && control.matchEvent(event)) {
controlName = opt.controlName
}
}
}
function controls () {
return Object.keys(self.controls).map(function mapControl (name) {
return {
controlName: name,
control: self.controls[name]
}
})
}
}
Svg.prototype._eventListeners = function (method) {
var self = this
Object.keys(this.listeners).forEach(function event (name) {
self.el[method](name, self.listeners[name])
})
}
Svg.prototype._down = function down (e) {
this.emit('drawing')
if (this.control && this.control.ondown) this.control.ondown(e)
}
Svg.prototype._move = function move (e) {
if (this.control && this.control.onmove) this.control.onmove(e)
}
Svg.prototype._up = function up (e) {
if (this.control && this.control.onup) this.control.onup(e)
}
Svg.prototype._pathSelected = function pathSelected (e) {
this.emit('drawing')
if (!this.control || !this.control.onpathselected) return
var target = e.target
var id = +target.getAttribute('data-id')
var path = this.eventStream.events.filter(findId)
if (!path.length) return
return this.control.onpathselected({path: path[0], e: e})
function findId (event) {
return event.id === id
}
}
Svg.prototype._resetStyle = function resetStyle () {
this.style = copy(this.DEFAULT_STYLE)
}
Svg.prototype._redraw = function redraw (events) {
if (!this.el) return
var self = this
events = events || self.eventStream.events
redraw.id = redraw.id || 0
self._triggerEventsChanged()
events.forEach(create)
this._addAnchorElements()
function create (event) {
if (event.type === 'style') {
self.style = xtend(self.DEFAULT_STYLE, event.args)
return
}
var el = event.el || (function () {
if (event.target) return event.target
var el = self.el.appendChild(createElement(event.type))
event.el = el
el.setAttribute('data-id', ++redraw.id)
event.id = redraw.id
el.addEventListener('mousedown', self._pathSelected.bind(self))
el.addEventListener('touchstart', self._pathSelected.bind(self))
return el
})()
for (var key in event.args) {
if (key !== 'value') {
el.setAttribute(key, event.args[key])
}
}
if (event.type !== 'move') {
if (event.args.value) {
el.innerHTML = ''
if (event.layout && event.layout.style && event.layout.style.width) {
var match = new RegExp('.{0,' + (event.layout.style.width / 5 | 0) + '}', 'g')
event.args.value.match(match).forEach(function wrap (text, i) {
if (i === 0) {
el.textContent = text
} else {
var tspan = createElement('tspan')
tspan.setAttribute('dy', '1.1em')
tspan.setAttribute('x', event.args.x)
tspan.textContent = text
el.appendChild(tspan)
}
})
} else {
el.textContent = event.args.value
}
}
}
if (event.type === 'delete') {
el.style.display = 'none'
} else {
if (!event.target) {
var style = copy(self.style)
if (event.type === 'text') style.fill = style.stroke
applyStyle(el, style)
applyLayout(event, el)
}
}
}
function applyLayout (event, el) {
if (event.layout && event.layout.class) {
el.setAttribute('class', event.layout.class)
}
if (event.layout && event.layout.font) {
if (self.fonts[event.layout.font.name]) {
self.font = copy(self.fonts[event.layout.font.name])
if (event.layout.font.size) {
self.font['font-size'] = event.layout.font.size
}
}
}
for (var key in self.font) el.setAttribute(key, self.font[key])
if (event.layout && event.layout.rotate) {
el.setAttribute('transform',
'rotate(' + event.layout.rotate.deg + ' ' + event.args.x + ' ' + event.args.y + ')'
)
}
}
function applyStyle (el, style) {
el.setAttribute('style', Object.keys(style).reduce(reduce, ''))
function reduce (result, key) {
return result + key + ':' + style[key] + ';'
}
}
}
function createElement (name) {
return document.createElementNS('http://www.w3.org/2000/svg', name)
}
Svg.prototype.resize = function resize (width, height) {
if (this.el) {
this.el.setAttribute('viewBox', '0 0 ' + width + ' ' + height)
}
}
Svg.prototype.setColor = function setColor (color) {
if (Color(this.style.stroke).rgbaString() === Color(color).rgbaString()) return
this.setStyle({stroke: Color(color).rgbaString()})
}
Svg.prototype.setStyle = function setStyle (opt) {
var style = xtend(this.DEFAULT_STYLE, opt)
var event = {
type: 'style',
args: style
}
if (!this.silent) this.eventStream.push(event)
this._redraw([event])
}
Svg.prototype.undo = function undo () {
var event = this.eventStream.pop()
if (event) {
this.deleted.push(event)
if (event.el) {
event.el.style.display = 'none'
}
}
this._resetEvents()
}
Svg.prototype.redo = function redo () {
var event = this.deleted.pop()
if (event) {
this.eventStream.push(event)
if (event.el) {
event.el.style.display = 'block'
}
}
this._resetEvents()
}
Svg.prototype.setGridStyle = function setGridStyle (style) {
style = style || this.gridStyle
if (style) {
this.gridStyle = style
var grid = this.el.querySelector('.grid')
Object.keys(style).forEach(function setStyle (key) {
grid.setAttribute(key, style[key])
})
}
}
Svg.prototype._grid = function toggleGrid () {
var grid = this.el.querySelector('.grid')
var gridUrl = 'url(#grid-' + this.idSequence + ')'
var gridDisplay = grid.getAttribute('fill')
gridDisplay = gridDisplay === 'transparent' ? gridUrl : 'transparent'
grid.setAttribute('fill', gridDisplay)
this.el.setAttribute('data-grid', gridDisplay === gridUrl)
this.setGridStyle()
}
Svg.prototype._anchorSelect = function anchorSelect (e) {
var id = e.target.getAttribute('data-anchor-id')
var target = this.el.querySelector('[data-id="' + id + '"]')
e.stopPropagation()
this._pathSelected({target: target, stopPropagation: e.stopPropagation.bind(e)})
}
Svg.prototype._removeAnchorElements = function removeAnchorElements () {
var self = this
;[].forEach.call(this.el.querySelectorAll('.anchor'), removeAnchor)
this.eventStream.events.forEach(resetAnchorEl)
function removeAnchor (el) {
el.removeEventListener('mousedown', self.listeners.anchorselect)
el.removeEventListener('touchstart', self.listeners.anchorselect)
el.parentNode.removeChild(el)
}
function resetAnchorEl (event) {
delete event.anchorEl
}
}
Svg.prototype._addAnchorElements = function addAnchorElements (control) {
if (control) addAnchorElements.control = control
control = addAnchorElements.control
if (!control) return
if (!this.opt.anchor) return
this.eventStream.events.forEach(add.bind(this))
function add (event) {
if (!event.el) return
var position = (this.opt.anchor.position || xy)(event)
if (!position.filter(Boolean).length) return
if (control === 'text' && !(event.target || event.el).nodeName.match(/text/i)) return
var el = event.anchorEl
if (!el) {
el = createElement('ellipse')
el.addEventListener('mousedown', this.listeners.anchorselect)
el.addEventListener('touchstart', this.listeners.anchorselect)
event.anchorEl = el
this.el.appendChild(el)
}
var id = (event.target || event.el).getAttribute('data-id')
el.style.display = (event.target || event.el).style.display
el.setAttribute('cx', position[0])
el.setAttribute('cy', position[1])
el.setAttribute('rx', this.opt.anchor.size || 14)
el.setAttribute('ry', this.opt.anchor.size || 14)
el.setAttribute('data-anchor-id', id)
el.setAttribute('class', 'anchor')
}
function xy (event) {
var el = (event.target || event.el)
var attributes = el.attributes
var args = [].reduce.call(attributes, function (sum, item) { sum[item.name] = item.value; return sum }, {})
if (event.type === 'text') args.value = el.textContent
if (!validEvent({args: args, type: event.type})) return []
if (event.type === 'ellipse') return [args.cx - args.rx, args.cy]
if (event.type === 'path') return path()
return [args.x, args.y]
function path () {
var POINT = /\d+\.?\d*\s*,\s*\d+\.?\d*/g
var d = args.d.match(POINT)[0].split(',').map(Number)
return [d[0], d[1]]
}
}
}
Svg.prototype.setClipboard = function setClipboard (copied) {
this.copied = copied
}
Svg.prototype.copy = function copy () {
this.copied = this.eventStream.toJSON()
this.emit('copy', this.copied)
}
Svg.prototype.cut = function cut () {
this.copy()
this._removeAllEvents()
this._resetEvents()
}
Svg.prototype._removeAllEvents = function removeAllEvents () {
var self = this
this.eventStream.events.forEach(remove)
function remove (event) {
if (!event.el || event.type === 'delete') return
self.eventStream.push({
type: 'delete',
target: event.el,
args: {},
path: event
})
}
this._removeAnchorElements()
}
Svg.prototype.paste = function paste (e) {
var pasteData
try {
var data = e.clipboardData.getData('text')
pasteData = JSON.parse(data)
} catch (x) {}
if (!pasteData && !this.copied) return
this._removeAllEvents()
Array.prototype.push.apply(this.eventStream.events, (pasteData || this.copied))
this._resetEvents()
}