2021-11-19 13:10:23 +03:00
|
|
|
import * as softReturnParser from '@tryghost/kg-parser-plugins/lib/cards/softReturn';
|
2018-08-08 14:53:35 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import Editor from 'mobiledoc-kit/editor/editor';
|
2022-02-03 21:50:49 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
2019-05-02 19:14:00 +03:00
|
|
|
import cleanBasicHtml from '@tryghost/kg-clean-basic-html';
|
2021-11-19 13:10:23 +03:00
|
|
|
import registerKeyCommands, {BASIC_KEY_COMMANDS, BASIC_KEY_COMMANDS_WITH_BR} from '../options/key-commands';
|
2019-01-22 16:09:38 +03:00
|
|
|
import validator from 'validator';
|
2019-01-28 12:35:58 +03:00
|
|
|
import {DRAG_DISABLED_DATA_ATTR} from '../lib/dnd/constants';
|
2021-11-11 14:18:51 +03:00
|
|
|
import {MOBILEDOC_VERSION, getBlankMobileDoc} from './koenig-editor';
|
2022-02-03 21:50:49 +03:00
|
|
|
import {action, computed} from '@ember/object';
|
2018-08-08 14:53:35 +03:00
|
|
|
import {arrayToMap, toggleSpecialFormatEditState} from './koenig-editor';
|
|
|
|
import {assign} from '@ember/polyfills';
|
2021-09-03 12:51:33 +03:00
|
|
|
import {getContentFromPasteEvent, parsePostFromPaste} from 'mobiledoc-kit/utils/parse-utils';
|
2018-08-08 14:53:35 +03:00
|
|
|
import {getLinkMarkupFromRange} from '../utils/markup-utils';
|
2021-11-18 14:57:41 +03:00
|
|
|
import {registerBasicInputTextExpansions} from '../options/text-expansions';
|
2021-11-19 13:10:23 +03:00
|
|
|
import {removeBR} from '../options/basic-html-parser-plugins';
|
2018-08-08 14:53:35 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2021-11-19 13:10:23 +03:00
|
|
|
import {softReturn as softReturnAtom} from '../options/atoms';
|
2018-08-08 14:53:35 +03:00
|
|
|
|
2021-11-18 15:24:56 +03:00
|
|
|
// TODO: extract core to share functionality between this and `{{koenig-editor}}`
|
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
const UNDO_DEPTH = 50;
|
|
|
|
|
2021-11-18 15:24:56 +03:00
|
|
|
// markups that should not be continued when typing and reverted to their
|
|
|
|
// text expansion style when backspacing over final char of markup
|
|
|
|
const SPECIAL_MARKUPS = {
|
|
|
|
S: '~~',
|
|
|
|
CODE: {
|
|
|
|
char: '`',
|
|
|
|
replace: false
|
|
|
|
},
|
|
|
|
SUP: '^',
|
|
|
|
SUB: '~'
|
|
|
|
};
|
2018-08-13 12:59:13 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@classic
|
|
|
|
export default class KoenigBasicHtmlInput extends Component {
|
2018-08-08 14:53:35 +03:00
|
|
|
// public attrs
|
2022-02-03 21:50:49 +03:00
|
|
|
autofocus = false;
|
|
|
|
html = null;
|
|
|
|
placeholder = '';
|
|
|
|
spellcheck = true;
|
|
|
|
defaultTag = 'p';
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
// internal properties
|
2022-02-03 21:50:49 +03:00
|
|
|
activeMarkupTagNames = null;
|
|
|
|
editor = null;
|
|
|
|
linkRange = null;
|
|
|
|
mobiledoc = null;
|
|
|
|
selectedRange = null;
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
// private properties
|
2022-02-03 21:50:49 +03:00
|
|
|
_hasFocus = false;
|
|
|
|
_lastMobiledoc = null;
|
|
|
|
_startedRunLoop = false;
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
// closure actions
|
2022-02-03 21:50:49 +03:00
|
|
|
willCreateEditor() {}
|
|
|
|
didCreateEditor() {}
|
|
|
|
onChange() {}
|
|
|
|
onNewline() {}
|
|
|
|
onFocus() {}
|
|
|
|
onBlur() {}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
/* computed properties -------------------------------------------------- */
|
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@computed('html')
|
|
|
|
get cleanHTML() {
|
2021-11-19 13:10:23 +03:00
|
|
|
return cleanBasicHtml(this.html || '', {allowBr: !!this.allowBr});
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
// merge in named options with any passed in `options` property data-bag
|
2022-02-03 21:50:49 +03:00
|
|
|
@computed('cleanHTML')
|
|
|
|
get editorOptions() {
|
2018-08-08 14:53:35 +03:00
|
|
|
let options = this.options || {};
|
|
|
|
let atoms = this.atoms || [];
|
|
|
|
let cards = this.cards || [];
|
|
|
|
|
2021-11-19 13:10:23 +03:00
|
|
|
atoms = [softReturnAtom].concat(atoms);
|
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
return assign({
|
2021-11-12 14:00:53 +03:00
|
|
|
html: `<${this.defaultTag}>${this.cleanHTML || ''}</${this.defaultTag}>`,
|
2018-08-08 14:53:35 +03:00
|
|
|
placeholder: this.placeholder,
|
|
|
|
spellcheck: this.spellcheck,
|
|
|
|
autofocus: this.autofocus,
|
|
|
|
cards,
|
|
|
|
atoms,
|
|
|
|
unknownCardHandler() {},
|
|
|
|
unknownAtomHandler() {}
|
|
|
|
}, options);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
/* lifecycle hooks ------------------------------------------------------ */
|
|
|
|
|
2020-04-17 16:11:19 +03:00
|
|
|
init() {
|
2022-02-03 21:50:49 +03:00
|
|
|
super.init(...arguments);
|
2021-11-18 15:24:56 +03:00
|
|
|
this.SPECIAL_MARKUPS = SPECIAL_MARKUPS;
|
2021-09-03 10:59:59 +03:00
|
|
|
this._lastSetHtml = this.html;
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2020-04-17 16:11:19 +03:00
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
didReceiveAttrs() {
|
2022-02-03 21:50:49 +03:00
|
|
|
super.didReceiveAttrs(...arguments);
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
// reset local mobiledoc if html has been changed upstream so that
|
|
|
|
// the html will be re-parsed by the mobiledoc-kit editor
|
2021-09-03 10:59:59 +03:00
|
|
|
if (this._lastSetHtml !== this.html) {
|
2018-08-08 14:53:35 +03:00
|
|
|
this.set('mobiledoc', null);
|
2021-09-03 10:59:59 +03:00
|
|
|
this._lastSetHtml = this.html;
|
2018-08-08 14:53:35 +03:00
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
willRender() {
|
2022-02-03 21:50:49 +03:00
|
|
|
super.willRender(...arguments);
|
2021-07-15 17:27:29 +03:00
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
let mobiledoc = this.mobiledoc;
|
|
|
|
|
|
|
|
if (!mobiledoc && !this.cleanHTML) {
|
2021-11-11 14:18:51 +03:00
|
|
|
mobiledoc = getBlankMobileDoc(this.defaultTag);
|
2018-08-08 14:53:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let mobiledocIsSame =
|
|
|
|
(this._lastMobiledoc && this._lastMobiledoc === mobiledoc);
|
|
|
|
let isEditingDisabledIsSame =
|
|
|
|
this._lastIsEditingDisabled === this.isEditingDisabled;
|
|
|
|
|
|
|
|
// no change to mobiledoc, no need to recreate the editor
|
|
|
|
if (mobiledocIsSame && isEditingDisabledIsSame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update our internal references
|
|
|
|
this._lastIsEditingDisabled = this.isEditingDisabled;
|
|
|
|
|
|
|
|
// trigger the willCreateEditor closure action
|
|
|
|
this.willCreateEditor();
|
|
|
|
|
|
|
|
// teardown any old editor that might be around
|
|
|
|
let editor = this.editor;
|
|
|
|
if (editor) {
|
|
|
|
editor.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new editor
|
|
|
|
let editorOptions = this.editorOptions;
|
|
|
|
editorOptions.mobiledoc = mobiledoc;
|
|
|
|
editorOptions.showLinkTooltips = false;
|
|
|
|
editorOptions.undoDepth = UNDO_DEPTH;
|
2021-11-19 13:10:23 +03:00
|
|
|
|
|
|
|
const parserPlugins = [];
|
|
|
|
if (this.allowBr) {
|
|
|
|
parserPlugins.push(softReturnParser.fromBr());
|
|
|
|
} else {
|
|
|
|
parserPlugins.push(removeBR);
|
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
editorOptions.parserPlugins = parserPlugins;
|
|
|
|
|
|
|
|
editor = new Editor(editorOptions);
|
|
|
|
|
2021-11-19 13:10:23 +03:00
|
|
|
registerKeyCommands(editor, this, this.allowBr ? BASIC_KEY_COMMANDS_WITH_BR : BASIC_KEY_COMMANDS);
|
2021-11-18 14:57:41 +03:00
|
|
|
registerBasicInputTextExpansions(editor);
|
2018-08-08 14:53:35 +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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.willHandleNewline((event) => {
|
|
|
|
run.join(() => {
|
|
|
|
this.willHandleNewline(event);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.didUpdatePost((postEditor) => {
|
|
|
|
run.join(() => {
|
|
|
|
this.didUpdatePost(postEditor);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.postDidChange(() => {
|
|
|
|
run.join(() => {
|
|
|
|
this.postDidChange(editor);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.cursorDidChange(() => {
|
|
|
|
run.join(() => {
|
|
|
|
this.cursorDidChange(editor);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.inputModeDidChange(() => {
|
|
|
|
if (this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
run.join(() => {
|
|
|
|
this.inputModeDidChange(editor);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.isEditingDisabled) {
|
|
|
|
editor.disableEditing();
|
|
|
|
}
|
|
|
|
|
|
|
|
// update mobiledoc reference to match initial editor state from parsed
|
|
|
|
// html. We use this value to compare on re-renders in case we need to
|
|
|
|
// re-parse from html
|
2020-04-17 00:11:45 +03:00
|
|
|
this.mobiledoc = editor.serialize(MOBILEDOC_VERSION);
|
2018-08-08 14:53:35 +03:00
|
|
|
this._lastMobiledoc = this.mobiledoc;
|
|
|
|
|
|
|
|
this.set('editor', editor);
|
|
|
|
this.didCreateEditor(editor);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
2018-08-13 12:59:13 +03:00
|
|
|
didInsertElement() {
|
2022-02-03 21:50:49 +03:00
|
|
|
super.didInsertElement(...arguments);
|
2018-08-13 12:59:13 +03:00
|
|
|
let editorElement = this.element.querySelector('[data-kg="editor"]');
|
|
|
|
|
|
|
|
this._pasteHandler = run.bind(this, this.handlePaste);
|
|
|
|
editorElement.addEventListener('paste', this._pasteHandler);
|
2019-01-28 12:35:58 +03:00
|
|
|
|
|
|
|
this.element.dataset[DRAG_DISABLED_DATA_ATTR] = 'true';
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-13 12:59:13 +03:00
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
// our ember component has rendered, now we need to render the mobiledoc
|
|
|
|
// editor itself if necessary
|
|
|
|
didRender() {
|
2022-02-03 21:50:49 +03:00
|
|
|
super.didRender(...arguments);
|
2018-08-08 14:53:35 +03:00
|
|
|
let {editor} = this;
|
|
|
|
if (!editor.hasRendered) {
|
|
|
|
let editorElement = this.element.querySelector('[data-kg="editor"]');
|
|
|
|
this._isRenderingEditor = true;
|
|
|
|
editor.render(editorElement);
|
|
|
|
this._isRenderingEditor = false;
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
willDestroyElement() {
|
2022-02-03 21:50:49 +03:00
|
|
|
super.willDestroyElement(...arguments);
|
2018-08-13 12:59:13 +03:00
|
|
|
|
|
|
|
let editorElement = this.element.querySelector('[data-kg="editor"]');
|
|
|
|
editorElement.removeEventListener('paste', this._pasteHandler);
|
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
this.editor.destroy();
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
toggleMarkup(markupTagName, postEditor) {
|
|
|
|
(postEditor || this.editor).toggleMarkup(markupTagName);
|
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
2022-02-03 21:50:49 +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
|
|
|
|
@action
|
|
|
|
editLink(range) {
|
|
|
|
let linkMarkup = getLinkMarkupFromRange(range);
|
|
|
|
if ((!range.isCollapsed || linkMarkup) && range.headSection.isMarkerable) {
|
|
|
|
this.set('linkRange', range);
|
2018-08-08 14:53:35 +03:00
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelEditLink() {
|
|
|
|
this.set('linkRange', null);
|
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
/* ember event handlers --------------------------------------------------*/
|
|
|
|
|
|
|
|
// handle focusin/focusout at the component level so that we don't trigger blur
|
|
|
|
// actions when clicking on toolbar buttons
|
|
|
|
focusIn(event) {
|
|
|
|
if (!this._hasFocus) {
|
|
|
|
this._hasFocus = true;
|
|
|
|
run.scheduleOnce('actions', this, this.onFocus, event);
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
focusOut(event) {
|
|
|
|
if (!event.relatedTarget || !this.element.contains(event.relatedTarget)) {
|
|
|
|
this._hasFocus = false;
|
|
|
|
run.scheduleOnce('actions', this, this.onBlur, event);
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
2018-08-13 12:59:13 +03:00
|
|
|
/* custom event handlers ------------------------------------------------ */
|
|
|
|
|
|
|
|
handlePaste(event) {
|
2021-09-03 12:51:33 +03:00
|
|
|
// prevent default editor paste, we want to sanitise the pasted post
|
|
|
|
// before inserting to avoid disparities arising from `didUpdatePost`
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
2018-08-13 12:59:13 +03:00
|
|
|
let {editor, editor: {range}} = this;
|
|
|
|
|
|
|
|
if (!editor.cursor.isAddressable(event.target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-03 12:51:33 +03:00
|
|
|
if (!range.isCollapsed) {
|
|
|
|
editor.performDelete();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (editor.post.isBlank) {
|
|
|
|
editor._insertEmptyMarkupSectionAtCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
const {text} = getContentFromPasteEvent(event);
|
|
|
|
|
2018-08-13 12:59:13 +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) {
|
|
|
|
let linkMarkup = editor.builder.createMarkup('a', {href: text});
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
postEditor.addMarkupToRange(range, linkMarkup);
|
|
|
|
});
|
|
|
|
editor.selectRange(range.tail);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 12:51:33 +03:00
|
|
|
|
|
|
|
const position = editor.range.head;
|
|
|
|
const targetFormat = event.shiftKey ? 'text' : 'html';
|
|
|
|
const pastedPost = parsePostFromPaste(event, editor, {targetFormat});
|
|
|
|
|
|
|
|
// Basic HTML post sanitisation - same operations as `didUpdatePost`
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
|
|
|
// remove non-markerable and non-list sections
|
|
|
|
pastedPost.sections.forEach((section) => {
|
|
|
|
if (!section.isMarkerable && !section.isListSection) {
|
|
|
|
pastedPost.sections.remove(section);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// remove all but first section
|
|
|
|
while (pastedPost.sections.length > 1) {
|
|
|
|
pastedPost.sections.remove(pastedPost.sections.tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert first item of list section to a paragraph
|
|
|
|
if (pastedPost.sections.head && pastedPost.sections.head.isListSection) {
|
|
|
|
let list = pastedPost.sections.head;
|
|
|
|
let listItem = list.items.head;
|
|
|
|
let newMarkers = listItem.markers.map(m => m.clone());
|
|
|
|
let p = editor.builder.createMarkupSection('p', newMarkers);
|
|
|
|
|
|
|
|
pastedPost.sections.remove(list);
|
|
|
|
pastedPost.sections.append(p);
|
|
|
|
}
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
|
|
|
// same as default
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
const newPosition = postEditor.insertPost(position, pastedPost);
|
|
|
|
postEditor.setRange(newPosition);
|
|
|
|
});
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-13 12:59:13 +03:00
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
/* mobiledoc event handlers ----------------------------------------------*/
|
|
|
|
|
|
|
|
willHandleNewline(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.onNewline();
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
// manipulate mobiledoc content before committing changes
|
|
|
|
// - only one section
|
|
|
|
// - first section must be a markerable section
|
|
|
|
// - if first section is a list, grab the content of the first list item
|
|
|
|
didUpdatePost(postEditor) {
|
|
|
|
let {builder, editor, editor: {post}} = postEditor;
|
|
|
|
|
2021-09-03 12:51:33 +03:00
|
|
|
// NOTE: Update `handlePaste` to match if basic html cleanup operations are changed
|
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
// remove any non-markerable sections
|
|
|
|
post.sections.forEach((section) => {
|
|
|
|
if (!section.isMarkerable && !section.isListSection) {
|
|
|
|
let reposition = section === editor.activeSection;
|
|
|
|
postEditor.removeSection(section);
|
|
|
|
if (reposition) {
|
|
|
|
postEditor.setRange(post.sections.head.tailPosition());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// strip all sections other than the first
|
|
|
|
if (post.sections.length > 1) {
|
|
|
|
while (post.sections.length > 1) {
|
|
|
|
postEditor.removeSection(post.sections.tail);
|
|
|
|
}
|
|
|
|
postEditor.setRange(post.sections.head.tailPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert list section to a paragraph section
|
2021-09-03 12:51:33 +03:00
|
|
|
if (post.sections.head && post.sections.head.isListSection) {
|
2018-08-08 14:53:35 +03:00
|
|
|
let list = post.sections.head;
|
|
|
|
let listItem = list.items.head;
|
|
|
|
let newMarkers = listItem.markers.map(m => m.clone());
|
|
|
|
let p = builder.createMarkupSection('p', newMarkers);
|
|
|
|
postEditor.replaceSection(list, p);
|
|
|
|
postEditor.setRange(post.sections.head.tailPosition());
|
|
|
|
}
|
2021-12-09 16:08:10 +03:00
|
|
|
|
|
|
|
// convert first section to the expected tag type (might be a heading, blockquote, etc)
|
|
|
|
if (post.sections.head) {
|
|
|
|
post.sections.head.tagName = this.defaultTag;
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
postDidChange() {
|
|
|
|
// trigger closure action
|
2021-09-03 10:59:59 +03:00
|
|
|
const html = this._getHTML();
|
|
|
|
this._lastSetHtml = html;
|
|
|
|
this.onChange(html);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
cursorDidChange(editor) {
|
|
|
|
// 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
|
|
|
|
toggleSpecialFormatEditState(editor);
|
|
|
|
|
|
|
|
// pass the selected range through to the toolbar + menu components
|
|
|
|
this.set('selectedRange', editor.range);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +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));
|
|
|
|
|
|
|
|
// On keyboard cursor movement our `cursorDidChange` toggle for special
|
|
|
|
// formats happens before mobiledoc's readstate updates the edit states
|
|
|
|
// so we have to re-do it here
|
|
|
|
// TODO: can we make the event order consistent in mobiledoc-kit?
|
|
|
|
toggleSpecialFormatEditState(editor);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.set('activeMarkupTagNames', markupTags);
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
/* private methods -------------------------------------------------------*/
|
|
|
|
|
|
|
|
// rather than parsing mobiledoc to HTML we can grab the HTML directly from
|
|
|
|
// inside the editor element because we should only be dealing with
|
|
|
|
// inline markup that directly maps to HTML elements
|
|
|
|
_getHTML() {
|
|
|
|
if (this.editor && this.editor.element) {
|
2021-11-17 12:42:17 +03:00
|
|
|
let firstParagraph = this.editor.element.querySelector(this.defaultTag) || this.editor.element.querySelector('p');
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
if (!firstParagraph) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:57:17 +03:00
|
|
|
let html = firstParagraph.innerHTML || '';
|
2021-11-19 13:10:23 +03:00
|
|
|
return cleanBasicHtml(html, {allowBr: !!this.allowBr});
|
2018-08-08 14:53:35 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|