From 692903f1a10606b0e14d50623663a4c591c42674 Mon Sep 17 00:00:00 2001 From: Cheng-Han, Wu Date: Wed, 20 Apr 2016 18:06:36 +0800 Subject: Support import from gist, fix some minor typo and issues --- public/js/index.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++--- public/views/body.ejs | 29 ++++++++++++++++++++++--- public/views/header.ejs | 4 ++++ 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/public/js/index.js b/public/js/index.js index 0e4fd21a..695d9d22 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -504,6 +504,7 @@ var ui = { import: { dropbox: $(".ui-import-dropbox"), googleDrive: $(".ui-import-google-drive"), + gist: $(".ui-import-gist"), clipboard: $(".ui-import-clipboard") }, beta: { @@ -1181,6 +1182,10 @@ function buildImportFromGoogleDrive() { } }); } +//import from gist +ui.toolbar.import.gist.click(function () { + //na +}); //import from clipboard ui.toolbar.import.clipboard.click(function () { //na @@ -1283,6 +1288,7 @@ function applyScrollspyActive(top, headerMap, headers, target, offset) { active.closest('li').addClass('active').parent().closest('li').addClass('active').parent().closest('li').addClass('active'); } +// clipboard modal //fix for wrong autofocus $('#clipboardModal').on('shown.bs.modal', function () { $('#clipboardModal').blur(); @@ -1298,10 +1304,55 @@ $("#clipboardModalConfirm").click(function () { $("#clipboardModalContent").html(''); } }); + +// refresh modal $('#refreshModalRefresh').click(function () { location.reload(true); }); +// gist import modal +$("#gistImportModalClear").click(function () { + $("#gistImportModalContent").val(''); +}); +$("#gistImportModalConfirm").click(function () { + var gisturl = $("#gistImportModalContent").val(); + if (!gisturl) return; + $('#gistImportModal').modal('hide'); + $("#gistImportModalContent").val(''); + if (!isValidURL(gisturl)) { + showMessageModal(' Import from Gist', 'Not a valid URL :(', '', '', false); + return; + } else { + var hostname = url('hostname', gisturl) + if (hostname !== 'gist.github.com') { + showMessageModal(' Import from Gist', 'Not a valid Gist URL :(', '', '', false); + } else { + ui.spinner.show(); + $.get('https://api.github.com/gists/' + url('-1', gisturl)) + .success(function (data) { + if (data.files) { + var contents = ""; + Object.keys(data.files).forEach(function (key) { + contents += key; + contents += '\n---\n'; + contents += data.files[key].content; + contents += '\n\n'; + }); + replaceAll(contents); + } else { + showMessageModal(' Import from Gist', 'Unable to fetch gist files :(', '', '', false); + } + }) + .error(function (data) { + showMessageModal(' Import from Gist', 'Not a valid Gist URL :(', '', JSON.stringify(data), false); + }) + .complete(function () { + ui.spinner.hide(); + }); + } + } +}); + function parseToEditor(data) { var parsed = toMarkdown(data); if (parsed) @@ -1320,9 +1371,9 @@ function replaceAll(data) { function importFromUrl(url) { //console.log(url); - if (url == null) return; + if (!url) return; if (!isValidURL(url)) { - showMessageModal(' Import from URL', 'Not valid URL :(', '', '', false); + showMessageModal(' Import from URL', 'Not a valid URL :(', '', '', false); return; } $.ajax({ @@ -1336,7 +1387,7 @@ function importFromUrl(url) { replaceAll(data); }, error: function (data) { - showMessageModal(' Import from URL', 'Import failed :(', '', data, false); + showMessageModal(' Import from URL', 'Import failed :(', '', JSON.stringify(data), false); }, complete: function () { ui.spinner.hide(); diff --git a/public/views/body.ejs b/public/views/body.ejs index 044f7e6d..771da880 100644 --- a/public/views/body.ejs +++ b/public/views/body.ejs @@ -36,6 +36,7 @@
+ + - \ No newline at end of file + + + +<%- include modal %> \ No newline at end of file diff --git a/public/views/header.ejs b/public/views/header.ejs index bf8f9f66..9e389ae6 100644 --- a/public/views/header.ejs +++ b/public/views/header.ejs @@ -48,6 +48,8 @@