forked from mycolorway/simditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.coffee
543 lines (435 loc) · 14.2 KB
/
image.coffee
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
534
535
536
537
538
539
540
541
542
543
class ImageButton extends Button
name: 'image'
icon: 'picture-o'
htmlTag: 'img'
disableTag: 'pre, table'
defaultImage: ''
needFocus: false
_init: () ->
if @editor.opts.imageButton
if Array.isArray(@editor.opts.imageButton)
@menu = []
for item in @editor.opts.imageButton
@menu.push
name: item + '-image'
text: @_t(item + 'Image')
else
@menu = false
else
if @editor.uploader?
@menu = [{
name: 'upload-image',
text: @_t 'uploadImage'
}, {
name: 'external-image',
text: @_t 'externalImage'
}]
else
@menu = false
@defaultImage = @editor.opts.defaultImage
@editor.body.on 'click', 'img:not([data-non-image])', (e) =>
$img = $(e.currentTarget)
#@popover.show $img
range = document.createRange()
range.selectNode $img[0]
@editor.selection.range range
unless @editor.util.support.onselectionchange
@editor.trigger 'selectionchanged'
false
@editor.body.on 'mouseup', 'img:not([data-non-image])', (e) ->
return false
@editor.on 'selectionchanged.image', =>
range = @editor.selection.range()
return unless range?
$contents = $(range.cloneContents()).contents()
if $contents.length == 1 and $contents.is('img:not([data-non-image])')
$img = $(range.startContainer).contents().eq(range.startOffset)
@popover.show $img
else
@popover.hide()
@editor.on 'valuechanged.image', =>
$masks = @editor.wrapper.find('.simditor-image-loading')
return unless $masks.length > 0
$masks.each (i, mask) =>
$mask = $(mask)
$img = $mask.data 'img'
unless $img and $img.parent().length > 0
$mask.remove()
if $img
file = $img.data 'file'
if file
@editor.uploader.cancel file
if @editor.body.find('img.uploading').length < 1
@editor.uploader.trigger 'uploadready', [file]
super()
render: (args...) ->
super args...
@popover = new ImagePopover
button: @
if @editor.opts.imageButton == 'upload'
@_initUploader @el
renderMenu: ->
super()
@_initUploader()
_initUploader: ($uploadItem = @menuEl.find('.menu-item-upload-image')) ->
unless @editor.uploader?
@el.find('.btn-upload').remove()
return
$input = null
createInput = =>
$input.remove() if $input
$input = $ '<input/>',
type: 'file'
title: @_t('uploadImage')
multiple: true
accept: 'image/*'
.appendTo($uploadItem)
createInput()
$uploadItem.on 'click mousedown', 'input[type=file]', (e) ->
e.stopPropagation()
$uploadItem.on 'change', 'input[type=file]', (e) =>
if @editor.inputManager.focused
@editor.uploader.upload($input, {
inline: true
})
createInput()
else
@editor.one 'focus', (e) =>
@editor.uploader.upload($input, {
inline: true
})
createInput()
@editor.focus()
@wrapper.removeClass('menu-on')
@editor.uploader.on 'beforeupload', (e, file) =>
return unless file.inline
if file.img
$img = $(file.img)
else
$img = @createImage(file.name)
#$img.click()
file.img = $img
$img.addClass 'uploading'
$img.data 'file', file
@editor.uploader.readImageFile file.obj, (img) =>
return unless $img.hasClass('uploading')
src = if img then img.src else @defaultImage
@loadImage $img, src, =>
if @popover.active
@popover.refresh()
@popover.srcEl.val(@_t('uploading'))
.prop('disabled', true)
uploadProgress = $.proxy @editor.util.throttle((e, file, loaded, total) ->
return unless file.inline
$mask = file.img.data('mask')
return unless $mask
$img = $mask.data('img')
unless $img.hasClass('uploading') and $img.parent().length > 0
$mask.remove()
return
percent = loaded / total
percent = (percent * 100).toFixed(0)
percent = 99 if percent > 99
$mask.find('.progress').height "#{100 - percent}%"
, 500), @
@editor.uploader.on 'uploadprogress', uploadProgress
@editor.uploader.on 'uploadsuccess', (e, file, result) =>
return unless file.inline
$img = file.img
return unless $img.hasClass('uploading') and $img.parent().length > 0
# in case mime type of response isnt correct
if typeof result != 'object'
try
result = $.parseJSON result
catch e
result =
success: false
if result.success == false
msg = result.msg || @_t('uploadFailed')
alert msg
img_path = @defaultImage
else
img_path = result.file_path
@loadImage $img, img_path, =>
$img.removeData 'file'
$img.removeClass 'uploading'
.removeClass 'loading'
$mask = $img.data('mask')
$mask.remove() if $mask
$img.removeData 'mask'
@editor.trigger 'valuechanged'
if @editor.body.find('img.uploading').length < 1
@editor.uploader.trigger 'uploadready', [file, result]
if @popover.active
@popover.srcEl.prop('disabled', false)
@popover.srcEl.val result.file_path
@editor.uploader.on 'uploaderror', (e, file, xhr) =>
return unless file.inline
return if xhr.statusText == 'abort'
if xhr.responseText
try
result = $.parseJSON xhr.responseText
msg = result.msg
catch e
msg = @_t('uploadError')
alert msg
$img = file.img
return unless $img.hasClass('uploading') and $img.parent().length > 0
@loadImage $img, @defaultImage, =>
$img.removeData 'file'
$img.removeClass 'uploading'
.removeClass 'loading'
$mask = $img.data('mask')
$mask.remove() if $mask
$img.removeData 'mask'
if @popover.active
@popover.srcEl.prop('disabled', false)
@popover.srcEl.val @defaultImage
@editor.trigger 'valuechanged'
if @editor.body.find('img.uploading').length < 1
@editor.uploader.trigger 'uploadready', [file, result]
_status: ->
@_disableStatus()
loadImage: ($img, src, callback) ->
positionMask = =>
imgOffset = $img.offset()
wrapperOffset = @editor.wrapper.offset()
$mask.css({
top: imgOffset.top - wrapperOffset.top
left: imgOffset.left - wrapperOffset.left
width: $img.width()
height: $img.height()
}).show()
$img.addClass('loading')
$mask = $img.data('mask')
if !$mask
$mask = $('''
<div class="simditor-image-loading">
<div class="progress"></div>
</div>
''')
.hide()
.appendTo(@editor.wrapper)
positionMask()
$img.data('mask', $mask)
$mask.data('img', $img)
img = new Image()
img.onload = =>
return if !$img.hasClass('loading') and !$img.hasClass('uploading')
width = img.width
height = img.height
$img.attr
src: src,
width: width,
height: height,
'data-image-size': width + ',' + height
.removeClass('loading')
if $img.hasClass('uploading') # img being uploaded
@editor.util.reflow @editor.body
positionMask()
else
$mask.remove()
$img.removeData('mask')
callback(img) if $.isFunction(callback)
img.onerror = ->
callback(false) if $.isFunction(callback)
$mask.remove()
$img.removeData('mask')
.removeClass('loading')
img.src = src
createImage: (name = 'Image') ->
@editor.focus() unless @editor.inputManager.focused
range = @editor.selection.range()
range.deleteContents()
@editor.selection.range range
# $block = @editor.selection.blockNodes().last()
# if $block.is('p') and [email protected] $block
# $block = $('<p/>').append(@editor.util.phBr).insertAfter($block)
# @editor.selection.setRangeAtStartOf $block, range
#else if $block.is('li')
#$block = @editor.util.furthestNode $block, 'ul, ol'
#$block = $('<p/>').append(@editor.util.phBr).insertAfter($block)
#@editor.selection.setRangeAtStartOf $block, range
$img = $('<img/>').attr('alt', name)
range.insertNode $img[0]
@editor.selection.setRangeAfter $img, range
@editor.trigger 'valuechanged'
# $nextBlock = $block.next 'p'
# unless $nextBlock.length > 0
# $nextBlock = $('<p/>').append(@editor.util.phBr).insertAfter($block)
# @editor.selection.setRangeAtStartOf $nextBlock
$img
command: (src) ->
$img = @createImage()
@loadImage $img, src || @defaultImage, =>
@editor.trigger 'valuechanged'
@editor.util.reflow $img
$img.click()
@popover.one 'popovershow', =>
@popover.srcEl.focus()
@popover.srcEl[0].select()
class ImagePopover extends Popover
offset:
top: 6
left: -4
render: ->
tpl = """
<div class="link-settings">
<div class="settings-field">
<label>#{ @_t 'imageUrl' }</label>
<input class="image-src" type="text" tabindex="1" />
<a class="btn-upload" href="javascript:;"
title="#{ @_t 'uploadImage' }" tabindex="-1">
<span class="simditor-icon simditor-icon-upload"></span>
</a>
</div>
<div class='settings-field'>
<label>#{ @_t 'imageAlt' }</label>
<input class="image-alt" id="image-alt" type="text" tabindex="1" />
</div>
<div class="settings-field">
<label>#{ @_t 'imageSize' }</label>
<input class="image-size" id="image-width" type="text" tabindex="2" />
<span class="times">×</span>
<input class="image-size" id="image-height" type="text" tabindex="3" />
<a class="btn-restore" href="javascript:;"
title="#{ @_t 'restoreImageSize' }" tabindex="-1">
<span class="simditor-icon simditor-icon-undo"></span>
</a>
</div>
</div>
"""
@el.addClass('image-popover')
.append(tpl)
@srcEl = @el.find '.image-src'
@widthEl = @el.find '#image-width'
@heightEl = @el.find '#image-height'
@altEl = @el.find '#image-alt'
@srcEl.on 'keydown', (e) =>
return unless e.which == 13 and [email protected]('uploading')
e.preventDefault()
range = document.createRange()
@button.editor.selection.setRangeAfter @target, range
@hide()
@srcEl.on 'blur', (e) =>
@_loadImage @srcEl.val()
@el.find('.image-size').on 'blur', (e) =>
@_resizeImg $(e.currentTarget)
@el.data('popover').refresh()
@el.find('.image-size').on 'keyup', (e) =>
inputEl = $(e.currentTarget)
unless e.which == 13 or e.which == 27 or e.which == 9
@_resizeImg inputEl, true
@el.find('.image-size').on 'keydown', (e) =>
inputEl = $(e.currentTarget)
if e.which == 13 or e.which == 27
e.preventDefault()
if e.which == 13
@_resizeImg inputEl
else
@_restoreImg()
$img = @target
@hide()
range = document.createRange()
@button.editor.selection.setRangeAfter $img, range
else if e.which == 9
@el.data('popover').refresh()
@altEl.on 'keydown', (e) =>
if e.which == 13
e.preventDefault()
range = document.createRange()
@button.editor.selection.setRangeAfter @target, range
@hide()
@altEl.on 'keyup', (e) =>
return if e.which == 13 or e.which == 27 or e.which == 9
@alt = @altEl.val()
@target.attr 'alt', @alt
@el.find('.btn-restore').on 'click', (e) =>
@_restoreImg()
@el.data('popover').refresh()
@editor.on 'valuechanged', (e) =>
@refresh() if @active
@_initUploader()
_initUploader: ->
$uploadBtn = @el.find('.btn-upload')
unless @editor.uploader?
$uploadBtn.remove()
return
createInput = =>
@input.remove() if @input
@input = $ '<input/>',
type: 'file'
title: @_t('uploadImage')
multiple: true
accept: 'image/*'
.appendTo($uploadBtn)
createInput()
@el.on 'click mousedown', 'input[type=file]', (e) ->
e.stopPropagation()
@el.on 'change', 'input[type=file]', (e) =>
@editor.uploader.upload(@input, {
inline: true,
img: @target
})
createInput()
_resizeImg: (inputEl, onlySetVal = false) ->
value = inputEl.val() * 1
return unless @target and ($.isNumeric(value) or value < 0)
if inputEl.is @widthEl
width = value
height = @height * value / @width
@heightEl.val height
else
height = value
width = @width * value / @height
@widthEl.val width
unless onlySetVal
@target.attr
width: width
height: height
@editor.trigger 'valuechanged'
_restoreImg: ->
size = @target.data('image-size')?.split(",") || [@width, @height]
@target.attr
width: size[0] * 1
height: size[1] * 1
@widthEl.val(size[0])
@heightEl.val(size[1])
@editor.trigger 'valuechanged'
_loadImage: (src, callback) ->
if /^data:image/.test(src) and not @editor.uploader
callback(false) if callback
return
return if @target.attr('src') == src
@button.loadImage @target, src, (img) =>
return unless img
if @active
@width = img.width
@height = img.height
@widthEl.val @width
@heightEl.val @height
if /^data:image/.test(src)
blob = @editor.util.dataURLtoBlob src
blob.name = "Base64 Image.png"
@editor.uploader.upload blob,
inline: true
img: @target
else
@editor.trigger 'valuechanged'
callback(img) if callback
show: (args...) ->
super args...
$img = @target
@width = $img.width()
@height = $img.height()
@alt = $img.attr 'alt'
if $img.hasClass 'uploading'
@srcEl.val @_t('uploading')
.prop 'disabled', true
else
@srcEl.val $img.attr('src')
.prop 'disabled', false
@widthEl.val @width
@heightEl.val @height
@altEl.val @alt
Simditor.Toolbar.addButton ImageButton