From e68d535fa267275bf92b8e81e8793b37734318b8 Mon Sep 17 00:00:00 2001 From: kostaldavid8 Date: Wed, 15 Feb 2017 22:18:20 +0100 Subject: [PATCH 1/6] Smart bullets When you hit enter on a line with a bullet, you get a new one on the new line. Also when you hit tab after a bullet, it automatically indents. It makes typing with bullets much more pleasant. --- browser/components/CodeEditor.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 6f19c49f..beee8961 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -57,17 +57,35 @@ export default class CodeEditor extends React.Component { inputStyle: 'textarea', extraKeys: { Tab: function (cm) { + let cursor = cm.getCursor() + let line = cm.getLine(cursor.line) if (cm.somethingSelected()) cm.indentSelection('add') else { - if (cm.getOption('indentWithTabs')) { - cm.execCommand('insertTab') + let tabs = cm.getOption('indentWithTabs') + if (line.trimLeft() === '- ' || line.trimLeft() === '* ') { + cm.execCommand('goLineStart') + if (tabs) cm.execCommand('insertTab'); else cm.execCommand('insertSoftTab') + cm.execCommand('goLineEnd') } else { - cm.execCommand('insertSoftTab') + if (tabs) cm.execCommand('insertTab'); else cm.execCommand('insertSoftTab') } } }, 'Cmd-T': function (cm) { // Do nothing + }, + Enter: function (cm) { + let cursor = cm.getCursor() + let line = cm.getLine(cursor.line) + let dash = line.trim().startsWith('- ') + if ((line.trim().startsWith('- ') || line.trim().startsWith('* '))) { + cm.execCommand('newlineAndIndent') + let range = {line: cursor.line + 1, ch: cm.getLine(cursor.line + 1).length} + console.log(range) + if (dash) cm.replaceRange('- ', range); else cm.replaceRange('* ', range) + } else { + cm.execCommand('newlineAndIndent') + } } } }) From 77eb19af405f3f5078136f5a136ccc719a311907 Mon Sep 17 00:00:00 2001 From: kostaldavid8 Date: Wed, 15 Feb 2017 23:30:56 +0100 Subject: [PATCH 2/6] Code style fixes --- browser/components/CodeEditor.js | 37 +++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index beee8961..03270de2 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -57,32 +57,43 @@ export default class CodeEditor extends React.Component { inputStyle: 'textarea', extraKeys: { Tab: function (cm) { - let cursor = cm.getCursor() - let line = cm.getLine(cursor.line) + const cursor = cm.getCursor() + const line = cm.getLine(cursor.line) if (cm.somethingSelected()) cm.indentSelection('add') else { - let tabs = cm.getOption('indentWithTabs') + const tabs = cm.getOption('indentWithTabs') if (line.trimLeft() === '- ' || line.trimLeft() === '* ') { cm.execCommand('goLineStart') - if (tabs) cm.execCommand('insertTab'); else cm.execCommand('insertSoftTab') + if (tabs) { + cm.execCommand('insertTab') + } else { + cm.execCommand('insertSoftTab') + } cm.execCommand('goLineEnd') } else { - if (tabs) cm.execCommand('insertTab'); else cm.execCommand('insertSoftTab') + if (tabs) { + cm.execCommand('insertTab') + } else { + cm.execCommand('insertSoftTab') + } } } }, 'Cmd-T': function (cm) { // Do nothing }, - Enter: function (cm) { - let cursor = cm.getCursor() - let line = cm.getLine(cursor.line) - let dash = line.trim().startsWith('- ') - if ((line.trim().startsWith('- ') || line.trim().startsWith('* '))) { + Enter: (cm) => { + const cursor = cm.getCursor() + const line = cm.getLine(cursor.line) + const dash = line.trim().startsWith('- ') + if (line.trim().startsWith('- ') || line.trim().startsWith('* ')) { cm.execCommand('newlineAndIndent') - let range = {line: cursor.line + 1, ch: cm.getLine(cursor.line + 1).length} - console.log(range) - if (dash) cm.replaceRange('- ', range); else cm.replaceRange('* ', range) + const range = {line: cursor.line + 1, ch: cm.getLine(cursor.line + 1).length} + if (dash) { + cm.replaceRange('- ', range) + } else { + cm.replaceRange('* ', range) + } } else { cm.execCommand('newlineAndIndent') } From d6171dc5020984cc752bfb7fa8c6bfeaf16ef7b5 Mon Sep 17 00:00:00 2001 From: kostaldavid8 Date: Thu, 16 Feb 2017 13:18:17 +0100 Subject: [PATCH 3/6] Smart numbered lists, too Has auto increment, but no auto indent on tab, I don't know what to do --- browser/components/CodeEditor.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 03270de2..ad650ccf 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -86,10 +86,14 @@ export default class CodeEditor extends React.Component { const cursor = cm.getCursor() const line = cm.getLine(cursor.line) const dash = line.trim().startsWith('- ') - if (line.trim().startsWith('- ') || line.trim().startsWith('* ')) { + const numberedListRegex = /(\d+)\. .+/ + const match = line.trim().match(numberedListRegex) + if (line.trim().startsWith('- ') || line.trim().startsWith('* ') || match) { cm.execCommand('newlineAndIndent') const range = {line: cursor.line + 1, ch: cm.getLine(cursor.line + 1).length} - if (dash) { + if (match) { + cm.replaceRange((parseInt(match[1]) + 1) + '. ', range) + } else if (dash) { cm.replaceRange('- ', range) } else { cm.replaceRange('* ', range) From c65db4e2b0b3028e921f6ded3b16450911c97b57 Mon Sep 17 00:00:00 2001 From: kostaldavid8 Date: Fri, 17 Feb 2017 10:03:53 +0100 Subject: [PATCH 4/6] Fixed image drag and drop Added escaping and changed function that wasn't working --- browser/components/CodeEditor.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index ad650ccf..19a9b3a7 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -201,13 +201,13 @@ export default class CodeEditor extends React.Component { e.preventDefault() let imagePath = e.dataTransfer.files[0].path let filename = path.basename(imagePath) - let imageMd = `![${filename}](${imagePath})` + let imageMd = `![${encodeURI(filename)}](${encodeURI(imagePath)})` this.insertImage(imageMd) } insertImage (imageMd) { - const textarea = this.editor.getInputField() - textarea.value = textarea.value.substr(0, textarea.selectionStart) + imageMd + textarea.value.substr(textarea.selectionEnd) + const cm = this.editor + cm.setValue(cm.getValue() + imageMd) } render () { From c56d232e5857733eef3f6f430a9683cbca7d58c0 Mon Sep 17 00:00:00 2001 From: kostaldavid8 Date: Mon, 20 Mar 2017 19:22:31 +0100 Subject: [PATCH 5/6] Added `+` as bullet, bug fix --- browser/components/CodeEditor.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 19a9b3a7..68c9d43c 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -62,7 +62,7 @@ export default class CodeEditor extends React.Component { if (cm.somethingSelected()) cm.indentSelection('add') else { const tabs = cm.getOption('indentWithTabs') - if (line.trimLeft() === '- ' || line.trimLeft() === '* ') { + if (line.trimLeft() === '- ' || line.trimLeft() === '* ' || line.trimLeft() === '+ ') { cm.execCommand('goLineStart') if (tabs) { cm.execCommand('insertTab') @@ -85,18 +85,30 @@ export default class CodeEditor extends React.Component { Enter: (cm) => { const cursor = cm.getCursor() const line = cm.getLine(cursor.line) - const dash = line.trim().startsWith('- ') - const numberedListRegex = /(\d+)\. .+/ + let bulletType; + if (line.trim().startsWith('- ')) { + bulletType = 1 // dash + } else if (line.trim().startsWith('* ')) { + bulletType = 2 // star + } else if (line.trim().startsWith('+ ')) { + bulletType = 3 // plus + } else { + bulletType = 0 // not a bullet + } + console.log(bulletType); + const numberedListRegex = /^(\d+)\. .+/ const match = line.trim().match(numberedListRegex) - if (line.trim().startsWith('- ') || line.trim().startsWith('* ') || match) { + if (bulletType !== 0 || match) { cm.execCommand('newlineAndIndent') const range = {line: cursor.line + 1, ch: cm.getLine(cursor.line + 1).length} if (match) { cm.replaceRange((parseInt(match[1]) + 1) + '. ', range) - } else if (dash) { + } else if (bulletType === 1) { cm.replaceRange('- ', range) - } else { + } else if (bulletType === 2) { cm.replaceRange('* ', range) + } else if (bulletType === 3) { + cm.replaceRange('+ ', range) } } else { cm.execCommand('newlineAndIndent') From 1e8e161a338d5bbd84fa51a09b42f6538af76c34 Mon Sep 17 00:00:00 2001 From: kostaldavid8 Date: Mon, 20 Mar 2017 19:42:14 +0100 Subject: [PATCH 6/6] remove console.log --- browser/components/CodeEditor.js | 1 - 1 file changed, 1 deletion(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 68c9d43c..27b430ea 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -95,7 +95,6 @@ export default class CodeEditor extends React.Component { } else { bulletType = 0 // not a bullet } - console.log(bulletType); const numberedListRegex = /^(\d+)\. .+/ const match = line.trim().match(numberedListRegex) if (bulletType !== 0 || match) {