2021-11-10 11:13:53 +03:00
|
|
|
import Browser from 'mobiledoc-kit/utils/browser';
|
2021-11-09 18:06:01 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
2021-11-10 11:13:53 +03:00
|
|
|
import {formatTextReplacementHtml} from './koenig-text-replacement-html-input';
|
2021-11-09 18:06:01 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {set} from '@ember/object';
|
|
|
|
|
2021-12-02 22:22:46 +03:00
|
|
|
export default class KoenigCardToggleComponent extends Component {
|
2021-11-09 18:06:01 +03:00
|
|
|
@service config;
|
|
|
|
@service feature;
|
|
|
|
@service store;
|
|
|
|
@service membersUtils;
|
|
|
|
@service ui;
|
|
|
|
|
2021-11-10 11:13:53 +03:00
|
|
|
get formattedHeading() {
|
|
|
|
return formatTextReplacementHtml(this.args.payload.heading);
|
|
|
|
}
|
|
|
|
|
|
|
|
get formattedContent() {
|
|
|
|
return formatTextReplacementHtml(this.args.payload.content);
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:06:01 +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 = {};
|
|
|
|
|
|
|
|
Object.entries(payloadDefaults).forEach(([key, value]) => {
|
|
|
|
if (this.args.payload[key] === undefined) {
|
|
|
|
this._updatePayloadAttr(key, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-11-10 11:13:53 +03:00
|
|
|
setContentText(text) {
|
|
|
|
this._updatePayloadAttr('content', text);
|
2021-11-09 18:06:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2021-11-10 11:13:53 +03:00
|
|
|
setHeadingText(text) {
|
|
|
|
this._updatePayloadAttr('heading', text);
|
2021-11-09 18:06:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
leaveEditMode() {
|
|
|
|
const {html, content, heading} = this.args.payload;
|
|
|
|
|
|
|
|
if (isBlank(html) && isBlank(heading) && isBlank(content)) {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2021-12-02 22:44:05 +03:00
|
|
|
@action
|
2021-12-02 23:08:52 +03:00
|
|
|
handleTab() {
|
|
|
|
let contentInput = this.element.querySelector('.kg-toggle-card-content .koenig-basic-html-textarea__editor');
|
2021-12-02 22:44:05 +03:00
|
|
|
|
|
|
|
if (contentInput) {
|
|
|
|
contentInput.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 11:13:53 +03:00
|
|
|
@action
|
2021-12-02 23:08:52 +03:00
|
|
|
registerContentEditor(textReplacementEditor) {
|
2021-11-10 11:13:53 +03:00
|
|
|
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;
|
|
|
|
|
2021-12-02 23:08:52 +03:00
|
|
|
run.scheduleOnce('afterRender', this, this._afterRender);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
registerHeadingEditor(textReplacementEditor) {
|
|
|
|
let commands = {
|
|
|
|
ENTER: this.handleTab,
|
|
|
|
TAB: this.handleTab,
|
|
|
|
'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;
|
|
|
|
|
2021-11-18 09:16:43 +03:00
|
|
|
run.scheduleOnce('afterRender', this, this._afterRender);
|
2021-11-10 11:13:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_enter(modifier) {
|
|
|
|
if (this.args.isEditing && (modifier === 'meta' || (modifier === 'crtl' && Browser.isWin()))) {
|
|
|
|
this.args.editCard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 09:16:43 +03:00
|
|
|
_afterRender() {
|
|
|
|
this._placeCursorAtEnd();
|
|
|
|
this._focusInput();
|
|
|
|
}
|
|
|
|
|
2021-11-10 11:13:53 +03:00
|
|
|
_placeCursorAtEnd() {
|
|
|
|
if (!this._textReplacementEditor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tailPosition = this._textReplacementEditor.post.tailPosition();
|
|
|
|
let rangeToSelect = tailPosition.toRange();
|
|
|
|
this._textReplacementEditor.selectRange(rangeToSelect);
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:06:01 +03:00
|
|
|
_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);
|
|
|
|
}
|
2021-11-18 09:16:43 +03:00
|
|
|
|
|
|
|
_focusInput() {
|
2021-12-02 22:22:46 +03:00
|
|
|
let headingInput = this.element.querySelector('.kg-toggle-card-heading .koenig-basic-html-input__editor');
|
2021-11-18 09:16:43 +03:00
|
|
|
|
|
|
|
if (headingInput) {
|
|
|
|
headingInput.focus();
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 18:06:01 +03:00
|
|
|
}
|