Skip to content

Commit 81dc74a

Browse files
committed
File adding support, editor enhancements, JS improved.
1 parent 50b377d commit 81dc74a

File tree

4 files changed

+83
-41
lines changed

4 files changed

+83
-41
lines changed

app/Repositories/Daemon/FileRepository.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function returnFileContents($file)
136136
*
137137
* @param string $file
138138
* @param string $content
139-
* @return boolean
139+
* @return bool
140140
*/
141141
public function saveFileContents($file, $content)
142142
{
@@ -149,23 +149,6 @@ public function saveFileContents($file, $content)
149149

150150
$file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/';
151151

152-
$res = $this->client->request('GET', '/server/files/stat/' . rawurlencode($file->dirname.$file->basename) , [
153-
'headers' => $this->headers
154-
]);
155-
156-
$stat = json_decode($res->getBody());
157-
if($res->getStatusCode() !== 200 || !isset($stat->size)) {
158-
throw new DisplayException('The daemon provided a non-200 error code on stat lookup: HTTP\\' . $res->getStatusCode());
159-
}
160-
161-
if (!in_array($stat->mime, HelperRepository::editableFiles())) {
162-
throw new DisplayException('You cannot edit that type of file (' . $stat->mime . ') through the panel.');
163-
}
164-
165-
if ($stat->size > 5000000) {
166-
throw new DisplayException('That file is too large to save in the browser, consider using a SFTP client.');
167-
}
168-
169152
$res = $this->client->request('POST', '/server/file/' . rawurlencode($file->dirname.$file->basename), [
170153
'headers' => $this->headers,
171154
'json' => [

resources/views/server/files/add.blade.php

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,44 @@
4040
<form method="post" id="new_file">
4141
<h4>/home/container/{{ $directory }} <input type="text" id="file_name" class="filename" value="newfile.txt" /></h4>
4242
<div class="form-group">
43-
<div>
44-
<textarea name="file_contents" id="fileContents" style="border: 1px solid #dddddd;" class="form-control console"></textarea>
45-
</div>
43+
<div id="fileContents" style="height:500px;"></div>
4644
</div>
4745
<div class="form-group">
48-
<div>
49-
<button class="btn btn-primary btn-sm" id="create_file">{{ trans('strings.save') }}</button>
50-
<button class="btn btn-default btn-sm" onclick="window.location='/server/{{ $server->uuidShort }}/files?dir=/{{ $directory }}';return false;">{{ trans('server.files.back') }}</button>
46+
<div class="row">
47+
<div class="col-md-8">
48+
<button class="btn btn-primary btn-sm" id="create_file">{{ trans('strings.save') }}</button>
49+
<button class="btn btn-default btn-sm" onclick="window.location='/server/{{ $server->uuidShort }}/files?dir=/{{ $directory }}';return false;">{{ trans('server.files.back') }}</button>
50+
</div>
51+
<div class="col-md-4 pull-right">
52+
<select name="aceMode" id="aceMode" class="form-control">
53+
<option value="assembly_x86">Assembly x86</option>
54+
<option value="c_cpp">C/C++</option>
55+
<option value="coffee">CoffeeScript</option>
56+
<option value="csharp">C#</option>
57+
<option value="css">CSS</option>
58+
<option value="golang">Go</option>
59+
<option value="haml">HAML</option>
60+
<option value="html">HTML</option>
61+
<option value="ini">INI</option>
62+
<option value="java">Java</option>
63+
<option value="javascript">JavaScript</option>
64+
<option value="json">JSON</option>
65+
<option value="lua">Lua</option>
66+
<option value="markdown">Markdown</option>
67+
<option value="mysql">MySQL</option>
68+
<option value="objectivec">Objective-C</option>
69+
<option value="perl">Perl</option>
70+
<option value="php">PHP</option>
71+
<option value="properties">Properties</option>
72+
<option value="python">Python</option>
73+
<option value="ruby">Ruby</option>
74+
<option value="rust">Rust</option>
75+
<option value="smarty">Smarty</option>
76+
<option value="textile" selected="selected">Plain Text</option>
77+
<option value="xml">XML</option>
78+
<option value="yaml">YAML</option>
79+
</select>
80+
</div>
5181
</div>
5282
</div>
5383
</form>
@@ -66,6 +96,8 @@
6696
@endcan
6797
</div>
6898
</div>
99+
{!! Theme::js('js/vendor/ace/ace.js') !!}
100+
{!! Theme::js('js/vendor/ace/ext-modelist.js') !!}
69101
<script>
70102
$(window).load(function () {
71103
@@ -170,35 +202,54 @@ function doNothing (e){
170202
}
171203
@endcan
172204
173-
$('textarea').keydown(function (e) {
174-
if (e.keyCode === 9) {
175-
var start = this.selectionStart;
176-
var end = this.selectionEnd;
177-
var value = $(this).val();
178-
$(this).val(value.substring(0, start) + '\t' + value.substring(end));
179-
this.selectionStart = this.selectionEnd = start + 1;
180-
e.preventDefault();
181-
}
205+
const Editor = ace.edit('fileContents');
206+
207+
Editor.setTheme('ace/theme/chrome');
208+
Editor.getSession().setUseWrapMode(true);
209+
Editor.setShowPrintMargin(false);
210+
211+
$('#aceMode').on('change', event => {
212+
Editor.getSession().setMode(`ace/mode/${$('#aceMode').val()}`);
213+
});
214+
215+
Editor.commands.addCommand({
216+
name: 'save',
217+
bindKey: {win: 'Ctrl-S', mac: 'Command-S'},
218+
exec: function(editor) {
219+
save();
220+
},
221+
readOnly: false
182222
});
183223
184-
$("#create_file").click(function(e) {
224+
$('#create_file').on('click', function (e) {
185225
e.preventDefault();
186-
$("#create_file").append(' <i class="fa fa-spinner fa fa-spin"></i>').addClass("disabled");
226+
save();
227+
});
228+
229+
function save() {
230+
var fileName = $('input[name="file"]').val();
231+
$('#create_file').append(' <i class="fa fa-spinner fa fa-spin"></i>').addClass('disabled');
187232
$.ajax({
188233
type: 'POST',
189234
url: '{{ route('server.files.save', $server->uuidShort) }}',
190235
headers: { 'X-CSRF-Token': '{{ csrf_token() }}' },
191236
data: {
192237
file: '{{ $directory }}' + $('#file_name').val(),
193-
contents: $('#fileContents').val()
238+
contents: Editor.getValue()
194239
}
195240
}).done(function (data) {
196241
window.location.replace('/server/{{ $server->uuidShort }}/files/edit/{{ $directory }}' + $('#file_name').val());
197242
}).fail(function (jqXHR) {
198-
$('#write_status').html('<div class="alert alert-danger">' + jqXHR.responseText + '</div>').show();
199-
$('#create_file').html('{{ trans('strings.save') }}').removeClass('disabled');
243+
$.notify({
244+
message: jqXHR.responseText
245+
}, {
246+
type: 'danger'
247+
});
248+
}).always(function () {
249+
$('#save_file').html('{{ trans('strings.save') }}').removeClass('disabled');
200250
});
201-
});
251+
}
252+
202253
203254
});
204255
</script>

resources/views/server/files/edit.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
const Editor = ace.edit('editor');
5151
const Modelist = ace.require('ace/ext/modelist')
5252
53-
Editor.setTheme('ace/theme/github');
53+
Editor.setTheme('ace/theme/chrome');
5454
Editor.getSession().setMode(Modelist.getModeForPath('{{ $stat->name }}').mode);
5555
Editor.getSession().setUseWrapMode(true);
5656
Editor.setShowPrintMargin(false);

resources/views/server/js/filemanager/actions.blade.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ class ActionsClass {
9999
inputField.focus();
100100
inputField.on('blur keypress', e => {
101101
// Save Field
102-
if (e.type === 'blur' || (e.type === 'keypress' && e.which === 27) || currentName === inputField.val()) {
102+
if (
103+
(e.type === 'keypress' && e.which === 27)
104+
|| e.type === 'blur'
105+
|| (e.type === 'keypress' && e.which === 13 && currentName === inputField.val())
106+
) {
103107
if (!_.isEmpty(currentLink)) {
104108
nameBlock.html(currentLink);
105109
} else {
@@ -110,6 +114,10 @@ class ActionsClass {
110114
return;
111115
}
112116

117+
if (e.type === 'keypress' && e.which !== 13) return;
118+
119+
console.log('did not');
120+
113121
inputLoader.show();
114122
const currentPath = decodeURIComponent(nameBlock.data('path'));
115123

0 commit comments

Comments
 (0)