1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Fixes #2560: Add toggling functionality for bold, italics and code in Editor Toolbar (#2565)

* Add toggling functionality for bold, italics and code in Editor toolbar

* Shorten code
This commit is contained in:
Mohammed Rabeeh 2020-03-07 04:20:49 +05:30 committed by GitHub
parent b31721b836
commit 67e5451c7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1504,12 +1504,29 @@ class NoteTextComponent extends React.Component {
this.scheduleSave();
}
toggleWrapSelection(strings1, strings2, defaultText) {
const selection = this.textOffsetSelection();
let string = this.state.note.body.substr(selection.start, selection.end - selection.start);
let replaced = false;
for (var i = 0; i < strings1.length; i++) {
if (string.startsWith(strings1[i]) && string.endsWith(strings1[i])) {
this.wrapSelectionWithStrings('', '', '', string.substr(strings1[i].length, selection.end - selection.start - (2 * strings1[i].length)));
replaced = true;
break;
}
}
if (!replaced) {
this.wrapSelectionWithStrings(strings1[0], strings2[0], defaultText);
}
}
commandTextBold() {
this.wrapSelectionWithStrings('**', '**', _('strong text'));
this.toggleWrapSelection(['**'], ['**'], _('strong text'));
}
commandTextItalic() {
this.wrapSelectionWithStrings('*', '*', _('emphasized text'));
this.toggleWrapSelection(['*', '_'], ['*', '_'], _('emphasized text'));
}
commandDateTime() {
@ -1525,9 +1542,13 @@ class NoteTextComponent extends React.Component {
if (match && match.length > 0) {
// Follow the same newline style
this.wrapSelectionWithStrings(`\`\`\`${match[0]}`, `${match[0]}\`\`\``);
if (string.startsWith('```') && string.endsWith('```')) {
this.wrapSelectionWithStrings('', '', '', string.substr(4, selection.end - selection.start - 8));
} else {
this.wrapSelectionWithStrings(`\`\`\`${match[0]}`, `${match[0]}\`\`\``);
}
} else {
this.wrapSelectionWithStrings('`', '`');
this.toggleWrapSelection(['`'], ['`'], '');
}
}