Fixed "special markups" not being separate across different editors

no issue

- if the character used to define a special markup is different such as in the text replacement editor, pressing Backspace was not inserting the correct characters
This commit is contained in:
Kevin Ansfield 2020-04-17 14:11:19 +01:00
parent 58f2755bcd
commit e191ffe8c9
4 changed files with 40 additions and 6 deletions

View File

@ -74,6 +74,11 @@ export default Component.extend({
/* lifecycle hooks ------------------------------------------------------ */
init() {
this._super(...arguments);
this.SPECIAL_MARKUPS = [];
},
didReceiveAttrs() {
this._super(...arguments);

View File

@ -238,6 +238,7 @@ export default Component.extend({
init() {
this._super(...arguments);
this.SPECIAL_MARKUPS = SPECIAL_MARKUPS;
// set a blank mobiledoc if we didn't receive anything
let mobiledoc = this.mobiledoc;

View File

@ -7,7 +7,7 @@ import registerKeyCommands, {TEXT_REPLACEMENT_KEY_COMMANDS} from '../options/key
import validator from 'validator';
import {BLANK_DOC, MOBILEDOC_VERSION} from './koenig-editor';
import {DRAG_DISABLED_DATA_ATTR} from '../lib/dnd/constants';
import {arrayToMap, toggleSpecialFormatEditState} from './koenig-editor';
import {arrayToMap} from './koenig-editor';
import {assign} from '@ember/polyfills';
import {computed} from '@ember/object';
import {getContentFromPasteEvent} from 'mobiledoc-kit/utils/parse-utils';
@ -21,13 +21,37 @@ const UNDO_DEPTH = 50;
// markups that should not be continued when typing and reverted to their
// text expansion style when backspacing over final char of markup
export const SPECIAL_MARKUPS = {
const SPECIAL_MARKUPS = {
S: '~~',
CODE: '{', // this is different because we use <code> to represent {} replacements
SUP: '^',
SUB: '~'
};
// 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
export 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);
});
}
}
});
}
}
export default Component.extend({
layout,
@ -87,6 +111,11 @@ export default Component.extend({
/* lifecycle hooks ------------------------------------------------------ */
init() {
this._super(...arguments);
this.SPECIAL_MARKUPS = SPECIAL_MARKUPS;
},
didReceiveAttrs() {
this._super(...arguments);

View File

@ -1,8 +1,7 @@
import Browser from 'mobiledoc-kit/utils/browser';
import {
CURSOR_AFTER,
CURSOR_BEFORE,
SPECIAL_MARKUPS
CURSOR_BEFORE
} from '../components/koenig-editor';
// Key commands will run any time a particular key or key combination is pressed
@ -179,7 +178,7 @@ export const DEFAULT_KEY_COMMANDS = [{
// 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 && offset !== 0) {
let specialMarkupTagNames = Object.keys(SPECIAL_MARKUPS);
let specialMarkupTagNames = Object.keys(koenig.SPECIAL_MARKUPS);
let hasReversed = false;
specialMarkupTagNames.forEach((tagName) => {
// only continue if we're about to delete a special markup
@ -190,7 +189,7 @@ export const DEFAULT_KEY_COMMANDS = [{
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 markdown = koenig.SPECIAL_MARKUPS[tagName];
let range = editor.range.expandByMarker(marker => !!marker.markups.includes(markup));
postEditor.insertText(range.head, markdown);
range = range.extend(markdown.length);