2018-05-15 17:26:34 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import Ember from 'ember';
|
|
|
|
import {computed} from '@ember/object';
|
2019-04-16 10:20:59 +03:00
|
|
|
import {utils as ghostHelperUtils} from '@tryghost/helpers';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2018-05-15 17:26:34 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
import {set} from '@ember/object';
|
|
|
|
|
|
|
|
const {Handlebars} = Ember;
|
2019-04-16 10:20:59 +03:00
|
|
|
const {countWords} = ghostHelperUtils;
|
2018-05-15 17:26:34 +03:00
|
|
|
|
2019-04-30 17:46:29 +03:00
|
|
|
const CM_MODE_MAP = {
|
|
|
|
html: 'htmlmixed',
|
|
|
|
xhtml: 'htmlmixed',
|
|
|
|
hbs: 'handlebars',
|
|
|
|
js: 'javascript'
|
|
|
|
};
|
|
|
|
|
2018-05-15 17:26:34 +03:00
|
|
|
export default Component.extend({
|
|
|
|
// attrs
|
|
|
|
payload: null,
|
|
|
|
isSelected: false,
|
|
|
|
isEditing: false,
|
|
|
|
headerOffset: 0,
|
2019-04-30 17:46:29 +03:00
|
|
|
showLanguageInput: true,
|
2018-05-15 17:26:34 +03:00
|
|
|
|
|
|
|
// closure actions
|
|
|
|
editCard() {},
|
|
|
|
saveCard() {},
|
|
|
|
selectCard() {},
|
2018-05-23 13:59:20 +03:00
|
|
|
deselectCard() {},
|
2018-05-15 17:26:34 +03:00
|
|
|
deleteCard() {},
|
2018-07-20 17:53:21 +03:00
|
|
|
registerComponent() {},
|
2019-04-30 19:14:49 +03:00
|
|
|
moveCursorToNextSection() {},
|
|
|
|
moveCursorToPrevSection() {},
|
|
|
|
addParagraphAfterCard() {},
|
2018-07-20 17:53:21 +03:00
|
|
|
|
2021-11-10 17:45:54 +03:00
|
|
|
isEmpty: computed('payload.code', function () {
|
|
|
|
return isBlank(this.payload.code);
|
|
|
|
}),
|
|
|
|
|
2018-07-20 17:53:21 +03:00
|
|
|
counts: computed('payload.code', function () {
|
|
|
|
return {wordCount: countWords(this.payload.code)};
|
|
|
|
}),
|
2018-05-15 17:26:34 +03:00
|
|
|
|
|
|
|
toolbar: computed('isEditing', function () {
|
2019-06-24 18:33:21 +03:00
|
|
|
if (this.isEditing) {
|
|
|
|
return false;
|
2018-05-15 17:26:34 +03:00
|
|
|
}
|
2019-06-24 18:33:21 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
items: [{
|
|
|
|
buttonClass: 'fw4 flex items-center white',
|
|
|
|
icon: 'koenig/kg-edit',
|
|
|
|
iconClass: 'fill-white',
|
|
|
|
title: 'Edit',
|
|
|
|
text: '',
|
|
|
|
action: run.bind(this, this.editCard)
|
|
|
|
}]
|
|
|
|
};
|
2018-05-15 17:26:34 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
escapedCode: computed('payload.code', function () {
|
|
|
|
let escapedCode = Handlebars.Utils.escapeExpression(this.payload.code);
|
|
|
|
return htmlSafe(escapedCode);
|
|
|
|
}),
|
|
|
|
|
2019-04-30 17:46:29 +03:00
|
|
|
cmMode: computed('payload.language', function () {
|
|
|
|
let {language} = this.payload;
|
|
|
|
return CM_MODE_MAP[language] || language;
|
|
|
|
}),
|
|
|
|
|
|
|
|
cardStyle: computed('isEditing', function () {
|
|
|
|
let style = this.isEditing ? 'background-color: #f4f8fb; border-color: #f4f8fb' : '';
|
|
|
|
return htmlSafe(style);
|
|
|
|
}),
|
|
|
|
|
|
|
|
languageInputStyle: computed('showLanguageInput', function () {
|
|
|
|
let styles = ['top: 6px', 'right: 6px'];
|
|
|
|
if (!this.showLanguageInput) {
|
|
|
|
styles.push('opacity: 0');
|
|
|
|
}
|
|
|
|
return htmlSafe(styles.join('; '));
|
|
|
|
}),
|
|
|
|
|
2018-05-15 17:26:34 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
let payload = this.payload || {};
|
|
|
|
|
|
|
|
// CodeMirror errors on a `null` or `undefined` value
|
|
|
|
if (!payload.code) {
|
|
|
|
set(payload, 'code', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('payload', payload);
|
2018-07-20 17:53:21 +03:00
|
|
|
|
|
|
|
this.registerComponent(this);
|
2018-05-15 17:26:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
updateCode(code) {
|
2019-04-30 17:46:29 +03:00
|
|
|
this._hideLanguageInput();
|
2018-05-15 17:26:34 +03:00
|
|
|
this._updatePayloadAttr('code', code);
|
|
|
|
},
|
|
|
|
|
2019-04-30 19:14:49 +03:00
|
|
|
updateCaption(caption) {
|
|
|
|
this._updatePayloadAttr('caption', caption);
|
|
|
|
},
|
|
|
|
|
2019-04-30 17:46:29 +03:00
|
|
|
enterEditMode() {
|
|
|
|
this._addMousemoveHandler();
|
|
|
|
},
|
|
|
|
|
2018-05-15 17:26:34 +03:00
|
|
|
leaveEditMode() {
|
2019-04-30 17:46:29 +03:00
|
|
|
this._removeMousemoveHandler();
|
|
|
|
|
2021-11-10 17:45:54 +03:00
|
|
|
if (this.isEmpty) {
|
2018-05-15 17:26:34 +03:00
|
|
|
// afterRender is required to avoid double modification of `isSelected`
|
|
|
|
// TODO: see if there's a way to avoid afterRender
|
2020-01-16 20:01:12 +03:00
|
|
|
run.scheduleOnce('afterRender', this, this.deleteCard);
|
2018-05-15 17:26:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updatePayloadAttr(attr, value) {
|
|
|
|
let payload = this.payload;
|
|
|
|
let save = this.saveCard;
|
|
|
|
|
|
|
|
set(payload, attr, value);
|
|
|
|
|
|
|
|
// update the mobiledoc and stay in edit mode
|
|
|
|
save(payload, false);
|
2019-04-30 17:46:29 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_hideLanguageInput() {
|
|
|
|
this.set('showLanguageInput', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
_showLanguageInput() {
|
|
|
|
this.set('showLanguageInput', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
_addMousemoveHandler() {
|
|
|
|
this._mousemoveHandler = run.bind(this, this._showLanguageInput);
|
|
|
|
window.addEventListener('mousemove', this._mousemoveHandler);
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeMousemoveHandler() {
|
|
|
|
window.removeEventListener('mousemove', this._mousemoveHandler);
|
2018-05-15 17:26:34 +03:00
|
|
|
}
|
|
|
|
});
|