2018-01-31 11:32:27 +03:00
|
|
|
import Component from '@ember/component';
|
2018-02-13 21:00:54 +03:00
|
|
|
import formatMarkdown from 'ghost-admin/utils/format-markdown';
|
2018-01-31 11:32:27 +03:00
|
|
|
import layout from '../templates/components/koenig-card-markdown';
|
2018-02-13 21:00:54 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {htmlSafe} from '@ember/string';
|
2018-03-15 20:54:15 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2018-02-13 21:00:54 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2018-01-31 11:32:27 +03:00
|
|
|
import {set} from '@ember/object';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
layout,
|
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
// attrs
|
2018-01-31 11:32:27 +03:00
|
|
|
payload: null,
|
2018-02-13 21:00:54 +03:00
|
|
|
isSelected: false,
|
|
|
|
isEditing: false,
|
|
|
|
|
|
|
|
// properties
|
|
|
|
toolbar: null,
|
|
|
|
|
|
|
|
// closure actions
|
2018-03-15 20:54:15 +03:00
|
|
|
saveCard() {},
|
|
|
|
selectCard() {},
|
|
|
|
deleteCard() {},
|
2018-02-13 21:00:54 +03:00
|
|
|
|
|
|
|
renderedMarkdown: computed('payload.markdown', function () {
|
|
|
|
return htmlSafe(formatMarkdown(this.get('payload.markdown')));
|
|
|
|
}),
|
2018-01-31 11:32:27 +03:00
|
|
|
|
2018-02-15 13:46:23 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set('toolbar', {
|
|
|
|
items: [{
|
|
|
|
icon: 'trash',
|
|
|
|
title: 'Delete',
|
|
|
|
action: () => {}
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-31 11:32:27 +03:00
|
|
|
actions: {
|
2018-02-13 21:00:54 +03:00
|
|
|
enterEditMode() {
|
|
|
|
// this action is called before the component is rendered so we
|
|
|
|
// need to wait to ensure that the textarea element is present
|
|
|
|
run.scheduleOnce('afterRender', this, this._focusTextarea);
|
|
|
|
},
|
|
|
|
|
2018-03-15 20:54:15 +03:00
|
|
|
leaveEditMode() {
|
|
|
|
if (isBlank(this.get('payload.markdown'))) {
|
|
|
|
// afterRender is required to avoid double modification of `isSelected`
|
|
|
|
// TODO: see if there's a way to avoid afterRender
|
|
|
|
run.scheduleOnce('afterRender', this, function () {
|
|
|
|
this.deleteCard();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-31 11:32:27 +03:00
|
|
|
updateMarkdown(markdown) {
|
|
|
|
let payload = this.get('payload');
|
|
|
|
let save = this.get('saveCard');
|
|
|
|
|
|
|
|
set(payload, 'markdown', markdown);
|
|
|
|
|
|
|
|
// update the mobiledoc and stay in edit mode
|
|
|
|
save(payload, false);
|
|
|
|
}
|
2018-02-13 21:00:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_focusTextarea() {
|
|
|
|
this.element.querySelector('textarea').focus();
|
2018-01-31 11:32:27 +03:00
|
|
|
}
|
|
|
|
});
|