2015-03-13 12:39:28 +03:00
|
|
|
import Ember from 'ember';
|
2016-06-30 21:14:25 +03:00
|
|
|
import Component from 'ember-component';
|
|
|
|
import EmberObject from 'ember-object';
|
|
|
|
import run from 'ember-runloop';
|
|
|
|
import {A as emberA} from 'ember-array/utils';
|
2016-05-24 15:06:59 +03:00
|
|
|
import {formatMarkdown} from 'ghost-admin/helpers/gh-format-markdown';
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-06-30 21:14:25 +03:00
|
|
|
// ember-cli-shims doesn't export uuid
|
|
|
|
const {uuid} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
2015-11-20 19:40:41 +03:00
|
|
|
_scrollWrapper: null,
|
|
|
|
|
2016-05-08 12:20:26 +03:00
|
|
|
previewHTML: '',
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2016-06-11 19:52:36 +03:00
|
|
|
this.set('imageUploadComponents', emberA([]));
|
2016-05-08 12:20:26 +03:00
|
|
|
this.buildPreviewHTML();
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
didInsertElement() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
2015-11-20 19:40:41 +03:00
|
|
|
this._scrollWrapper = this.$().closest('.entry-preview-content');
|
2015-10-06 15:14:00 +03:00
|
|
|
this.adjustScrollPosition(this.get('scrollPosition'));
|
2015-03-13 12:39:28 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
didReceiveAttrs(attrs) {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (!attrs.oldAttrs) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-06 15:14:00 +03:00
|
|
|
|
|
|
|
if (attrs.newAttrs.scrollPosition && attrs.newAttrs.scrollPosition.value !== attrs.oldAttrs.scrollPosition.value) {
|
|
|
|
this.adjustScrollPosition(attrs.newAttrs.scrollPosition.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attrs.newAttrs.markdown.value !== attrs.oldAttrs.markdown.value) {
|
2016-05-08 12:20:26 +03:00
|
|
|
run.throttle(this, this.buildPreviewHTML, 30, false);
|
2015-10-06 15:14:00 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
adjustScrollPosition(scrollPosition) {
|
|
|
|
let scrollWrapper = this._scrollWrapper;
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2015-06-13 17:34:09 +03:00
|
|
|
if (scrollWrapper) {
|
|
|
|
scrollWrapper.scrollTop(scrollPosition);
|
|
|
|
}
|
2015-10-06 15:14:00 +03:00
|
|
|
},
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-05-08 12:20:26 +03:00
|
|
|
buildPreviewHTML() {
|
|
|
|
let markdown = this.get('markdown');
|
|
|
|
let html = formatMarkdown([markdown]).string;
|
|
|
|
let template = document.createElement('template');
|
|
|
|
template.innerHTML = html;
|
|
|
|
let fragment = template.content;
|
|
|
|
let dropzones = fragment.querySelectorAll('.js-drop-zone');
|
|
|
|
let components = this.get('imageUploadComponents');
|
|
|
|
|
|
|
|
if (dropzones.length !== components.length) {
|
2016-06-11 19:52:36 +03:00
|
|
|
components = emberA([]);
|
2016-05-08 12:20:26 +03:00
|
|
|
this.set('imageUploadComponents', components);
|
|
|
|
}
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-05-08 12:20:26 +03:00
|
|
|
[...dropzones].forEach((oldEl, i) => {
|
|
|
|
let el = oldEl.cloneNode(true);
|
|
|
|
let component = components[i];
|
|
|
|
let uploadTarget = el.querySelector('.js-upload-target');
|
2016-02-26 16:25:47 +03:00
|
|
|
let id = uuid();
|
|
|
|
let destinationElementId = `image-uploader-${id}`;
|
2016-05-08 12:20:26 +03:00
|
|
|
let src;
|
|
|
|
|
|
|
|
if (uploadTarget) {
|
|
|
|
src = uploadTarget.getAttribute('src');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (component) {
|
|
|
|
component.set('destinationElementId', destinationElementId);
|
|
|
|
component.set('src', src);
|
|
|
|
} else {
|
2016-06-11 19:52:36 +03:00
|
|
|
let imageUpload = EmberObject.create({
|
2016-05-08 12:20:26 +03:00
|
|
|
destinationElementId,
|
|
|
|
id,
|
|
|
|
src,
|
|
|
|
index: i
|
|
|
|
});
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-05-08 12:20:26 +03:00
|
|
|
this.get('imageUploadComponents').pushObject(imageUpload);
|
|
|
|
}
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
el.id = destinationElementId;
|
2016-05-08 12:20:26 +03:00
|
|
|
el.innerHTML = '';
|
|
|
|
el.classList.remove('image-uploader');
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-05-18 23:52:07 +03:00
|
|
|
oldEl.parentNode.replaceChild(el, oldEl);
|
2016-02-26 16:25:47 +03:00
|
|
|
});
|
2016-05-08 12:20:26 +03:00
|
|
|
|
|
|
|
this.set('previewHTML', fragment);
|
2016-02-26 16:25:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
updateImageSrc(index, url) {
|
|
|
|
this.attrs.updateImageSrc(index, url);
|
|
|
|
},
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
updateHeight() {
|
|
|
|
this.attrs.updateHeight(this.$().height());
|
2015-10-06 15:14:00 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-13 12:39:28 +03:00
|
|
|
});
|