2018-01-31 11:32:27 +03:00
|
|
|
import Component from '@ember/component';
|
2018-02-13 21:00:54 +03:00
|
|
|
import formatMarkdown from 'ghost-admin/utils/format-markdown';
|
|
|
|
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-03-15 20:54:15 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2018-02-13 21:00:54 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2018-01-31 11:32:27 +03:00
|
|
|
import {set} from '@ember/object';
|
2018-07-18 16:34:11 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2018-01-31 11:32:27 +03:00
|
|
|
|
2019-04-16 10:20:59 +03:00
|
|
|
const {countWords, countImages} = ghostHelperUtils;
|
2018-04-24 15:10:20 +03:00
|
|
|
const MIN_HEIGHT = 130;
|
|
|
|
|
2018-01-31 11:32:27 +03:00
|
|
|
export default Component.extend({
|
2018-02-13 21:00:54 +03:00
|
|
|
// attrs
|
2018-01-31 11:32:27 +03:00
|
|
|
payload: null,
|
2018-02-13 21:00:54 +03:00
|
|
|
isSelected: false,
|
|
|
|
isEditing: false,
|
2018-05-01 17:55:51 +03:00
|
|
|
headerOffset: 0,
|
2018-02-13 21:00:54 +03:00
|
|
|
|
2018-04-24 15:10:20 +03:00
|
|
|
// internal attrs
|
|
|
|
bottomOffset: 0,
|
2018-07-18 16:34:11 +03:00
|
|
|
preventClick: false,
|
2018-04-24 15:10:20 +03:00
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
// closure actions
|
2018-04-03 20:34:01 +03:00
|
|
|
editCard() {},
|
2018-03-15 20:54:15 +03:00
|
|
|
saveCard() {},
|
|
|
|
selectCard() {},
|
2018-05-23 13:59:20 +03:00
|
|
|
deselectCard() {},
|
2018-03-15 20:54:15 +03:00
|
|
|
deleteCard() {},
|
2018-07-20 17:53:21 +03:00
|
|
|
registerComponent() {},
|
|
|
|
|
2021-11-10 17:45:54 +03:00
|
|
|
isEmpty: computed('payload.markdown', function () {
|
|
|
|
return isBlank(this.payload.markdown);
|
|
|
|
}),
|
|
|
|
|
2018-07-20 17:53:21 +03:00
|
|
|
counts: computed('renderedMarkdown', function () {
|
|
|
|
return {
|
2019-04-16 10:20:59 +03:00
|
|
|
wordCount: countWords(this.renderedMarkdown),
|
2018-07-20 17:53:21 +03:00
|
|
|
imageCount: countImages(this.renderedMarkdown)
|
|
|
|
};
|
|
|
|
}),
|
2018-02-13 21:00:54 +03:00
|
|
|
|
|
|
|
renderedMarkdown: computed('payload.markdown', function () {
|
2018-05-01 19:13:53 +03:00
|
|
|
return htmlSafe(formatMarkdown(this.payload.markdown));
|
2018-02-13 21:00:54 +03:00
|
|
|
}),
|
2018-01-31 11:32:27 +03:00
|
|
|
|
2018-04-03 20:34:01 +03:00
|
|
|
toolbar: computed('isEditing', function () {
|
2019-06-24 18:33:21 +03:00
|
|
|
if (this.isEditing) {
|
|
|
|
return false;
|
2018-04-03 20:34:01 +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-04-03 20:34:01 +03:00
|
|
|
}),
|
2018-02-15 13:46:23 +03:00
|
|
|
|
2018-04-24 15:10:20 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2018-05-01 19:13:53 +03:00
|
|
|
|
|
|
|
if (!this.payload) {
|
|
|
|
this.set('payload', {});
|
|
|
|
}
|
|
|
|
|
2018-04-24 15:10:20 +03:00
|
|
|
// subtract toolbar height from MIN_HEIGHT so the trigger happens at
|
|
|
|
// the expected position without forcing the min height to be too small
|
2020-10-01 12:06:53 +03:00
|
|
|
this.set('bottomOffset', -MIN_HEIGHT);
|
2018-07-20 17:53:21 +03:00
|
|
|
|
|
|
|
this.registerComponent(this);
|
2018-04-24 15:10:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._teardownResizeHandler();
|
|
|
|
},
|
|
|
|
|
2018-01-31 11:32:27 +03:00
|
|
|
actions: {
|
2018-02-13 21:00:54 +03:00
|
|
|
enterEditMode() {
|
2018-07-18 16:34:11 +03:00
|
|
|
this._preventAccidentalClick.perform();
|
2018-02-13 21:00:54 +03:00
|
|
|
},
|
|
|
|
|
2018-03-15 20:54:15 +03:00
|
|
|
leaveEditMode() {
|
2021-11-10 17:45:54 +03:00
|
|
|
if (this.isEmpty) {
|
2018-03-15 20:54:15 +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-03-15 20:54:15 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-31 11:32:27 +03:00
|
|
|
updateMarkdown(markdown) {
|
2018-05-01 19:13:53 +03:00
|
|
|
let payload = this.payload;
|
|
|
|
let save = this.saveCard;
|
2018-01-31 11:32:27 +03:00
|
|
|
|
|
|
|
set(payload, 'markdown', markdown);
|
|
|
|
|
|
|
|
// update the mobiledoc and stay in edit mode
|
|
|
|
save(payload, false);
|
2018-04-24 15:10:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// fires if top comes into view 0 px from viewport top
|
|
|
|
// fires if top comes into view MIN_HEIGHTpx above viewport bottom
|
|
|
|
topEntered() {
|
|
|
|
this._isTopVisible = true;
|
|
|
|
run.scheduleOnce('actions', this, this._applyToolbarStyles);
|
|
|
|
},
|
|
|
|
|
|
|
|
// fires if top leaves viewport 0 px from viewport top
|
|
|
|
// fires if top leaves viewport MIN_HEIGHTpx above viewport bottom
|
|
|
|
topExited() {
|
|
|
|
let top = this._topElement.getBoundingClientRect().top;
|
|
|
|
this._isTopVisible = false;
|
|
|
|
this._isTopAbove = top < 0;
|
|
|
|
run.scheduleOnce('actions', this, this._applyToolbarStyles);
|
|
|
|
},
|
|
|
|
|
|
|
|
bottomEntered() {
|
|
|
|
this._isBottomVisible = true;
|
|
|
|
run.scheduleOnce('actions', this, this._applyToolbarStyles);
|
|
|
|
},
|
|
|
|
|
|
|
|
bottomExited() {
|
|
|
|
let top = this._bottomElement.getBoundingClientRect().top;
|
|
|
|
this._isBottomVisible = false;
|
|
|
|
this._isBottomBelow = top > window.innerHeight;
|
|
|
|
run.scheduleOnce('actions', this, this._applyToolbarStyles);
|
|
|
|
},
|
|
|
|
|
|
|
|
registerTop(element) {
|
|
|
|
this._topElement = element;
|
|
|
|
},
|
|
|
|
|
|
|
|
registerBottom(element) {
|
|
|
|
this._bottomElement = element;
|
2018-01-31 11:32:27 +03:00
|
|
|
}
|
2018-02-13 21:00:54 +03:00
|
|
|
},
|
|
|
|
|
2018-04-24 15:10:20 +03:00
|
|
|
_applyToolbarStyles() {
|
|
|
|
let toolbar = this.element.querySelector('.editor-toolbar');
|
|
|
|
|
|
|
|
if (!toolbar) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let {left, width} = this._containerDimensions();
|
|
|
|
|
|
|
|
let style = '';
|
|
|
|
let stuckTop = `top: ${MIN_HEIGHT}px; bottom: auto`;
|
|
|
|
let fixedBottom = `position: fixed; left: ${left + 1}px; width: ${width - 2}px`;
|
|
|
|
let stuckBottom = '';
|
|
|
|
|
|
|
|
if (this._isTopVisible && this._isBottomVisible) {
|
|
|
|
style = stuckBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._isTopVisible && !this._isBottomVisible) {
|
|
|
|
style = fixedBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._isTopVisible && !this._isTopAbove) {
|
|
|
|
style = stuckTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._isTopVisible && this._isBottomVisible) {
|
|
|
|
style = stuckBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._isTopVisible && !this._isBottomVisible && this._isTopAbove && this._isBottomBelow) {
|
|
|
|
style = fixedBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up resize watchers if in fixed position because we have to
|
|
|
|
// recalculate left position and width
|
|
|
|
if (!this._resizeHandler && style === fixedBottom) {
|
|
|
|
this._setupResizeHandler();
|
|
|
|
} else if (this._resizeHandler && style !== fixedBottom) {
|
|
|
|
this._teardownResizeHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
// account for the mobile nav bar when in fixed position
|
|
|
|
if (style === fixedBottom) {
|
|
|
|
let mobileNav = document.querySelector('.gh-mobile-nav-bar');
|
|
|
|
|
|
|
|
if (mobileNav.offsetHeight) {
|
|
|
|
style = `${style}; bottom: ${mobileNav.offsetHeight}px`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toolbar.setAttribute('style', style);
|
|
|
|
},
|
|
|
|
|
|
|
|
_containerDimensions() {
|
|
|
|
return this.element.querySelector('.kg-card-selected').getBoundingClientRect();
|
|
|
|
},
|
|
|
|
|
|
|
|
_setupResizeHandler() {
|
|
|
|
if (this._resizeHandler) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._resizeHandler = run.bind(this, this._applyToolbarStyles);
|
|
|
|
window.addEventListener('resize', this._resizeHandler);
|
|
|
|
},
|
|
|
|
|
|
|
|
_teardownResizeHandler() {
|
|
|
|
window.removeEventListener('resize', this._resizeHandler);
|
|
|
|
this._resizeHandler = null;
|
2018-07-18 16:34:11 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// when entering edit mode it can be easy to accidentally click where the
|
|
|
|
// toolbar is inserted. Setting `preventClick` to true adds an overlay, so
|
|
|
|
// we set that for half a second to stop double-clicks hitting the toolbar
|
|
|
|
_preventAccidentalClick: task(function* () {
|
|
|
|
this.set('preventClick', true);
|
|
|
|
yield timeout(500);
|
|
|
|
this.set('preventClick', false);
|
|
|
|
})
|
2018-04-24 15:10:20 +03:00
|
|
|
|
2018-01-31 11:32:27 +03:00
|
|
|
});
|