Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions spec/images.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="medium-insert-images"><figure><img src="delete-image1.jpg" alt=""></figure></div>');
$p.before('<div class="medium-insert-images"><figure><img src="delete-image2.jpg" alt=""></figure></div>');

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');
Expand Down
52 changes: 26 additions & 26 deletions src/js/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
$('<div></div>').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) {
$('<div></div>').html(selectedHtml).find('.medium-insert-images img').each(function () {
images.push($(this));
});
}
}
}

Expand Down