2020-04-06 12:56:40 +03:00
|
|
|
import Browser from 'mobiledoc-kit/utils/browser';
|
|
|
|
import Component from '@ember/component';
|
2020-04-17 18:00:19 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {formatTextReplacementHtml} from './koenig-text-replacement-html-input';
|
2020-04-06 12:56:40 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
import {set} from '@ember/object';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
// attrs
|
|
|
|
payload: null,
|
|
|
|
isSelected: false,
|
|
|
|
isEditing: false,
|
|
|
|
|
|
|
|
// closure actions
|
|
|
|
selectCard() {},
|
|
|
|
deselectCard() {},
|
|
|
|
editCard() {},
|
|
|
|
saveCard() {},
|
|
|
|
deleteCard() {},
|
|
|
|
moveCursorToNextSection() {},
|
|
|
|
moveCursorToPrevSection() {},
|
|
|
|
addParagraphAfterCard() {},
|
|
|
|
registerComponent() {},
|
|
|
|
|
2021-11-10 17:45:54 +03:00
|
|
|
isEmpty: computed('payload.html', function () {
|
|
|
|
return isBlank(this.payload.html);
|
|
|
|
}),
|
|
|
|
|
2020-04-17 18:00:19 +03:00
|
|
|
formattedHtml: computed('payload.html', function () {
|
|
|
|
return formatTextReplacementHtml(this.payload.html);
|
|
|
|
}),
|
|
|
|
|
2020-12-03 13:26:21 +03:00
|
|
|
toolbar: computed('isEditing', function () {
|
|
|
|
if (this.isEditing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
items: [{
|
|
|
|
buttonClass: 'fw4 flex items-center white',
|
|
|
|
icon: 'koenig/kg-edit',
|
|
|
|
iconClass: 'fill-white',
|
|
|
|
title: 'Edit',
|
|
|
|
text: '',
|
|
|
|
action: run.bind(this, this.editCard)
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2020-04-06 12:56:40 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.registerComponent(this);
|
2020-04-23 14:11:23 +03:00
|
|
|
|
|
|
|
if (!this.payload.html) {
|
|
|
|
this._updatePayloadAttr('html', '<p>Hey {first_name, "there"},</p>');
|
|
|
|
}
|
2020-04-06 12:56:40 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
updateHtml(html) {
|
|
|
|
this._updatePayloadAttr('html', html);
|
|
|
|
},
|
|
|
|
|
|
|
|
registerEditor(textReplacementEditor) {
|
|
|
|
let commands = {
|
|
|
|
'META+ENTER': run.bind(this, this._enter, 'meta'),
|
|
|
|
'CTRL+ENTER': run.bind(this, this._enter, 'ctrl')
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(commands).forEach((str) => {
|
|
|
|
textReplacementEditor.registerKeyCommand({
|
|
|
|
str,
|
|
|
|
run() {
|
|
|
|
return commands[str](textReplacementEditor, str);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this._textReplacementEditor = textReplacementEditor;
|
2020-04-23 14:11:23 +03:00
|
|
|
|
|
|
|
run.scheduleOnce('afterRender', this, this._placeCursorAtEnd);
|
2020-04-06 12:56:40 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
leaveEditMode() {
|
2021-11-10 17:45:54 +03:00
|
|
|
if (this.isEmpty) {
|
2020-04-06 12:56:40 +03:00
|
|
|
// afterRender is required to avoid double modification of `isSelected`
|
|
|
|
// TODO: see if there's a way to avoid afterRender
|
|
|
|
run.scheduleOnce('afterRender', this, this.deleteCard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_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);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* key commands ----------------------------------------------------------*/
|
|
|
|
|
|
|
|
_enter(modifier) {
|
|
|
|
if (this.isEditing && (modifier === 'meta' || (modifier === 'crtl' && Browser.isWin()))) {
|
|
|
|
this.editCard();
|
|
|
|
}
|
2020-04-23 14:11:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_placeCursorAtEnd() {
|
|
|
|
if (!this._textReplacementEditor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tailPosition = this._textReplacementEditor.post.tailPosition();
|
|
|
|
let rangeToSelect = tailPosition.toRange();
|
|
|
|
this._textReplacementEditor.selectRange(rangeToSelect);
|
2020-04-06 12:56:40 +03:00
|
|
|
}
|
|
|
|
});
|