1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00

Fixed {} chars being removed when applying text replacement format

no issue

- we want to keep the `{` and `}` chars when applying/removing formatting from text replacement strings so that you can see the full string with formatting
- added ability to specify a non-replaced special format which is used by the <kbd>Backspace</kbd> handler
- added helper utility to the text expansions for applying formatting to a match without replacing the "markdown" characters
This commit is contained in:
Kevin Ansfield 2020-04-17 14:49:54 +01:00
parent e191ffe8c9
commit 2e63e0f496
3 changed files with 46 additions and 9 deletions

View file

@ -23,7 +23,10 @@ const UNDO_DEPTH = 50;
// text expansion style when backspacing over final char of markup
const SPECIAL_MARKUPS = {
S: '~~',
CODE: '{', // this is different because we use <code> to represent {} replacements
CODE: {
char: '{', // this is different because we use <code> to represent {} replacements
replace: false
},
SUP: '^',
SUB: '~'
};

View file

@ -190,14 +190,30 @@ export const DEFAULT_KEY_COMMANDS = [{
// wrap with the text expansion, remove formatting, then delete the last char
editor.run((postEditor) => {
let markdown = koenig.SPECIAL_MARKUPS[tagName];
let replace = true;
if (typeof markdown === 'object') {
replace = markdown.replace;
markdown = markdown.char;
}
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);
// replaced markdown (default) will have chars removed when formatted
// and added back when the format is removed by backspace
if (replace) {
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);
} else {
postEditor.toggleMarkup(tagName, range);
let endPos = postEditor.deleteAtPosition(range.tail);
postEditor.setRange(endPos);
}
});
hasReversed = true;
}

View file

@ -76,6 +76,24 @@ function _addMarkdownMarkup(_this, editor, matches, markupStr) {
}, 10);
}
function _addNonReplacedMarkdownMarkup(_this, editor, matches, markupStr) {
let {range} = editor;
let match = matches[0].trim();
range = range.extend(-(match.length));
editor.run((postEditor) => {
let markup = editor.builder.createMarkup(markupStr);
postEditor.addMarkupToRange(range, markup);
postEditor.setRange(range.tail.toRange());
});
// must be scheduled so that the toggle isn't reset automatically
// by mobiledoc-kit re-setting state after the range is updated
run.later(_this, function () {
editor.toggleMarkup(markupStr);
}, 10);
}
function _matchStrongStar(editor, text) {
let matches = text.match(/(?:^|\s)\*\*([^\s*]+|[^\s*][^*]*[^\s])\*\*$/);
if (matches) {
@ -534,7 +552,7 @@ export function registerTextReplacementTextExpansions(editor, koenig) {
let match = text.match(/(?:^|\s)\{([^\s{}]+|[^\s{}][^{}]*[^\s{}])\}$/);
if (match) {
_addMarkdownMarkup(this, editor, match, 'code');
_addNonReplacedMarkdownMarkup(this, editor, match, 'code');
}
}
});