-
- // Helper to replace editor content
- function replaceContent(search, replace) {
- let text = cm.getValue();
- let cursor = cm.listSelections();
- cm.setValue(text.replace(search, replace));
- cm.setSelections(cursor);
- }
-
- // Helper to replace the start of the line
- function replaceLineStart(newStart) {
- let cursor = cm.getCursor();
- let lineContent = cm.getLine(cursor.line);
- let lineLen = lineContent.length;
- let lineStart = lineContent.split(' ')[0];
-
- // Remove symbol if already set
- if (lineStart === newStart) {
- lineContent = lineContent.replace(`${newStart} `, '');
- cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
- cm.setCursor({line: cursor.line, ch: cursor.ch - (newStart.length + 1)});
- return;
- }
-
- let alreadySymbol = /^[#>`]/.test(lineStart);
- let posDif = 0;
- if (alreadySymbol) {
- posDif = newStart.length - lineStart.length;
- lineContent = lineContent.replace(lineStart, newStart).trim();
- } else if (newStart !== '') {
- posDif = newStart.length + 1;
- lineContent = newStart + ' ' + lineContent;
- }
- cm.replaceRange(lineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
- cm.setCursor({line: cursor.line, ch: cursor.ch + posDif});
- }
-
- function wrapLine(start, end) {
- let cursor = cm.getCursor();
- let lineContent = cm.getLine(cursor.line);
- let lineLen = lineContent.length;
- let newLineContent = lineContent;
-
- if (lineContent.indexOf(start) === 0 && lineContent.slice(-end.length) === end) {
- newLineContent = lineContent.slice(start.length, lineContent.length - end.length);
- } else {
- newLineContent = `${start}${lineContent}${end}`;
- }
-
- cm.replaceRange(newLineContent, {line: cursor.line, ch: 0}, {line: cursor.line, ch: lineLen});
- cm.setCursor({line: cursor.line, ch: cursor.ch + start.length});
- }
-
- function wrapSelection(start, end) {
- let selection = cm.getSelection();
- if (selection === '') return wrapLine(start, end);
-
- let newSelection = selection;
- let frontDiff = 0;
- let endDiff = 0;
-
- if (selection.indexOf(start) === 0 && selection.slice(-end.length) === end) {
- newSelection = selection.slice(start.length, selection.length - end.length);
- endDiff = -(end.length + start.length);
- } else {
- newSelection = `${start}${selection}${end}`;
- endDiff = start.length + end.length;
- }
-
- let selections = cm.listSelections()[0];
- cm.replaceSelection(newSelection);
- let headFirst = selections.head.ch <= selections.anchor.ch;
- selections.head.ch += headFirst ? frontDiff : endDiff;
- selections.anchor.ch += headFirst ? endDiff : frontDiff;
- cm.setSelections([selections]);
- }
-
- // Handle image upload and add image into markdown content
- function uploadImage(file) {
- if (file === null || file.type.indexOf('image') !== 0) return;
- let ext = 'png';
-
- if (file.name) {
- let fileNameMatches = file.name.match(/\.(.+)$/);
- if (fileNameMatches.length > 1) ext = fileNameMatches[1];
- }
-
- // Insert image into markdown
- const id = "image-" + Math.random().toString(16).slice(2);
- const placeholderImage = window.baseUrl(`/loading.gif#upload${id}`);
- const selectedText = cm.getSelection();
- const placeHolderText = ``;
- const cursor = cm.getCursor();
- cm.replaceSelection(placeHolderText);
- cm.setCursor({line: cursor.line, ch: cursor.ch + selectedText.length + 3});
-
- const remoteFilename = "image-" + Date.now() + "." + ext;
- const formData = new FormData();
- formData.append('file', file, remoteFilename);
- formData.append('uploaded_to', context.pageId);
-
- window.$http.post('/images/gallery', formData).then(resp => {
- const newContent = `[](${resp.data.url})`;
- replaceContent(placeHolderText, newContent);
- }).catch(err => {
- window.$events.emit('error', trans('errors.image_upload_error'));
- replaceContent(placeHolderText, selectedText);
- console.log(err);
- });
- }
-
- function insertLink() {
- let cursorPos = cm.getCursor('from');
- let selectedText = cm.getSelection() || '';
- let newText = `[${selectedText}]()`;
- cm.focus();
- cm.replaceSelection(newText);
- let cursorPosDiff = (selectedText === '') ? -3 : -1;
- cm.setCursor(cursorPos.line, cursorPos.ch + newText.length+cursorPosDiff);