From 17f0a04cecdd56e2c9036ebb5ba8d2b84987ab55 Mon Sep 17 00:00:00 2001 From: noelzubin Date: Thu, 5 Jul 2018 04:04:10 +0530 Subject: [PATCH 1/2] Add Ctrl+I and Ctrl+B support to format text Ctrl+I Now toggle Italics Ctrl+B Now toggle Bold Note: If there is a newline between the selected text this wont work since codemirror doesn't wrap text with newline in them anyway --- browser/components/CodeEditor.js | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index a4d2278e2..9b06e9c27 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -153,8 +153,69 @@ export default class CodeEditor extends React.Component { 'Cmd-T': function (cm) { // Do nothing }, + 'Ctrl-I': function(cm) { + if(cm.somethingSelected()) { + let selection = cm.getSelection() + let str = selection.trim() + let index = selection.indexOf(str) + + if(str.length === 0 || str.match(/\n\s*\n/) !== null) + return + + let newString + let boldAndItalic = str.match(/^\*\*\*(.*)\*\*\*$/) + let bold = str.match(/^\*\*(.*)\*\*$/) + let italic = str.match(/^\*(.*)\*$/) + if(boldAndItalic !== null) { + newString = boldAndItalic[1] + newString = `**${newString}**` + } else if(bold !== null) { + newString = bold[1] + newString = `***${newString}***` + } else if(italic !== null) { + newString = italic[1] + newString = newString + } else { + newString = `*${str}*` + } + + let newSelection = selection.substr(0, index) + newString + selection.substr(index + selection.length) + cm.replaceSelection(newSelection) + } + }, + 'Ctrl-B': function(cm) { + if(cm.somethingSelected()) { + let selection = cm.getSelection() + let str = selection.trim() + let index = selection.indexOf(str) + + if(str.length === 0 || str.match(/\n\s*\n/) !== null) + return + + let newString + let boldAndItalic = str.match(/^\*\*\*(.*)\*\*\*$/) + let bold = str.match(/^\*\*(.*)\*\*$/) + let italic = str.match(/^\*(.*)\*$/) + if(boldAndItalic !== null) { + newString = boldAndItalic[1] + newString = `*${newString}*` + } else if(bold !== null) { + newString = bold[1] + newString = newString + } else if(italic !== null) { + newString = italic[1] + newString = `***${newString}***` + } else { + newString = `**${str}**` + } + + let newSelection = selection.substr(0, index) + newString + selection.substr(index + selection.length) + cm.replaceSelection(newSelection) + } + }, Enter: 'boostNewLineAndIndentContinueMarkdownList', 'Ctrl-C': (cm) => { + console.log('here', cm) if (cm.getOption('keyMap').substr(0, 3) === 'vim') { document.execCommand('copy') } From 49e173e1f242bc336ccbcf2e77500ea587c58ca7 Mon Sep 17 00:00:00 2001 From: noelzubin Date: Wed, 18 Jul 2018 01:16:31 +0530 Subject: [PATCH 2/2] refacor code; fix linting; add mac support --- browser/components/CodeEditor.js | 127 +++++++++++++++++-------------- 1 file changed, 68 insertions(+), 59 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 9b06e9c27..83551853c 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -153,69 +153,20 @@ export default class CodeEditor extends React.Component { 'Cmd-T': function (cm) { // Do nothing }, - 'Ctrl-I': function(cm) { - if(cm.somethingSelected()) { - let selection = cm.getSelection() - let str = selection.trim() - let index = selection.indexOf(str) - - if(str.length === 0 || str.match(/\n\s*\n/) !== null) - return - - let newString - let boldAndItalic = str.match(/^\*\*\*(.*)\*\*\*$/) - let bold = str.match(/^\*\*(.*)\*\*$/) - let italic = str.match(/^\*(.*)\*$/) - if(boldAndItalic !== null) { - newString = boldAndItalic[1] - newString = `**${newString}**` - } else if(bold !== null) { - newString = bold[1] - newString = `***${newString}***` - } else if(italic !== null) { - newString = italic[1] - newString = newString - } else { - newString = `*${str}*` - } - - let newSelection = selection.substr(0, index) + newString + selection.substr(index + selection.length) - cm.replaceSelection(newSelection) - } + 'Cmd-I': cm => { + if (global.process.platform === 'darwin') this.applyItalic(cm) }, - 'Ctrl-B': function(cm) { - if(cm.somethingSelected()) { - let selection = cm.getSelection() - let str = selection.trim() - let index = selection.indexOf(str) - - if(str.length === 0 || str.match(/\n\s*\n/) !== null) - return - - let newString - let boldAndItalic = str.match(/^\*\*\*(.*)\*\*\*$/) - let bold = str.match(/^\*\*(.*)\*\*$/) - let italic = str.match(/^\*(.*)\*$/) - if(boldAndItalic !== null) { - newString = boldAndItalic[1] - newString = `*${newString}*` - } else if(bold !== null) { - newString = bold[1] - newString = newString - } else if(italic !== null) { - newString = italic[1] - newString = `***${newString}***` - } else { - newString = `**${str}**` - } - - let newSelection = selection.substr(0, index) + newString + selection.substr(index + selection.length) - cm.replaceSelection(newSelection) - } + 'Ctrl-I': cm => { + if (global.process.platform !== 'darwin') this.applyItalic(cm) + }, + 'Cmd-B': cm => { + if (global.process.platform === 'darwin') this.applyBold(cm) + }, + 'Ctrl-B': cm => { + if (global.process.platform !== 'darwin') this.applyBold(cm) }, Enter: 'boostNewLineAndIndentContinueMarkdownList', 'Ctrl-C': (cm) => { - console.log('here', cm) if (cm.getOption('keyMap').substr(0, 3) === 'vim') { document.execCommand('copy') } @@ -245,6 +196,64 @@ export default class CodeEditor extends React.Component { CodeMirror.Vim.map('ZZ', ':q', 'normal') } + applyItalic (cm) { + if (cm.somethingSelected()) { + const selection = cm.getSelection() + const str = selection.trim() + const index = selection.indexOf(str) + + if (str.length === 0 || str.match(/\n\s*\n/) !== null) return + + let newString + const boldAndItalic = str.match(/^\*\*\*(.*)\*\*\*$/) + const bold = str.match(/^\*\*(.*)\*\*$/) + const italic = str.match(/^\*(.*)\*$/) + if (boldAndItalic !== null) { + newString = boldAndItalic[1] + newString = `**${newString}**` + } else if (bold !== null) { + newString = bold[1] + newString = `***${newString}***` + } else if (italic !== null) { + newString = italic[1] + } else { + newString = `*${str}*` + } + + const newSelection = selection.substr(0, index) + newString + selection.substr(index + selection.length) + cm.replaceSelection(newSelection) + } + } + + applyBold (cm) { + if (cm.somethingSelected()) { + const selection = cm.getSelection() + const str = selection.trim() + const index = selection.indexOf(str) + + if (str.length === 0 || str.match(/\n\s*\n/) !== null) return + + let newString + const boldAndItalic = str.match(/^\*\*\*(.*)\*\*\*$/) + const bold = str.match(/^\*\*(.*)\*\*$/) + const italic = str.match(/^\*(.*)\*$/) + if (boldAndItalic !== null) { + newString = boldAndItalic[1] + newString = `*${newString}*` + } else if (bold !== null) { + newString = bold[1] + } else if (italic !== null) { + newString = italic[1] + newString = `***${newString}***` + } else { + newString = `**${str}**` + } + + const newSelection = selection.substr(0, index) + newString + selection.substr(index + selection.length) + cm.replaceSelection(newSelection) + } + } + expandSnippet (line, cursor, cm, snippets) { const wordBeforeCursor = this.getWordBeforeCursor(line, cursor.line, cursor.ch) const templateCursorString = ':{}'