2018-04-03 15:43:08 +03:00
|
|
|
|
import {run} from '@ember/runloop';
|
2017-05-08 12:44:02 +03:00
|
|
|
|
|
|
|
|
|
// Text expansions watch text entry events and will look for matches, replacing
|
|
|
|
|
// the matches with additional markup, atoms, or cards
|
|
|
|
|
// https://github.com/bustlelabs/mobiledoc-kit#responding-to-text-input
|
2017-02-27 07:44:15 +03:00
|
|
|
|
|
2018-06-15 14:12:12 +03:00
|
|
|
|
export function replaceWithListSection(editor, matches, listTagName) {
|
|
|
|
|
let {range, range: {head, head: {section}}} = editor;
|
|
|
|
|
let text = section.textUntil(head);
|
|
|
|
|
|
|
|
|
|
// we don't want to convert to a heading if the user has not just
|
|
|
|
|
// finished typing the markdown (eg, they've made a previous
|
|
|
|
|
// heading expansion then Cmd-Z'ed it to get the text back then
|
|
|
|
|
// starts typing at the end of the heading)
|
|
|
|
|
if (text !== matches[0]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 16:00:00 +03:00
|
|
|
|
if (section.isListItem) {
|
|
|
|
|
if (section.parent.tagName === listTagName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// toggle all list items to the new list type
|
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
|
let listRange = section.parent.toRange();
|
|
|
|
|
let {post} = editor;
|
|
|
|
|
|
|
|
|
|
range = range.extend(-(matches[0].length));
|
|
|
|
|
postEditor.deleteRange(range);
|
|
|
|
|
|
|
|
|
|
let cursorListItem;
|
|
|
|
|
post.walkMarkerableSections(listRange, (listItem) => {
|
|
|
|
|
let isCursorListItem = listItem === section;
|
|
|
|
|
let changedListItem = postEditor.changeSectionTagName(listItem, listTagName);
|
|
|
|
|
|
|
|
|
|
if (isCursorListItem) {
|
|
|
|
|
cursorListItem = changedListItem;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
postEditor.setRange(cursorListItem.headPosition().toRange());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 14:12:12 +03:00
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
|
range = range.extend(-(matches[0].length));
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
postEditor.setRange(position);
|
|
|
|
|
postEditor.toggleSection(listTagName);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 14:53:35 +03:00
|
|
|
|
function registerInlineMarkdownTextExpansions(editor) {
|
2018-01-30 13:01:07 +03:00
|
|
|
|
/* inline markdown ------------------------------------------------------ */
|
|
|
|
|
|
2018-05-14 19:29:49 +03:00
|
|
|
|
// --\s = en dash –
|
|
|
|
|
// ---. = em dash —
|
|
|
|
|
// separate to the grouped replacement functions because we're matching on
|
|
|
|
|
// the trailing character which can be anything
|
|
|
|
|
editor.onTextInput({
|
|
|
|
|
name: 'hyphens',
|
|
|
|
|
match: /---?.$/,
|
|
|
|
|
run(editor) {
|
|
|
|
|
let {range} = editor;
|
|
|
|
|
|
|
|
|
|
let text = editor.range.head.section.textUntil(editor.range.head);
|
|
|
|
|
|
|
|
|
|
// do not match if we're in code formatting
|
|
|
|
|
if (editor.hasActiveMarkup('code') || text.match(/[^\s]?`[^\s]/)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let ndashMatch = text.match(/[^-]--(\s)$/);
|
|
|
|
|
if (ndashMatch) {
|
|
|
|
|
let match = ndashMatch[0];
|
|
|
|
|
range = range.extend(-(match.length - 1));
|
|
|
|
|
|
|
|
|
|
if (editor.detectMarkupInRange(range, 'code')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return editor.run((postEditor) => {
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
postEditor.insertText(position, `–${ndashMatch[1]}`);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mdashMatch = text.match(/---([^-])$/);
|
|
|
|
|
if (mdashMatch) {
|
|
|
|
|
let match = mdashMatch[0];
|
|
|
|
|
range = range.extend(-(match.length));
|
|
|
|
|
|
|
|
|
|
if (editor.detectMarkupInRange(range, 'code')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return editor.run((postEditor) => {
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
postEditor.insertText(position, `—${mdashMatch[1]}`);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-06-01 19:53:15 +03:00
|
|
|
|
// We don't want to run all our content rules on every text entry event,
|
|
|
|
|
// instead we check to see if this text entry event could match a content
|
|
|
|
|
// rule, and only then run the rules. Right now we only want to match
|
|
|
|
|
// content ending with *, _, ), ~, and `. This could increase as we support
|
|
|
|
|
// more markdown.
|
|
|
|
|
|
|
|
|
|
editor.onTextInput({
|
|
|
|
|
name: 'inline_markdown',
|
|
|
|
|
match: /[*_)~`^]$/,
|
|
|
|
|
run(editor, matches) {
|
|
|
|
|
let text = editor.range.head.section.textUntil(editor.range.head);
|
|
|
|
|
|
|
|
|
|
switch (matches[0]) {
|
|
|
|
|
case '*':
|
|
|
|
|
matchStrongStar(editor, text);
|
|
|
|
|
matchEmStar(editor, text);
|
|
|
|
|
break;
|
|
|
|
|
case '_':
|
|
|
|
|
matchStrongUnderscore(editor, text);
|
|
|
|
|
matchEmUnderscore(editor, text);
|
|
|
|
|
break;
|
|
|
|
|
case ')':
|
|
|
|
|
matchLink(editor, text);
|
|
|
|
|
matchImage(editor, text);
|
|
|
|
|
break;
|
|
|
|
|
case '~':
|
|
|
|
|
matchSub(editor, text);
|
|
|
|
|
matchStrikethrough(editor, text);
|
|
|
|
|
break;
|
|
|
|
|
case '`':
|
|
|
|
|
matchCode(editor, text);
|
|
|
|
|
break;
|
|
|
|
|
case '^':
|
|
|
|
|
matchSup(editor, text);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-04-03 15:43:08 +03:00
|
|
|
|
function matchStrongStar(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
|
let {range} = editor;
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let matches = text.match(/(?:^|\s)\*\*([^\s*]+|[^\s*][^*]*[^\s])\*\*/);
|
2017-03-02 19:51:57 +03:00
|
|
|
|
if (matches) {
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let match = matches[0].trim();
|
2018-03-15 20:13:50 +03:00
|
|
|
|
range = range.extend(-(match.length));
|
2018-04-03 15:43:08 +03:00
|
|
|
|
|
2017-03-02 19:51:57 +03:00
|
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let bold = postEditor.builder.createMarkup('strong');
|
2018-04-10 17:04:18 +03:00
|
|
|
|
postEditor.insertTextWithMarkup(position, matches[1], [bold]);
|
2017-02-27 07:44:15 +03:00
|
|
|
|
});
|
2018-04-03 15:43:08 +03:00
|
|
|
|
|
2018-04-10 17:04:18 +03:00
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
2019-02-24 14:31:36 +03:00
|
|
|
|
// by mobiledoc-kit re-setting state after the range is updated
|
|
|
|
|
run.later(this, function () {
|
2018-04-10 17:04:18 +03:00
|
|
|
|
editor.toggleMarkup('strong');
|
2019-02-24 14:31:36 +03:00
|
|
|
|
}, 10);
|
2018-04-10 17:04:18 +03:00
|
|
|
|
}
|
2017-02-27 07:44:15 +03:00
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
|
2018-04-03 15:43:08 +03:00
|
|
|
|
function matchStrongUnderscore(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
|
let {range} = editor;
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let matches = text.match(/(?:^|\s)__([^\s_]+|[^\s_][^_]*[^\s])__/);
|
2017-03-02 19:51:57 +03:00
|
|
|
|
if (matches) {
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let match = matches[0].trim();
|
2018-03-15 20:13:50 +03:00
|
|
|
|
range = range.extend(-(match.length));
|
2018-04-03 15:43:08 +03:00
|
|
|
|
|
2017-03-02 19:51:57 +03:00
|
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let bold = postEditor.builder.createMarkup('strong');
|
2018-04-10 17:04:18 +03:00
|
|
|
|
postEditor.insertTextWithMarkup(position, matches[1], [bold]);
|
2018-04-03 15:43:08 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
2019-02-24 14:31:36 +03:00
|
|
|
|
// by mobiledoc-kit re-setting state after the range is updated
|
|
|
|
|
run.later(this, function () {
|
2018-04-03 15:43:08 +03:00
|
|
|
|
editor.toggleMarkup('strong');
|
2019-02-24 14:31:36 +03:00
|
|
|
|
}, 10);
|
2017-02-27 07:44:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
|
2018-04-03 15:43:08 +03:00
|
|
|
|
function matchEmStar(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
|
let {range} = editor;
|
2018-04-10 17:04:18 +03:00
|
|
|
|
// (?:^|\s) - match beginning of input or a starting space (don't capture)
|
|
|
|
|
// \* - match leading *
|
|
|
|
|
// ( - start capturing group
|
|
|
|
|
// [^\s*]+ - match a stretch with no spaces or * chars
|
|
|
|
|
// | - OR
|
|
|
|
|
// [^\s*] - match a single non-space or * char | this group will only match at
|
|
|
|
|
// [^*]* - match zero or more non * chars | least two chars so we need the
|
|
|
|
|
// [^\s] - match a single non-space char | [^\s*]+ to match single chars
|
|
|
|
|
// ) - end capturing group
|
|
|
|
|
// \* - match trailing *
|
|
|
|
|
//
|
|
|
|
|
// input = " *foo*"
|
|
|
|
|
// matches[0] = " *foo*"
|
|
|
|
|
// matches[1] = "foo"
|
|
|
|
|
let matches = text.match(/(?:^|\s)\*([^\s*]+|[^\s*][^*]*[^\s])\*/);
|
2017-03-02 19:51:57 +03:00
|
|
|
|
if (matches) {
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let match = matches[0].trim();
|
2017-02-27 07:44:15 +03:00
|
|
|
|
range = range.extend(-(match.length));
|
2018-04-03 15:43:08 +03:00
|
|
|
|
|
2017-03-02 19:51:57 +03:00
|
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let em = postEditor.builder.createMarkup('em');
|
2018-04-10 17:04:18 +03:00
|
|
|
|
postEditor.insertTextWithMarkup(position, matches[1], [em]);
|
2018-04-03 15:43:08 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
2019-02-24 14:31:36 +03:00
|
|
|
|
// by mobiledoc-kit re-setting state after the range is updated
|
|
|
|
|
run.later(this, function () {
|
2018-04-03 15:43:08 +03:00
|
|
|
|
editor.toggleMarkup('em');
|
2019-02-24 14:31:36 +03:00
|
|
|
|
}, 10);
|
2017-02-27 07:44:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
|
2018-04-03 15:43:08 +03:00
|
|
|
|
function matchEmUnderscore(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
|
let {range} = editor;
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let matches = text.match(/(?:^|\s)_([^\s_]+|[^\s_][^_]*[^\s])_/);
|
2017-03-02 19:51:57 +03:00
|
|
|
|
if (matches) {
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let match = matches[0].trim();
|
2017-02-27 07:44:15 +03:00
|
|
|
|
range = range.extend(-(match.length));
|
2018-04-03 15:43:08 +03:00
|
|
|
|
|
2017-03-02 19:51:57 +03:00
|
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let em = postEditor.builder.createMarkup('em');
|
2018-04-10 17:04:18 +03:00
|
|
|
|
postEditor.insertTextWithMarkup(position, matches[1], [em]);
|
2018-04-03 15:43:08 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
2019-02-24 14:31:36 +03:00
|
|
|
|
// by mobiledoc-kit re-setting state after the range is updated
|
|
|
|
|
run.later(this, function () {
|
2018-04-18 12:17:13 +03:00
|
|
|
|
editor.toggleMarkup('em');
|
2019-02-24 14:31:36 +03:00
|
|
|
|
}, 10);
|
2017-02-27 07:44:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
|
2018-06-01 19:53:15 +03:00
|
|
|
|
function matchSub(editor, text) {
|
|
|
|
|
let {range} = editor;
|
|
|
|
|
let matches = text.match(/(^|[^~])~([^\s~]+|[^\s~][^~]*[^\s])~/);
|
|
|
|
|
if (matches) {
|
|
|
|
|
let match = matches[0].trim();
|
|
|
|
|
range = range.extend(-(match.length - matches[1].trim().length));
|
|
|
|
|
|
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let sub = postEditor.builder.createMarkup('sub');
|
|
|
|
|
postEditor.insertTextWithMarkup(position, matches[2], [sub]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
2019-02-24 14:31:36 +03:00
|
|
|
|
// by mobiledoc-kit re-setting state after the range is updated
|
|
|
|
|
run.later(this, function () {
|
2018-06-01 19:53:15 +03:00
|
|
|
|
editor.toggleMarkup('sub');
|
2019-02-24 14:31:36 +03:00
|
|
|
|
}, 10);
|
2018-06-01 19:53:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 15:43:08 +03:00
|
|
|
|
function matchStrikethrough(editor, text) {
|
2018-03-15 20:13:50 +03:00
|
|
|
|
let {range} = editor;
|
2018-04-12 14:40:15 +03:00
|
|
|
|
let matches = text.match(/(?:^|\s)~~([^\s~]+|[^\s~][^~]*[^\s])~~/);
|
2018-03-15 20:13:50 +03:00
|
|
|
|
if (matches) {
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let match = matches[0].trim();
|
2018-03-15 20:13:50 +03:00
|
|
|
|
range = range.extend(-(match.length));
|
2018-04-03 15:43:08 +03:00
|
|
|
|
|
2018-03-15 20:13:50 +03:00
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let s = postEditor.builder.createMarkup('s');
|
2018-04-10 17:04:18 +03:00
|
|
|
|
postEditor.insertTextWithMarkup(position, matches[1], [s]);
|
2018-04-03 15:43:08 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
2019-02-24 14:31:36 +03:00
|
|
|
|
// by mobiledoc-kit re-setting state after the range is updated
|
|
|
|
|
run.later(this, function () {
|
2018-04-03 15:43:08 +03:00
|
|
|
|
editor.toggleMarkup('s');
|
2019-02-24 14:31:36 +03:00
|
|
|
|
}, 10);
|
2018-03-15 20:13:50 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 15:43:08 +03:00
|
|
|
|
function matchCode(editor, text) {
|
2018-03-15 20:13:50 +03:00
|
|
|
|
let {range} = editor;
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let matches = text.match(/(?:^|\s)`([^\s`]+|[^\s`][^`]*[^\s`])`/);
|
2018-03-15 20:13:50 +03:00
|
|
|
|
if (matches) {
|
2018-04-10 17:04:18 +03:00
|
|
|
|
let match = matches[0].trim();
|
2018-03-15 20:13:50 +03:00
|
|
|
|
range = range.extend(-(match.length));
|
2018-04-03 15:43:08 +03:00
|
|
|
|
|
2018-03-15 20:13:50 +03:00
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let code = postEditor.builder.createMarkup('code');
|
2018-04-10 17:04:18 +03:00
|
|
|
|
postEditor.insertTextWithMarkup(position, matches[1], [code]);
|
2018-04-03 15:43:08 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
2019-02-24 14:31:36 +03:00
|
|
|
|
// by mobiledoc-kit re-setting state after the range is updated
|
|
|
|
|
run.later(this, function () {
|
2018-04-03 15:43:08 +03:00
|
|
|
|
editor.toggleMarkup('code');
|
2019-02-24 14:31:36 +03:00
|
|
|
|
}, 10);
|
2018-03-15 20:13:50 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 19:53:15 +03:00
|
|
|
|
function matchSup(editor, text) {
|
|
|
|
|
let {range} = editor;
|
|
|
|
|
let matches = text.match(/\^([^\s^]+|[^\s^][^^]*[^\s^])\^/);
|
|
|
|
|
if (matches) {
|
|
|
|
|
let match = matches[0].trim();
|
|
|
|
|
range = range.extend(-(match.length));
|
|
|
|
|
|
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let sup = postEditor.builder.createMarkup('sup');
|
|
|
|
|
postEditor.insertTextWithMarkup(position, matches[1], [sup]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
2019-02-24 14:31:36 +03:00
|
|
|
|
// by mobiledoc-kit re-setting state after the range is updated
|
|
|
|
|
run.later(this, function () {
|
2018-06-01 19:53:15 +03:00
|
|
|
|
editor.toggleMarkup('sup');
|
2019-02-24 14:31:36 +03:00
|
|
|
|
}, 10);
|
2018-06-01 19:53:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 15:43:08 +03:00
|
|
|
|
function matchLink(editor, text) {
|
2017-03-02 19:51:57 +03:00
|
|
|
|
let {range} = editor;
|
2018-04-26 19:40:38 +03:00
|
|
|
|
let matches = text.match(/(?:^|\s)\[([^\s\]]+|[^\s\]][^\]]*[^\s\]])\]\(([^\s)]+|[^\s)][^)]*[^\s)])\)/);
|
2017-03-02 19:51:57 +03:00
|
|
|
|
if (matches) {
|
2018-04-26 19:40:38 +03:00
|
|
|
|
let url = matches[2];
|
|
|
|
|
let text = matches[1] || url;
|
|
|
|
|
let match = matches[0].trim();
|
2017-02-27 07:44:15 +03:00
|
|
|
|
range = range.extend(-match.length);
|
2018-04-03 15:43:08 +03:00
|
|
|
|
|
2017-03-02 19:51:57 +03:00
|
|
|
|
editor.run((postEditor) => {
|
2017-02-27 07:44:15 +03:00
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
let a = postEditor.builder.createMarkup('a', {href: url});
|
2018-04-03 15:43:08 +03:00
|
|
|
|
postEditor.insertTextWithMarkup(position, text, [a]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// must be scheduled so that the toggle isn't reset automatically
|
|
|
|
|
run.schedule('actions', this, function () {
|
2018-04-18 12:17:13 +03:00
|
|
|
|
editor.toggleMarkup('a');
|
2017-02-27 07:44:15 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-02 19:51:57 +03:00
|
|
|
|
|
2018-03-15 20:54:15 +03:00
|
|
|
|
function matchImage(editor, text) {
|
2018-01-30 23:46:03 +03:00
|
|
|
|
let matches = text.match(/^!\[(.*?)\]\((.*?)\)$/);
|
|
|
|
|
if (matches) {
|
|
|
|
|
let {range: {head, head: {section}}} = editor;
|
2018-04-26 19:40:38 +03:00
|
|
|
|
let src = matches[2].trim();
|
|
|
|
|
let alt = matches[1].trim();
|
2018-01-30 23:46:03 +03:00
|
|
|
|
|
|
|
|
|
// skip if cursor is not at end of section
|
|
|
|
|
if (!head.isTail()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mobiledoc lists don't support cards
|
|
|
|
|
if (section.isListItem) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
editor.run((postEditor) => {
|
2018-02-01 14:26:24 +03:00
|
|
|
|
let card = postEditor.builder.createCardSection('image', {src, alt});
|
2018-01-30 23:46:03 +03:00
|
|
|
|
// need to check the section before replacing else it will always
|
|
|
|
|
// add a trailing paragraph
|
|
|
|
|
let needsTrailingParagraph = !section.next;
|
|
|
|
|
|
|
|
|
|
editor.range.extend(-(matches[0].length));
|
|
|
|
|
postEditor.replaceSection(editor.range.headSection, card);
|
|
|
|
|
|
|
|
|
|
if (needsTrailingParagraph) {
|
|
|
|
|
let newSection = editor.builder.createMarkupSection('p');
|
|
|
|
|
postEditor.insertSectionAtEnd(newSection);
|
|
|
|
|
postEditor.setRange(newSection.tailPosition());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-27 07:44:15 +03:00
|
|
|
|
}
|
2018-08-08 14:53:35 +03:00
|
|
|
|
|
|
|
|
|
export default function (editor, koenig) {
|
|
|
|
|
/* block level markdown ------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
editor.unregisterTextInputHandler('heading');
|
|
|
|
|
editor.onTextInput({
|
|
|
|
|
name: 'md_heading',
|
|
|
|
|
match: /^(#{1,6}) /,
|
|
|
|
|
run(editor, matches) {
|
|
|
|
|
let hashes = matches[1];
|
|
|
|
|
let headingTag = `h${hashes.length}`;
|
|
|
|
|
let {range} = editor;
|
|
|
|
|
let text = range.head.section.textUntil(range.head);
|
|
|
|
|
|
|
|
|
|
// we don't want to convert to a heading if the user has not just
|
|
|
|
|
// finished typing the markdown (eg, they've made a previous
|
|
|
|
|
// heading expansion then Cmd-Z'ed it to get the text back then
|
|
|
|
|
// starts typing at the end of the heading)
|
|
|
|
|
if (text !== matches[0]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
|
range = range.extend(-(matches[0].length));
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
postEditor.setRange(position);
|
|
|
|
|
|
|
|
|
|
// toggleHeaderSection will remove all formatting except links
|
|
|
|
|
koenig.send('toggleHeaderSection', headingTag, postEditor);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
editor.unregisterTextInputHandler('ul');
|
|
|
|
|
editor.onTextInput({
|
|
|
|
|
name: 'md_ul',
|
|
|
|
|
match: /^\* |^- /,
|
|
|
|
|
run(editor, matches) {
|
|
|
|
|
replaceWithListSection(editor, matches, 'ul');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
editor.unregisterTextInputHandler('ol');
|
|
|
|
|
editor.onTextInput({
|
|
|
|
|
name: 'md_ol',
|
|
|
|
|
match: /^1\.? /,
|
|
|
|
|
run(editor, matches) {
|
|
|
|
|
replaceWithListSection(editor, matches, 'ol');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
editor.onTextInput({
|
|
|
|
|
name: 'md_blockquote',
|
|
|
|
|
match: /^> /,
|
|
|
|
|
run(editor, matches) {
|
|
|
|
|
let {range} = editor;
|
|
|
|
|
let {head, head: {section}} = range;
|
|
|
|
|
let text = section.textUntil(head);
|
|
|
|
|
|
|
|
|
|
// ensure cursor is at the end of the matched text so we don't
|
|
|
|
|
// convert text the users wants to start with `> ` and that we're
|
|
|
|
|
// not already on a blockquote section
|
|
|
|
|
if (text === matches[0] && section.tagName !== 'blockquote') {
|
|
|
|
|
editor.run((postEditor) => {
|
|
|
|
|
range = range.extend(-(matches[0].length));
|
|
|
|
|
let position = postEditor.deleteRange(range);
|
|
|
|
|
postEditor.setRange(position);
|
|
|
|
|
|
|
|
|
|
koenig.send('toggleSection', 'blockquote', postEditor);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
editor.onTextInput({
|
|
|
|
|
name: 'md_hr',
|
|
|
|
|
match: /^---$/,
|
|
|
|
|
run(editor) {
|
|
|
|
|
let {range: {head, head: {section}}} = editor;
|
|
|
|
|
|
|
|
|
|
// Skip if cursor is not at end of section
|
|
|
|
|
if (!head.isTail()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip if section is a list item
|
|
|
|
|
if (section.isListItem) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
koenig.send('replaceWithCardSection', 'hr', section.toRange());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
editor.onTextInput({
|
|
|
|
|
name: 'md_code',
|
|
|
|
|
match: /^```$/,
|
|
|
|
|
run(editor) {
|
|
|
|
|
let {range: {head, head: {section}}} = editor;
|
|
|
|
|
|
|
|
|
|
// Skip if cursor is not at end of section
|
|
|
|
|
if (!head.isTail()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip if section is a list item
|
|
|
|
|
if (section.isListItem) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
koenig.send('replaceWithCardSection', 'code', section.toRange());
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-08-08 17:52:13 +03:00
|
|
|
|
|
|
|
|
|
/* inline markdown -------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
// must come after block expansions so that the smart hyphens expansion
|
|
|
|
|
// doesn't break the divider card expansion
|
|
|
|
|
registerInlineMarkdownTextExpansions(editor);
|
2018-08-08 14:53:35 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: reduce duplication
|
|
|
|
|
export function registerBasicTextExpansions(editor) {
|
|
|
|
|
// unregister mobiledoc-kit's block-level text handlers
|
|
|
|
|
editor.unregisterTextInputHandler('heading');
|
|
|
|
|
editor.unregisterTextInputHandler('ul');
|
|
|
|
|
editor.unregisterTextInputHandler('ol');
|
|
|
|
|
|
|
|
|
|
registerInlineMarkdownTextExpansions(editor);
|
|
|
|
|
}
|