]>
git.ipfire.org Git - ipfire.org.git/blob - src/static/js/editor.js
3 this.parent
= $(parent
);
6 this.textarea
= this.parent
.find("textarea");
8 // Make the textarea magic
9 this.activateTextArea();
14 console
.log("Editor initialised for " + this.parent
);
20 // Remember any selected text
21 this.textarea
.on("select keyup click", function(e
) {
22 // Ignore any keyboard shortcuts
28 start
: this.selectionStart
,
29 end
: this.selectionEnd
,
30 text
: this.value
.slice(this.selectionStart
, this.selectionEnd
),
31 length
: this.selectionEnd
- this.selectionStart
,
34 console
.debug("Something got selected:");
35 console
.debug(editor
.selection
);
38 // Bind keyboard shortcuts
39 this.textarea
.on("keyup", function(e
) {
40 // If Ctrl wasn't pressed this isn't for us
70 this.parent
.find("#bold").click(this.bold
.bind(this));
71 this.parent
.find("#italic").click(this.italic
.bind(this));
72 this.parent
.find("#code").click(this.code
.bind(this));
75 this.parent
.find("#headline").click(this.headline
.bind(this));
76 this.parent
.find("#headline-down").click(this.headline_down
.bind(this));
77 this.parent
.find("#headline-up").click(this.headline_up
.bind(this));
80 // Functions to modify the text
82 replaceSelection(replacement
) {
83 // Get the DOM element
84 var textarea
= this.textarea
.get(0);
86 // Write text to textarea and move the cursor to the end
87 textarea
.setRangeText(replacement
,
88 this.selection
.start
, this.selection
.end
, "end");
91 insertAtCursor(insertion
) {
92 this.replaceSelection(insertion
);
96 console
.debug("Converting into bold: " + this.selection
.text
);
97 this.replaceSelection("**" + this.selection
.text
+ "**");
101 console
.debug("Converting into italic: " + this.selection
.text
);
102 this.replaceSelection("*" + this.selection
.text
+ "*");
106 var multiline
= this.selection
.text
.indexOf("\n");
108 if (multiline
>= 0) {
109 this.replaceSelection("```\n" + this.selection
.text
+ "\n```\n\n");
111 this.replaceSelection("`" + this.selection
.text
+ "`");
118 // Get all text between start and cursor position
119 var text
= this.textarea
.val().slice(0, this.selection
.start
);
121 // Split it in lines and reverse
122 var lines
= text
.split("\n");
125 for (var line
of lines
) {
128 // Return the number of # found in the nearest headline
129 var match
= line
.match("^(#+)");
131 return match
[1].length
;
135 // If nothing was found, we are on level one
139 insertHeadline(offset
) {
140 // Find level of headlines
141 var level
= Math
.max(this.findLevel() + offset
, 1);
143 console
.debug("Adding headline (" + level
+ ")");
144 var headline
= "#".repeat(level
);
146 if (this.selection
.length
== 0) {
149 // Add some space if we don't have any, yet
150 if (!this.selection
.text
.startsWith(" "))
153 headline
+= this.selection
.text
+ "\n\n";
156 this.replaceSelection(headline
);
160 return this.insertHeadline(0);
164 return this.insertHeadline(1);
168 return this.insertHeadline(-1);
172 $(document
).ready(function() {
173 // Initialise all editors
174 $(".editor").each(function(i
, e
) {