Skip to content

Commit

Permalink
Merge pull request #1096 from julien/issue/1048
Browse files Browse the repository at this point in the history
Fixes #1048 | Add button to remove images
  • Loading branch information
wincent authored Feb 14, 2019
2 parents c4087f6 + f231cc8 commit 02c33d5
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 6 deletions.
34 changes: 34 additions & 0 deletions src/adapter/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Base from '../oop/base';
import Selections from '../selections/selections';
import UI from '../components/main.jsx';

import {removeImageCommand} from '../commands';

import React from 'react';
import ReactDOM from 'react-dom';

Expand Down Expand Up @@ -95,6 +97,22 @@ extend(

let editable = editor.editable();

let extraCommands = this.get('extraCommands');

const extraCommandKeys = Object.keys(extraCommands);
for (let i = 0; i < extraCommandKeys.length; i++) {
const commandName = extraCommandKeys[i];

if (editor.commands[commandName]) {
continue;
}

editor.addCommand(
commandName,
extraCommands[commandName]
);
}

editable.addClass('ae-editable');
}.bind(this)
);
Expand Down Expand Up @@ -488,6 +506,22 @@ extend(
value: 100,
},

/**
* The list of extra commands to be added to the editor.
*
* @memberof Core
* @instance
* @property extraCommands
* @type {Object}
*/
extraCommands: {
validator: Lang.isObject,
value: {
removeImage: removeImageCommand,
},
writeOnce: true,
},

/**
* Specifies the extra plugins which have to be loaded to the current CKEditor instance in order to
* make AlloyEditor to work properly.
Expand Down
1 change: 1 addition & 0 deletions src/assets/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"pasteVideoLink": "Paste Video Link",
"platformNotSupported": "Sorry, this platform is not supported",
"primary": "Primary",
"removeImage": "Remove Image",
"removeLink": "Remove link",
"rows": "Rows",
"success": "Success",
Expand Down
3 changes: 3 additions & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import removeImageCommand from './remove-image';

export {removeImageCommand};
29 changes: 29 additions & 0 deletions src/commands/remove-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const removeImageCommand = {
exec: editor => {
const selection = editor.getSelection();

if (selection) {
const ranges = selection.getRanges();
const startContainer = ranges[0].startContainer;

const nextRange = new CKEDITOR.dom.range(startContainer);
nextRange.setStart(startContainer, 0);
nextRange.setEnd(startContainer, 0);

const selectedElement = selection.getSelectedElement();

if (selectedElement && selectedElement.getName() === 'img') {
const native = selection.getNative();
if (native) {
native.removeAllRanges();
}

selection.selectRanges([nextRange]);

selectedElement.remove();
}
}
},
};

export default removeImageCommand;
37 changes: 37 additions & 0 deletions src/components/buttons/button-remove-image.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import ButtonCommand from '../base/button-command.js';
import ButtonIcon from './button-icon.jsx';
import ButtonStateClasses from '../base/button-state-classes.js';
import React from 'react';

/**
* The ButtonRemoveImage class removes an image using a CKEDITOR.command.
*
* @class ButtonRemoveImage
* @uses ButtonCommand
*/
class ButtonRemoveImage extends React.Component {
static defaultProps = {
command: 'removeImage',
};

static key = 'removeImage';

/**
* @inheritDoc
*/
render() {
const cssClass = `ae-button ${this.getStateClasses()}`;

return (
<button
aria-label={AlloyEditor.Strings.removeImage}
aria-pressed={cssClass.indexOf('pressed') !== -1}
className={cssClass}
onClick={this.execCommand}>
<ButtonIcon symbol="times-circle" />
</button>
);
}
}

export default ButtonCommand(ButtonStateClasses(ButtonRemoveImage));
2 changes: 2 additions & 0 deletions src/components/buttons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import ButtonParagraphCenter from './button-paragraph-center.jsx';
import ButtonParagraphJustify from './button-paragraph-justify.jsx';
import ButtonQuote from './button-quote.jsx';
import ButtonRemoveFormat from './button-remove-format.jsx';
import ButtonRemoveImage from './button-remove-image.jsx';
import ButtonSeparator from './button-separator.jsx';
import ButtonSpacing from './button-spacing.jsx';
import ButtonStrike from './button-strike.jsx';
Expand Down Expand Up @@ -103,6 +104,7 @@ export default {
[ButtonParagraphJustify.key]: ButtonParagraphJustify,
[ButtonQuote.key]: ButtonQuote,
[ButtonRemoveFormat.key]: ButtonRemoveFormat,
[ButtonRemoveImage.key]: ButtonRemoveImage,
[ButtonSeparator.key]: ButtonSeparator,
[ButtonSpacing.key]: ButtonSpacing,
[ButtonStrike.key]: ButtonStrike,
Expand Down
7 changes: 1 addition & 6 deletions src/selections/selections.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ const Selections = [
},
{
name: 'image',
buttons: [
'imageLeft',
'imageCenter',
'imageRight',
'AccessibilityImageAlt',
],
buttons: ['imageLeft', 'imageCenter', 'imageRight', 'removeImage'],
setPosition: SelectionSetPosition.image,
test: SelectionTest.image,
},
Expand Down

0 comments on commit 02c33d5

Please sign in to comment.