From e2c4e6316d6a9c4f564273088206e5143a01cec2 Mon Sep 17 00:00:00 2001 From: Mantas Date: Sun, 15 Nov 2020 10:54:59 +0200 Subject: [PATCH] Fixes #3281 - Removing image in text field with backspaces causes browser to go back --- .../lib/base/jquery.enableObjectResizingShim.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/app/lib/base/jquery.enableObjectResizingShim.js b/app/assets/javascripts/app/lib/base/jquery.enableObjectResizingShim.js index 4490f8c54..9ac886e1e 100644 --- a/app/assets/javascripts/app/lib/base/jquery.enableObjectResizingShim.js +++ b/app/assets/javascripts/app/lib/base/jquery.enableObjectResizingShim.js @@ -87,11 +87,25 @@ } Editor.prototype.onKeydown = function (event) { + var elem = this.$element.closest('[contenteditable=true]') + var previous = this.$element.parent().parent().prev() this.destroy() switch (event.keyCode) { case 8: // backspace this.$element.remove() + event.preventDefault() + + if(previous[0]){ + range = document.createRange() + range.selectNode(previous[0]) + range.setStart(range.endContainer, range.endOffset) + document.getSelection().removeAllRanges() + document.getSelection().addRange(range) + } + + elem.focus() + break default: event.stopPropagation() @@ -173,4 +187,4 @@ }); } -}(jQuery, window)); \ No newline at end of file +}(jQuery, window));