Ghost/ghost/admin/lib/koenig-editor/addon/components/koenig-card-code.js

163 lines
3.9 KiB
JavaScript
Raw Normal View History

import Component from '@ember/component';
import Ember from 'ember';
import classic from 'ember-classic-decorator';
import {action, computed, set} from '@ember/object';
import {utils as ghostHelperUtils} from '@tryghost/helpers';
import {htmlSafe} from '@ember/template';
import {isBlank} from '@ember/utils';
import {run} from '@ember/runloop';
const {Handlebars} = Ember;
const {countWords} = ghostHelperUtils;
const CM_MODE_MAP = {
html: 'htmlmixed',
xhtml: 'htmlmixed',
hbs: 'handlebars',
js: 'javascript'
};
@classic
export default class KoenigCardCode extends Component {
// attrs
payload = null;
isSelected = false;
isEditing = false;
headerOffset = 0;
showLanguageInput = true;
// closure actions
editCard() {}
saveCard() {}
selectCard() {}
deselectCard() {}
deleteCard() {}
registerComponent() {}
moveCursorToNextSection() {}
moveCursorToPrevSection() {}
addParagraphAfterCard() {}
@computed('payload.code')
get isEmpty() {
Removed duplication of "delete if empty" card logic no issue The logic for "delete if empty" was duplicated in two places: 1. when `createComponentCard` is used to register a card, this option method was used when cleaning up a post when first rendering (cards in empty state can be saved before the editor auto-removes them but we don't want to show them again) 2. inside of card's own delete-if-empty handling on certain actions such as deselection or leaving edit mode - added an `ifEmpty` property to each card component - used by the editor's first-render cleanup routing if the property is present - can be re-used internally for the card's own deselect/exit-edit-mode behaviour - updated the cleanup routine in `<KoenigEditor>` - added a `allComponentCardsRegistered` property that will return `true` when the `.component` property is set on every card (the property is set during card component initialisation so we're at the mercy of Ember's render process so not all card components will be immediately registered) - swapped `_cleanup` for `_cleanupTask` that will wait for `allComponentCardsRegistered` to be `true` before performing cleanup, ensuring that we always have access to the card component's `isEmpty` property even when Ember renders cards across multiple render batches - checks for `isEmpty` being a boolean and will delete the card if it's value is `true` - updated all cards that had delete-if-empty behaviour - added `isEmpty` properties - removed duplicated logic in the `createComponentCard` calls
2021-11-10 17:45:54 +03:00
return isBlank(this.payload.code);
}
Removed duplication of "delete if empty" card logic no issue The logic for "delete if empty" was duplicated in two places: 1. when `createComponentCard` is used to register a card, this option method was used when cleaning up a post when first rendering (cards in empty state can be saved before the editor auto-removes them but we don't want to show them again) 2. inside of card's own delete-if-empty handling on certain actions such as deselection or leaving edit mode - added an `ifEmpty` property to each card component - used by the editor's first-render cleanup routing if the property is present - can be re-used internally for the card's own deselect/exit-edit-mode behaviour - updated the cleanup routine in `<KoenigEditor>` - added a `allComponentCardsRegistered` property that will return `true` when the `.component` property is set on every card (the property is set during card component initialisation so we're at the mercy of Ember's render process so not all card components will be immediately registered) - swapped `_cleanup` for `_cleanupTask` that will wait for `allComponentCardsRegistered` to be `true` before performing cleanup, ensuring that we always have access to the card component's `isEmpty` property even when Ember renders cards across multiple render batches - checks for `isEmpty` being a boolean and will delete the card if it's value is `true` - updated all cards that had delete-if-empty behaviour - added `isEmpty` properties - removed duplicated logic in the `createComponentCard` calls
2021-11-10 17:45:54 +03:00
@computed('payload.code')
get counts() {
return {wordCount: countWords(this.payload.code)};
}
@computed('isEditing')
get toolbar() {
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)
}]
};
}
@computed('payload.code')
get escapedCode() {
let escapedCode = Handlebars.Utils.escapeExpression(this.payload.code);
return htmlSafe(escapedCode);
}
@computed('payload.language')
get cmMode() {
let {language} = this.payload;
return CM_MODE_MAP[language] || language;
}
@computed('isEditing')
get cardStyle() {
let style = this.isEditing ? 'background-color: #f4f8fb; border-color: #f4f8fb' : '';
return htmlSafe(style);
}
@computed('showLanguageInput')
get languageInputStyle() {
let styles = ['top: 6px', 'right: 6px'];
if (!this.showLanguageInput) {
styles.push('opacity: 0');
}
return htmlSafe(styles.join('; '));
}
init() {
super.init(...arguments);
let payload = this.payload || {};
// CodeMirror errors on a `null` or `undefined` value
if (!payload.code) {
set(payload, 'code', '');
}
this.set('payload', payload);
this.registerComponent(this);
}
@action
updateCode(code) {
this._hideLanguageInput();
this._updatePayloadAttr('code', code);
}
@action
updateCaption(caption) {
this._updatePayloadAttr('caption', caption);
}
@action
enterEditMode() {
this._addMousemoveHandler();
}
@action
leaveEditMode() {
this._removeMousemoveHandler();
if (this.isEmpty) {
// 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);
}
_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);
}
}