forked from mycolorway/simditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.coffee
461 lines (362 loc) · 12.1 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
class ImageButton extends Button
name: 'image'
icon: 'picture-o'
htmlTag: 'img'
disableTag: 'pre, table'
defaultImage: ''
needFocus: false
_init: () ->
if @editor.uploader?
@menu = [{
name: 'upload-image',
text: @_t 'localImage'
}, {
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.selectRange range
@editor.trigger 'selectionchanged' unless @editor.util.support.onselectionchange
false
@editor.body.on 'mouseup', 'img:not([data-non-image])', (e) =>
return false
@editor.on 'selectionchanged.image', =>
range = @editor.selection.getRange()
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: @
renderMenu: ->
super()
$uploadItem = @menuEl.find('.menu-item-upload-image')
$input = null
createInput = =>
$input.remove() if $input
$input = $('<input type="file" title="' + @_t('uploadImage') + '" 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')
@_initUploader()
_initUploader: ->
unless @editor.uploader?
@el.find('.btn-upload').remove()
return
@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)
@editor.uploader.on 'uploadprogress', (e, file, loaded, total) =>
return unless file.inline
percent = loaded / total
percent = (percent * 100).toFixed(0)
percent = 99 if percent > 99
$mask = file.img.data('mask')
if $mask
$img = $mask.data('img')
$txt = $mask.find('span')
if $img and $img.parent().length > 0 and percent != $txt.text()
$txt.text(percent)
else
$mask.remove()
@editor.uploader.on 'uploadsuccess', (e, file, result) =>
return unless file.inline
$img = file.img
$img.removeData 'file'
$img.removeClass 'uploading'
$mask = $img.data('mask')
$mask.remove() if $mask
$img.removeData 'mask'
if result.success == false
msg = result.msg || @_t('uploadFailed')
alert msg
$img.attr 'src', @defaultImage
else
$img.attr 'src', result.file_path
if @popover.active
@popover.srcEl.prop('disabled', false)
@popover.srcEl.val result.file_path
@editor.trigger 'valuechanged'
if @editor.body.find('img.uploading').length < 1
@editor.uploader.trigger 'uploadready', [file, result]
@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
$img.removeData 'file'
$img.removeClass 'uploading'
$mask = $img.data('mask')
$mask.remove() if $mask
$img.removeData 'mask'
$img.attr 'src', @defaultImage
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: ($node) ->
@setDisabled $node.is(@disableTag) if $node?
return true if @disabled
loadImage: ($img, src, callback) ->
$mask = $img.data('mask')
if !$mask
$mask = $('<div class="simditor-image-loading"><span></span></div>')
.hide()
.appendTo(@editor.wrapper)
$mask.addClass('uploading') if $img.hasClass('uploading')
$img.data('mask', $mask)
$mask.data('img', $img)
img = new Image()
img.onload = =>
# in case upload faster than image preview (usually happens in dev)
return if $mask.hasClass('uploading') and !$img.hasClass('uploading')
width = img.width
height = img.height
$img.attr
src: src,
'data-image-size': width + ',' + height
if $img.hasClass 'uploading' # img being uploaded
@editor.util.reflow @editor.body
wrapperOffset = @editor.wrapper.offset()
imgOffset = $img.offset()
$mask.css({
top: imgOffset.top - wrapperOffset.top,
left: imgOffset.left - wrapperOffset.left,
width: $img.width(),
height: $img.height()
}).show()
else
$mask.remove()
$img.removeData('mask')
callback(img)
img.onerror = =>
callback(false)
$mask.remove()
$img.removeData('mask')
img.src = src
createImage: (name = 'Image') ->
@editor.focus() unless @editor.inputManager.focused
range = @editor.selection.getRange()
range.deleteContents()
$block = @editor.util.closestBlockEl()
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]
$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 or @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="fa fa-upload"></span>
</a>
</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="fa fa-reply"></span>
</a>
</div>
</div>
"""
@el.addClass('image-popover')
.append(tpl)
@srcEl = @el.find '.image-src'
@srcEl.on 'keydown', (e) =>
return unless e.which == 13 or e.which == 27
e.preventDefault()
hideAndFocus = =>
@button.editor.body.focus()
@button.editor.selection.setRangeAfter @target
@hide()
if e.which == 13 and [email protected]('uploading')
src = @srcEl.val()
if /^data:image/.test(src) and not @editor.uploader
hideAndFocus()
return
@button.loadImage @target, src, (success) =>
return unless success
if /^data:image/.test(src)
blob = @editor.util.dataURLtoBlob src
blob.name = "Base64 Image.png"
@editor.uploader.upload blob,
inline: true
img: @target
else
hideAndFocus()
@editor.trigger 'valuechanged'
else
hideAndFocus()
@widthEl = @el.find '#image-width'
@heightEl = @el.find '#image-height'
@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()
@button.editor.body.focus()
@button.editor.selection.setRangeAfter @target
@hide()
else if e.which == 9
@el.data('popover').refresh()
@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') + '" 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 $.isNumeric(value) or value < 0
if inputEl.is @widthEl
height = @height * value / @width
@heightEl.val height
else
width = @width * value / @height
@widthEl.val width
unless onlySetVal
@target.attr
width: width || value
height: height || value
_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])
show: (args...) ->
super args...
$img = @target
@width = $img.width()
@height = $img.height()
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
Simditor.Toolbar.addButton ImageButton