From: Michael Tremer Date: Tue, 27 Aug 2019 13:10:00 +0000 (+0100) Subject: wiki: Add editor elements to insert headlines X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=18c363578bdf14415519d68a16707ac08e4781dd;p=ipfire.org.git wiki: Add editor elements to insert headlines Signed-off-by: Michael Tremer --- diff --git a/src/static/js/editor.js b/src/static/js/editor.js index 854d01e2..7639ae2e 100644 --- a/src/static/js/editor.js +++ b/src/static/js/editor.js @@ -18,11 +18,12 @@ class Editor { var editor = this; // Remember any selected text - this.textarea.select(function() { + this.textarea.on("select keyup click", function(e) { editor.selection = { start : this.selectionStart, end : this.selectionEnd, - text : this.value.slice(this.selectionStart, this.selectionEnd) + text : this.value.slice(this.selectionStart, this.selectionEnd), + length: this.selectionEnd - this.selectionStart, }; console.debug("Something got selected:"); @@ -31,8 +32,14 @@ class Editor { } bindKeys() { + // Typography this.parent.find("#bold").click(this.bold.bind(this)); this.parent.find("#italic").click(this.italic.bind(this)); + + // Headlines + this.parent.find("#headline").click(this.headline.bind(this)); + this.parent.find("#headline-down").click(this.headline_down.bind(this)); + this.parent.find("#headline-up").click(this.headline_up.bind(this)); } // Functions to modify the text @@ -46,6 +53,10 @@ class Editor { this.textarea.val(text); } + insertAtCursor(insertion) { + this.replaceSelection(insertion); + } + bold() { console.debug("Converting into bold: " + this.selection.text); this.replaceSelection("**" + this.selection.text + "**"); @@ -55,6 +66,62 @@ class Editor { console.debug("Converting into italic: " + this.selection.text); this.replaceSelection("*" + this.selection.text + "*"); } + + // Headlines + + findLevel() { + // Get all text between start and cursor position + var text = this.textarea.val().slice(0, this.selection.start); + + // Split it in lines and reverse + var lines = text.split("\n"); + lines.reverse(); + + for (var line of lines) { + console.debug(line); + + // Return the number of # found in the nearest headline + var match = line.match("^(#+)"); + if (match) { + return match[1].length; + } + } + + // If nothing was found, we are on level one + return 1; + } + + insertHeadline(offset) { + // Find level of headlines + var level = Math.max(this.findLevel() + offset, 1); + + console.debug("Adding headline (" + level + ")"); + var headline = "#".repeat(level); + + if (this.selection.length == 0) { + headline += " ..."; + } else { + // Add some space if we don't have any, yet + if (!this.selection.text.startsWith(" ")) + headline += " "; + + headline += this.selection.text + "\n\n"; + } + + this.replaceSelection(headline); + } + + headline() { + return this.insertHeadline(0); + } + + headline_down() { + return this.insertHeadline(1); + } + + headline_up() { + return this.insertHeadline(-1); + } } $(document).ready(function() { diff --git a/src/templates/wiki/edit.html b/src/templates/wiki/edit.html index 841bb9c5..3e8cf270 100644 --- a/src/templates/wiki/edit.html +++ b/src/templates/wiki/edit.html @@ -31,6 +31,18 @@ +
+ + + +
+