|
493 | 493 | */ |
494 | 494 |
|
495 | 495 | Images.prototype.removeImage = function (e) { |
496 | | - var $image, $parent, $empty; |
| 496 | + var images = [], |
| 497 | + $selectedImage = this.$el.find('.medium-insert-image-active'), |
| 498 | + $parent, $empty, selection, range, current, caretPosition, $current, $sibling, selectedHtml, i; |
497 | 499 |
|
498 | 500 | if (e.which === 8 || e.which === 46) { |
499 | | - $image = this.$el.find('.medium-insert-image-active'); |
| 501 | + if ($selectedImage.length) { |
| 502 | + images.push($selectedImage); |
| 503 | + } |
500 | 504 |
|
501 | | - if ($image.length) { |
502 | | - e.preventDefault(); |
| 505 | + // Remove image even if it's not selected, but backspace/del is pressed in text |
| 506 | + selection = window.getSelection(); |
| 507 | + if (selection && selection.rangeCount) { |
| 508 | + range = selection.getRangeAt(0); |
| 509 | + current = range.commonAncestorContainer; |
| 510 | + $current = current.nodeName === '#text' ? $(current).parent() : $(current); |
| 511 | + caretPosition = MediumEditor.selection.getCaretOffsets(current).left; |
| 512 | + |
| 513 | + // Is backspace pressed and caret is at the beginning of a paragraph, get previous element |
| 514 | + if (e.which === 8 && caretPosition === 0) { |
| 515 | + $sibling = $current.prev(); |
| 516 | + // Is del pressed and caret is at the end of a paragraph, get next element |
| 517 | + } else if (e.which === 46 && caretPosition === $current.text().length) { |
| 518 | + $sibling = $current.next(); |
| 519 | + } |
503 | 520 |
|
504 | | - this.deleteFile($image.attr('src')); |
| 521 | + if ($sibling && $sibling.hasClass('medium-insert-images')) { |
| 522 | + images.push($sibling.find('img')); |
| 523 | + } |
505 | 524 |
|
506 | | - $parent = $image.closest('.medium-insert-images'); |
507 | | - $image.closest('figure').remove(); |
| 525 | + // If text is selected, find images in the selection |
| 526 | + selectedHtml = MediumEditor.selection.getSelectionHtml(document); |
| 527 | + if (selectedHtml) { |
| 528 | + $('<div></div>').html(selectedHtml).find('.medium-insert-images img').each(function () { |
| 529 | + images.push($(this)); |
| 530 | + }); |
| 531 | + } |
| 532 | + } |
508 | 533 |
|
509 | | - $('.medium-insert-images-toolbar, .medium-insert-images-toolbar2').remove(); |
| 534 | + if (images.length) { |
| 535 | + for (i = 0; i < images.length; i++) { |
| 536 | + this.deleteFile(images[i].attr('src')); |
510 | 537 |
|
511 | | - if ($parent.find('figure').length === 0) { |
512 | | - $empty = $parent.next(); |
513 | | - if ($empty.is('p') === false || $empty.text() !== '') { |
514 | | - $empty = $(this.templates['src/js/templates/core-empty-line.hbs']().trim()); |
515 | | - $parent.before($empty); |
516 | | - } |
517 | | - $parent.remove(); |
| 538 | + $parent = images[i].closest('.medium-insert-images'); |
| 539 | + images[i].closest('figure').remove(); |
518 | 540 |
|
519 | | - // Hide addons |
520 | | - this.core.hideAddons(); |
| 541 | + if ($parent.find('figure').length === 0) { |
| 542 | + $empty = $parent.next(); |
| 543 | + if ($empty.is('p') === false || $empty.text() !== '') { |
| 544 | + $empty = $(this.templates['src/js/templates/core-empty-line.hbs']().trim()); |
| 545 | + $parent.before($empty); |
| 546 | + } |
| 547 | + $parent.remove(); |
| 548 | + } |
| 549 | + } |
521 | 550 |
|
| 551 | + // Hide addons |
| 552 | + this.core.hideAddons(); |
| 553 | + if (!selectedHtml && $empty) { |
| 554 | + e.preventDefault(); |
522 | 555 | this.core.moveCaret($empty); |
523 | 556 | } |
524 | 557 |
|
| 558 | + $('.medium-insert-images-toolbar, .medium-insert-images-toolbar2').remove(); |
525 | 559 | this.core.triggerInput(); |
526 | 560 | } |
527 | 561 | } |
|
0 commit comments