|
@@ -3,10 +3,22 @@ class App.UiElement.richtext.additions.RichTextToolPopupImage extends App.UiElem
|
|
|
labelExisting: 'Replace'
|
|
|
|
|
|
apply: (callback) ->
|
|
|
- @el.find('btn--create').attr('disabled', true)
|
|
|
+ @el.find('.btn--create').attr('disabled', true)
|
|
|
|
|
|
file = @el.find('input')[0].files[0]
|
|
|
|
|
|
+ fileSizeInMb = file.size/1024/1024
|
|
|
+
|
|
|
+ # The browser may fail while reading too large files as data URL.
|
|
|
+ # Here we introduce a safe limit check in order to prevent silent errors.
|
|
|
+ if fileSizeInMb > 25
|
|
|
+ console.error('App.UiElement.richtext.additions.RichTextToolPopupImage', 'image file size too large', fileSizeInMb, 'in mb')
|
|
|
+ @onClear()
|
|
|
+ new App.ControllerErrorModal(
|
|
|
+ message: __('Image file size is too large, please try inserting a smaller file.')
|
|
|
+ )
|
|
|
+ return
|
|
|
+
|
|
|
reader = new FileReader()
|
|
|
|
|
|
reader.addEventListener('load', =>
|
|
@@ -16,40 +28,48 @@ class App.UiElement.richtext.additions.RichTextToolPopupImage extends App.UiElem
|
|
|
|
|
|
reader.readAsDataURL(file)
|
|
|
|
|
|
- applyOnto: (dom, base64) ->
|
|
|
+ applyOnto: (dom, base64, width) ->
|
|
|
dom.attr('src', base64)
|
|
|
+ dom.attr('width', width)
|
|
|
|
|
|
insertImage: (base64) ->
|
|
|
textEditor = $(@event.currentTarget).closest('.richtext.form-control').find('[contenteditable]')
|
|
|
|
|
|
- switch @selection.type
|
|
|
- when 'existing'
|
|
|
- @applyOnto(@selection.dom, base64)
|
|
|
- when 'append'
|
|
|
- newElem = $('<img>')[0]
|
|
|
- newElem.src = base64
|
|
|
- newElem.style = 'width: 1000px; max-width: 100%;'
|
|
|
- @selection.dom.append(newElem)
|
|
|
- when 'caret'
|
|
|
- newElem = $('<img>')
|
|
|
- newElem.attr('src', base64)
|
|
|
- newElem.attr('style', 'width: 1000px; max-width: 100%;')
|
|
|
-
|
|
|
- surroundingDom = @selection.dom[0]
|
|
|
-
|
|
|
- if surroundingDom instanceof Text
|
|
|
- @selection.dom[0].splitText(@selection.offset)
|
|
|
-
|
|
|
- newElem.insertAfter(@selection.dom)
|
|
|
- when 'range'
|
|
|
- newElem = $('<img>')
|
|
|
- newElem.attr('src', base64)
|
|
|
- newElem.attr('style', 'width: 1000px; max-width: 100%;')
|
|
|
-
|
|
|
- placeholder = textEditor.find('span.highlight-emulator')
|
|
|
-
|
|
|
- placeholder.empty()
|
|
|
- placeholder.append(newElem)
|
|
|
+ insert = (dataUrl, width, height, isResized) =>
|
|
|
+ switch @selection.type
|
|
|
+ when 'existing'
|
|
|
+ @applyOnto(@selection.dom, dataUrl, width)
|
|
|
+ when 'append'
|
|
|
+ newElem = $('<img>')[0]
|
|
|
+ newElem.src = dataUrl
|
|
|
+ newElem.style = 'width: ' + width + 'px; max-width: 100%;'
|
|
|
+ @selection.dom.append(newElem)
|
|
|
+ when 'caret'
|
|
|
+ newElem = $('<img>')
|
|
|
+ newElem.attr('src', dataUrl)
|
|
|
+ newElem.attr('style', 'width: ' + width + 'px; max-width: 100%;')
|
|
|
+
|
|
|
+ surroundingDom = @selection.dom[0]
|
|
|
+
|
|
|
+ if surroundingDom instanceof Text
|
|
|
+ @selection.dom[0].splitText(@selection.offset)
|
|
|
+
|
|
|
+ newElem.insertAfter(@selection.dom)
|
|
|
+ when 'range'
|
|
|
+ newElem = $('<img>')
|
|
|
+ newElem.attr('src', dataUrl)
|
|
|
+ newElem.attr('style', 'width: ' + width + 'px; max-width: 100%;')
|
|
|
+
|
|
|
+ placeholder = textEditor.find('span.highlight-emulator')
|
|
|
+
|
|
|
+ placeholder.empty()
|
|
|
+ placeholder.append(newElem)
|
|
|
+
|
|
|
+ App.ImageService.resize(base64, 1200, 'auto', 2, @getImageType(base64), 'auto', insert)
|
|
|
+
|
|
|
+ getImageType: (base64) ->
|
|
|
+ match = base64.match(/^data:(image\/\w+);base64,/)
|
|
|
+ if match then match[1] else null
|
|
|
|
|
|
clear: ->
|
|
|
switch @selection.type
|