Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'Ctrl-Enter': (cm) => {
// Implement middle-of-line insert line below behaviour (see #101)
CodeMirror.commands['goLineEnd'](cm)
CodeMirror.commands['newlineAndIndent'](cm)
},
'Shift-Ctrl-Enter': (cm) => {
}
const code = doc.getRange(from, to);
const res = tidy[cm.getOption('source')](code, {
indent_size: cm.getOption('indentUnit'),
indent_with_tabs: cm.getOption('indentWithTabs'),
...(cm.getOption('autoFormatOptions') || {}),
});
const cursor = cm.getCursor();
doc.replaceRange(res, from, to);
cm.setCursor(cursor); // adjust for offset?
});
CodeMirror.commands.autoFormat = cm => cm.autoFormat(cm);
"code_block": CodeBlockSpan,
"strong": StrongSpan,
"emphasis": EmphasisSpan,
"code": CodeSpan,
"image": ImageSpan,
"link": LinkSpan,
"elision": ElisionSpan,
}
//---------------------------------------------------------
// Editor
//---------------------------------------------------------
// Register static commands
let _rawUndo = CodeMirror.commands["undo"];
CodeMirror.commands["undo"] = function(editor:RawEditor) {
if(!editor.markdownEditor) _rawUndo.apply(this, arguments);
else editor.markdownEditor.undo();
}
let _rawRedo = CodeMirror.commands["redo"];
CodeMirror.commands["redo"] = function(editor:RawEditor) {
if(!editor.markdownEditor) _rawRedo.apply(this, arguments);
else editor.markdownEditor.redo();
}
interface RawEditor extends CodeMirror.Editor {
markdownEditor?:MarkdownEditor
}
interface HistoryItem { finalized?: boolean, changes:Change[] }
class MarkdownEditor {
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
import CodeMirror from 'codemirror';
const DEFAULT_OPTIONS = { indent: true };
let nonWS = /[^\s\u00a0]/;
let cmPos = CodeMirror.Pos;
function firstNonWS (str) {
let found = str.search(nonWS);
return found === -1 ? 0 : found;
}
CodeMirror.commands.toggleComment = function (cm) {
cm.toggleComment();
};
CodeMirror.defineExtension('toggleComment', function (options = DEFAULT_OPTIONS) {
let cm = this;
let minLine = Infinity;
let ranges = cm.listSelections();
let mode = null;
for (let i = ranges.length - 1; i >= 0; i--) {
let from = ranges[i].from();
let to = ranges[i].to();
if (from.line >= minLine) {
continue;
}
import './editor/sublime';
import './editor/scheme';
import './editor/emacs';
import {send, socket} from './connect';
import {cm_help} from './config';
import {cm_open, cm_save} from './file';
import {state, log} from './utils';
import {cm_view} from './editor/editor';
import {pipe} from './pipe';
CodeMirror.commands.view = cm_view;
CodeMirror.commands.help = cm_help;
CodeMirror.commands.save = cm_save;
CodeMirror.commands.open = cm_open;
CodeMirror.commands.interrupt = cm => {
if (state.error) state.error.clear();
send('kill', 'SIGINT');
};
log('connecting to server...\n');
socket.onmessage = event => pipe(JSON.parse(event.data));
socket.onerror = event => console.error(event);
socket.onopen = event => log('connected.\n');
socket.onclose = event => log('\nlost connection to server. reload the page.\n');
onKeyUp(cm: CodeMirror.EditorFromTextArea, event: KeyboardEvent) {
if ((!cm.state.completionActive && (event.keyCode > 64 && event.keyCode < 91)) || event.keyCode === 219) {
(CodeMirror.commands as any).autocomplete(cm, null, { completeSingle: false });
}
}
};
CodeMirror.commands.favaFormat = (cm: Editor) => {
putAPI("format_source", { source: cm.getValue() }).then(
data => {
const scrollPosition = cm.getScrollInfo().top;
cm.setValue(data);
cm.scrollTo(null, scrollPosition);
},
error => {
notify(error, "error");
}
);
};
CodeMirror.commands.favaToggleComment = (cm: Editor) => {
const doc = cm.getDoc();
const args = {
from: doc.getCursor("start"),
to: doc.getCursor("end"),
options: { lineComment: ";" },
};
if (!cm.uncomment(args.from, args.to, args.options)) {
cm.lineComment(args.from, args.to, args.options);
}
};
CodeMirror.commands.favaCenterCursor = (cm: Editor) => {
const { top } = cm.cursorCoords(true, "local");
const height = cm.getScrollInfo().clientHeight;
cm.scrollTo(null, top - height / 2);
};
findPrevDiff(dv.chunks, line, isOrig) :
findNextDiff(dv.chunks, line, isOrig);
if (pos !== null && (found === null ||
(dir === DiffDirection.Previous ? pos > found : pos < found))) {
found = pos;
}
}
}
if (found !== null) {
cm.getDoc().setCursor(found, 0);
} else {
return CodeMirror.Pass;
}
}
CodeMirror.commands.goNextDiff = function(cm: CodeMirror.Editor) {
return goNearbyDiff(cm, DiffDirection.Next);
};
CodeMirror.commands.goPrevDiff = function(cm: CodeMirror.Editor) {
return goNearbyDiff(cm, DiffDirection.Previous);
};
export function addCommand(
name: string,
command: (cm: CodeMirror.Editor) => void
) {
CodeMirror.commands[name] = command;
}
}
exec(text) {
this.isEvaluating = true;
var rv = this.evaluate(text);
if (!rv) return;
this.isEvaluating = false;
var doc = this.input.getDoc();
if (rv.error && !rv.recoverable) {
this.appendEntry(text, rv.completionValue);
this.addHistory(text);
this.resetInput();
} else if (rv.recoverable) {
CodeMirror.commands.newlineAndIndent(this.input);
} else {
this.appendEntry(text, rv.completionValue);
this.addHistory(text);
this.resetInput();
}
doc.eachLine((line) => {
if (line.lineNo() === 0) this.input.setMarker(line, prompt);
else this.input.setMarker(line, promptContinuation);
});
}