Skip to content

Commit

Permalink
Embed the comment editor (#1204)
Browse files Browse the repository at this point in the history
* Few minor HTML fixes
* Rewrote client side script to not be in jQuery. Polyfill for older browsers.. applies to #904
* Reference FAQ and GFM

Closes #1197

Auto-merge
  • Loading branch information
Martii committed Oct 27, 2017
1 parent c91be85 commit 7433c5e
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 95 deletions.
10 changes: 6 additions & 4 deletions views/includes/commentEditor.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
</div>
<div class="topic-post-contents row">
<div class="user-data">
<textarea name="comment-content" data-provide="markdown" data-iconlibrary="fa" class="col-xs-12" placeholder="Type here using Markdown." required></textarea>
<textarea name="comment-content" data-provide="markdown" data-iconlibrary="fa" class="col-xs-12" placeholder="Type here using Markdown." required="required"></textarea>
</div>
<section class="post-menu-area">
<nav class="post-controls text-right">
<button class="btn btn-sm btn-success" title="submit your comment" type="submit">
<div class="submit-panel btn-toolbar">
<a href="/about/Frequently-Asked-Questions"><i class="fa fa-book"></i></a>
<a href="https://guides.github.com/features/mastering-markdown/" title="GitHub Flavor Markdown compatible"><i class="octicon octicon-markdown"></i></a>
<button class="btn btn-sm btn-success pull-right" title="submit your comment" type="submit">
<i class="fa fa-fw fa-reply"></i> Submit
</button>
</nav>
</div>
</section>
</div>
</div>
Expand Down
42 changes: 34 additions & 8 deletions views/includes/commentForm.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
<div id="reply-control" class="collapse">
<form action="{{{discussion.discussionPageUrl}}}" method="post">
<div class="container-fluid row user-content">
<div class="submit-panel pull-right btn-toolbar">
<button class="btn btn-sm btn-danger" type="button" data-toggle="collapse" data-target="#reply-control"><i class="fa fa-close"></i> Cancel</button>
<button class="btn btn-sm btn-success" type="submit"><i class="fa fa-reply"></i> Reply</button>
<div class="topic-post list-group-item">
<article>
<div class="row">
<div class="topic-avatar col-sm-2">
<a href="{{{authedUser.userPageUrl}}}">
<i class="fa fa-user"></i>
</a>
</div>
<div class="topic-body col-sm-10 container-fluid">
<div class="topic-meta-data row">
<div class="names pull-left">
<span>
<a href="{{{authedUser.userPageUrl}}}" class="username">{{authedUser.name}}</a> <span class="label label-default">{{authedUser.roleName}}</span>
</span>
</div>
<div class="post-info pull-right">
<a class="post-date">
<time datetime="" title=""></time>
</a>
</div>
</div>
<div class="topic-post-contents row">
<form action="{{{discussion.discussionPageUrl}}}" method="post">
<div class="container-fluid row user-content">
<textarea name="comment-content" data-provide="markdown" data-iconlibrary="fa" class="col-xs-12" placeholder="Type here using Markdown." required="required"></textarea>
<div class="submit-panel btn-toolbar">
<a href="/about/Frequently-Asked-Questions"><i class="fa fa-book"></i></a>
<a href="https://guides.github.com/features/mastering-markdown/"><i class="octicon octicon-markdown" title="GitHub Flavor Markdown compatible"></i></a>
<button class="btn-sm btn btn-success pull-right" type="submit"><i class="fa fa-reply"></i> Reply</button>
</div>
</div>
</form>
</div>
</div>
<textarea name="comment-content" data-provide="markdown" data-iconlibrary="fa" class="col-xs-12" placeholder="Type here using Markdown."></textarea>
</div>
</form>
</article>
</div>
79 changes: 23 additions & 56 deletions views/includes/scripts/commentReplyScript.html
Original file line number Diff line number Diff line change
@@ -1,80 +1,47 @@
<script type="text/javascript">
(function () {
'use strict';

var events = 'DOMContentLoaded load resize scroll';
var handler = null;
var didCallback = false;
{{> includes/scripts/polyfillElementClosest.js }}

$('#reply-control').on('show.bs.collapse', function () {
// Show spacer div
$('#show-reply-form-when-visible').css({
height:
{{#paginationRendered}}'210px'{{/paginationRendered}}
{{^paginationRendered}}'268px'{{/paginationRendered}}
});
});

$('#reply-control').on('hide.bs.collapse', function () {
// Hide spacer div
$('#show-reply-form-when-visible').css({
height: '0'
});

// Clear text value from reply box
$('#reply-control textarea[name="comment-content"]').val('');
});

$('.btn-comment-reply').click(function (aE) {
var $comment = $(aE.target).closest('.topic-post');
var $author = $comment.find('.topic-meta-data .names .username').first();
var $replyTextarea = $('#reply-control textarea[name="comment-content"]');
function onClick(aEv) {
var commentNode = document.querySelector('textarea[name="comment-content"]');
var containerNode = aEv.target.closest('.topic-post');
var authorNode = containerNode.querySelector('.topic-meta-data .names .username');
var value = null;
var reUrl = null;
var url = null;

$('#reply-control').collapse('show');
document.location.hash = containerNode.id;

document.location.hash = $comment.attr('id');
url = document.location.pathname + document.location.search + document.location.hash;
url = url.replace(/\(/g, '%28').replace(/\)/g, '%29');

reUrl = document.location.pathname +
document.location.search +
document.location.hash;
reUrl = reUrl.replace(/\(/g, '%28').replace(/\)/g, '%29');
value = commentNode.value;

value = $replyTextarea.val();
if (!/^\s*$/.test(value)) {
// Add linebreaks if reply already started.
value += '\n\n';
}
value += '[Re](' + reUrl + '): ';
value += '@' + $author.text() + ': ';
value += ' \n';
$replyTextarea.val(value);
$replyTextarea.focus();
});

function callback(aEl) {
if (!didCallback) {
didCallback = true;
value += '[Re](' + url + '): ';
value += '@' + authorNode.textContent + ': ';
value += ' \n';

$('#reply-control').collapse('show');
$('#reply-control textarea[name="comment-content"]').focus();
}
commentNode.value = value;
commentNode.focus();
}

{{> includes/scripts/isElementInViewport.js }}

function fireIfElementVisible(aEl, aCallback) {
return function () {
if (isElementInViewport(aEl)) {
$(window).off(events, handler);
function onDOMContentLoaded(aEv) {
var i = null;
var thisNode = null;
var replyNodes = document.querySelectorAll('.btn-comment-reply');

aCallback(aEl);
}
for (i = 0; thisNode = replyNodes[i++];) {
thisNode.addEventListener('click', onClick);
}
}

handler = fireIfElementVisible($('#show-reply-form-when-visible'), callback);
$(window).on(events, handler);
document.addEventListener('DOMContentLoaded', onDOMContentLoaded);

})();
</script>
18 changes: 18 additions & 0 deletions views/includes/scripts/polyfillElementClosest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// IE8+, Opera Presto, and older browser Element.Polyfill

if (window.Element && !Element.prototype.closest) {
Element.prototype.closest =
function(aSelector) {
var matches = (this.document || this.ownerDocument).querySelectorAll(aSelector);
var i;
var el = this;

do {
i = matches.length;
while (--i >= 0 && matches.item(i) !== el) {
};
} while ((i < 0) && (el = el.parentElement));

return el;
};
}
17 changes: 6 additions & 11 deletions views/pages/discussionPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
<title>{{title}}</title>
{{> includes/head.html }}
<link rel="stylesheet" type="text/css" media="all" href="/redist/npm/bootstrap-markdown/css/bootstrap-markdown.min.css">
<style>
#show-reply-form-when-visible {
-moz-transition: height 1s ease;
-ms-transition: height 1s ease;
-o-transition: height 1s ease;
-webkit-transition: height 1s ease;
transition: height 1s ease;
}
</style>
</head>
<body>
{{> includes/header.html }}
Expand Down Expand Up @@ -45,6 +36,11 @@
</div>
{{/paginationRendered}}
</section>
{{#authedUser}}
<section class="topic-area list-group">
{{> includes/commentForm.html }}
</section>
{{/authedUser}}
</div>
</div>
</div>
Expand All @@ -54,8 +50,7 @@
</div>
</div>
</div>
<div id="show-reply-form-when-visible"></div>
{{> includes/commentForm.html }}

{{> includes/footer.html }}
{{#paginationRendered}}
{{> includes/scripts/showTopPagination.html }}
Expand Down
2 changes: 1 addition & 1 deletion views/pages/newDiscussionPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ol class="breadcrumb col-xs-12">
<li><a href="/forum">Forum</a></li>
<li><a href="{{{category.categoryPageUrl}}}">{{category.name}}</a></li>
<li><input type="text" id="discussion-topic" name="discussion-topic" size="60" placeholder="Topic" required></li>
<li><input type="text" id="discussion-topic" name="discussion-topic" size="60" placeholder="Topic" required="required"></li>
</ol>
</div>
<div class="container-fluid comments">
Expand Down
20 changes: 7 additions & 13 deletions views/pages/scriptIssuePage.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
{{> includes/head.html }}
<link rel="stylesheet" type="text/css" media="all" href="/redist/npm/bootstrap-markdown/css/bootstrap-markdown.min.css">
<link rel="stylesheet" type="text/css" media="all" href="/css/scriptPage.css">
<style>
#show-reply-form-when-visible {
-moz-transition: height 1s ease;
-ms-transition: height 1s ease;
-o-transition: height 1s ease;
-webkit-transition: height 1s ease;
transition: height 1s ease;
}
</style>
</head>
<body>
{{> includes/header.html }}
Expand Down Expand Up @@ -53,6 +44,11 @@
</div>
{{/paginationRendered}}
</section>
{{#authedUser}}
<section class="topic-area list-group">
{{> includes/commentForm.html }}
</section>
{{/authedUser}}
</div>
</div>
</div>
Expand All @@ -63,16 +59,14 @@
</div>
</div>

<div id="show-reply-form-when-visible"></div>
{{> includes/commentForm.html }}
{{> includes/footer.html }}
{{#paginationRendered}}
{{> includes/scripts/showTopPagination.html }}
{{/paginationRendered}}
{{> includes/scripts/lazyIconScript.html }}
{{#authedUser}}
{{> includes/scripts/markdownEditor.html }}
{{> includes/scripts/commentReplyScript.html }}
{{> includes/scripts/markdownEditor.html }}
{{> includes/scripts/commentReplyScript.html }}
{{/authedUser}}
{{^authedUser}}
{{> includes/scripts/commentReplyTooltip.html }}
Expand Down
2 changes: 1 addition & 1 deletion views/pages/scriptNewIssuePage.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<li><a href="{{{script.scriptPageUrl}}}">{{script.name}}</a></li>
<li><a href="{{{category.categoryPageUrl}}}">{{category.name}}</a></li>
<li class="col-xs-12">
<input type="text" id="discussion-topic" name="discussion-topic" size="60" placeholder="Topic" required>
<input type="text" id="discussion-topic" name="discussion-topic" size="60" placeholder="Topic" required="required">
</li>
</ol>
</div>
Expand Down
2 changes: 1 addition & 1 deletion views/pages/scriptViewSourcePage.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<div class="pull-left">
{{#isLib}}
{{#newScript}}
<strong>Library Name:</strong> <input type="text" name="script_name" value="" required>
<strong>Library Name:</strong> <input type="text" name="script_name" value="" required="required">
{{/newScript}}
{{^newScript}}
<input type="hidden" name="script_name" value="{{scriptName}}" />
Expand Down

0 comments on commit 7433c5e

Please sign in to comment.