nextcloud/apps/files_texteditor/js/editor.js

282 lines
9.0 KiB
JavaScript
Raw Normal View History

2011-10-02 01:48:00 +04:00
function setEditorSize(){
// Sets the size of the text editor window.
fillWindow($('#editor'));
2011-10-02 01:48:00 +04:00
}
function getFileExtension(file){
var parts=file.split('.');
return parts[parts.length-1];
}
function setSyntaxMode(ext){
// Loads the syntax mode files and tells the editor
var filetype = new Array();
// Todo finish these
filetype["h"] = "c_cpp";
filetype["c"] = "c_cpp";
filetype["clj"] = "clojure";
filetype["coffee"] = "coffee"; // coffescript can be compiled to javascript
2012-01-02 00:04:46 +04:00
filetype["coldfusion"] = "cfc";
filetype["cpp"] = "c_cpp";
filetype["cs"] = "csharp";
2011-10-02 01:48:00 +04:00
filetype["css"] = "css";
filetype["groovy"] = "groovy";
2012-01-02 00:04:46 +04:00
filetype["haxe"] = "hx";
filetype["html"] = "html";
filetype["java"] = "java";
filetype["js"] = "javascript";
filetype["json"] = "json";
2012-01-02 00:04:46 +04:00
filetype["latex"] = "latex";
filetype["lua"] = "lua";
filetype["markdown"] = "markdown"; // also: .md .markdown .mdown .mdwn
filetype["ml"] = "ocaml";
filetype["mli"] = "ocaml";
2011-10-02 01:48:00 +04:00
filetype["pl"] = "perl";
filetype["php"] = "php";
2012-01-02 00:04:46 +04:00
filetype["powershell"] = "ps1";
2011-10-02 01:48:00 +04:00
filetype["py"] = "python";
filetype["rb"] = "ruby";
filetype["scad"] = "scad"; // seems to be something like 3d model files printed with e.g. reprap
filetype["scala"] = "scala";
filetype["scss"] = "scss"; // "sassy css"
2012-01-02 00:04:46 +04:00
filetype["sql"] = "sql";
filetype["svg"] = "svg";
filetype["textile"] = "textile"; // related to markdown
2011-10-02 01:48:00 +04:00
filetype["xml"] = "xml";
if(filetype[ext]!=null){
// Then it must be in the array, so load the custom syntax mode
// Set the syntax mode
OC.addScript('files_texteditor','aceeditor/mode-'+filetype[ext], function(){
var SyntaxMode = require("ace/mode/"+filetype[ext]).Mode;
window.aceEditor.getSession().setMode(new SyntaxMode());
});
}
}
2012-01-09 21:22:51 +04:00
function showControls(filename,writeperms){
2011-10-02 01:48:00 +04:00
// Loads the control bar at the top.
2011-10-04 01:59:40 +04:00
$('.actions,#file_action_panel').fadeOut('slow').promise().done(function() {
2011-10-02 01:48:00 +04:00
// Load the new toolbar.
2012-01-14 19:36:57 +04:00
var editorcontrols;
2012-01-09 21:22:51 +04:00
if(writeperms=="true"){
2012-01-10 02:42:28 +04:00
var editorcontrols = '<button id="editor_save">'+t('files_texteditor','Save')+'</button><div class="separator"></div><label for="gotolineval">Go to line:</label><input type="text" id="gotolineval"><div class="separator"></div>';
2012-01-09 21:22:51 +04:00
}
2012-01-10 02:22:12 +04:00
var html = '<label for="editorseachval">Search:</label><input type="text" name="editorsearchval" id="editorsearchval"><div class="separator"></div><button id="editor_close">'+t('files_texteditor','Close')+'</button>';
$('#controls').append(html);
$('#editorbar').fadeIn('slow');
var breadcrumbhtml = '<div class="crumb svg" id="breadcrumb_file" style="background-image:url(&quot;../core/img/breadcrumb.png&quot;)"><p>'+filename+'</p></div>';
2012-01-10 01:16:41 +04:00
$('.actions').before(breadcrumbhtml).before(editorcontrols);
2011-10-02 01:48:00 +04:00
});
}
2011-10-04 01:59:40 +04:00
2011-10-02 01:48:00 +04:00
function bindControlEvents(){
$("#editor_save").die('click',doFileSave).live('click',doFileSave);
$('#editor_close').die('click',hideFileEditor).live('click',hideFileEditor);
2012-01-10 02:42:28 +04:00
$('#gotolineval').die('keyup', goToLine).live('keyup', goToLine);
2012-01-10 02:22:12 +04:00
$('#editorsearchval').die('keyup', doSearch).live('keyup', doSearch);
$('#clearsearchbtn').die('click', resetSearch).live('click', resetSearch);
$('#nextsearchbtn').die('click', nextSearchResult).live('click', nextSearchResult);
}
2012-01-10 01:16:41 +04:00
// returns true or false if the editor is in view or not
function editorIsShown(){
// Not working as intended. Always returns true.
return is_editor_shown;
2011-10-04 01:59:40 +04:00
}
2012-01-10 01:16:41 +04:00
// Moves the editor view to the line number speificed in #gotolineval
function goToLine(){
// Go to the line specified
window.aceEditor.gotoLine($('#gotolineval').val());
}
2012-01-10 02:22:12 +04:00
//resets the search
function resetSearch(){
$('#editorsearchval').val('');
$('#nextsearchbtn').remove();
$('#clearsearchbtn').remove();
window.aceEditor.gotoLine(0);
}
// moves the cursor to the next search resukt
function nextSearchResult(){
window.aceEditor.findNext();
}
// Performs the initial search
function doSearch(){
// check if search box empty?
if($('#editorsearchval').val()==''){
// Hide clear button
window.aceEditor.gotoLine(0);
$('#nextsearchbtn').remove();
$('#clearsearchbtn').remove();
} else {
// New search
// Reset cursor
window.aceEditor.gotoLine(0);
// Do search
window.aceEditor.find($('#editorsearchval').val(),{
backwards: false,
wrap: false,
caseSensitive: false,
wholeWord: false,
regExp: false
});
// Show next and clear buttons
// check if already there
if($('#nextsearchbtn').length==0){
var nextbtnhtml = '<button id="nextsearchbtn">Next</button>';
var clearbtnhtml = '<button id="clearsearchbtn">Clear</button>';
$('#editorsearchval').after(nextbtnhtml).after(clearbtnhtml);
}
}
}
2012-01-10 01:16:41 +04:00
// Tries to save the file.
2011-10-04 01:59:40 +04:00
function doFileSave(){
if(editorIsShown()){
// Get file path
var path = $('#editor').attr('data-dir')+'/'+$('#editor').attr('data-filename');
// Get original mtime
var mtime = $('#editor').attr('data-mtime');
// Show saving spinner
$("#editor_save").die('click',doFileSave);
2012-01-09 22:17:26 +04:00
$('#save_result').remove();
$('#editor_save').text(t('files_texteditor','Saving...'));//after('<img id="saving_icon" src="'+OC.filePath('core','img','loading.gif')+'"></img>');
// Get the data
var filecontents = window.aceEditor.getSession().getValue();
// Send the data
$.post(OC.filePath('files_texteditor','ajax','savefile.php'), { filecontents: filecontents, path: path, mtime: mtime },function(jsondata){
if(jsondata.status!='success'){
// Save failed
2012-01-09 22:17:26 +04:00
$('#editor_save').text(t('files_texteditor','Save'));
$('#editor_save').after('<p id="save_result" style="float: left">Failed to save file</p>');
2012-01-09 22:17:26 +04:00
$("#editor_save").live('click',doFileSave);
} else {
// Save OK
2012-01-09 21:03:19 +04:00
// Update mtime
$('#editor').attr('data-mtime',jsondata.data.mtime);
2012-01-09 22:17:26 +04:00
$('#editor_save').text(t('files_texteditor','Save'));
$("#editor_save").live('click',doFileSave);
}
},'json');
}
2011-10-04 01:59:40 +04:00
};
2012-01-10 01:16:41 +04:00
// Gives the editor focus
function giveEditorFocus(){
window.aceEditor.focus();
};
2012-01-10 01:16:41 +04:00
// Loads the file editor. Accepts two parameters, dir and filename.
2011-10-02 01:48:00 +04:00
function showFileEditor(dir,filename){
if(!editorIsShown()){
// Loads the file editor and display it.
var data = $.getJSON(
OC.filePath('files_texteditor','ajax','loadfile.php'),
{file:filename,dir:dir},
function(result){
if(result.status == 'success'){
// Save mtime
$('#editor').attr('data-mtime', result.data.mtime);
// Initialise the editor
showControls(filename,result.data.write);
$('table').fadeOut('slow', function() {
// Update document title
document.title = filename;
$('#editor').text(result.data.filecontents);
$('#editor').attr('data-dir', dir);
$('#editor').attr('data-filename', filename);
window.aceEditor = ace.edit("editor");
aceEditor.setShowPrintMargin(false);
if(result.data.write=='false'){
aceEditor.setReadOnly(true);
}
setEditorSize();
setSyntaxMode(getFileExtension(filename));
OC.addScript('files_texteditor','aceeditor/theme-clouds', function(){
window.aceEditor.setTheme("ace/theme/clouds");
});
});
} else {
// Failed to get the file.
alert(result.data.message);
}
// End success
}
// End ajax
);
is_editor_shown = true;
}
2011-10-02 01:48:00 +04:00
}
2012-01-10 01:16:41 +04:00
// Fades out the editor.
2011-10-02 01:48:00 +04:00
function hideFileEditor(){
2012-01-10 02:22:12 +04:00
// Fades out editor controls
$('#controls > :not(.actions,#file_access_panel,.crumb),#breadcrumb_file').fadeOut('slow',function(){
2012-01-10 02:22:12 +04:00
$(this).remove();
});
// Fade out editor
$('#editor').fadeOut('slow', function(){
2012-01-10 02:22:12 +04:00
$(this).remove();
2012-01-10 02:38:19 +04:00
// Reset document title
document.title = "ownCloud";
var editorhtml = '<div id="editor"></div>';
$('table').after(editorhtml);
$('.actions,#file_access_panel').fadeIn('slow');
$('table').fadeIn('slow');
});
is_editor_shown = false;
2011-10-02 01:48:00 +04:00
}
// Keyboard Shortcuts
var ctrlBtn = false;
2012-01-10 01:16:41 +04:00
2012-01-24 00:59:37 +04:00
// TODO fix detection of ctrl keyup
2012-01-10 01:16:41 +04:00
// returns true if ctrl+s or cmd+s is being pressed
function checkForSaveKeyPress(e){
2011-12-31 16:28:25 +04:00
if(e.which == 17 || e.which == 91) ctrlBtn=true;
if(e.which == 83 && ctrlBtn == true) {
e.preventDefault();
$('#editor_save').trigger('click');
return false;
}
}
2012-01-10 01:16:41 +04:00
// resizes the editor window
2011-10-02 01:48:00 +04:00
$(window).resize(function() {
setEditorSize();
});
var is_editor_shown = false;
$(document).ready(function(){
if(typeof FileActions!=='undefined'){
FileActions.register('text','Edit','',function(filename){
showFileEditor($('#dir').val(),filename);
});
FileActions.setDefault('text','Edit');
FileActions.register('application/xml','Edit','',function(filename){
showFileEditor($('#dir').val(),filename);
});
FileActions.setDefault('application/xml','Edit');
}
2011-10-05 04:44:24 +04:00
OC.search.customResults.Text=function(row,item){
var text=item.link.substr(item.link.indexOf('file=')+5);
var a=row.find('a');
a.data('file',text);
a.attr('href','#');
a.click(function(){
var file=text.split('/').pop();
var dir=text.substr(0,text.length-file.length-1);
showFileEditor(dir,file);
});
}
2012-01-10 01:16:41 +04:00
// Binds the file save and close editor events, and gotoline button
bindControlEvents();
// Binds the save keyboard shortcut events
2012-01-24 00:59:37 +04:00
//$(document).unbind('keydown').bind('keydown',checkForSaveKeyPress);
});