2021-06-16 19:56:25 +03:00
|
|
|
import Component from '@glimmer/component';
|
2023-10-10 20:30:47 +03:00
|
|
|
import cleanBasicHtml from '@tryghost/kg-clean-basic-html';
|
2021-06-16 19:56:25 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-11-15 19:38:57 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2021-06-16 19:56:25 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
2023-10-10 20:30:47 +03:00
|
|
|
function hasParagraphWrapper(html) {
|
|
|
|
const domParser = new DOMParser();
|
|
|
|
const doc = domParser.parseFromString(html, 'text/html');
|
|
|
|
|
|
|
|
return doc.body?.firstElementChild?.tagName === 'P';
|
|
|
|
}
|
|
|
|
|
2021-06-16 19:56:25 +03:00
|
|
|
export default class GhEditorFeatureImageComponent extends Component {
|
2021-11-15 19:38:57 +03:00
|
|
|
@service settings;
|
|
|
|
|
2021-06-16 19:56:25 +03:00
|
|
|
@tracked isEditingAlt = false;
|
|
|
|
@tracked captionInputFocused = false;
|
|
|
|
@tracked showUnsplashSelector = false;
|
2021-07-16 17:00:48 +03:00
|
|
|
@tracked canDrop = false;
|
2023-12-07 18:46:23 +03:00
|
|
|
@tracked tkCount = 0;
|
2021-06-16 19:56:25 +03:00
|
|
|
|
2023-10-10 20:30:47 +03:00
|
|
|
get caption() {
|
|
|
|
const content = this.args.caption;
|
|
|
|
if (!content) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// wrap in a paragraph, so it gets parsed correctly
|
|
|
|
return hasParagraphWrapper(content) ? content : `<p>${content}</p>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setCaption(html) {
|
|
|
|
const cleanedHtml = cleanBasicHtml(html || '', {firstChildInnerContent: true});
|
|
|
|
this.args.updateCaption(cleanedHtml);
|
|
|
|
}
|
|
|
|
|
2023-12-07 18:46:23 +03:00
|
|
|
@action
|
|
|
|
registerEditorAPI(API) {
|
|
|
|
this.editorAPI = API;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
focusCaptionEditor() {
|
|
|
|
if (this.editorAPI) {
|
|
|
|
this.editorAPI.focusEditor({position: 'bottom'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:44:55 +03:00
|
|
|
@action
|
|
|
|
handleCaptionBlur() {
|
|
|
|
this.captionInputFocused = false;
|
|
|
|
this.args.handleCaptionBlur();
|
|
|
|
}
|
|
|
|
|
2021-06-16 19:56:25 +03:00
|
|
|
@action
|
|
|
|
setUploadedImage(results) {
|
|
|
|
if (results[0]) {
|
|
|
|
this.args.updateImage(results[0].url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setUnsplashImage({src, caption}) {
|
|
|
|
this.args.updateImage(src);
|
|
|
|
this.args.updateCaption(caption);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleUnsplashSelector() {
|
|
|
|
this.showUnsplashSelector = !this.showUnsplashSelector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleAltEditing() {
|
|
|
|
this.isEditingAlt = !this.isEditingAlt;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
onAltInput(event) {
|
|
|
|
this.args.updateAlt(event.target.value);
|
|
|
|
}
|
2021-07-16 17:00:48 +03:00
|
|
|
|
|
|
|
@action
|
|
|
|
dragOver(event) {
|
|
|
|
if (!event.dataTransfer.files) {
|
|
|
|
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.canDrop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
dragLeave(event) {
|
|
|
|
if (!event.dataTransfer.files) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
this.canDrop = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
drop(setFiles, event) {
|
|
|
|
if (!event.dataTransfer.files) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.canDrop = false;
|
|
|
|
|
|
|
|
setFiles(event.dataTransfer.files);
|
|
|
|
}
|
2023-04-19 13:57:26 +03:00
|
|
|
|
|
|
|
@action
|
|
|
|
saveImage(setFiles, imageFile) {
|
|
|
|
this.canDrop = false;
|
|
|
|
setFiles([imageFile]);
|
|
|
|
}
|
2023-12-07 18:46:23 +03:00
|
|
|
|
|
|
|
@action
|
|
|
|
onTKCountChange(count) {
|
|
|
|
if (this.args.onTKCountChange) {
|
|
|
|
this.tkCount = count;
|
|
|
|
this.args.onTKCountChange(count);
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 19:56:25 +03:00
|
|
|
}
|