2018-02-22 23:41:40 +03:00
|
|
|
import $ from 'jquery';
|
2018-01-30 23:46:03 +03:00
|
|
|
import Component from '@ember/component';
|
2018-08-08 14:53:35 +03:00
|
|
|
import countWords, {stripTags} from '../utils/count-words';
|
2018-01-30 23:46:03 +03:00
|
|
|
import layout from '../templates/components/koenig-card-image';
|
2018-02-22 23:41:40 +03:00
|
|
|
import {
|
|
|
|
IMAGE_EXTENSIONS,
|
|
|
|
IMAGE_MIME_TYPES
|
|
|
|
} from 'ghost-admin/components/gh-image-uploader';
|
2018-08-09 19:55:11 +03:00
|
|
|
import {computed, set, setProperties} from '@ember/object';
|
2018-02-22 23:41:40 +03:00
|
|
|
import {htmlSafe} from '@ember/string';
|
2018-05-29 17:23:50 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
2018-02-22 23:41:40 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2018-05-08 12:26:46 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2018-01-30 23:46:03 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
2018-05-08 12:26:46 +03:00
|
|
|
ui: service(),
|
2018-02-22 23:41:40 +03:00
|
|
|
layout,
|
|
|
|
|
|
|
|
// attrs
|
2018-05-29 17:23:50 +03:00
|
|
|
files: null,
|
2018-02-22 23:41:40 +03:00
|
|
|
payload: null,
|
|
|
|
isSelected: false,
|
|
|
|
isEditing: false,
|
|
|
|
imageExtensions: IMAGE_EXTENSIONS,
|
|
|
|
imageMimeTypes: IMAGE_MIME_TYPES,
|
|
|
|
|
2018-06-20 14:34:30 +03:00
|
|
|
// properties
|
|
|
|
handlesDragDrop: true,
|
|
|
|
|
2018-02-22 23:41:40 +03:00
|
|
|
// closure actions
|
|
|
|
selectCard() {},
|
2018-05-23 13:59:20 +03:00
|
|
|
deselectCard() {},
|
2018-02-22 23:41:40 +03:00
|
|
|
editCard() {},
|
|
|
|
saveCard() {},
|
2018-08-09 19:55:11 +03:00
|
|
|
deleteCard() {},
|
2018-02-22 23:41:40 +03:00
|
|
|
moveCursorToNextSection() {},
|
|
|
|
moveCursorToPrevSection() {},
|
|
|
|
addParagraphAfterCard() {},
|
2018-07-20 17:53:21 +03:00
|
|
|
registerComponent() {},
|
|
|
|
|
2018-08-09 19:55:11 +03:00
|
|
|
imageSelector: computed('payload.imageSelector', function () {
|
|
|
|
let selector = this.payload.imageSelector;
|
|
|
|
let imageSelectors = {
|
|
|
|
unsplash: 'gh-unsplash'
|
|
|
|
};
|
|
|
|
|
|
|
|
return imageSelectors[selector];
|
|
|
|
}),
|
|
|
|
|
2018-07-20 17:53:21 +03:00
|
|
|
counts: computed('payload.{src,caption}', function () {
|
|
|
|
let wordCount = 0;
|
|
|
|
let imageCount = 0;
|
|
|
|
|
|
|
|
if (this.payload.src) {
|
|
|
|
imageCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.payload.caption) {
|
2018-08-08 14:53:35 +03:00
|
|
|
wordCount += countWords(stripTags(this.payload.caption));
|
2018-07-20 17:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {wordCount, imageCount};
|
|
|
|
}),
|
2018-02-22 23:41:40 +03:00
|
|
|
|
2018-07-23 14:21:32 +03:00
|
|
|
kgImgStyle: computed('payload.cardWidth', function () {
|
|
|
|
let cardWidth = this.payload.cardWidth;
|
2018-04-10 20:39:30 +03:00
|
|
|
|
2018-07-23 14:21:32 +03:00
|
|
|
if (cardWidth === 'wide') {
|
2018-04-10 20:39:30 +03:00
|
|
|
return 'image-wide';
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:21:32 +03:00
|
|
|
if (cardWidth === 'full') {
|
2018-04-10 20:39:30 +03:00
|
|
|
return 'image-full';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'image-normal';
|
|
|
|
}),
|
|
|
|
|
2018-07-23 14:21:32 +03:00
|
|
|
toolbar: computed('payload.{cardWidth,src}', function () {
|
|
|
|
let cardWidth = this.payload.cardWidth;
|
2018-04-10 20:39:30 +03:00
|
|
|
let items = [];
|
|
|
|
|
2018-05-31 11:45:58 +03:00
|
|
|
if (this.payload.src) {
|
|
|
|
items.push({
|
|
|
|
title: 'Regular',
|
|
|
|
icon: 'koenig/kg-img-regular',
|
2018-08-08 15:14:02 +03:00
|
|
|
iconClass: `${!cardWidth ? 'fill-blue-l2' : 'fill-white'}`,
|
2018-07-23 14:21:32 +03:00
|
|
|
action: run.bind(this, this._changeCardWidth, '')
|
2018-05-31 11:45:58 +03:00
|
|
|
});
|
2018-04-10 20:39:30 +03:00
|
|
|
|
2018-05-31 11:45:58 +03:00
|
|
|
items.push({
|
|
|
|
title: 'Wide',
|
|
|
|
icon: 'koenig/kg-img-wide',
|
2018-08-08 15:14:02 +03:00
|
|
|
iconClass: `${cardWidth === 'wide' ? 'fill-blue-l2' : 'fill-white'}`,
|
2018-07-23 14:21:32 +03:00
|
|
|
action: run.bind(this, this._changeCardWidth, 'wide')
|
2018-05-31 11:45:58 +03:00
|
|
|
});
|
2018-04-10 20:39:30 +03:00
|
|
|
|
2018-05-31 11:45:58 +03:00
|
|
|
items.push({
|
|
|
|
title: 'Full',
|
|
|
|
icon: 'koenig/kg-img-full',
|
2018-08-08 15:14:02 +03:00
|
|
|
iconClass: `${cardWidth === 'full' ? 'fill-blue-l2' : 'fill-white'}`,
|
2018-07-23 14:21:32 +03:00
|
|
|
action: run.bind(this, this._changeCardWidth, 'full')
|
2018-05-31 11:45:58 +03:00
|
|
|
});
|
2018-04-10 20:39:30 +03:00
|
|
|
|
2018-04-11 18:48:01 +03:00
|
|
|
items.push({divider: true});
|
|
|
|
|
2018-04-10 20:39:30 +03:00
|
|
|
items.push({
|
|
|
|
title: 'Replace image',
|
|
|
|
icon: 'koenig/kg-replace',
|
2018-08-08 15:14:02 +03:00
|
|
|
iconClass: 'fill-white',
|
2018-04-10 20:39:30 +03:00
|
|
|
action: run.bind(this, this._triggerFileDialog)
|
|
|
|
});
|
2018-02-22 23:41:40 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 11:45:58 +03:00
|
|
|
if (items.length > 0) {
|
|
|
|
return {items};
|
|
|
|
}
|
2018-02-22 23:41:40 +03:00
|
|
|
}),
|
|
|
|
|
2018-05-01 19:13:53 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (!this.payload) {
|
|
|
|
this.set('payload', {});
|
|
|
|
}
|
2018-07-20 17:53:21 +03:00
|
|
|
|
|
|
|
this.registerComponent(this);
|
2018-05-01 19:13:53 +03:00
|
|
|
},
|
|
|
|
|
2018-05-29 17:23:50 +03:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
// `payload.files` can be set if we have an externaly set image that
|
|
|
|
// should be uploaded. Typical example would be from a paste or drag/drop
|
|
|
|
if (!isEmpty(this.payload.files)) {
|
|
|
|
run.schedule('afterRender', this, function () {
|
|
|
|
this.set('files', this.payload.files);
|
|
|
|
|
|
|
|
// we don't want to persist any file data in the document
|
|
|
|
delete this.payload.files;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-22 23:41:40 +03:00
|
|
|
actions: {
|
|
|
|
updateSrc(images) {
|
|
|
|
let [image] = images;
|
|
|
|
this._updatePayloadAttr('src', image.url);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateCaption(caption) {
|
|
|
|
this._updatePayloadAttr('caption', caption);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a file selection dialog - Triggered by "Upload Image" buttons,
|
|
|
|
* searches for the hidden file input within the .gh-setting element
|
|
|
|
* containing the clicked button then simulates a click
|
|
|
|
* @param {MouseEvent} event - MouseEvent fired by the button click
|
|
|
|
*/
|
|
|
|
triggerFileDialog(event) {
|
|
|
|
this._triggerFileDialog(event);
|
|
|
|
},
|
|
|
|
|
|
|
|
setPreviewSrc(files) {
|
|
|
|
let file = files[0];
|
|
|
|
if (file) {
|
|
|
|
let reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
this.set('previewSrc', htmlSafe(e.target.result));
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
resetSrcs() {
|
|
|
|
this.set('previewSrc', null);
|
|
|
|
this._updatePayloadAttr('src', null);
|
2018-08-09 19:55:11 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
selectFromImageSelector({src, caption, alt}) {
|
|
|
|
let {payload, saveCard} = this;
|
2018-08-11 20:30:59 +03:00
|
|
|
let searchTerm;
|
2018-08-09 19:55:11 +03:00
|
|
|
|
2018-08-11 20:30:59 +03:00
|
|
|
setProperties(payload, {src, caption, alt, searchTerm});
|
|
|
|
|
|
|
|
this.send('closeImageSelector');
|
2018-08-09 19:55:11 +03:00
|
|
|
|
|
|
|
saveCard(payload, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
closeImageSelector() {
|
|
|
|
if (!this.payload.src) {
|
|
|
|
return this.deleteCard();
|
|
|
|
}
|
|
|
|
|
|
|
|
set(this.payload, 'imageSelector', undefined);
|
2018-08-11 20:30:59 +03:00
|
|
|
|
|
|
|
// ensure focus is returned to the editor so that the card which
|
|
|
|
// appears selected behaves as if it's selected
|
|
|
|
this.editor.focus();
|
2018-02-22 23:41:40 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-20 14:34:30 +03:00
|
|
|
dragOver(event) {
|
|
|
|
if (!event.dataTransfer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is needed to work around inconsistencies with dropping files
|
|
|
|
// from Chrome's downloads bar
|
|
|
|
if (navigator.userAgent.indexOf('Chrome') > -1) {
|
|
|
|
let eA = event.dataTransfer.effectAllowed;
|
|
|
|
event.dataTransfer.dropEffect = (eA === 'move' || eA === 'linkMove') ? 'move' : 'copy';
|
|
|
|
}
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.set('isDraggedOver', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
dragLeave(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.set('isDraggedOver', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
drop(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.set('isDraggedOver', false);
|
|
|
|
|
|
|
|
if (event.dataTransfer.files) {
|
|
|
|
this.set('files', [event.dataTransfer.files[0]]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-23 14:21:32 +03:00
|
|
|
_changeCardWidth(cardWidth) {
|
|
|
|
this._updatePayloadAttr('cardWidth', cardWidth);
|
2018-04-10 20:39:30 +03:00
|
|
|
},
|
|
|
|
|
2018-02-22 23:41:40 +03:00
|
|
|
_updatePayloadAttr(attr, value) {
|
2018-05-01 19:13:53 +03:00
|
|
|
let payload = this.payload;
|
|
|
|
let save = this.saveCard;
|
2018-02-22 23:41:40 +03:00
|
|
|
|
|
|
|
set(payload, attr, value);
|
|
|
|
|
|
|
|
// update the mobiledoc and stay in edit mode
|
|
|
|
save(payload, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
_triggerFileDialog(event) {
|
|
|
|
let target = event && event.target || this.element;
|
|
|
|
|
|
|
|
// simulate click to open file dialog
|
|
|
|
// using jQuery because IE11 doesn't support MouseEvent
|
|
|
|
$(target)
|
|
|
|
.closest('.__mobiledoc-card')
|
|
|
|
.find('input[type="file"]')
|
|
|
|
.click();
|
|
|
|
}
|
2018-01-30 23:46:03 +03:00
|
|
|
});
|