2018-01-30 13:01:07 +03:00
|
|
|
/*
|
|
|
|
* Based on ember-mobiledoc-editor
|
|
|
|
* https://github.com/bustle/ember-mobiledoc-editor
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import Editor from 'mobiledoc-kit/editor/editor';
|
2018-06-14 18:55:02 +03:00
|
|
|
import EmberObject, {computed, get} from '@ember/object';
|
2018-05-16 15:08:11 +03:00
|
|
|
import Key from 'mobiledoc-kit/utils/key';
|
2018-05-14 17:13:47 +03:00
|
|
|
import MobiledocRange from 'mobiledoc-kit/utils/cursor/range';
|
2018-07-20 17:53:21 +03:00
|
|
|
import calculateReadingTime from '../utils/reading-time';
|
|
|
|
import countWords from '../utils/count-words';
|
2018-01-30 13:58:28 +03:00
|
|
|
import defaultAtoms from '../options/atoms';
|
2018-01-30 18:18:08 +03:00
|
|
|
import defaultCards from '../options/cards';
|
2018-05-16 15:08:11 +03:00
|
|
|
import formatMarkdown from 'ghost-admin/utils/format-markdown';
|
2018-01-30 13:01:07 +03:00
|
|
|
import layout from '../templates/components/koenig-editor';
|
2018-05-17 18:46:25 +03:00
|
|
|
import parserPlugins from '../options/parser-plugins';
|
2018-01-30 13:58:28 +03:00
|
|
|
import registerKeyCommands from '../options/key-commands';
|
2018-01-30 13:01:07 +03:00
|
|
|
import registerTextExpansions from '../options/text-expansions';
|
2018-04-13 19:53:40 +03:00
|
|
|
import validator from 'npm:validator';
|
2018-01-30 18:18:08 +03:00
|
|
|
import {A} from '@ember/array';
|
2018-01-30 13:01:07 +03:00
|
|
|
import {MOBILEDOC_VERSION} from 'mobiledoc-kit/renderers/mobiledoc';
|
|
|
|
import {assign} from '@ember/polyfills';
|
|
|
|
import {camelize, capitalize} from '@ember/string';
|
2018-01-30 18:18:08 +03:00
|
|
|
import {copy} from '@ember/object/internals';
|
2018-04-13 19:53:40 +03:00
|
|
|
import {getContentFromPasteEvent} from 'mobiledoc-kit/utils/parse-utils';
|
2018-04-26 19:04:40 +03:00
|
|
|
import {getLinkMarkupFromRange} from '../utils/markup-utils';
|
2018-05-03 19:52:39 +03:00
|
|
|
import {getOwner} from '@ember/application';
|
|
|
|
import {guidFor} from '@ember/object/internals';
|
2018-06-14 18:55:02 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2018-01-30 13:01:07 +03:00
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
|
2018-04-25 14:46:50 +03:00
|
|
|
const UNDO_DEPTH = 50;
|
|
|
|
|
2018-01-30 18:18:08 +03:00
|
|
|
export const ADD_CARD_HOOK = 'addComponent';
|
|
|
|
export const REMOVE_CARD_HOOK = 'removeComponent';
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
// used in test helpers to grab a reference to the underlying mobiledoc editor
|
|
|
|
export const TESTING_EXPANDO_PROPERTY = '__mobiledoc_kit_editor';
|
|
|
|
|
|
|
|
// blank doc contains a single empty paragraph so that there's some content for
|
|
|
|
// the cursor to start in
|
|
|
|
export const BLANK_DOC = {
|
|
|
|
version: MOBILEDOC_VERSION,
|
|
|
|
markups: [],
|
|
|
|
atoms: [],
|
|
|
|
cards: [],
|
|
|
|
sections: [
|
|
|
|
[1, 'p', [
|
|
|
|
[0, [], 0, '']
|
|
|
|
]]
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2018-02-01 14:26:24 +03:00
|
|
|
// map card names to component names
|
|
|
|
export const CARD_COMPONENT_MAP = {
|
|
|
|
hr: 'koenig-card-hr',
|
|
|
|
image: 'koenig-card-image',
|
|
|
|
markdown: 'koenig-card-markdown',
|
2018-02-03 21:20:50 +03:00
|
|
|
'card-markdown': 'koenig-card-markdown', // backwards-compat with markdown editor
|
2018-05-15 17:26:34 +03:00
|
|
|
html: 'koenig-card-html',
|
2018-06-07 13:34:50 +03:00
|
|
|
code: 'koenig-card-code',
|
|
|
|
embed: 'koenig-card-embed'
|
2018-02-01 14:26:24 +03:00
|
|
|
};
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
export const CURSOR_BEFORE = -1;
|
|
|
|
export const CURSOR_AFTER = 1;
|
|
|
|
export const NO_CURSOR_MOVEMENT = 0;
|
2018-02-04 22:35:44 +03:00
|
|
|
|
2018-04-12 19:20:27 +03:00
|
|
|
// markups that should not be continued when typing and reverted to their
|
2018-05-09 20:11:22 +03:00
|
|
|
// text expansion style when backspacing over final char of markup
|
|
|
|
export const SPECIAL_MARKUPS = {
|
2018-04-12 19:20:27 +03:00
|
|
|
S: '~~',
|
2018-06-01 19:53:15 +03:00
|
|
|
CODE: '`',
|
|
|
|
SUP: '^',
|
|
|
|
SUB: '~'
|
2018-04-12 19:20:27 +03:00
|
|
|
};
|
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
export function arrayToMap(array) {
|
2018-01-30 13:01:07 +03:00
|
|
|
let map = Object.create(null);
|
|
|
|
array.forEach((key) => {
|
|
|
|
if (key) { // skip undefined/falsy key values
|
|
|
|
key = `is${capitalize(camelize(key))}`;
|
|
|
|
map[key] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2018-05-17 12:47:44 +03:00
|
|
|
// if the cursor is at the end of one of our "special" markups that can only be
|
|
|
|
// toggled via markdown expansions then we want to ensure that the markup is
|
|
|
|
// removed from the edit state so that you can type without being stuck with
|
|
|
|
// the special formatting
|
2018-08-08 14:53:35 +03:00
|
|
|
export function toggleSpecialFormatEditState(editor) {
|
2018-05-17 12:47:44 +03:00
|
|
|
let {head, isCollapsed} = editor.range;
|
|
|
|
if (isCollapsed) {
|
|
|
|
Object.keys(SPECIAL_MARKUPS).forEach((tagName) => {
|
|
|
|
tagName = tagName.toLowerCase();
|
|
|
|
if (head.marker && head.marker.hasMarkup(tagName) && editor._editState.activeMarkups.findBy('tagName', tagName)) {
|
|
|
|
let nextMarker = head.markerIn(1);
|
|
|
|
if (!nextMarker || !nextMarker.hasMarkup(tagName)) {
|
|
|
|
// there is a bug somehwhere that means after pasting
|
|
|
|
// content the _editState can end up with multiple
|
|
|
|
// instances of the markup so we need to toggle all of them
|
|
|
|
editor._editState.activeMarkups.filterBy('tagName', tagName).forEach((markup) => {
|
|
|
|
editor._editState.toggleMarkupState(markup);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:14:39 +03:00
|
|
|
// helper function to insert image cards at or after the current active section
|
|
|
|
// used when pasting or dropping image files
|
|
|
|
function insertImageCards(files, postEditor) {
|
|
|
|
let {builder, editor} = postEditor;
|
2018-06-19 16:42:24 +03:00
|
|
|
let collection = editor.post.sections;
|
|
|
|
let section = editor.activeSection;
|
2018-05-30 19:14:39 +03:00
|
|
|
|
2018-08-01 12:00:05 +03:00
|
|
|
// when dropping an image on the editor before it's had focus there will be
|
|
|
|
// no active section so we insert the image at the end of the document
|
|
|
|
if (!section) {
|
|
|
|
section = editor.post.sections.tail;
|
|
|
|
|
|
|
|
// create a blank paragraph at the end of the document if needed because
|
|
|
|
// we use `insertSectionBefore` and don't want the image to be added
|
|
|
|
// before the last card
|
|
|
|
if (!section.isMarkerable) {
|
|
|
|
let blank = builder.createMarkupSection();
|
|
|
|
postEditor.insertSectionAtEnd(blank);
|
|
|
|
postEditor.setRange(blank.toRange());
|
|
|
|
section = postEditor._range.head.section;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:42:24 +03:00
|
|
|
// place the card after the active section
|
|
|
|
if (!section.isBlank && !section.isListItem && section.next) {
|
|
|
|
section = section.next;
|
2018-05-30 19:14:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-19 16:42:24 +03:00
|
|
|
// list items cannot contain card sections so insert a blank paragraph after
|
|
|
|
// the whole list ready to be replaced by the image cards
|
|
|
|
if (section.isListItem) {
|
|
|
|
let list = section.parent;
|
|
|
|
let blank = builder.createMarkupSection();
|
|
|
|
if (list.next) {
|
|
|
|
postEditor.insertSectionBefore(collection, blank, list.next);
|
|
|
|
} else {
|
|
|
|
postEditor.insertSectionAtEnd(blank);
|
|
|
|
}
|
|
|
|
postEditor.setRange(blank.toRange());
|
|
|
|
section = postEditor._range.head.section;
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert an image card for each image, keep track of the last card to be
|
|
|
|
// inserted so that the cursor can be placed on it at the end
|
|
|
|
let lastImageSection;
|
2018-05-30 19:14:39 +03:00
|
|
|
files.forEach((file) => {
|
|
|
|
let payload = {
|
|
|
|
files: [file]
|
|
|
|
};
|
2018-06-19 16:42:24 +03:00
|
|
|
lastImageSection = builder.createCardSection('image', payload);
|
|
|
|
postEditor.insertSectionBefore(collection, lastImageSection, section);
|
2018-05-30 19:14:39 +03:00
|
|
|
});
|
2018-06-19 16:42:24 +03:00
|
|
|
|
|
|
|
// remove the current section if it's blank - avoids unexpected blank
|
|
|
|
// paragraph after the insert is complete
|
|
|
|
if (section.isBlank) {
|
|
|
|
postEditor.removeSection(section);
|
|
|
|
}
|
|
|
|
|
|
|
|
// place cursor on the last inserted image
|
|
|
|
postEditor.setRange(lastImageSection.tailPosition());
|
2018-05-30 19:14:39 +03:00
|
|
|
}
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
export default Component.extend({
|
|
|
|
layout,
|
|
|
|
tagName: 'article',
|
2018-05-01 15:47:13 +03:00
|
|
|
classNames: ['koenig-editor', 'w-100', 'flex-grow', 'relative', 'center', 'mb0', 'mt0'],
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// public attrs
|
|
|
|
mobiledoc: null,
|
|
|
|
placeholder: 'Write here...',
|
|
|
|
autofocus: false,
|
|
|
|
spellcheck: true,
|
|
|
|
options: null,
|
2018-05-01 17:55:51 +03:00
|
|
|
headerOffset: 0,
|
2018-05-30 19:14:39 +03:00
|
|
|
dropTargetSelector: null,
|
|
|
|
scrollContainerSelector: null,
|
|
|
|
scrollOffsetTopSelector: null,
|
|
|
|
scrollOffsetBottomSelector: null,
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// internal properties
|
|
|
|
editor: null,
|
|
|
|
activeMarkupTagNames: null,
|
|
|
|
activeSectionTagNames: null,
|
|
|
|
selectedRange: null,
|
2018-01-30 18:18:08 +03:00
|
|
|
componentCards: null,
|
2018-04-06 19:49:16 +03:00
|
|
|
linkRange: null,
|
2018-05-09 20:11:22 +03:00
|
|
|
selectedCard: null,
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// private properties
|
|
|
|
_localMobiledoc: null,
|
|
|
|
_upstreamMobiledoc: null,
|
|
|
|
_startedRunLoop: false,
|
|
|
|
_lastIsEditingDisabled: false,
|
|
|
|
_isRenderingEditor: false,
|
2018-05-09 20:11:22 +03:00
|
|
|
_skipCursorChange: false,
|
2018-05-16 15:08:11 +03:00
|
|
|
_modifierKeys: null,
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// closure actions
|
|
|
|
willCreateEditor() {},
|
|
|
|
didCreateEditor() {},
|
|
|
|
onChange() {},
|
2018-02-04 22:35:44 +03:00
|
|
|
cursorDidExitAtTop() {},
|
2018-07-20 17:53:21 +03:00
|
|
|
wordCountDidChange() {},
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
/* computed properties -------------------------------------------------- */
|
|
|
|
|
2018-05-24 15:30:50 +03:00
|
|
|
// merge in named options with any passed in `options` property data-bag
|
2018-01-30 13:01:07 +03:00
|
|
|
editorOptions: computed(function () {
|
2018-05-01 19:13:53 +03:00
|
|
|
let options = this.options || {};
|
|
|
|
let atoms = this.atoms || [];
|
|
|
|
let cards = this.cards || [];
|
2018-01-30 13:58:28 +03:00
|
|
|
|
2018-01-30 18:18:08 +03:00
|
|
|
// add our default atoms and cards, we want the defaults to be first so
|
|
|
|
// that they can be overridden by any passed-in atoms or cards.
|
|
|
|
// Use Array.concat to avoid modifying any passed in array references
|
2018-01-30 13:58:28 +03:00
|
|
|
atoms = Array.concat(defaultAtoms, atoms);
|
2018-01-30 18:18:08 +03:00
|
|
|
cards = Array.concat(defaultCards, cards);
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
return assign({
|
2018-05-01 19:13:53 +03:00
|
|
|
placeholder: this.placeholder,
|
|
|
|
spellcheck: this.spellcheck,
|
|
|
|
autofocus: this.autofocus,
|
2018-01-30 18:18:08 +03:00
|
|
|
atoms,
|
|
|
|
cards
|
2018-01-30 13:01:07 +03:00
|
|
|
}, options);
|
|
|
|
}),
|
|
|
|
|
|
|
|
/* lifecycle hooks ------------------------------------------------------ */
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
// set a blank mobiledoc if we didn't receive anything
|
2018-05-01 19:13:53 +03:00
|
|
|
let mobiledoc = this.mobiledoc;
|
2018-01-30 13:01:07 +03:00
|
|
|
if (!mobiledoc) {
|
|
|
|
mobiledoc = BLANK_DOC;
|
|
|
|
this.set('mobiledoc', mobiledoc);
|
|
|
|
}
|
|
|
|
|
2018-01-30 18:18:08 +03:00
|
|
|
this.set('componentCards', A([]));
|
|
|
|
this.set('activeMarkupTagNames', {});
|
|
|
|
this.set('activeSectionTagNames', {});
|
|
|
|
|
2018-05-16 15:08:11 +03:00
|
|
|
this._modifierKeys = {
|
|
|
|
shift: false,
|
|
|
|
alt: false,
|
|
|
|
ctrl: false
|
|
|
|
};
|
|
|
|
|
2018-05-24 15:30:50 +03:00
|
|
|
// track mousedown/mouseup on the window rather than the ember component
|
|
|
|
// so that we're sure to get the events even when they start outside of
|
|
|
|
// this component or end outside the window.
|
|
|
|
// Mouse events are used to track when a mousebutton is down so that we
|
|
|
|
// can disable automatic cursor-in-viewport scrolling
|
|
|
|
this._onMousedownHandler = run.bind(this, this.handleMousedown);
|
|
|
|
window.addEventListener('mousedown', this._onMousedownHandler);
|
|
|
|
this._onMouseupHandler = run.bind(this, this.handleMouseup);
|
|
|
|
window.addEventListener('mouseup', this._onMouseupHandler);
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
this._startedRunLoop = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
willRender() {
|
|
|
|
// use a default mobiledoc. If there are no changes then return early
|
2018-05-01 19:13:53 +03:00
|
|
|
let mobiledoc = this.mobiledoc || BLANK_DOC;
|
2018-01-30 13:01:07 +03:00
|
|
|
let mobiledocIsSame =
|
|
|
|
(this._localMobiledoc && this._localMobiledoc === mobiledoc) ||
|
|
|
|
(this._upstreamMobiledoc && this._upstreamMobiledoc === mobiledoc);
|
|
|
|
let isEditingDisabledIsSame =
|
2018-05-01 19:13:53 +03:00
|
|
|
this._lastIsEditingDisabled === this.isEditingDisabled;
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// no change to mobiledoc, no need to recreate the editor
|
|
|
|
if (mobiledocIsSame && isEditingDisabledIsSame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update our internal references
|
2018-05-01 19:13:53 +03:00
|
|
|
this._lastIsEditingDisabled = this.isEditingDisabled;
|
2018-01-30 13:01:07 +03:00
|
|
|
this._upstreamMobiledoc = mobiledoc;
|
|
|
|
this._localMobiledoc = null;
|
|
|
|
|
|
|
|
// trigger the willCreateEditor closure action
|
|
|
|
this.willCreateEditor();
|
|
|
|
|
|
|
|
// teardown any old editor that might be around
|
2018-05-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-01-30 13:01:07 +03:00
|
|
|
if (editor) {
|
|
|
|
editor.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new editor
|
2018-05-01 19:13:53 +03:00
|
|
|
let editorOptions = this.editorOptions;
|
2018-01-30 13:01:07 +03:00
|
|
|
editorOptions.mobiledoc = mobiledoc;
|
2018-04-11 20:42:27 +03:00
|
|
|
editorOptions.showLinkTooltips = false;
|
2018-04-25 14:46:50 +03:00
|
|
|
editorOptions.undoDepth = UNDO_DEPTH;
|
2018-05-17 18:46:25 +03:00
|
|
|
editorOptions.parserPlugins = parserPlugins;
|
2018-01-30 18:18:08 +03:00
|
|
|
|
|
|
|
let componentHooks = {
|
|
|
|
// triggered when a card section is added to the mobiledoc
|
2018-04-18 12:52:49 +03:00
|
|
|
[ADD_CARD_HOOK]: ({env, options, payload}, koenigOptions) => {
|
2018-01-30 18:18:08 +03:00
|
|
|
let cardName = env.name;
|
2018-02-01 14:26:24 +03:00
|
|
|
let componentName = CARD_COMPONENT_MAP[cardName];
|
2018-01-30 18:18:08 +03:00
|
|
|
|
|
|
|
// the payload must be copied to avoid sharing the reference
|
2018-05-29 17:23:50 +03:00
|
|
|
// `payload.files` is special because it's set by paste/drag-n-drop
|
|
|
|
// events and can't be copied for security reasons
|
|
|
|
let {files} = payload;
|
|
|
|
let payloadCopy = copy(payload, true);
|
|
|
|
payloadCopy.files = files;
|
2018-01-30 18:18:08 +03:00
|
|
|
|
2018-02-01 14:26:24 +03:00
|
|
|
// all of the properties that will be passed through to the
|
2018-01-30 18:18:08 +03:00
|
|
|
// component cards via the template
|
|
|
|
let card = EmberObject.create({
|
|
|
|
cardName,
|
2018-02-01 14:26:24 +03:00
|
|
|
componentName,
|
2018-04-18 12:52:49 +03:00
|
|
|
koenigOptions,
|
2018-05-29 17:23:50 +03:00
|
|
|
payload: payloadCopy,
|
2018-01-30 18:18:08 +03:00
|
|
|
env,
|
|
|
|
options,
|
|
|
|
editor,
|
2018-02-13 21:00:54 +03:00
|
|
|
postModel: env.postModel,
|
|
|
|
isSelected: false,
|
|
|
|
isEditing: false
|
2018-01-30 18:18:08 +03:00
|
|
|
});
|
|
|
|
|
2018-05-03 19:52:39 +03:00
|
|
|
// the desination element is the container that gets rendered
|
|
|
|
// inside the editor, once rendered we use {{-in-element}} to
|
|
|
|
// wormhole in the actual ember component
|
|
|
|
let cardId = guidFor(card);
|
|
|
|
let destinationElementId = `koenig-editor-card-${cardId}`;
|
|
|
|
let destinationElement = document.createElement('div');
|
|
|
|
destinationElement.id = destinationElementId;
|
|
|
|
|
|
|
|
card.setProperties({
|
|
|
|
destinationElementId,
|
|
|
|
destinationElement
|
|
|
|
});
|
|
|
|
|
2018-01-30 18:18:08 +03:00
|
|
|
// after render we render the full ember card via {{-in-element}}
|
|
|
|
run.schedule('afterRender', () => {
|
2018-05-01 19:13:53 +03:00
|
|
|
this.componentCards.pushObject(card);
|
2018-01-30 18:18:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// render the destination element inside the editor
|
|
|
|
return {card, element: destinationElement};
|
|
|
|
},
|
|
|
|
// triggered when a card section is removed from the mobiledoc
|
|
|
|
[REMOVE_CARD_HOOK]: (card) => {
|
2018-05-01 19:13:53 +03:00
|
|
|
this.componentCards.removeObject(card);
|
2018-01-30 18:18:08 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
editorOptions.cardOptions = componentHooks;
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
editor = new Editor(editorOptions);
|
|
|
|
|
|
|
|
// set up key commands and text expansions (MD conversion)
|
2018-01-30 13:58:28 +03:00
|
|
|
// TODO: this will override any passed in options, we should allow the
|
|
|
|
// default behaviour to be overridden by addon consumers
|
2018-04-26 19:04:40 +03:00
|
|
|
registerKeyCommands(editor, this);
|
2018-04-25 15:12:59 +03:00
|
|
|
registerTextExpansions(editor, this);
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// set up editor hooks
|
|
|
|
editor.willRender(() => {
|
|
|
|
// The editor's render/rerender will happen after this `editor.willRender`,
|
|
|
|
// so we explicitly start a runloop here if there is none, so that the
|
|
|
|
// add/remove card hooks happen inside a runloop.
|
|
|
|
// When pasting text that gets turned into a card, for example,
|
|
|
|
// the add card hook would run outside the runloop if we didn't begin a new
|
|
|
|
// one now.
|
|
|
|
if (!run.currentRunLoop) {
|
|
|
|
this._startedRunLoop = true;
|
|
|
|
run.begin();
|
|
|
|
}
|
|
|
|
});
|
2018-04-13 18:15:43 +03:00
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
editor.didRender(() => {
|
|
|
|
// if we had explicitly started a runloop in `editor.willRender`,
|
|
|
|
// we must explicitly end it here
|
|
|
|
if (this._startedRunLoop) {
|
|
|
|
this._startedRunLoop = false;
|
|
|
|
run.end();
|
|
|
|
}
|
2018-08-10 19:31:32 +03:00
|
|
|
|
|
|
|
if (this._cleanupScheduled) {
|
|
|
|
run.schedule('afterRender', this, this._cleanup);
|
|
|
|
}
|
2018-01-30 13:01:07 +03:00
|
|
|
});
|
2018-04-13 18:15:43 +03:00
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
editor.postDidChange(() => {
|
|
|
|
run.join(() => {
|
|
|
|
this.postDidChange(editor);
|
|
|
|
});
|
|
|
|
});
|
2018-04-13 18:15:43 +03:00
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
editor.cursorDidChange(() => {
|
|
|
|
run.join(() => {
|
|
|
|
this.cursorDidChange(editor);
|
|
|
|
});
|
|
|
|
});
|
2018-04-13 18:15:43 +03:00
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
editor.inputModeDidChange(() => {
|
|
|
|
if (this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
run.join(() => {
|
|
|
|
this.inputModeDidChange(editor);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-01 19:13:53 +03:00
|
|
|
if (this.isEditingDisabled) {
|
2018-01-30 13:01:07 +03:00
|
|
|
editor.disableEditing();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('editor', editor);
|
2018-06-14 18:55:02 +03:00
|
|
|
this.didCreateEditor(this);
|
2018-07-20 17:53:21 +03:00
|
|
|
|
|
|
|
run.schedule('afterRender', this, this._calculateWordCount);
|
2018-01-30 13:01:07 +03:00
|
|
|
},
|
|
|
|
|
2018-04-13 19:53:40 +03:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
2018-05-02 19:31:17 +03:00
|
|
|
let editorElement = this.element.querySelector('[data-kg="editor"]');
|
2018-04-13 19:53:40 +03:00
|
|
|
|
|
|
|
this._pasteHandler = run.bind(this, this.handlePaste);
|
|
|
|
editorElement.addEventListener('paste', this._pasteHandler);
|
2018-05-24 15:30:50 +03:00
|
|
|
|
|
|
|
if (this.scrollContainerSelector) {
|
|
|
|
this._scrollContainer = document.querySelector(this.scrollContainerSelector);
|
|
|
|
}
|
2018-05-30 19:14:39 +03:00
|
|
|
|
|
|
|
this._dropTarget = document.querySelector(this.dropTargetSelector) || this.element;
|
|
|
|
this._dragOverHandler = run.bind(this, this.handleDragOver);
|
|
|
|
this._dragLeaveHandler = run.bind(this, this.handleDragLeave);
|
|
|
|
this._dropHandler = run.bind(this, this.handleDrop);
|
|
|
|
this._dropTarget.addEventListener('dragover', this._dragOverHandler);
|
|
|
|
this._dropTarget.addEventListener('dragleave', this._dragLeaveHandler);
|
|
|
|
this._dropTarget.addEventListener('drop', this._dropHandler);
|
2018-04-13 19:53:40 +03:00
|
|
|
},
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
// our ember component has rendered, now we need to render the mobiledoc
|
|
|
|
// editor itself if necessary
|
|
|
|
didRender() {
|
|
|
|
this._super(...arguments);
|
2018-05-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-01-30 13:01:07 +03:00
|
|
|
if (!editor.hasRendered) {
|
2018-05-02 19:31:17 +03:00
|
|
|
let editorElement = this.element.querySelector('[data-kg="editor"]');
|
2018-01-30 13:01:07 +03:00
|
|
|
this._isRenderingEditor = true;
|
|
|
|
editor.render(editorElement);
|
|
|
|
this._isRenderingEditor = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
2018-05-30 19:14:39 +03:00
|
|
|
let {editor, _dropTarget} = this;
|
2018-04-13 19:53:40 +03:00
|
|
|
|
2018-05-30 19:14:39 +03:00
|
|
|
_dropTarget.removeEventListener('dragover', this._dragOverHandler);
|
|
|
|
_dropTarget.removeEventListener('dragleave', this._dragLeaveHandler);
|
|
|
|
_dropTarget.removeEventListener('drop', this._dropHandler);
|
|
|
|
|
|
|
|
let editorElement = this.element.querySelector('[data-kg="editor"]');
|
2018-04-13 19:53:40 +03:00
|
|
|
editorElement.removeEventListener('paste', this._pasteHandler);
|
2018-05-30 19:14:39 +03:00
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
editor.destroy();
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2018-05-23 16:49:39 +03:00
|
|
|
exitCursorAtTop() {
|
|
|
|
if (this.selectedCard) {
|
|
|
|
this.deselectCard(this.selectedCard);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cursorDidExitAtTop();
|
|
|
|
},
|
|
|
|
|
2018-05-15 14:08:58 +03:00
|
|
|
toggleMarkup(markupTagName, postEditor) {
|
|
|
|
(postEditor || this.editor).toggleMarkup(markupTagName);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleSection(sectionTagName, postEditor) {
|
|
|
|
(postEditor || this.editor).toggleSection(sectionTagName);
|
2018-01-30 13:01:07 +03:00
|
|
|
},
|
|
|
|
|
2018-05-17 17:00:43 +03:00
|
|
|
toggleHeaderSection(headingTagName, postEditor, options = {}) {
|
2018-05-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-05-15 14:08:58 +03:00
|
|
|
|
|
|
|
// skip toggle if we already have the same heading level
|
2018-05-17 17:00:43 +03:00
|
|
|
if (!options.force && editor.activeSection.tagName === headingTagName) {
|
2018-05-15 14:08:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let operation = function (postEditor) {
|
|
|
|
// strip all formatting aside from links
|
|
|
|
postEditor.removeMarkupFromRange(
|
|
|
|
editor.activeSection.toRange(),
|
|
|
|
m => m.tagName !== 'a'
|
|
|
|
);
|
|
|
|
|
|
|
|
postEditor.toggleSection(headingTagName);
|
|
|
|
};
|
|
|
|
|
|
|
|
this._performEdit(operation, postEditor);
|
2018-02-02 18:22:37 +03:00
|
|
|
},
|
|
|
|
|
2018-06-07 13:34:50 +03:00
|
|
|
replaceWithCardSection(cardName, range, payload) {
|
2018-05-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-02-02 18:22:37 +03:00
|
|
|
let {head: {section}} = range;
|
|
|
|
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
let {builder} = postEditor;
|
2018-06-07 13:34:50 +03:00
|
|
|
let card = builder.createCardSection(cardName, payload);
|
2018-04-25 15:12:59 +03:00
|
|
|
let nextSection = section.next;
|
|
|
|
let needsTrailingParagraph = !nextSection;
|
2018-02-02 18:22:37 +03:00
|
|
|
|
|
|
|
postEditor.replaceSection(section, card);
|
|
|
|
|
2018-04-25 15:12:59 +03:00
|
|
|
// add an empty paragraph after if necessary so writing can continue
|
2018-02-02 18:22:37 +03:00
|
|
|
if (needsTrailingParagraph) {
|
|
|
|
let newSection = postEditor.builder.createMarkupSection('p');
|
|
|
|
postEditor.insertSectionAtEnd(newSection);
|
|
|
|
postEditor.setRange(newSection.tailPosition());
|
2018-04-25 15:12:59 +03:00
|
|
|
} else {
|
|
|
|
postEditor.setRange(nextSection.headPosition());
|
2018-02-02 18:22:37 +03:00
|
|
|
}
|
|
|
|
});
|
2018-02-13 21:00:54 +03:00
|
|
|
|
|
|
|
// cards are pushed on to the `componentCards` array so we can
|
|
|
|
// assume that the last card in the list is the one we want to
|
|
|
|
// select. Needs to be scheduled afterRender so that the new card
|
|
|
|
// is actually present
|
|
|
|
run.schedule('afterRender', this, function () {
|
2018-05-01 19:13:53 +03:00
|
|
|
let card = this.componentCards.lastObject;
|
2018-06-07 13:34:50 +03:00
|
|
|
|
2018-05-01 19:13:53 +03:00
|
|
|
if (card.koenigOptions.hasEditMode) {
|
2018-04-18 12:52:49 +03:00
|
|
|
this.editCard(card);
|
2018-05-01 19:13:53 +03:00
|
|
|
} else if (card.koenigOptions.selectAfterInsert) {
|
2018-04-18 12:52:49 +03:00
|
|
|
this.selectCard(card);
|
|
|
|
}
|
2018-02-13 21:00:54 +03:00
|
|
|
});
|
2018-02-02 18:22:37 +03:00
|
|
|
},
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
selectCard(card) {
|
2018-02-13 21:00:54 +03:00
|
|
|
this.selectCard(card);
|
|
|
|
},
|
2018-02-04 22:35:44 +03:00
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
editCard(card) {
|
|
|
|
this.editCard(card);
|
2018-02-04 22:35:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
deselectCard(card) {
|
2018-02-13 21:00:54 +03:00
|
|
|
this.deselectCard(card);
|
2018-02-22 23:41:40 +03:00
|
|
|
},
|
|
|
|
|
2018-04-06 19:49:16 +03:00
|
|
|
// range should be set to the full extent of the selection or the
|
|
|
|
// appropriate <a> markup. If there's a selection when the link edit
|
|
|
|
// component renders it will re-select when finished which should
|
|
|
|
// trigger the normal toolbar
|
|
|
|
editLink(range) {
|
2018-04-26 19:04:40 +03:00
|
|
|
let linkMarkup = getLinkMarkupFromRange(range);
|
|
|
|
if ((!range.isCollapsed || linkMarkup) && range.headSection.isMarkerable) {
|
|
|
|
this.set('linkRange', range);
|
|
|
|
}
|
2018-04-06 19:49:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
cancelEditLink() {
|
|
|
|
this.set('linkRange', null);
|
|
|
|
},
|
|
|
|
|
2018-05-23 17:12:30 +03:00
|
|
|
deleteCard(card, cursorMovement = CURSOR_AFTER) {
|
2018-05-09 20:11:22 +03:00
|
|
|
this.deleteCard(card, cursorMovement);
|
2018-03-15 20:54:15 +03:00
|
|
|
},
|
|
|
|
|
2018-02-22 23:41:40 +03:00
|
|
|
moveCursorToPrevSection(card) {
|
2018-05-09 20:11:22 +03:00
|
|
|
let section = this.getSectionFromCard(card);
|
2018-02-22 23:41:40 +03:00
|
|
|
|
|
|
|
if (section.prev) {
|
|
|
|
this.deselectCard(card);
|
2018-05-09 20:11:22 +03:00
|
|
|
this.moveCaretToTailOfSection(section.prev, false);
|
2018-02-22 23:41:40 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
moveCursorToNextSection(card) {
|
2018-05-09 20:11:22 +03:00
|
|
|
let section = this.getSectionFromCard(card);
|
2018-02-22 23:41:40 +03:00
|
|
|
|
|
|
|
if (section.next) {
|
|
|
|
this.deselectCard(card);
|
2018-05-09 20:11:22 +03:00
|
|
|
this.moveCaretToHeadOfSection(section.next, false);
|
2018-02-22 23:41:40 +03:00
|
|
|
} else {
|
|
|
|
this.send('addParagraphAfterCard', card);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addParagraphAfterCard(card) {
|
2018-05-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-05-09 20:11:22 +03:00
|
|
|
let section = this.getSectionFromCard(card);
|
2018-02-22 23:41:40 +03:00
|
|
|
let collection = section.parent.sections;
|
|
|
|
let nextSection = section.next;
|
|
|
|
|
|
|
|
this.deselectCard(card);
|
|
|
|
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
let {builder} = postEditor;
|
|
|
|
let newPara = builder.createMarkupSection('p');
|
|
|
|
|
|
|
|
if (nextSection) {
|
|
|
|
postEditor.insertSectionBefore(collection, newPara, nextSection);
|
|
|
|
} else {
|
|
|
|
postEditor.insertSectionAtEnd(newPara);
|
|
|
|
}
|
|
|
|
|
|
|
|
postEditor.setRange(newPara.tailPosition());
|
|
|
|
});
|
2018-01-30 13:01:07 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-14 18:55:02 +03:00
|
|
|
/* public interface ----------------------------------------------------- */
|
2018-08-10 19:31:32 +03:00
|
|
|
// TODO: find a better way to expose the public interface?
|
|
|
|
|
|
|
|
// HACK: this scheduled cleanup is a bit hacky. We call .cleanup when
|
|
|
|
// initializing Koenig in our editor controller but we have to wait for
|
|
|
|
// rendering to finish so that componentCards is populated, even then
|
|
|
|
// it's unlikely the card.component registration has finished.
|
|
|
|
//
|
|
|
|
// TODO: see if there's a way we can perform cleanup directly on the
|
|
|
|
// mobiledoc, maybe with a "cleanupOnInit" option so that we modify the
|
|
|
|
// mobiledoc before we start rendering
|
2018-06-14 18:55:02 +03:00
|
|
|
cleanup() {
|
2018-08-10 19:31:32 +03:00
|
|
|
this._cleanupScheduled = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_cleanup() {
|
2018-06-14 18:55:02 +03:00
|
|
|
this.componentCards.forEach((card) => {
|
2018-08-10 19:31:32 +03:00
|
|
|
let shouldDelete = card.koenigOptions.deleteIfEmpty;
|
|
|
|
|
|
|
|
if (!shouldDelete) {
|
2018-06-14 18:55:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof shouldDelete === 'string') {
|
|
|
|
let payloadKey = shouldDelete;
|
|
|
|
shouldDelete = card => isBlank(get(card, payloadKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldDelete(card)) {
|
|
|
|
this.deleteCard(card, NO_CURSOR_MOVEMENT);
|
|
|
|
}
|
|
|
|
});
|
2018-08-10 19:31:32 +03:00
|
|
|
this._cleanupScheduled = false;
|
2018-06-14 18:55:02 +03:00
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
/* mobiledoc event handlers --------------------------------------------- */
|
2018-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
postDidChange(editor) {
|
2018-05-01 19:13:53 +03:00
|
|
|
let serializeVersion = this.serializeVersion;
|
2018-01-30 13:01:07 +03:00
|
|
|
let updatedMobiledoc = editor.serialize(serializeVersion);
|
|
|
|
this._localMobiledoc = updatedMobiledoc;
|
|
|
|
|
|
|
|
// trigger closure action
|
|
|
|
this.onChange(updatedMobiledoc);
|
2018-07-20 17:53:21 +03:00
|
|
|
|
|
|
|
// re-calculate word count
|
|
|
|
this._calculateWordCount();
|
2018-01-30 13:01:07 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
cursorDidChange(editor) {
|
2018-05-14 17:13:47 +03:00
|
|
|
let {head, tail, direction, isCollapsed, head: {section}} = editor.range;
|
2018-02-15 18:52:08 +03:00
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
// sometimes we perform a programatic edit that causes a cursor change
|
|
|
|
// but we actually want to skip the default behaviour because we've
|
|
|
|
// already handled it, e.g. on card insertion, manual card selection
|
|
|
|
if (this._skipCursorChange) {
|
|
|
|
this._skipCursorChange = false;
|
2018-02-15 18:02:37 +03:00
|
|
|
this.set('selectedRange', editor.range);
|
2018-05-24 15:30:50 +03:00
|
|
|
this._scrollCursorIntoView();
|
2018-02-13 21:00:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 01:46:27 +03:00
|
|
|
// ignore the cursor moving from one end to the other within a selected
|
|
|
|
// card section, clicking and other interactions within a card can cause
|
|
|
|
// this to happen and we don't want to select/deselect accidentally.
|
|
|
|
// See the up/down/left/right key handlers for the card selection
|
2018-05-09 20:11:22 +03:00
|
|
|
if (this.selectedCard && this.selectedCard.postModel === section) {
|
2018-02-04 22:35:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// select the card if the cursor is on the before/after ‌ char
|
|
|
|
if (section && isCollapsed && section.type === 'card-section') {
|
|
|
|
if (head.offset === 0 || head.offset === 1) {
|
2018-03-14 15:13:54 +03:00
|
|
|
// select card after render to ensure that our componentCards
|
|
|
|
// attr is populated
|
|
|
|
run.schedule('afterRender', this, () => {
|
2018-05-09 20:11:22 +03:00
|
|
|
let card = this.getCardFromSection(section);
|
2018-03-14 15:13:54 +03:00
|
|
|
this.selectCard(card);
|
|
|
|
this.set('selectedRange', editor.range);
|
|
|
|
});
|
2018-02-04 22:35:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deselect any selected card because the cursor is no longer on a card
|
2018-05-09 20:11:22 +03:00
|
|
|
if (this.selectedCard && !editor.range.isBlank) {
|
|
|
|
this.deselectCard(this.selectedCard);
|
2018-02-04 22:35:44 +03:00
|
|
|
}
|
|
|
|
|
2018-04-12 19:20:27 +03:00
|
|
|
// if we have `code` or ~strike~ formatting to the left but not the right
|
|
|
|
// then toggle the formatting - these formats should only be creatable
|
|
|
|
// through the text expansions
|
2018-05-17 12:47:44 +03:00
|
|
|
toggleSpecialFormatEditState(editor);
|
2018-04-12 19:20:27 +03:00
|
|
|
|
2018-05-14 17:13:47 +03:00
|
|
|
// do not include the tail section if it's offset is 0
|
|
|
|
// fixes triple-click unexpectedly selecting two sections for section-level formatting
|
|
|
|
// https://github.com/bustle/mobiledoc-kit/issues/597
|
2018-05-17 15:56:37 +03:00
|
|
|
if (direction === 1 && !isCollapsed && tail.offset === 0 && tail.section.prev) {
|
2018-05-14 17:13:47 +03:00
|
|
|
let finalSection = tail.section.prev;
|
|
|
|
let newRange = new MobiledocRange(head, finalSection.tailPosition());
|
2018-05-23 16:20:03 +03:00
|
|
|
return editor.selectRange(newRange);
|
2018-05-14 17:13:47 +03:00
|
|
|
}
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
// pass the selected range through to the toolbar + menu components
|
2018-01-30 13:01:07 +03:00
|
|
|
this.set('selectedRange', editor.range);
|
2018-05-24 15:30:50 +03:00
|
|
|
this._scrollCursorIntoView();
|
2018-01-30 13:01:07 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// fired when the active section(s) or markup(s) at the current cursor
|
|
|
|
// position or selection have changed. We use this event to update the
|
|
|
|
// activeMarkup/section tag lists which control button states in our popup
|
|
|
|
// toolbar
|
|
|
|
inputModeDidChange(editor) {
|
|
|
|
let markupTags = arrayToMap(editor.activeMarkups.map(m => m.tagName));
|
|
|
|
// editor.activeSections are leaf sections.
|
|
|
|
// Map parent section tag names (e.g. 'p', 'ul', 'ol') so that list buttons
|
|
|
|
// are updated.
|
|
|
|
// eslint-disable-next-line no-confusing-arrow
|
|
|
|
let sectionParentTagNames = editor.activeSections.map(s => s.isNested ? s.parent.tagName : s.tagName);
|
|
|
|
let sectionTags = arrayToMap(sectionParentTagNames);
|
|
|
|
|
2018-04-12 19:20:27 +03:00
|
|
|
// On keyboard cursor movement our `cursorDidChange` toggle for special
|
2018-05-17 12:47:44 +03:00
|
|
|
// formats happens before mobiledoc's readstate updates the edit states
|
2018-04-12 19:20:27 +03:00
|
|
|
// so we have to re-do it here
|
2018-05-17 12:47:44 +03:00
|
|
|
// TODO: can we make the event order consistent in mobiledoc-kit?
|
|
|
|
toggleSpecialFormatEditState(editor);
|
2018-04-12 19:20:27 +03:00
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
// Avoid updating this component's properties synchronously while
|
|
|
|
// rendering the editor (after rendering the component) because it
|
|
|
|
// causes Ember to display deprecation warnings
|
|
|
|
if (this._isRenderingEditor) {
|
|
|
|
run.schedule('afterRender', () => {
|
|
|
|
this.set('activeMarkupTagNames', markupTags);
|
|
|
|
this.set('activeSectionTagNames', sectionTags);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.set('activeMarkupTagNames', markupTags);
|
|
|
|
this.set('activeSectionTagNames', sectionTags);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
/* custom event handlers ------------------------------------------------ */
|
2018-02-13 21:00:54 +03:00
|
|
|
|
2018-04-13 19:53:40 +03:00
|
|
|
handlePaste(event) {
|
2018-05-30 19:14:39 +03:00
|
|
|
let {editor} = this;
|
2018-05-28 13:28:25 +03:00
|
|
|
|
|
|
|
// don't trigger our paste handling for pastes within cards or outside
|
|
|
|
// of the editor canvas. Avoids double-paste of content when pasting
|
|
|
|
// into cards
|
2018-05-30 19:14:39 +03:00
|
|
|
if (!editor.cursor.isAddressable(event.target)) {
|
2018-05-28 13:28:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:23:50 +03:00
|
|
|
// if we have image files pasted, create an image card for each and set
|
|
|
|
// the payload.files property which will cause the image to be auto-uploaded
|
|
|
|
// NOTE: browser support varies as of May 2018:
|
|
|
|
// - Safari: will paste all images
|
|
|
|
// - Chrome: will only paste the first image
|
|
|
|
// - Firefox: will not paste any images
|
|
|
|
let images = Array.from(event.clipboardData.files).filter(file => file.type.indexOf('image') > -1);
|
|
|
|
if (images.length > 0) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
|
|
editor.run((postEditor) => {
|
2018-05-30 19:14:39 +03:00
|
|
|
insertImageCards(images, postEditor);
|
2018-05-29 17:23:50 +03:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-13 19:53:40 +03:00
|
|
|
let range = editor.range;
|
2018-05-28 13:28:25 +03:00
|
|
|
let {html, text} = getContentFromPasteEvent(event);
|
2018-04-13 19:53:40 +03:00
|
|
|
|
2018-08-02 15:09:32 +03:00
|
|
|
if (text && validator.isURL(text)) {
|
|
|
|
// if we have a text selection, make that selection a link
|
|
|
|
if (range && !range.isCollapsed && range.headSection === range.tailSection && range.headSection.isMarkerable) {
|
2018-04-13 19:53:40 +03:00
|
|
|
let linkMarkup = editor.builder.createMarkup('a', {href: text});
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
postEditor.addMarkupToRange(range, linkMarkup);
|
|
|
|
});
|
|
|
|
editor.selectRange(range.tail);
|
2018-08-02 15:09:32 +03:00
|
|
|
|
|
|
|
// prevent mobiledoc's default paste event handler firing
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there's no selection and cursor is on an empty paragraph,
|
|
|
|
// insert the url as an embed card, unless SHIFT is pressed. Setting
|
|
|
|
// the `linkOnError` option results in an immediate switch to a
|
|
|
|
// plain link if the embed fails for any reason (eg, unknown provider)
|
|
|
|
if (range && range.isCollapsed && range.headSection.isBlank && !range.headSection.isListItem) {
|
|
|
|
if (!this._modifierKeys.shift) {
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
let payload = {url: text, linkOnError: true};
|
|
|
|
let card = postEditor.builder.createCardSection('embed', payload);
|
2018-08-02 17:10:00 +03:00
|
|
|
let nextSection = range.headSection.next;
|
|
|
|
|
2018-08-02 15:09:32 +03:00
|
|
|
postEditor.replaceSection(range.headSection, card);
|
2018-08-02 17:10:00 +03:00
|
|
|
|
|
|
|
// move caret to the next section, creating a blank one
|
|
|
|
// if none exists
|
|
|
|
if (nextSection) {
|
|
|
|
postEditor.setRange(nextSection.headPosition());
|
|
|
|
} else {
|
|
|
|
let newSection = postEditor.builder.createMarkupSection('p');
|
|
|
|
postEditor.insertSectionAtEnd(newSection);
|
|
|
|
postEditor.setRange(newSection.headPosition());
|
|
|
|
}
|
2018-08-02 15:09:32 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// ensure the pasted URL is still auto-linked when Shift is pressed
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
let linkMarkup = editor.builder.createMarkup('a', {href: text});
|
|
|
|
postEditor.insertTextWithMarkup(range.head, text, [linkMarkup]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-13 19:53:40 +03:00
|
|
|
// prevent mobiledoc's default paste event handler firing
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
2018-05-16 15:08:11 +03:00
|
|
|
return;
|
2018-04-13 19:53:40 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 15:08:11 +03:00
|
|
|
|
|
|
|
// if plain text is pasted we run it through our markdown parser so that
|
|
|
|
// we get better output than mobiledoc's default text parsing and we can
|
|
|
|
// provide an easier MD->Mobiledoc conversion route
|
2018-08-02 15:09:32 +03:00
|
|
|
// NOTE: will not work in Edge which only ever exposes `html`
|
2018-05-16 15:08:11 +03:00
|
|
|
if (text && !html && !this._modifierKeys.shift) {
|
|
|
|
// prevent mobiledoc's default paste event handler firing
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
|
|
// we can't modify the paste event itself so we trigger a mock
|
|
|
|
// paste event with our own data
|
|
|
|
let pasteEvent = {
|
|
|
|
type: 'paste',
|
|
|
|
preventDefault() {},
|
|
|
|
target: editor.element,
|
|
|
|
clipboardData: {
|
|
|
|
getData(type) {
|
|
|
|
if (type === 'text/html') {
|
|
|
|
return formatMarkdown(text, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
editor.triggerEvent(editor.element, 'paste', pasteEvent);
|
|
|
|
}
|
2018-05-17 16:42:20 +03:00
|
|
|
|
|
|
|
// we need to standardise HTML here because parserPlugins do not get
|
|
|
|
// passed inline markup such as `<b>` or `<i>`
|
|
|
|
if (html) {
|
|
|
|
// prevent mobiledoc's default paste event handler firing
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
|
|
let normalizedHtml = html
|
|
|
|
.replace(/<b(\s|>)/gi, '<strong$1')
|
|
|
|
.replace(/<\/b>/gi, '</strong>')
|
|
|
|
.replace(/<i(\s|>)/gi, '<em$1')
|
|
|
|
.replace(/<\/i>/gi, '</em>');
|
|
|
|
|
|
|
|
// we can't modify the paste event itself so we trigger a mock
|
|
|
|
// paste event with our own data
|
|
|
|
let pasteEvent = {
|
|
|
|
type: 'paste',
|
|
|
|
preventDefault() {},
|
|
|
|
target: editor.element,
|
|
|
|
clipboardData: {
|
|
|
|
getData(type) {
|
|
|
|
if (type === 'text/plain') {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
if (type === 'text/html') {
|
2018-06-18 20:48:32 +03:00
|
|
|
return normalizedHtml;
|
2018-05-17 16:42:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
editor.triggerEvent(editor.element, 'paste', pasteEvent);
|
|
|
|
}
|
2018-04-13 19:53:40 +03:00
|
|
|
},
|
|
|
|
|
2018-05-24 15:30:50 +03:00
|
|
|
handleMousedown(event) {
|
|
|
|
// we only care about the left mouse button
|
|
|
|
if (event.which === 1) {
|
|
|
|
this._isMouseDown = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleMouseup(event) {
|
|
|
|
if (event.which === 1) {
|
|
|
|
this._isMouseDown = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-30 19:14:39 +03:00
|
|
|
handleDragOver(event) {
|
2018-06-20 14:34:30 +03:00
|
|
|
if (!event.dataTransfer || event.target.closest('.__mobiledoc-card')) {
|
2018-05-30 19:14:39 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is needed to work around inconsistencies with dropping files
|
|
|
|
// from Chrome's downloads bar
|
|
|
|
if (navigator.userAgent.indexOf('Chrome') > -1) {
|
|
|
|
let eA = event.dataTransfer.effectAllowed;
|
|
|
|
event.dataTransfer.dropEffect = (eA === 'move' || eA === 'linkMove') ? 'move' : 'copy';
|
|
|
|
}
|
|
|
|
|
|
|
|
// indicate to the browser that we want to handle drop behaviour here
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
|
|
|
|
handleDragLeave(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
|
|
|
|
handleDrop(event) {
|
|
|
|
// drops on cards that are in an edit state should be cancelled
|
|
|
|
// editable cards should handle drag-n-drop themselves if needed
|
|
|
|
let cardElem = event.target.closest('.__mobiledoc-card');
|
|
|
|
if (cardElem) {
|
|
|
|
let cardId = cardElem.firstChild.id;
|
|
|
|
let card = this.componentCards.findBy('destinationElementId', cardId);
|
2018-08-01 14:36:02 +03:00
|
|
|
if (card.isEditing || card.component.handlesDragDrop) {
|
2018-05-30 19:14:39 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (event.dataTransfer.files) {
|
|
|
|
let images = Array.from(event.dataTransfer.files).filter(file => file.type.indexOf('image') > -1);
|
|
|
|
if (images.length > 0) {
|
|
|
|
this.editor.run((postEditor) => {
|
|
|
|
insertImageCards(images, postEditor);
|
|
|
|
});
|
2018-08-01 12:00:05 +03:00
|
|
|
this._scrollCursorIntoView({jumpToCard: true});
|
2018-05-30 19:14:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
/* Ember event handlers ------------------------------------------------- */
|
|
|
|
|
|
|
|
// disable dragging
|
|
|
|
dragStart(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
|
2018-05-16 15:08:11 +03:00
|
|
|
// we keep track of the modifier keys that are pressed so that in other event
|
|
|
|
// handlers we can adjust the behaviour. Necessary because the browser doesn't
|
|
|
|
// natively provide any info on non-key events about which keys are pressed
|
|
|
|
keyDown(event) {
|
|
|
|
let key = Key.fromEvent(event);
|
|
|
|
this._updateModifiersFromKey(key, {isDown: true});
|
|
|
|
},
|
|
|
|
|
|
|
|
keyUp(event) {
|
|
|
|
let key = Key.fromEvent(event);
|
|
|
|
this._updateModifiersFromKey(key, {isDown: false});
|
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
/* public methods ------------------------------------------------------- */
|
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
selectCard(card, isEditing = false) {
|
|
|
|
// no-op if card is already selected
|
2018-05-09 20:11:22 +03:00
|
|
|
if (card === this.selectedCard && isEditing === card.isEditing) {
|
2018-02-13 21:00:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// deselect any already selected card
|
2018-05-09 20:11:22 +03:00
|
|
|
if (this.selectedCard && card !== this.selectedCard) {
|
|
|
|
this.deselectCard(this.selectedCard);
|
2018-02-13 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// setting a card as selected trigger's the cards didReceiveAttrs
|
|
|
|
// hook where the actual selection state change happens. Put into edit
|
|
|
|
// mode if necessary
|
|
|
|
card.setProperties({
|
|
|
|
isEditing,
|
|
|
|
isSelected: true
|
|
|
|
});
|
2018-05-09 20:11:22 +03:00
|
|
|
this.selectedCard = card;
|
2018-02-13 21:00:54 +03:00
|
|
|
|
|
|
|
// hide the cursor and place it after the card so that ENTER can
|
|
|
|
// create a new paragraph and cursorDidExitAtTop gets fired on LEFT
|
|
|
|
// if the card is at the top of the document
|
|
|
|
this._hideCursor();
|
2018-05-09 20:11:22 +03:00
|
|
|
let section = this.getSectionFromCard(card);
|
|
|
|
this.moveCaretToTailOfSection(section);
|
2018-02-13 21:00:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
editCard(card) {
|
|
|
|
// no-op if card is already being edited
|
2018-05-09 20:11:22 +03:00
|
|
|
if (card === this.selectedCard && card.isEditing) {
|
2018-02-13 21:00:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// select the card with edit mode
|
|
|
|
this.selectCard(card, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
deselectCard(card) {
|
|
|
|
card.set('isEditing', false);
|
|
|
|
card.set('isSelected', false);
|
2018-05-09 20:11:22 +03:00
|
|
|
this.selectedCard = null;
|
2018-02-13 21:00:54 +03:00
|
|
|
this._showCursor();
|
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
deleteCard(card, cursorDirection) {
|
|
|
|
this.editor.run((postEditor) => {
|
|
|
|
let section = card.env.postModel;
|
|
|
|
let nextPosition;
|
2018-04-12 13:14:47 +03:00
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
if (cursorDirection === CURSOR_BEFORE) {
|
|
|
|
nextPosition = section.prev && section.prev.tailPosition();
|
|
|
|
} else {
|
|
|
|
nextPosition = section.next && section.next.headPosition();
|
|
|
|
}
|
2018-04-12 13:14:47 +03:00
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
postEditor.removeSection(section);
|
2018-01-30 13:01:07 +03:00
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
// if there's no prev or next section then the doc is empty, we want
|
|
|
|
// to add a blank paragraph and place the cursor in it
|
|
|
|
if (cursorDirection !== NO_CURSOR_MOVEMENT && !nextPosition) {
|
|
|
|
let {builder} = postEditor;
|
|
|
|
let newPara = builder.createMarkupSection('p');
|
|
|
|
postEditor.insertSectionAtEnd(newPara);
|
|
|
|
return postEditor.setRange(newPara.tailPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursorDirection !== NO_CURSOR_MOVEMENT) {
|
|
|
|
return postEditor.setRange(nextPosition);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getCardFromSection(section) {
|
2018-02-04 22:35:44 +03:00
|
|
|
if (!section || section.type !== 'card-section') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cardId = section.renderNode.element.querySelector('.__mobiledoc-card').firstChild.id;
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
return this.componentCards.findBy('destinationElementId', cardId);
|
2018-02-04 22:35:44 +03:00
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
getSectionFromCard(card) {
|
2018-02-04 22:35:44 +03:00
|
|
|
return card.env.postModel;
|
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
moveCaretToHeadOfSection(section, skipCursorChange = true) {
|
|
|
|
this.moveCaretToSection(section, 'head', skipCursorChange);
|
2018-02-22 23:41:40 +03:00
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
moveCaretToTailOfSection(section, skipCursorChange = true) {
|
|
|
|
this.moveCaretToSection(section, 'tail', skipCursorChange);
|
2018-02-22 23:41:40 +03:00
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
moveCaretToSection(section, position, skipCursorChange = true) {
|
2018-05-23 16:20:03 +03:00
|
|
|
let sectionPosition = position === 'head' ? section.headPosition() : section.tailPosition();
|
|
|
|
let range = sectionPosition.toRange();
|
2018-02-22 01:46:27 +03:00
|
|
|
|
2018-05-23 16:20:03 +03:00
|
|
|
// don't trigger another cursor change selection after selecting
|
|
|
|
if (skipCursorChange && !range.isEqual(this.editor.range)) {
|
|
|
|
this._skipCursorChange = true;
|
|
|
|
}
|
2018-02-22 01:46:27 +03:00
|
|
|
|
2018-05-23 16:20:03 +03:00
|
|
|
this.editor.selectRange(range);
|
2018-02-22 01:46:27 +03:00
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
/* internal methods ----------------------------------------------------- */
|
2018-02-04 22:35:44 +03:00
|
|
|
|
2018-05-15 14:08:58 +03:00
|
|
|
// nested editor.run loops will create additional undo steps so this is a
|
|
|
|
// shortcut for when we already have a postEditor
|
|
|
|
_performEdit(editOperation, postEditor) {
|
|
|
|
if (postEditor) {
|
|
|
|
editOperation(postEditor);
|
|
|
|
} else {
|
|
|
|
this.editor.run((postEditor) => {
|
|
|
|
editOperation(postEditor);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
_hideCursor() {
|
|
|
|
this.editor.element.style.caretColor = 'transparent';
|
|
|
|
},
|
|
|
|
|
|
|
|
_showCursor() {
|
|
|
|
this.editor.element.style.caretColor = 'auto';
|
|
|
|
},
|
|
|
|
|
2018-05-16 15:08:11 +03:00
|
|
|
_updateModifiersFromKey(key, {isDown}) {
|
|
|
|
if (key.isShiftKey()) {
|
|
|
|
this._modifierKeys.shift = isDown;
|
|
|
|
} else if (key.isAltKey()) {
|
|
|
|
this._modifierKeys.alt = isDown;
|
|
|
|
} else if (key.isCtrlKey()) {
|
|
|
|
this._modifierKeys.ctrl = isDown;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-01 12:00:05 +03:00
|
|
|
_scrollCursorIntoView(options = {jumpToCard: false}) {
|
2018-05-24 15:30:50 +03:00
|
|
|
// disable auto-scroll if the mouse or shift key is being used to create
|
|
|
|
// a selection - the browser handles scrolling well in this case
|
|
|
|
if (!this._scrollContainer || this._isMouseDown || this._modifierKeys.shift) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let {range} = this.editor;
|
|
|
|
let selection = window.getSelection();
|
2018-06-21 13:03:41 +03:00
|
|
|
let windowRange;
|
|
|
|
// Safari can throw an IndexSizeError from selection.getRangeAt(0)
|
|
|
|
if (selection.type !== 'None') {
|
|
|
|
windowRange = selection && selection.getRangeAt(0);
|
|
|
|
}
|
2018-05-24 15:30:50 +03:00
|
|
|
let element = range.head && range.head.section && range.head.section.renderNode && range.head.section.renderNode.element;
|
|
|
|
|
2018-06-06 16:51:52 +03:00
|
|
|
// prevent scroll jumps when a card is selected
|
2018-08-01 12:00:05 +03:00
|
|
|
if (!options.jumpToCard && range && range.head.section && range.head.section.isCardSection) {
|
2018-06-06 16:51:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-24 15:30:50 +03:00
|
|
|
if (windowRange) {
|
|
|
|
// cursorTop is relative to the window rather than document or scroll container
|
|
|
|
let {top: cursorTop, height: cursorHeight} = windowRange.getBoundingClientRect();
|
|
|
|
let viewportHeight = window.innerHeight;
|
|
|
|
let offsetTop = 0;
|
|
|
|
let offsetBottom = 0;
|
|
|
|
let scrollTop = this._scrollContainer.scrollTop;
|
|
|
|
|
|
|
|
if (this.scrollOffsetTopSelector) {
|
|
|
|
let topElement = document.querySelector(this.scrollOffsetTopSelector);
|
|
|
|
offsetTop = topElement ? topElement.offsetHeight : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.scrollOffsetBottomSelector) {
|
|
|
|
let bottomElement = document.querySelector(this.scrollOffsetBottomSelector);
|
|
|
|
offsetBottom = bottomElement ? bottomElement.offsetHeight : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for empty paragraphs the window selection range will be 0,0,0,0
|
|
|
|
// so grab the element's bounding rect instead
|
|
|
|
if (cursorTop === 0 && cursorHeight === 0) {
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
({top: cursorTop, height: cursorHeight} = element.getBoundingClientRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep cursor in view at the top
|
|
|
|
if (cursorTop < 0 + offsetTop) {
|
|
|
|
this._scrollContainer.scrollTop = scrollTop - offsetTop + cursorTop - 20;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cursorBottom = cursorTop + cursorHeight;
|
|
|
|
let paddingBottom = 0;
|
|
|
|
let distanceFromViewportBottom = cursorBottom - viewportHeight;
|
|
|
|
let atBottom = false;
|
|
|
|
|
|
|
|
// if we're at the bottom of the doc we should keep the bottom
|
|
|
|
// padding in view, otherwise just scroll to keep the cursor in view
|
|
|
|
if (this._scrollContainer.scrollTop + this._scrollContainer.offsetHeight + 200 >= this._scrollContainer.scrollHeight) {
|
|
|
|
atBottom = true;
|
|
|
|
paddingBottom = parseFloat(getComputedStyle(this.element.parentNode).getPropertyValue('padding-bottom'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursorBottom > viewportHeight - offsetBottom - paddingBottom) {
|
|
|
|
if (atBottom) {
|
|
|
|
this._scrollContainer.scrollTop = this._scrollContainer.scrollHeight;
|
|
|
|
} else {
|
|
|
|
this._scrollContainer.scrollTop = scrollTop + offsetBottom + distanceFromViewportBottom + 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-30 19:14:39 +03:00
|
|
|
},
|
|
|
|
|
2018-07-20 17:53:21 +03:00
|
|
|
// calculate the number of words in rich-text sections and query cards for
|
|
|
|
// their own word and image counts. Image counts are used for reading-time
|
|
|
|
_calculateWordCount() {
|
|
|
|
run.throttle(this, this._throttledWordCount, 100, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
_throttledWordCount() {
|
|
|
|
let wordCount = 0;
|
|
|
|
let imageCount = 0;
|
|
|
|
|
|
|
|
this.editor.post.walkAllLeafSections((section) => {
|
|
|
|
if (section.isCardSection) {
|
|
|
|
// get counts from card components
|
|
|
|
let card = this.getCardFromSection(section);
|
|
|
|
let cardCounts = get(card, 'component.counts') || {};
|
|
|
|
wordCount += cardCounts.wordCount || 0;
|
|
|
|
imageCount += cardCounts.imageCount || 0;
|
|
|
|
} else {
|
|
|
|
wordCount += countWords(section.text);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (wordCount !== this.wordCount || imageCount !== this.imageCount) {
|
|
|
|
let readingTime = calculateReadingTime({wordCount, imageCount});
|
|
|
|
|
|
|
|
this.wordCount = wordCount;
|
|
|
|
this.imageCount = imageCount;
|
|
|
|
this.readingTime = readingTime;
|
|
|
|
|
|
|
|
this.wordCountDidChange({wordCount, imageCount, readingTime});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-30 19:14:39 +03:00
|
|
|
// store a reference to the editor for the acceptance test helpers
|
|
|
|
_setExpandoProperty(editor) {
|
|
|
|
let config = getOwner(this).resolveRegistration('config:environment');
|
|
|
|
if (this.element && config.environment === 'test') {
|
|
|
|
this.element[TESTING_EXPANDO_PROPERTY] = editor;
|
|
|
|
}
|
2018-01-30 13:01:07 +03:00
|
|
|
}
|
|
|
|
});
|