From ce594b0b5a4e92e50ecc75e7a7384b991f721b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Vi=E1=BB=87t=20H=C6=B0ng?= Date: Thu, 17 May 2018 00:14:09 +0700 Subject: [PATCH] added note template feature --- browser/components/CodeEditor.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 301915f5..9859716d 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -197,13 +197,33 @@ export default class CodeEditor extends React.Component { expandSnippet (line, cursor, cm, snippets) { const wordBeforeCursor = this.getWordBeforeCursor(line, cursor.line, cursor.ch) + const templateCursorString = ':{}' for (let i = 0; i < snippets.length; i++) { if (snippets[i].prefix.indexOf(wordBeforeCursor.text) !== -1) { - cm.replaceRange( - snippets[i].content, - wordBeforeCursor.range.from, - wordBeforeCursor.range.to - ) + if (snippets[i].content.indexOf(templateCursorString) !== -1) { + let snippetLines = snippets[i].content.split('\n') + let cursorLineNumber = 0 + let cursorLinePosition = 0 + for (let j = 0; j < snippetLines.length; j++) { + let cursorIndex = snippetLines[j].indexOf(templateCursorString) + if (cursorIndex !== -1) { + cursorLineNumber = j + cursorLinePosition = cursorIndex + cm.replaceRange( + snippets[i].content.replace(templateCursorString, ''), + wordBeforeCursor.range.from, + wordBeforeCursor.range.to + ) + cm.setCursor({ line: cursor.line + cursorLineNumber, ch: cursorLinePosition }) + } + } + } else { + cm.replaceRange( + snippets[i].content, + wordBeforeCursor.range.from, + wordBeforeCursor.range.to + ) + } return true } }