2021-11-09 12:39:47 +03:00
|
|
|
import Browser from 'mobiledoc-kit/utils/browser';
|
|
|
|
import Component from '@glimmer/component';
|
2021-11-15 16:18:55 +03:00
|
|
|
import {EmojiButton} from '@joeattardi/emoji-button';
|
2021-11-09 12:39:47 +03:00
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {set} from '@ember/object';
|
|
|
|
|
|
|
|
export default class KoenigCardCalloutComponent extends Component {
|
|
|
|
@service config;
|
|
|
|
@service feature;
|
|
|
|
@service store;
|
|
|
|
@service membersUtils;
|
|
|
|
@service ui;
|
|
|
|
|
2021-11-10 17:45:54 +03:00
|
|
|
get isEmpty() {
|
|
|
|
return isBlank(this.args.payload.calloutText);
|
|
|
|
}
|
|
|
|
|
2021-11-11 17:48:07 +03:00
|
|
|
backgroundColors = [
|
|
|
|
{name: 'Grey', color: 'grey'},
|
|
|
|
{name: 'White', color: 'white'},
|
|
|
|
{name: 'Blue', color: 'blue'},
|
|
|
|
{name: 'Green', color: 'green'},
|
|
|
|
{name: 'Yellow', color: 'yellow'},
|
|
|
|
{name: 'Red', color: 'red'},
|
|
|
|
{name: 'Pink', color: 'pink'},
|
|
|
|
{name: 'Purple', color: 'purple'},
|
|
|
|
{name: 'Brand color', color: 'accent'}
|
|
|
|
];
|
2021-11-10 20:31:00 +03:00
|
|
|
|
|
|
|
get selectedBackgroundColor() {
|
|
|
|
return this.backgroundColors.find(option => option.color === this.args.payload.backgroundColor);
|
2021-11-09 12:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get toolbar() {
|
|
|
|
if (this.args.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.args.editCard)
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.args.registerComponent(this);
|
|
|
|
|
|
|
|
const payloadDefaults = {
|
2021-11-10 20:31:00 +03:00
|
|
|
calloutEmoji: '💡',
|
|
|
|
calloutText: '',
|
2021-11-11 17:48:07 +03:00
|
|
|
backgroundColor: 'grey'
|
2021-11-09 12:39:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.entries(payloadDefaults).forEach(([key, value]) => {
|
|
|
|
if (this.args.payload[key] === undefined) {
|
|
|
|
this._updatePayloadAttr(key, value);
|
|
|
|
}
|
|
|
|
});
|
2021-11-15 16:18:55 +03:00
|
|
|
|
|
|
|
this.picker = new EmojiButton();
|
|
|
|
this.picker.on('emoji', (selection) => {
|
|
|
|
this.setCalloutEmoji(selection.emoji);
|
|
|
|
});
|
2021-11-09 12:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// required for snippet rects to be calculated - editor reaches in to component,
|
|
|
|
// expecting a non-Glimmer component with a .element property
|
|
|
|
@action
|
|
|
|
registerElement(element) {
|
|
|
|
this.element = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setCalloutText(text) {
|
|
|
|
this._updatePayloadAttr('calloutText', text);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setCalloutEmoji(emoji) {
|
|
|
|
this._updatePayloadAttr('calloutEmoji', emoji);
|
|
|
|
}
|
|
|
|
|
2021-11-10 20:31:00 +03:00
|
|
|
@action
|
|
|
|
setBackgroundColor(option) {
|
|
|
|
this._updatePayloadAttr('backgroundColor', option.color);
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:39:47 +03:00
|
|
|
@action
|
|
|
|
leaveEditMode() {
|
2021-11-10 17:45:54 +03:00
|
|
|
if (this.isEmpty) {
|
2021-11-09 12:39:47 +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.args.deleteCard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
focusElement(selector, event) {
|
|
|
|
event.preventDefault();
|
|
|
|
document.querySelector(selector)?.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
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;
|
|
|
|
|
|
|
|
run.scheduleOnce('afterRender', this, this._placeCursorAtEnd);
|
|
|
|
}
|
|
|
|
|
2021-11-15 16:18:55 +03:00
|
|
|
@action
|
|
|
|
changeEmoji(event) {
|
|
|
|
this.picker.togglePicker(event.target);
|
|
|
|
}
|
|
|
|
|
2021-11-10 17:51:11 +03:00
|
|
|
@action
|
|
|
|
toggleEmoji() {
|
|
|
|
this._updatePayloadAttr('calloutEmoji', this.args.payload.calloutEmoji ? '' : '💡');
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:39:47 +03:00
|
|
|
_enter(modifier) {
|
|
|
|
if (this.args.isEditing && (modifier === 'meta' || (modifier === 'crtl' && Browser.isWin()))) {
|
|
|
|
this.args.editCard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_placeCursorAtEnd() {
|
|
|
|
if (!this._textReplacementEditor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tailPosition = this._textReplacementEditor.post.tailPosition();
|
|
|
|
let rangeToSelect = tailPosition.toRange();
|
|
|
|
this._textReplacementEditor.selectRange(rangeToSelect);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updatePayloadAttr(attr, value) {
|
|
|
|
let payload = this.args.payload;
|
|
|
|
|
|
|
|
set(payload, attr, value);
|
|
|
|
|
|
|
|
// update the mobiledoc and stay in edit mode
|
|
|
|
this.args.saveCard?.(payload, false);
|
|
|
|
}
|
|
|
|
}
|