From b85bc591266bf2e0ffbfdb6c55c0f209ec1de994 Mon Sep 17 00:00:00 2001 From: Shogo Ishikura Date: Sun, 24 Mar 2019 18:01:29 +0900 Subject: [PATCH 1/2] Fixed a bug where deleting one image will delete all the images --- src/js/images.js | 52 ++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/js/images.js b/src/js/images.js index 4de62ebc3..c152860cf 100644 --- a/src/js/images.js +++ b/src/js/images.js @@ -515,34 +515,34 @@ if (e.which === 8 || e.which === 46) { if ($selectedImage.length) { images.push($selectedImage); - } - - // Remove image even if it's not selected, but backspace/del is pressed in text - selection = window.getSelection(); - if (selection && selection.rangeCount) { - range = selection.getRangeAt(0); - current = range.commonAncestorContainer; - $current = current.nodeName === '#text' || current.nodeName === 'BR' ? $(current).parent() : $(current); - caretPosition = MediumEditor.selection.getCaretOffsets(current).left; - - // Is backspace pressed and caret is at the beginning of a paragraph, get previous element - if (e.which === 8 && caretPosition === 0) { - $sibling = $current.prev(); - // Is del pressed and caret is at the end of a paragraph, get next element - } else if (e.which === 46 && caretPosition === $current.text().length) { - $sibling = $current.next(); - } + } else { + // Remove image even if it's not selected, but backspace/del is pressed in text + selection = window.getSelection(); + if (selection && selection.rangeCount) { + range = selection.getRangeAt(0); + current = range.commonAncestorContainer; + $current = current.nodeName === '#text' || current.nodeName === 'BR' ? $(current).parent() : $(current); + caretPosition = MediumEditor.selection.getCaretOffsets(current).left; + + // Is backspace pressed and caret is at the beginning of a paragraph, get previous element + if (e.which === 8 && caretPosition === 0) { + $sibling = $current.prev(); + // Is del pressed and caret is at the end of a paragraph, get next element + } else if (e.which === 46 && caretPosition === $current.text().length) { + $sibling = $current.next(); + } - if ($sibling && $sibling.hasClass('medium-insert-images')) { - images.push($sibling.find('img')); - } + if ($sibling && $sibling.hasClass('medium-insert-images')) { + images.push($sibling.find('img')); + } - // If text is selected, find images in the selection - selectedHtml = MediumEditor.selection.getSelectionHtml(document); - if (selectedHtml) { - $('
').html(selectedHtml).find('.medium-insert-images img').each(function () { - images.push($(this)); - }); + // If text is selected, find images in the selection + selectedHtml = MediumEditor.selection.getSelectionHtml(document); + if (selectedHtml) { + $('
').html(selectedHtml).find('.medium-insert-images img').each(function () { + images.push($(this)); + }); + } } } From a398f0f0137b7ee6eb96fd4debcfa533945bf976 Mon Sep 17 00:00:00 2001 From: Shogo Ishikura Date: Sun, 24 Mar 2019 21:27:38 +0900 Subject: [PATCH 2/2] Added case to images.spec.js --- spec/images.spec.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/images.spec.js b/spec/images.spec.js index 1d1364da7..5f00b919a 100644 --- a/spec/images.spec.js +++ b/spec/images.spec.js @@ -307,6 +307,22 @@ describe('Images addon', function () { expect($('.medium-insert-images-toolbar').length).toEqual(0); }); + it('only the selected image is removed', function () { + var $p = this.$el.find('p'), + $event = $.Event('keydown'); + + $event.which = 8; + + $p.before('
'); + $p.before('
'); + + this.$el.find('img').first().addClass('medium-insert-image-active'); + this.$el.trigger($event); + + expect(this.$el.find('.medium-insert-images').length).toEqual(1); + expect(this.$el.find('img').first().attr('src')).toEqual('delete-image2.jpg'); + }); + it('fires deleteFile function even when images isn\'t selected but backspace is pressed in text', function () { var $p = this.$el.find('p'), $event = $.Event('keydown');