Otak-atik codemirror.js plus informasi dari Stackoverflow,ternyata bisa juga diubah.
- Buka codemirror.js dengan editor favorit.
- Search fungsi deleteLine
- Tambahkan beberapa baris berikut
deleteLine: function (cm) { return deleteNearSelection(cm, function (range) { return ({
from: Pos(range.from().line, 0),
to: clipPos(cm.doc, Pos(range.to().line + 1, 0))
}); }); },
//
//Start duplicateLine
duplicateLine : function(cm){
var current_cursor = cm.doc.getCursor();
var line_content = cm.doc.getLine(current_cursor.line);
CodeMirror.commands.goLineEnd(cm);
CodeMirror.commands.newlineAndIndent(cm);
cm.doc.replaceSelection(line_content);
cm.doc.setCursor(current_cursor.line + 1, current_cursor.ch);
},
//End duplicateLine
//
delLineLeft: function (cm) { return deleteNearSelection(cm, function (range) { return ({
from: Pos(range.from().line, 0), to: range.from()
}); }); },
- Search lagi fungsi deleteLine
kemudian tambahkan perintah keyboard berikut. .... tidak usah ditulis maksudnya itu dan seterusnya :D
keyMap.pcDefault = { "Ctrl-A": "selectAll", "Alt-D": "deleteLine", "Ctrl-D": "duplicateLine", "Ctrl-Z": "undo", .....
- Simpan dan cobalah code mirror di browser
- Jika belum bisa, clear cache browser anda.
UPDATE :
Mengambil dari https://codemirror.net/keymap/sublime.js, duplicateLine lebih mantab denga script berikut :
==========================
duplicateLine : function(cm){
var rangeCount = cm.listSelections().length;
for (var i = 0; i < rangeCount; i++) {
var range = cm.listSelections()[i];
if (range.empty())
cm.replaceRange(cm.getLine(range.head.line) + "\n", Pos(range.head.line, 0));
else
cm.replaceRange(cm.getRange(range.from(), range.to()), range.from());
}
cm.scrollIntoView();
},
==========================
Jika script sebelumnya hanya single line, script ini mendukung duplicate multiple line.
0 comments:
Post a Comment