2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-12-14 15:52:53 +03:00
|
|
|
import ShortcutsMixin from 'ghost/mixins/shortcuts';
|
|
|
|
import imageManager from 'ghost/utils/ed-image-manager';
|
|
|
|
import editorShortcuts from 'ghost/utils/editor-shortcuts';
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {Component, computed, run} = Ember;
|
|
|
|
const {equal} = computed;
|
|
|
|
|
2015-12-14 15:52:53 +03:00
|
|
|
export default Component.extend(ShortcutsMixin, {
|
2014-05-31 22:32:22 +04:00
|
|
|
tagName: 'section',
|
2015-12-14 15:52:53 +03:00
|
|
|
classNames: ['view-container', 'view-editor'],
|
2015-06-13 17:34:09 +03:00
|
|
|
|
2015-12-14 15:52:53 +03:00
|
|
|
activeTab: 'markdown',
|
|
|
|
editor: null,
|
|
|
|
editorDisabled: undefined,
|
|
|
|
editorScrollInfo: null, // updated when gh-ed-editor component scrolls
|
|
|
|
height: null, // updated when markdown is rendered
|
|
|
|
shouldFocusEditor: false,
|
2015-11-18 13:50:48 +03:00
|
|
|
showCopyHTMLModal: false,
|
|
|
|
copyHTMLModalContent: null,
|
|
|
|
|
2015-12-14 15:52:53 +03:00
|
|
|
shortcuts: editorShortcuts,
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
markdownActive: equal('activeTab', 'markdown'),
|
|
|
|
previewActive: equal('activeTab', 'preview'),
|
2015-06-13 20:53:43 +03:00
|
|
|
|
|
|
|
// HTML Preview listens to scrollPosition and updates its scrollTop value
|
|
|
|
// This property receives scrollInfo from the textEditor, and height from the preview pane, and will update the
|
|
|
|
// scrollPosition value such that when either scrolling or typing-at-the-end of the text editor the preview pane
|
|
|
|
// stays in sync
|
2015-10-28 14:36:45 +03:00
|
|
|
scrollPosition: computed('editorScrollInfo', 'height', function () {
|
|
|
|
let scrollInfo = this.get('editorScrollInfo');
|
2015-12-14 15:52:53 +03:00
|
|
|
let {$previewContent, $previewViewPort} = this;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
if (!scrollInfo || !$previewContent || !$previewViewPort) {
|
2015-06-13 20:53:43 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
let previewHeight = $previewContent.height() - $previewViewPort.height();
|
|
|
|
let previewPosition, ratio;
|
2015-06-13 20:53:43 +03:00
|
|
|
|
|
|
|
ratio = previewHeight / scrollInfo.diff;
|
|
|
|
previewPosition = scrollInfo.top * ratio;
|
|
|
|
|
|
|
|
return previewPosition;
|
2015-09-01 14:22:10 +03:00
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
didInsertElement() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
2015-12-14 15:52:53 +03:00
|
|
|
this.registerShortcuts();
|
|
|
|
run.scheduleOnce('afterRender', this, this._cacheElements);
|
2015-10-28 14:36:45 +03:00
|
|
|
},
|
|
|
|
|
2015-12-14 15:52:53 +03:00
|
|
|
willDestroyElement() {
|
2016-04-06 18:26:58 +03:00
|
|
|
if (this.get('onTeardown')) {
|
|
|
|
this.get('onTeardown')();
|
2015-12-14 15:52:53 +03:00
|
|
|
}
|
|
|
|
this.removeShortcuts();
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2015-12-14 15:52:53 +03:00
|
|
|
_cacheElements() {
|
2015-10-28 14:36:45 +03:00
|
|
|
// cache these elements for use in other methods
|
2015-12-14 15:52:53 +03:00
|
|
|
this.$previewViewPort = this.$('.js-entry-preview-content');
|
|
|
|
this.$previewContent = this.$('.js-rendered-markdown');
|
2015-10-28 14:36:45 +03:00
|
|
|
},
|
2015-09-01 14:22:10 +03:00
|
|
|
|
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
selectTab(tab) {
|
2015-09-01 14:22:10 +03:00
|
|
|
this.set('activeTab', tab);
|
2015-11-18 13:50:48 +03:00
|
|
|
},
|
|
|
|
|
2015-12-14 15:52:53 +03:00
|
|
|
updateScrollInfo(scrollInfo) {
|
|
|
|
this.set('editorScrollInfo', scrollInfo);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateHeight(height) {
|
|
|
|
this.set('height', height);
|
|
|
|
},
|
|
|
|
|
|
|
|
// set from a `sendAction` on the gh-ed-editor component,
|
|
|
|
// so that we get a reference for handling uploads.
|
|
|
|
setEditor(editor) {
|
|
|
|
this.set('editor', editor);
|
|
|
|
},
|
|
|
|
|
|
|
|
disableEditor() {
|
|
|
|
this.set('editorDisabled', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
enableEditor() {
|
|
|
|
this.set('editorDisabled', undefined);
|
|
|
|
},
|
|
|
|
|
|
|
|
// The actual functionality is implemented in utils/ed-editor-shortcuts
|
|
|
|
editorShortcut(options) {
|
|
|
|
if (this.editor.$().is(':focus')) {
|
|
|
|
this.editor.shortcut(options.type);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Match the uploaded file to a line in the editor, and update that line with a path reference
|
|
|
|
// ensuring that everything ends up in the correct place and format.
|
|
|
|
handleImgUpload(e, resultSrc) {
|
|
|
|
let editor = this.get('editor');
|
|
|
|
let editorValue = editor.getValue();
|
|
|
|
let replacement = imageManager.getSrcRange(editorValue, e.target);
|
|
|
|
let cursorPosition;
|
|
|
|
|
|
|
|
if (replacement) {
|
|
|
|
cursorPosition = replacement.start + resultSrc.length + 1;
|
|
|
|
if (replacement.needsParens) {
|
|
|
|
resultSrc = `(${resultSrc})`;
|
|
|
|
}
|
|
|
|
editor.replaceSelection(resultSrc, replacement.start, replacement.end, cursorPosition);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
toggleCopyHTMLModal(generatedHTML) {
|
|
|
|
this.set('copyHTMLModalContent', generatedHTML);
|
|
|
|
this.toggleProperty('showCopyHTMLModal');
|
2015-09-01 14:22:10 +03:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 10:02:21 +04:00
|
|
|
});
|