2
0
mirror of https://github.com/Ionaru/easy-markdown-editor synced 2025-09-24 16:40:55 -06:00

Added the ability to underline selected text in EasyMDE.

Added the ability to underline selected text in EasyMDE.

You can now add a button with the following options:

name: "underline",
action: EasyMDE.toggleUnderline,
className: "fa fa-underline",
title: "Underline"

This has had to replace the option to use "__" for bold, as that is what various markdown renderers (Showdown etc.) have decided to use.

EasyMDE.toggleUnderline works in exactly the same way as the EasyMDE.toggleBold function, just with "__" instead of "**".
This commit is contained in:
Jacob Clayden 2018-08-30 05:16:16 +01:00
parent 579f61b0c7
commit 8913fb446c

View File

@ -23,6 +23,7 @@ var anchorToExternalRegex = new RegExp(/(<a.*?https?:\/\/.*?[^a]>)+?/g);
var bindings = {
'toggleBold': toggleBold,
'toggleItalic': toggleItalic,
'toggleUnderline': toggleUnderline,
'drawLink': drawLink,
'toggleHeadingSmaller': toggleHeadingSmaller,
'toggleHeadingBigger': toggleHeadingBigger,
@ -48,6 +49,7 @@ var bindings = {
var shortcuts = {
'toggleBold': 'Cmd-B',
'toggleItalic': 'Cmd-I',
'toggleUnderline': 'Cmd-U',
'drawLink': 'Cmd-K',
'toggleHeadingSmaller': 'Cmd-H',
'toggleHeadingBigger': 'Shift-Cmd-H',
@ -195,6 +197,8 @@ function getState(cm, pos) {
ret.quote = true;
} else if (data === 'em') {
ret.italic = true;
} else if (data === 'u') {
ret.underline = true;
} else if (data === 'quote') {
ret.quote = true;
} else if (data === 'strikethrough') {
@ -283,6 +287,14 @@ function toggleItalic(editor) {
}
/**
* Action for toggling underline.
*/
function toggleUnderline(editor) {
_toggleBlock(editor, 'underline', editor.options.blockStyles.underline);
}
/**
* Action for toggling strikethrough.
*/
@ -1022,11 +1034,14 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
start = text.slice(0, startPoint.ch);
end = text.slice(startPoint.ch);
if (type == 'bold') {
start = start.replace(/(\*\*|__)(?![\s\S]*(\*\*|__))/, '');
end = end.replace(/(\*\*|__)/, '');
start = start.replace(/\*\*(?![\s\S]*\*\*)/, '');
end = end.replace(/\*\*/, '');
} else if (type == 'italic') {
start = start.replace(/(\*|_)(?![\s\S]*(\*|_))/, '');
end = end.replace(/(\*|_)/, '');
} else if (type == 'underline') {
start = start.replace(/__(?![\s\S]*__)/, '');
end = end.replace(/__/, '');
} else if (type == 'strikethrough') {
start = start.replace(/(\*\*|~~)(?![\s\S]*(\*\*|~~))/, '');
end = end.replace(/(\*\*|~~)/, '');
@ -1039,7 +1054,7 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
ch: 99999999999999
});
if (type == 'bold' || type == 'strikethrough') {
if (type == 'bold' || type == 'underline' || type == 'strikethrough') {
startPoint.ch -= 2;
if (startPoint !== endPoint) {
endPoint.ch -= 2;
@ -1058,6 +1073,8 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
} else if (type == 'italic') {
text = text.split('*').join('');
text = text.split('_').join('');
} else if (type == 'underline') {
text = text.split('__').join('');
} else if (type == 'strikethrough') {
text = text.split('~~').join('');
}
@ -1154,6 +1171,12 @@ var toolbarBuiltInButtons = {
title: 'Italic',
default: true
},
'underline': {
name: 'underline',
action: toggleUnderline,
className: 'fa fa-underline',
title: 'Underline'
},
'strikethrough': {
name: 'strikethrough',
action: toggleStrikethrough,
@ -1336,7 +1359,8 @@ var promptTexts = {
var blockStyles = {
'bold': '**',
'code': '```',
'italic': '*'
'italic': '*',
'underline': '__'
};
/**
@ -1995,6 +2019,7 @@ EasyMDE.prototype.value = function (val) {
*/
EasyMDE.toggleBold = toggleBold;
EasyMDE.toggleItalic = toggleItalic;
EasyMDE.toggleUnderline = toggleUnderline;
EasyMDE.toggleStrikethrough = toggleStrikethrough;
EasyMDE.toggleBlockquote = toggleBlockquote;
EasyMDE.toggleHeadingSmaller = toggleHeadingSmaller;
@ -2025,6 +2050,9 @@ EasyMDE.prototype.toggleBold = function () {
EasyMDE.prototype.toggleItalic = function () {
toggleItalic(this);
};
EasyMDE.prototype.toggleUnderline = function () {
toggleUnderline(this);
};
EasyMDE.prototype.toggleStrikethrough = function () {
toggleStrikethrough(this);
};