Skip to content

Commit 7e1e38e

Browse files
prandlaveluca93
authored andcommitted
AWS: new code popup
* Use prism.js for syntax highlighting. * Use html <dialog> for slightly nicer UI.
1 parent 8b62501 commit 7e1e38e

File tree

10 files changed

+86
-332
lines changed

10 files changed

+86
-332
lines changed

cms/server/admin/static/aws_style.css

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,32 +262,20 @@ ul.normal_list {
262262
}
263263

264264

265-
#subpage {
265+
#subpage_popup {
266266
position: fixed;
267-
left: 345px;
268-
top: 0px;
269-
bottom: 0px;
270-
display: none;
271-
margin-bottom: 1em;
272-
padding: 1.5em 1em 0 1em;
273-
z-index: 10;
274-
}
275-
276-
#subpage_background {
277-
position: fixed;
278-
left: 345px;
279-
top:0px;
280-
bottom: 0px;
281-
right: 0px;
282-
background: white;
283-
z-index: -1;
284-
}
285-
286-
#subpage_content {
287-
background:white;
288-
overflow: auto;
289-
z-index: 2;
290-
height: 100%;
267+
top: 0;
268+
bottom: 0;
269+
box-sizing: border-box;
270+
max-width: min(100% - 2em, 1000px);
271+
max-height: calc(100% - 2em);
272+
margin-right: 1em;
273+
overflow-y: scroll;
274+
}
275+
#subpage_close {
276+
position: absolute;
277+
right: 1em;
278+
top: 1em;
291279
}
292280

293281
#subpage h1 {

cms/server/admin/static/aws_utils.js

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,27 @@ CMS.AWSUtils.create_url_builder = function(url_root) {
6969
* content.
7070
*/
7171
CMS.AWSUtils.prototype.display_subpage = function(elements) {
72+
var content = $("#subpage_content");
73+
content.empty();
7274
// TODO: update jQuery to allow appending of arrays of elements.
7375
for (var i = 0; i < elements.length; ++i) {
74-
elements[i].appendTo($("#subpage_content"));
76+
elements[i].appendTo(content);
7577
}
76-
$("#subpage").show();
78+
document.getElementById('subpage_popup').show();
7779
};
7880

81+
// set up some event listeners for the subpage
82+
window.addEventListener('DOMContentLoaded', () => {
83+
$('#subpage_close').on('click', () => {
84+
document.getElementById('subpage_popup').close();
85+
});
86+
});
7987

80-
/**
81-
* Hides a subpage previously displayed.
82-
*/
83-
CMS.AWSUtils.prototype.hide_subpage = function() {
84-
$("#subpage").hide();
85-
$("#subpage_content").empty();
86-
};
88+
document.addEventListener('keydown', function(event) {
89+
if (event.key === "Escape") {
90+
document.getElementById('subpage_popup').close();
91+
}
92+
});
8793

8894

8995
/**
@@ -108,20 +114,42 @@ CMS.AWSUtils.prototype.file_received = function(response, error) {
108114
this.display_subpage(elements);
109115
return;
110116
}
111-
var pre_class = "";
112-
// TODO: add more languages.
113-
if (file_name.match(/.c(|pp)$/i)) {
114-
pre_class = "brush: cpp";
115-
} else if (file_name.match(/.pas$/i)) {
116-
pre_class = "brush: delphi";
117+
// TODO: update if adding a new language to cms
118+
// (need to also update the prism bundle then)
119+
var extension_to_lang = {
120+
'cs': 'csharp',
121+
'cpp': 'cpp',
122+
'c': 'c',
123+
'h': 'c',
124+
'go': 'go',
125+
'hs': 'haskell',
126+
'java': 'java',
127+
'js': 'javascript',
128+
'php': 'php',
129+
'py': 'python',
130+
'rs': 'rust',
117131
}
132+
var file_ext = file_name.split('.').pop();
133+
var lang_name = extension_to_lang[file_ext] || file_ext;
134+
118135
elements.push($('<h1>').text(file_name));
119136
elements.push($('<a>').text("Download").prop("href", url));
120-
elements.push($('<pre>').text(response).prop("id", "source_container")
121-
.prop("class", pre_class));
122-
137+
elements.push($('<span>').text(" - "))
138+
elements.push($('<a>').text("copy").prop('href', '#').on('click', (event) => {
139+
var range = document.createRange();
140+
var code_area = $('#subpage_content code')[0];
141+
range.setStartBefore(code_area);
142+
range.setEndAfter(code_area);
143+
window.getSelection().removeAllRanges();
144+
window.getSelection().addRange(range);
145+
// execCommand is deprecated, but this seems much less annoying than the new clipboard api
146+
document.execCommand('copy');
147+
event.preventDefault(); // prevent the <a> from navigating
148+
}));
149+
var codearea = $('<code>').text(response).addClass('line-numbers').addClass('language-' + lang_name);
150+
elements.push($('<pre>').append(codearea));
123151
this.display_subpage(elements);
124-
SyntaxHighlighter.highlight();
152+
Prism.highlightAllUnder(document.getElementById('subpage_content'));
125153
}
126154
};
127155

cms/server/admin/static/prism.css

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)