summaryrefslogtreecommitdiff
path: root/public/js/lib/editor/utils.js
diff options
context:
space:
mode:
authorEdgar Zanella Alvarenga2018-06-19 16:03:32 +0200
committerEdgar Zanella Alvarenga2018-06-19 16:03:56 +0200
commita8b664fdb5b425625928d0ce6ae9cdbd78fb3833 (patch)
tree796556715b880d7d76f27bf3ad38e06fea3fd358 /public/js/lib/editor/utils.js
parent82c7f9d07cfecfc110af8c8fc0e47edf070596b2 (diff)
Add a toolbar to Codemirror editor
Signed-off-by: Edgar Zanella Alvarenga <e@vaz.io>
Diffstat (limited to 'public/js/lib/editor/utils.js')
-rw-r--r--public/js/lib/editor/utils.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/public/js/lib/editor/utils.js b/public/js/lib/editor/utils.js
index 3702a166..f1053c4c 100644
--- a/public/js/lib/editor/utils.js
+++ b/public/js/lib/editor/utils.js
@@ -46,3 +46,43 @@ export function wrapTextWith (editor, cm, symbol) {
}
}
}
+
+export function insertText (cm, text, cursorEnd = 0) {
+ var cursor = cm.getCursor()
+ cm.replaceSelection(text, cursor, cursor)
+ cm.focus()
+ cm.setCursor({line: cursor.line, ch: cursor.ch + cursorEnd})
+}
+
+export function insertHeader (cm) {
+ let cursor = cm.getCursor()
+ let startOfLine = {line: cursor.line, ch: 0}
+ let startOfLineText = cm.getRange(startOfLine, {line: cursor.line, ch: 1})
+ // See if it is already a header
+ if (startOfLineText === '#') {
+ cm.replaceRange('#', startOfLine, startOfLine)
+ } else {
+ cm.replaceRange('# ', startOfLine, startOfLine)
+ }
+ cm.focus()
+}
+
+export function insertOnStartOfLines (cm, symbol, cursorEnd) {
+ let cursor = cm.getCursor()
+ var ranges = cm.listSelections()
+
+ for (let i = 0; i < ranges.length; i++) {
+ var range = ranges[i]
+ if (!range.empty()) {
+ const from = range.from()
+ const to = range.to()
+ for (let j = from.line; j <= to.line; ++j) {
+ cm.replaceRange(symbol, {line: j, ch: 0}, {line: j, ch: 0})
+ }
+ } else {
+ cm.replaceRange(symbol, {line: cursor.line, ch: 0}, {line: cursor.line, ch: 0})
+ }
+ }
+ cm.setCursor({line: cursor.line, ch: (cursorEnd)? cursorEnd : cursor.ch})
+ cm.focus()
+}