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-05-03 19:52:39 +03:00
|
|
|
import EmberObject, {computed} 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-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-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',
|
|
|
|
code: 'koenig-card-code'
|
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: '~~',
|
|
|
|
CODE: '`'
|
|
|
|
};
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
function arrayToMap(array) {
|
|
|
|
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
|
|
|
|
function toggleSpecialFormatEditState(editor) {
|
|
|
|
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-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,
|
|
|
|
scrollContainer: '',
|
2018-05-01 17:55:51 +03:00
|
|
|
headerOffset: 0,
|
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-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
/* computed properties -------------------------------------------------- */
|
|
|
|
|
|
|
|
// merge in named options with the `options` property data-bag
|
2018-01-30 18:18:08 +03:00
|
|
|
// TODO: what is the `options` property data-bag and when/where does it get set?
|
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-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
|
|
|
|
payload = copy(payload, true);
|
|
|
|
|
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-01-30 18:18:08 +03:00
|
|
|
payload,
|
|
|
|
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-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);
|
|
|
|
this.didCreateEditor(editor);
|
|
|
|
},
|
|
|
|
|
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-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-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-05-02 19:31:17 +03:00
|
|
|
let editorElement = this.element.querySelector('[data-kg="editor"]');
|
2018-04-13 19:53:40 +03:00
|
|
|
|
|
|
|
editorElement.removeEventListener('paste', this._pasteHandler);
|
2018-01-30 13:01:07 +03:00
|
|
|
editor.destroy();
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
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
|
|
|
},
|
|
|
|
|
|
|
|
replaceWithCardSection(cardName, range) {
|
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;
|
|
|
|
let card = builder.createCardSection(cardName);
|
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;
|
|
|
|
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-03-15 20:54:15 +03:00
|
|
|
deleteCard(card, cursorMovement = NO_CURSOR_MOVEMENT) {
|
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-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);
|
|
|
|
},
|
|
|
|
|
|
|
|
cursorDidChange(editor) {
|
2018-05-14 17:13:47 +03:00
|
|
|
let {head, tail, direction, isCollapsed, head: {section}} = editor.range;
|
2018-05-18 15:55:46 +03:00
|
|
|
let isPasting = this._isPasting;
|
|
|
|
this._isPasting = false;
|
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-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') {
|
2018-05-18 15:55:46 +03:00
|
|
|
if (isPasting) {
|
|
|
|
// when pasting, if the last section added is a card we don't want to
|
|
|
|
// select it (it breaks the undo stack) but rather create an empty
|
|
|
|
// paragraph underneath
|
|
|
|
// TODO: why does the undo stack break?
|
|
|
|
let collection = section.parent.sections;
|
|
|
|
let nextSection = section.next;
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
let newSection = postEditor.builder.createMarkupSection('p');
|
|
|
|
|
|
|
|
if (nextSection) {
|
|
|
|
postEditor.insertSectionBefore(collection, newSection, nextSection);
|
|
|
|
} else {
|
|
|
|
postEditor.insertSectionAtEnd(newSection);
|
|
|
|
}
|
|
|
|
|
|
|
|
postEditor.setRange(newSection.tailPosition());
|
|
|
|
this.set('selectedRange', newSection.tailPosition());
|
|
|
|
this._skipCursorChange = true;
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
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());
|
|
|
|
|
|
|
|
return editor.run((postEditor) => {
|
|
|
|
postEditor.setRange(newRange);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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-01 19:13:53 +03:00
|
|
|
let editor = this.editor;
|
2018-04-13 19:53:40 +03:00
|
|
|
let range = editor.range;
|
|
|
|
|
2018-05-18 15:55:46 +03:00
|
|
|
// when pasting, if the last section added is a card we don't want to
|
|
|
|
// select it (it breaks the undo stack) but rather create an empty
|
|
|
|
// paragraph underneath
|
|
|
|
this._isPasting = true;
|
|
|
|
|
2018-05-16 15:08:11 +03:00
|
|
|
// if a URL is pasted and we have a selection, make that selection a link
|
2018-04-13 19:53:40 +03:00
|
|
|
if (range && !range.isCollapsed && range.headSection === range.tailSection && range.headSection.isMarkerable) {
|
|
|
|
let {text} = getContentFromPasteEvent(event);
|
|
|
|
if (text && validator.isURL(text)) {
|
|
|
|
let linkMarkup = editor.builder.createMarkup('a', {href: text});
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
postEditor.addMarkupToRange(range, linkMarkup);
|
|
|
|
});
|
|
|
|
editor.selectRange(range.tail);
|
|
|
|
// 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
|
|
|
|
// NOTE: will not work in IE/Edge which only ever expose `html`
|
|
|
|
let {html, text} = getContentFromPasteEvent(event);
|
|
|
|
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-05-18 15:55:46 +03:00
|
|
|
// HACK: mobiledoc-kit won't parse top-level <img> or
|
|
|
|
// other "unknown" elements so we wrap everything here
|
|
|
|
// so that we don't get blank posts and elements are
|
|
|
|
// passed through to our parser plugins correctly
|
|
|
|
// TODO: fix parsing in mobiledoc, related issues:
|
|
|
|
// https://github.com/bustle/mobiledoc-kit/issues/619
|
|
|
|
// https://github.com/bustle/mobiledoc-kit/issues/494
|
|
|
|
return `<div>${normalizedHtml}</div>`;
|
2018-05-17 16:42:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
editor.triggerEvent(editor.element, 'paste', pasteEvent);
|
|
|
|
}
|
2018-04-13 19:53:40 +03:00
|
|
|
},
|
|
|
|
|
2018-05-09 20:11:22 +03:00
|
|
|
/* Ember event handlers ------------------------------------------------- */
|
|
|
|
|
|
|
|
// disable dragging
|
|
|
|
// TODO: needs testing for how this interacts with cards that have drag behaviour
|
|
|
|
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-02-22 01:46:27 +03:00
|
|
|
this.editor.run((postEditor) => {
|
2018-02-22 23:41:40 +03:00
|
|
|
let sectionPosition = position === 'head' ? section.headPosition() : section.tailPosition();
|
|
|
|
let range = sectionPosition.toRange();
|
2018-02-22 01:46:27 +03:00
|
|
|
|
|
|
|
// don't trigger another cursor change selection after selecting
|
|
|
|
if (skipCursorChange && !range.isEqual(this.editor.range)) {
|
|
|
|
this._skipCursorChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
postEditor.setRange(range);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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-01-30 13:01:07 +03:00
|
|
|
// store a reference to the editor for the acceptance test helpers
|
|
|
|
_setExpandoProperty(editor) {
|
2018-05-03 19:52:39 +03:00
|
|
|
let config = getOwner(this).resolveRegistration('config:environment');
|
|
|
|
if (this.element && config.environment === 'test') {
|
2018-01-30 13:01:07 +03:00
|
|
|
this.element[TESTING_EXPANDO_PROPERTY] = editor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|