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';
|
|
|
|
import Ember from 'ember';
|
2018-01-30 18:18:08 +03:00
|
|
|
import EmberObject from '@ember/object';
|
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-01-30 13:01:07 +03:00
|
|
|
import layout from '../templates/components/koenig-editor';
|
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';
|
|
|
|
import {computed} from '@ember/object';
|
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-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
|
|
|
|
html: 'koenig-card-html'
|
2018-02-01 14:26:24 +03:00
|
|
|
};
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
const CURSOR_BEFORE = -1;
|
|
|
|
const CURSOR_AFTER = 1;
|
2018-03-15 20:54:15 +03:00
|
|
|
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
|
|
|
|
// text expansion style when backspacing over findal char of markup
|
|
|
|
const SPECIAL_MARKUPS = {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
layout,
|
|
|
|
|
|
|
|
tagName: 'article',
|
|
|
|
classNames: ['koenig-editor'],
|
|
|
|
|
|
|
|
// public attrs
|
|
|
|
mobiledoc: null,
|
|
|
|
placeholder: 'Write here...',
|
|
|
|
autofocus: false,
|
|
|
|
spellcheck: true,
|
|
|
|
options: null,
|
|
|
|
scrollContainer: '',
|
|
|
|
|
|
|
|
// 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-01-30 13:01:07 +03:00
|
|
|
|
|
|
|
// private properties
|
|
|
|
_localMobiledoc: null,
|
|
|
|
_upstreamMobiledoc: null,
|
|
|
|
_startedRunLoop: false,
|
|
|
|
_lastIsEditingDisabled: false,
|
|
|
|
_isRenderingEditor: false,
|
2018-02-04 22:35:44 +03:00
|
|
|
_selectedCard: 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 () {
|
|
|
|
let options = this.get('options') || {};
|
2018-01-30 13:58:28 +03:00
|
|
|
let atoms = this.get('atoms') || [];
|
2018-01-30 18:18:08 +03:00
|
|
|
let cards = this.get('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({
|
|
|
|
placeholder: this.get('placeholder'),
|
|
|
|
spellcheck: this.get('spellcheck'),
|
2018-01-30 13:58:28 +03:00
|
|
|
autofocus: this.get('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
|
|
|
|
let mobiledoc = this.get('mobiledoc');
|
|
|
|
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-01-30 13:01:07 +03:00
|
|
|
this._startedRunLoop = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
willRender() {
|
|
|
|
// use a default mobiledoc. If there are no changes then return early
|
|
|
|
let mobiledoc = this.get('mobiledoc') || BLANK_DOC;
|
|
|
|
let mobiledocIsSame =
|
|
|
|
(this._localMobiledoc && this._localMobiledoc === mobiledoc) ||
|
|
|
|
(this._upstreamMobiledoc && this._upstreamMobiledoc === mobiledoc);
|
|
|
|
let isEditingDisabledIsSame =
|
|
|
|
this._lastIsEditingDisabled === this.get('isEditingDisabled');
|
|
|
|
|
|
|
|
// no change to mobiledoc, no need to recreate the editor
|
|
|
|
if (mobiledocIsSame && isEditingDisabledIsSame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update our internal references
|
|
|
|
this._lastIsEditingDisabled = this.get('isEditingDisabled');
|
|
|
|
this._upstreamMobiledoc = mobiledoc;
|
|
|
|
this._localMobiledoc = null;
|
|
|
|
|
|
|
|
// trigger the willCreateEditor closure action
|
|
|
|
this.willCreateEditor();
|
|
|
|
|
|
|
|
// teardown any old editor that might be around
|
|
|
|
let editor = this.get('editor');
|
|
|
|
if (editor) {
|
|
|
|
editor.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new editor
|
|
|
|
let editorOptions = this.get('editorOptions');
|
|
|
|
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-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 cardId = Ember.uuid();
|
|
|
|
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 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 destinationElementId = `koenig-editor-card-${cardId}`;
|
|
|
|
let destinationElement = document.createElement('div');
|
|
|
|
destinationElement.id = destinationElementId;
|
|
|
|
|
|
|
|
// 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({
|
|
|
|
destinationElement,
|
|
|
|
destinationElementId,
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
// after render we render the full ember card via {{-in-element}}
|
|
|
|
run.schedule('afterRender', () => {
|
|
|
|
this.get('componentCards').pushObject(card);
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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) => {
|
|
|
|
this.get('componentCards').removeObject(card);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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
|
|
|
|
registerKeyCommands(editor);
|
2018-04-25 15:12:59 +03:00
|
|
|
registerTextExpansions(editor, this);
|
2018-01-30 13:01:07 +03:00
|
|
|
|
2018-04-13 18:15:43 +03:00
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'ENTER',
|
|
|
|
run: run.bind(this, this.handleEnterKey, editor)
|
|
|
|
}),
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
// the cursor is always positioned after a selected card so DELETE wont
|
|
|
|
// work to remove the card like BACKSPACE does. Add a custom command to
|
|
|
|
// override the default behaviour when a card is selected
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'DEL',
|
2018-04-13 18:15:43 +03:00
|
|
|
run: run.bind(this, this.handleDelKey, editor)
|
2018-02-04 22:35:44 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
// by default mobiledoc-kit will remove the selected card but replace it
|
|
|
|
// with a blank paragraph, we want the cursor to go to the previous
|
|
|
|
// section instead
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'BACKSPACE',
|
2018-04-13 18:15:43 +03:00
|
|
|
run: run.bind(this, this.handleBackspaceKey, editor)
|
2018-02-04 22:35:44 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'UP',
|
2018-04-13 18:15:43 +03:00
|
|
|
run: run.bind(this, this.handleUpKey, editor)
|
2018-02-04 22:35:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'LEFT',
|
2018-04-13 18:15:43 +03:00
|
|
|
run: run.bind(this, this.handleLeftKey, editor)
|
2018-02-04 22:35:44 +03:00
|
|
|
});
|
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
editor.registerKeyCommand({
|
|
|
|
str: 'META+ENTER',
|
2018-04-13 18:15:43 +03:00
|
|
|
run: run.bind(this, this.handleCmdEnter, editor)
|
2018-02-13 21:00:54 +03:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.get('isEditingDisabled')) {
|
|
|
|
editor.disableEditing();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('editor', editor);
|
|
|
|
this.didCreateEditor(editor);
|
|
|
|
},
|
|
|
|
|
2018-04-13 19:53:40 +03:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
let editorElement = this.element.querySelector('.koenig-editor__editor');
|
|
|
|
|
|
|
|
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);
|
|
|
|
let editor = this.get('editor');
|
|
|
|
if (!editor.hasRendered) {
|
|
|
|
let editorElement = this.element.querySelector('.koenig-editor__editor');
|
|
|
|
this._isRenderingEditor = true;
|
|
|
|
editor.render(editorElement);
|
|
|
|
this._isRenderingEditor = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
let editor = this.get('editor');
|
2018-04-13 19:53:40 +03:00
|
|
|
let editorElement = this.element.querySelector('.koenig-editor__editor');
|
|
|
|
|
|
|
|
editorElement.removeEventListener('paste', this._pasteHandler);
|
2018-01-30 13:01:07 +03:00
|
|
|
editor.destroy();
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
toggleMarkup(markupTagName) {
|
|
|
|
let editor = this.get('editor');
|
|
|
|
editor.toggleMarkup(markupTagName);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleSection(sectionTagName) {
|
|
|
|
let editor = this.get('editor');
|
|
|
|
editor.toggleSection(sectionTagName);
|
2018-02-02 18:22:37 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
replaceWithCardSection(cardName, range) {
|
|
|
|
let editor = this.get('editor');
|
|
|
|
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 () {
|
|
|
|
let card = this.get('componentCards.lastObject');
|
2018-04-18 12:52:49 +03:00
|
|
|
if (card.get('koenigOptions.hasEditMode')) {
|
|
|
|
this.editCard(card);
|
|
|
|
} else if (card.get('koenigOptions.selectAfterInsert')) {
|
|
|
|
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) {
|
|
|
|
this.set('linkRange', range);
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelEditLink() {
|
|
|
|
this.set('linkRange', null);
|
|
|
|
},
|
|
|
|
|
2018-03-15 20:54:15 +03:00
|
|
|
deleteCard(card, cursorMovement = NO_CURSOR_MOVEMENT) {
|
|
|
|
this._deleteCard(card, cursorMovement);
|
|
|
|
},
|
|
|
|
|
2018-02-22 23:41:40 +03:00
|
|
|
moveCursorToPrevSection(card) {
|
|
|
|
let section = this._getSectionFromCard(card);
|
|
|
|
|
|
|
|
if (section.prev) {
|
|
|
|
this.deselectCard(card);
|
|
|
|
this._moveCaretToTailOfSection(section.prev, false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
moveCursorToNextSection(card) {
|
|
|
|
let section = this._getSectionFromCard(card);
|
|
|
|
|
|
|
|
if (section.next) {
|
|
|
|
this.deselectCard(card);
|
|
|
|
this._moveCaretToHeadOfSection(section.next, false);
|
|
|
|
} else {
|
|
|
|
this.send('addParagraphAfterCard', card);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addParagraphAfterCard(card) {
|
|
|
|
let editor = this.get('editor');
|
|
|
|
let section = this._getSectionFromCard(card);
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* public methods ------------------------------------------------------- */
|
|
|
|
|
|
|
|
postDidChange(editor) {
|
|
|
|
let serializeVersion = this.get('serializeVersion');
|
|
|
|
let updatedMobiledoc = editor.serialize(serializeVersion);
|
|
|
|
this._localMobiledoc = updatedMobiledoc;
|
|
|
|
|
|
|
|
// trigger closure action
|
|
|
|
this.onChange(updatedMobiledoc);
|
|
|
|
},
|
|
|
|
|
|
|
|
cursorDidChange(editor) {
|
2018-02-15 18:52:08 +03:00
|
|
|
let {head, isCollapsed, head: {section}} = editor.range;
|
|
|
|
|
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
|
|
|
|
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, () => {
|
|
|
|
let card = this._getCardFromSection(section);
|
|
|
|
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-04-20 13:34:17 +03:00
|
|
|
if (this._selectedCard && !editor.range.isBlank) {
|
2018-02-13 21:00:54 +03:00
|
|
|
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
|
|
|
|
// HACK: this is largely duplicated in `inputModeDidChange` to work
|
|
|
|
// around an event ordering bug - see comments there
|
|
|
|
if (isCollapsed && head.marker) {
|
|
|
|
Object.keys(SPECIAL_MARKUPS).forEach((tagName) => {
|
|
|
|
if (head.marker.hasMarkup(tagName)) {
|
|
|
|
let nextMarker = head.markerIn(1);
|
|
|
|
if (!nextMarker || !nextMarker.hasMarkup(tagName)) {
|
|
|
|
run.next(this, function () {
|
|
|
|
editor.toggleMarkup(tagName);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// HACK: this is largly duplicated with our `cursorDidChange` handling.
|
|
|
|
// On keyboard cursor movement our `cursorDidChange` toggle for special
|
|
|
|
// formats happens before mobiledoc's readstate updates activeMarkups
|
|
|
|
// so we have to re-do it here
|
|
|
|
let {head, isCollapsed} = editor.range;
|
|
|
|
if (isCollapsed) {
|
|
|
|
let activeMarkupTagNames = editor.activeMarkups.mapBy('tagName');
|
|
|
|
Object.keys(SPECIAL_MARKUPS).forEach((tagName) => {
|
|
|
|
if (activeMarkupTagNames.includes(tagName.toLowerCase())) {
|
|
|
|
let nextMarker = head.markerIn(1);
|
|
|
|
if (!nextMarker || !nextMarker.hasMarkup(tagName)) {
|
|
|
|
return editor.toggleMarkup(tagName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-04-13 18:15:43 +03:00
|
|
|
handleEnterKey(editor) {
|
|
|
|
let {isCollapsed, head: {offset, section}} = editor.range;
|
|
|
|
|
|
|
|
// if cursor is at beginning of a heading, insert a blank paragraph above
|
|
|
|
if (isCollapsed && offset === 0 && section.tagName.match(/^h\d$/)) {
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
let newPara = postEditor.builder.createMarkupSection('p');
|
|
|
|
let collection = section.parent.sections;
|
|
|
|
postEditor.insertSectionBefore(collection, newPara, section);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
handleBackspaceKey(editor) {
|
2018-04-12 19:20:27 +03:00
|
|
|
let {head, isCollapsed, head: {marker, offset, section}} = editor.range;
|
2018-02-04 22:35:44 +03:00
|
|
|
|
|
|
|
// if a card is selected we should delete the card then place the cursor
|
|
|
|
// at the end of the previous section
|
|
|
|
if (this._selectedCard) {
|
|
|
|
let cursorPosition = section.prev ? CURSOR_BEFORE : CURSOR_AFTER;
|
|
|
|
this._deleteCard(this._selectedCard, cursorPosition);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-14 19:13:33 +03:00
|
|
|
// if the caret is at the beginning of the doc, on a blank para, and
|
|
|
|
// there are more sections then delete the para and trigger the
|
|
|
|
// `cursorDidExitAtTop` closure action
|
|
|
|
let isFirstSection = section === section.parent.sections.head;
|
|
|
|
if (isFirstSection && isCollapsed && offset === 0 && (section.isBlank || section.text === '') && section.next) {
|
|
|
|
this.editor.run((postEditor) => {
|
|
|
|
postEditor.removeSection(section);
|
|
|
|
});
|
|
|
|
|
|
|
|
// allow default behaviour which will trigger `cursorDidChange` and
|
|
|
|
// fire our `cursorDidExitAtTop` action
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
// if the section about to be deleted by a backspace is a card then
|
2018-02-15 19:31:51 +03:00
|
|
|
// actually delete the card rather than selecting it.
|
|
|
|
// However, if the current paragraph is blank then delete the paragraph
|
|
|
|
// instead - allows blank paragraphs between cards to be deleted and
|
|
|
|
// feels more natural
|
|
|
|
if (isCollapsed && offset === 0 && section.prev && section.prev.type === 'card-section' && !section.isBlank) {
|
2018-02-04 22:35:44 +03:00
|
|
|
let card = this._getCardFromSection(section.prev);
|
2018-02-13 21:00:54 +03:00
|
|
|
this._deleteCard(card, CURSOR_AFTER);
|
2018-02-04 22:35:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:15:43 +03:00
|
|
|
// if cursor is at the beginning of a heading and previous section is a
|
|
|
|
// blank paragraph, delete the blank paragraph
|
|
|
|
if (isCollapsed && offset === 0 && section.tagName.match(/^h\d$/) && section.prev.tagName === 'p' && section.prev.isBlank) {
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
postEditor.removeSection(section.prev);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-12 19:20:27 +03:00
|
|
|
// if the markup about to be deleted is a special format (code, strike)
|
|
|
|
// then undo the text expansion to allow it to be extended
|
|
|
|
if (isCollapsed && marker) {
|
|
|
|
let specialMarkupTagNames = Object.keys(SPECIAL_MARKUPS);
|
|
|
|
let hasReversed = false;
|
|
|
|
specialMarkupTagNames.forEach((tagName) => {
|
|
|
|
// only continue if we're about to delete a special markup
|
|
|
|
let markup = marker.markups.find(markup => markup.tagName.toUpperCase() === tagName);
|
|
|
|
if (markup) {
|
|
|
|
let nextMarker = head.markerIn(1);
|
|
|
|
// ensure we're at the end of the markup not inside it
|
|
|
|
if (!nextMarker || !nextMarker.hasMarkup(tagName)) {
|
|
|
|
// wrap with the text expansion, remove formatting, then delete the last char
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
let markdown = SPECIAL_MARKUPS[tagName];
|
|
|
|
let range = editor.range.expandByMarker(marker => !!marker.markups.includes(markup));
|
|
|
|
postEditor.insertText(range.head, markdown);
|
|
|
|
range = range.extend(markdown.length);
|
|
|
|
let endPos = postEditor.insertText(range.tail, markdown);
|
|
|
|
range = range.extend(markdown.length);
|
|
|
|
postEditor.toggleMarkup(tagName, range);
|
|
|
|
endPos = postEditor.deleteAtPosition(endPos, -1);
|
|
|
|
postEditor.setRange(endPos);
|
|
|
|
});
|
|
|
|
hasReversed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (hasReversed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-04-13 18:15:43 +03:00
|
|
|
handleDelKey(editor) {
|
|
|
|
let {isCollapsed, head: {offset, section}} = editor.range;
|
2018-02-04 22:35:44 +03:00
|
|
|
|
|
|
|
// if a card is selected we should delete the card then place the cursor
|
|
|
|
// at the beginning of the next section or select the following card
|
|
|
|
if (this._selectedCard) {
|
|
|
|
let selectNextCard = section.next.type === 'card-section';
|
|
|
|
let nextCard = this._getCardFromSection(section.next);
|
|
|
|
|
|
|
|
this._deleteCard(this._selectedCard, CURSOR_AFTER);
|
|
|
|
|
|
|
|
if (selectNextCard) {
|
2018-02-13 21:00:54 +03:00
|
|
|
this.selectCard(nextCard);
|
2018-02-04 22:35:44 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the section about to be deleted by a DEL is a card then actually
|
2018-03-14 14:01:28 +03:00
|
|
|
// delete the card rather than selecting it
|
|
|
|
// However, if the current paragraph is blank then delete the paragraph
|
|
|
|
// instead - allows blank paragraphs between cards to be deleted and
|
|
|
|
// feels more natural
|
|
|
|
if (isCollapsed && offset === section.length && section.next && section.next.type === 'card-section' && !section.isBlank) {
|
2018-02-04 22:35:44 +03:00
|
|
|
let card = this._getCardFromSection(section.next);
|
|
|
|
this._deleteCard(card, CURSOR_BEFORE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-02-20 18:34:09 +03:00
|
|
|
// trigger a closure action to indicate that the caret "left" the top of
|
|
|
|
// the editor canvas when pressing UP with the caret at the beginning of
|
|
|
|
// the doc
|
2018-02-04 22:35:44 +03:00
|
|
|
handleUpKey(editor) {
|
|
|
|
let {isCollapsed, head: {offset, section}} = editor.range;
|
2018-04-10 13:26:22 +03:00
|
|
|
let prevSection = section.isListItem ? section.parent.prev : section.prev;
|
2018-02-04 22:35:44 +03:00
|
|
|
|
2018-04-10 13:26:22 +03:00
|
|
|
if (isCollapsed && offset === 0 && !prevSection) {
|
2018-02-04 22:35:44 +03:00
|
|
|
this.cursorDidExitAtTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
handleLeftKey(editor) {
|
|
|
|
let {isCollapsed, head: {offset, section}} = editor.range;
|
|
|
|
|
2018-02-22 01:46:27 +03:00
|
|
|
// trigger a closure action to indicate that the caret "left" the top of
|
|
|
|
// the editor canvas if the caret is at the very beginning of the doc
|
2018-04-10 13:26:22 +03:00
|
|
|
let prevSection = section.isListItem ? section.parent.prev : section.prev;
|
|
|
|
if (isCollapsed && offset === 0 && !prevSection) {
|
2018-02-04 22:35:44 +03:00
|
|
|
this.cursorDidExitAtTop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 01:46:27 +03:00
|
|
|
// if we have a selected card move the caret to end of the previous
|
|
|
|
// section because the cursor will likely be at the end of the card
|
|
|
|
// section meaning the default behaviour would move the cursor to the
|
|
|
|
// beginning and require two key presses instead of one
|
|
|
|
if (this._selectedCard && this._selectedCard.postModel === section) {
|
|
|
|
this._moveCaretToTailOfSection(section.prev, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-02-20 18:34:09 +03:00
|
|
|
// CMD+ENTER is our keyboard shortcut for putting a selected card into
|
|
|
|
// edit mode
|
2018-02-13 21:00:54 +03:00
|
|
|
handleCmdEnter() {
|
|
|
|
if (this._selectedCard) {
|
|
|
|
this.editCard(this._selectedCard);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-04-13 19:53:40 +03:00
|
|
|
// if a URL is pasted and we have a selection, make that selection a link
|
|
|
|
handlePaste(event) {
|
|
|
|
let editor = this.get('editor');
|
|
|
|
let range = editor.range;
|
|
|
|
|
|
|
|
// only attempt link if we have a text selection in a single section
|
|
|
|
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-02-13 21:00:54 +03:00
|
|
|
selectCard(card, isEditing = false) {
|
|
|
|
// no-op if card is already selected
|
|
|
|
if (card === this._selectedCard && isEditing === card.isEditing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// deselect any already selected card
|
|
|
|
if (this._selectedCard && card !== this._selectedCard) {
|
|
|
|
this.deselectCard(this._selectedCard);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
});
|
|
|
|
this._selectedCard = card;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
let section = this._getSectionFromCard(card);
|
2018-02-22 01:46:27 +03:00
|
|
|
this._moveCaretToTailOfSection(section);
|
2018-02-13 21:00:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
editCard(card) {
|
|
|
|
// no-op if card is already being edited
|
|
|
|
if (card === this._selectedCard && card.isEditing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// select the card with edit mode
|
|
|
|
this.selectCard(card, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
deselectCard(card) {
|
|
|
|
card.set('isEditing', false);
|
|
|
|
card.set('isSelected', false);
|
|
|
|
this._selectedCard = null;
|
|
|
|
this._showCursor();
|
|
|
|
},
|
|
|
|
|
2018-04-12 13:14:47 +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-01-30 13:01:07 +03:00
|
|
|
/* internal methods ----------------------------------------------------- */
|
|
|
|
|
2018-02-04 22:35:44 +03:00
|
|
|
_getCardFromSection(section) {
|
|
|
|
if (!section || section.type !== 'card-section') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cardId = section.renderNode.element.querySelector('.__mobiledoc-card').firstChild.id;
|
|
|
|
let cards = this.get('componentCards');
|
|
|
|
|
|
|
|
return cards.findBy('destinationElementId', cardId);
|
|
|
|
},
|
|
|
|
|
|
|
|
_getSectionFromCard(card) {
|
|
|
|
return card.env.postModel;
|
|
|
|
},
|
|
|
|
|
2018-02-22 23:41:40 +03:00
|
|
|
_moveCaretToHeadOfSection(section, skipCursorChange = true) {
|
|
|
|
this._moveCaretToSection('head', section, skipCursorChange);
|
|
|
|
},
|
|
|
|
|
2018-02-22 01:46:27 +03:00
|
|
|
_moveCaretToTailOfSection(section, skipCursorChange = true) {
|
2018-02-22 23:41:40 +03:00
|
|
|
this._moveCaretToSection('tail', section, skipCursorChange);
|
|
|
|
},
|
|
|
|
|
|
|
|
_moveCaretToSection(position, section, 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-02-04 22:35:44 +03:00
|
|
|
_deleteCard(card, cursorDirection) {
|
|
|
|
this.editor.run((postEditor) => {
|
|
|
|
let section = card.env.postModel;
|
2018-03-14 19:30:28 +03:00
|
|
|
let nextPosition;
|
2018-02-04 22:35:44 +03:00
|
|
|
|
|
|
|
if (cursorDirection === CURSOR_BEFORE) {
|
2018-03-14 19:30:28 +03:00
|
|
|
nextPosition = section.prev.tailPosition();
|
2018-02-04 22:35:44 +03:00
|
|
|
} else {
|
2018-03-14 19:30:28 +03:00
|
|
|
nextPosition = section.next.headPosition();
|
2018-02-04 22:35:44 +03:00
|
|
|
}
|
|
|
|
|
2018-03-14 19:30:28 +03:00
|
|
|
postEditor.removeSection(section);
|
2018-03-15 20:54:15 +03:00
|
|
|
|
|
|
|
if (cursorDirection !== NO_CURSOR_MOVEMENT) {
|
|
|
|
postEditor.setRange(nextPosition);
|
|
|
|
}
|
2018-02-04 22:35:44 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_hideCursor() {
|
|
|
|
this.editor.element.style.caretColor = 'transparent';
|
|
|
|
},
|
|
|
|
|
|
|
|
_showCursor() {
|
|
|
|
this.editor.element.style.caretColor = 'auto';
|
|
|
|
},
|
|
|
|
|
2018-01-30 13:01:07 +03:00
|
|
|
// store a reference to the editor for the acceptance test helpers
|
|
|
|
_setExpandoProperty(editor) {
|
|
|
|
if (this.element && Ember.testing) {
|
|
|
|
this.element[TESTING_EXPANDO_PROPERTY] = editor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|