]>
git.ipfire.org Git - ipfire.org.git/blob - src/static/js/editor.js
11be2666f6f33dbf6f85e123c6afcb24a96e9ce2
3 this.parent
= $(parent
);
6 this.textarea
= this.parent
.find("textarea");
8 // Initialise selection
16 // Make the textarea magic
17 this.activateTextArea();
22 console
.log("Editor initialised for " + this.parent
);
28 // Remember any selected text
29 this.textarea
.on("select keyup click", function(e
) {
30 // Ignore any keyboard shortcuts
36 start
: this.selectionStart
,
37 end
: this.selectionEnd
,
38 text
: this.value
.slice(this.selectionStart
, this.selectionEnd
),
39 length
: this.selectionEnd
- this.selectionStart
,
42 console
.debug("Something got selected:");
43 console
.debug(editor
.selection
);
46 // Bind keyboard shortcuts
47 this.textarea
.on("keyup", function(e
) {
48 // If Ctrl wasn't pressed this isn't for us
83 this.parent
.find("#bold").click(this.bold
.bind(this));
84 this.parent
.find("#italic").click(this.italic
.bind(this));
85 this.parent
.find("#code").click(this.code
.bind(this));
88 this.parent
.find("#headline").click(this.headline
.bind(this));
89 this.parent
.find("#headline-down").click(this.headline_down
.bind(this));
90 this.parent
.find("#headline-up").click(this.headline_up
.bind(this));
93 this.parent
.find("#link").click(this.link
.bind(this));
96 // Functions to modify the text
98 replaceSelection(replacement
) {
99 // Get the DOM element
100 var textarea
= this.textarea
.get(0);
102 // Write text to textarea and move the cursor to the end
103 textarea
.setRangeText(replacement
,
104 this.selection
.start
, this.selection
.end
, "end");
107 insertAtCursor(insertion
) {
108 this.replaceSelection(insertion
);
112 console
.debug("Converting into bold: " + this.selection
.text
);
113 this.replaceSelection("**" + this.selection
.text
+ "**");
117 console
.debug("Converting into italic: " + this.selection
.text
);
118 this.replaceSelection("*" + this.selection
.text
+ "*");
122 var multiline
= this.selection
.text
.indexOf("\n");
124 if (multiline
>= 0) {
125 this.replaceSelection("```\n" + this.selection
.text
+ "\n```\n\n");
127 this.replaceSelection("`" + this.selection
.text
+ "`");
133 if (this.selection
.text
.startsWith("https://") || this.selection
.text
.startsWith("http://")) {
134 this.replaceSelection("[" + this.selection
.text
+ "](" + this.selection
.text
+ ")");
135 // Handle selected text
137 this.replaceSelection("[" + this.selection
.text
+ "]()")
144 // Get all text between start and cursor position
145 var text
= this.textarea
.val().slice(0, this.selection
.start
);
147 // Split it in lines and reverse
148 var lines
= text
.split("\n");
151 for (var line
of lines
) {
154 // Return the number of # found in the nearest headline
155 var match
= line
.match("^(#+)");
157 return match
[1].length
;
161 // If nothing was found, we are on level one
165 insertHeadline(offset
) {
166 // Find level of headlines
167 var level
= Math
.max(this.findLevel() + offset
, 1);
169 console
.debug("Adding headline (" + level
+ ")");
170 var headline
= "#".repeat(level
);
172 if (this.selection
.length
== 0) {
175 // Add some space if we don't have any, yet
176 if (!this.selection
.text
.startsWith(" "))
179 headline
+= this.selection
.text
+ "\n\n";
182 this.replaceSelection(headline
);
186 return this.insertHeadline(0);
190 return this.insertHeadline(1);
194 return this.insertHeadline(-1);
198 $(document
).ready(function() {
199 // Initialise all editors
200 $(".editor").each(function(i
, e
) {