Skip to content

Commit 692903f

Browse files
committed
Support import from gist, fix some minor typo and issues
1 parent 49b51e4 commit 692903f

3 files changed

Lines changed: 84 additions & 6 deletions

File tree

public/js/index.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ var ui = {
504504
import: {
505505
dropbox: $(".ui-import-dropbox"),
506506
googleDrive: $(".ui-import-google-drive"),
507+
gist: $(".ui-import-gist"),
507508
clipboard: $(".ui-import-clipboard")
508509
},
509510
beta: {
@@ -1181,6 +1182,10 @@ function buildImportFromGoogleDrive() {
11811182
}
11821183
});
11831184
}
1185+
//import from gist
1186+
ui.toolbar.import.gist.click(function () {
1187+
//na
1188+
});
11841189
//import from clipboard
11851190
ui.toolbar.import.clipboard.click(function () {
11861191
//na
@@ -1283,6 +1288,7 @@ function applyScrollspyActive(top, headerMap, headers, target, offset) {
12831288
active.closest('li').addClass('active').parent().closest('li').addClass('active').parent().closest('li').addClass('active');
12841289
}
12851290

1291+
// clipboard modal
12861292
//fix for wrong autofocus
12871293
$('#clipboardModal').on('shown.bs.modal', function () {
12881294
$('#clipboardModal').blur();
@@ -1298,10 +1304,55 @@ $("#clipboardModalConfirm").click(function () {
12981304
$("#clipboardModalContent").html('');
12991305
}
13001306
});
1307+
1308+
// refresh modal
13011309
$('#refreshModalRefresh').click(function () {
13021310
location.reload(true);
13031311
});
13041312

1313+
// gist import modal
1314+
$("#gistImportModalClear").click(function () {
1315+
$("#gistImportModalContent").val('');
1316+
});
1317+
$("#gistImportModalConfirm").click(function () {
1318+
var gisturl = $("#gistImportModalContent").val();
1319+
if (!gisturl) return;
1320+
$('#gistImportModal').modal('hide');
1321+
$("#gistImportModalContent").val('');
1322+
if (!isValidURL(gisturl)) {
1323+
showMessageModal('<i class="fa fa-github"></i> Import from Gist', 'Not a valid URL :(', '', '', false);
1324+
return;
1325+
} else {
1326+
var hostname = url('hostname', gisturl)
1327+
if (hostname !== 'gist.github.com') {
1328+
showMessageModal('<i class="fa fa-github"></i> Import from Gist', 'Not a valid Gist URL :(', '', '', false);
1329+
} else {
1330+
ui.spinner.show();
1331+
$.get('https://api.github.com/gists/' + url('-1', gisturl))
1332+
.success(function (data) {
1333+
if (data.files) {
1334+
var contents = "";
1335+
Object.keys(data.files).forEach(function (key) {
1336+
contents += key;
1337+
contents += '\n---\n';
1338+
contents += data.files[key].content;
1339+
contents += '\n\n';
1340+
});
1341+
replaceAll(contents);
1342+
} else {
1343+
showMessageModal('<i class="fa fa-github"></i> Import from Gist', 'Unable to fetch gist files :(', '', '', false);
1344+
}
1345+
})
1346+
.error(function (data) {
1347+
showMessageModal('<i class="fa fa-github"></i> Import from Gist', 'Not a valid Gist URL :(', '', JSON.stringify(data), false);
1348+
})
1349+
.complete(function () {
1350+
ui.spinner.hide();
1351+
});
1352+
}
1353+
}
1354+
});
1355+
13051356
function parseToEditor(data) {
13061357
var parsed = toMarkdown(data);
13071358
if (parsed)
@@ -1320,9 +1371,9 @@ function replaceAll(data) {
13201371

13211372
function importFromUrl(url) {
13221373
//console.log(url);
1323-
if (url == null) return;
1374+
if (!url) return;
13241375
if (!isValidURL(url)) {
1325-
showMessageModal('<i class="fa fa-cloud-download"></i> Import from URL', 'Not valid URL :(', '', '', false);
1376+
showMessageModal('<i class="fa fa-cloud-download"></i> Import from URL', 'Not a valid URL :(', '', '', false);
13261377
return;
13271378
}
13281379
$.ajax({
@@ -1336,7 +1387,7 @@ function importFromUrl(url) {
13361387
replaceAll(data);
13371388
},
13381389
error: function (data) {
1339-
showMessageModal('<i class="fa fa-cloud-download"></i> Import from URL', 'Import failed :(', '', data, false);
1390+
showMessageModal('<i class="fa fa-cloud-download"></i> Import from URL', 'Import failed :(', '', JSON.stringify(data), false);
13401391
},
13411392
complete: function () {
13421393
ui.spinner.hide();

public/views/body.ejs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<div id="toc-affix" class="ui-affix-toc ui-toc-dropdown unselectable hidden-print" data-spy="affix" style="top:51px;display:none;"></div>
3737
</div>
3838
</div>
39+
<!-- clipboard modal -->
3940
<div class="modal fade" id="clipboardModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
4041
<div class="modal-dialog modal-lg">
4142
<div class="modal-content">
@@ -55,6 +56,7 @@
5556
</div>
5657
</div>
5758
</div>
59+
<!-- refresh modal -->
5860
<div class="modal fade" id="refreshModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
5961
<div class="modal-dialog modal-sm">
6062
<div class="modal-content">
@@ -102,7 +104,7 @@
102104
</div>
103105
<div class="modal-body" style="color:black;">
104106
<h5>Sorry, you've reached the max length this note can be.</h5>
105-
<strong>Please reduce the content or divided it to more notes, thank you!</strong>
107+
<strong>Please reduce the content or divide it to more notes, thank you!</strong>
106108
</div>
107109
<div class="modal-footer">
108110
<button type="button" class="btn btn-warning" data-dismiss="modal">OK</button>
@@ -121,11 +123,32 @@
121123
</div>
122124
<div class="modal-body" style="color:black;">
123125
<h5></h5>
124-
<a target="_blank"></a>
126+
<a target="_blank" style="word-break: break-all;"></a>
125127
</div>
126128
<div class="modal-footer">
127129
<button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
128130
</div>
129131
</div>
130132
</div>
131-
</div>
133+
</div>
134+
<!-- gist import modal -->
135+
<div class="modal fade" id="gistImportModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
136+
<div class="modal-dialog">
137+
<div class="modal-content">
138+
<div class="modal-header">
139+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
140+
</button>
141+
<h4 class="modal-title" id="myModalLabel">Import from Gist</h4>
142+
</div>
143+
<div class="modal-body">
144+
<input type="url" class="form-control" placeholder="Paste your gist url here... (like: https://gist.github.com/username/gistid)" id="gistImportModalContent">
145+
</div>
146+
<div class="modal-footer">
147+
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
148+
<button type="button" class="btn btn-danger" id="gistImportModalClear">Clear</button>
149+
<button type="button" class="btn btn-primary" id="gistImportModalConfirm">Import</button>
150+
</div>
151+
</div>
152+
</div>
153+
</div>
154+
<%- include modal %>

public/views/header.ejs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
</li>
4949
<li role="presentation"><a role="menuitem" class="ui-import-google-drive" tabindex="-1" href="#" target="_self"><i class="fa fa-cloud-download fa-fw"></i> Google Drive</a>
5050
</li>
51+
<li role="presentation"><a role="menuitem" class="ui-import-gist" href="#" data-toggle="modal" data-target="#gistImportModal"><i class="fa fa-github fa-fw"></i> Gist</a>
52+
</li>
5153
<li role="presentation"><a role="menuitem" class="ui-import-clipboard" href="#" data-toggle="modal" data-target="#clipboardModal"><i class="fa fa-clipboard fa-fw"></i> Clipboard</a>
5254
</li>
5355
<li class="divider"></li>
@@ -131,6 +133,8 @@
131133
</li>
132134
<li role="presentation"><a role="menuitem" class="ui-import-google-drive" tabindex="-1" href="#" target="_self"><i class="fa fa-cloud-download fa-fw"></i> Google Drive</a>
133135
</li>
136+
<li role="presentation"><a role="menuitem" class="ui-import-gist" href="#" data-toggle="modal" data-target="#gistImportModal"><i class="fa fa-github fa-fw"></i> Gist</a>
137+
</li>
134138
<li role="presentation"><a role="menuitem" class="ui-import-clipboard" href="#" data-toggle="modal" data-target="#clipboardModal"><i class="fa fa-clipboard fa-fw"></i> Clipboard</a>
135139
</li>
136140
<li class="divider"></li>

0 commit comments

Comments
 (0)