Skip to content

Instantly share code, notes, and snippets.

@j4rv
Last active September 18, 2019 10:38
Show Gist options
  • Save j4rv/f6cf90a595e41ab720d3893f30ee60ef to your computer and use it in GitHub Desktop.
Save j4rv/f6cf90a595e41ab720d3893f30ee60ef to your computer and use it in GitHub Desktop.
Comment Drafts (ScriptRunner for JIRA)
Create a Script Fragment that calls the above JS code with the context "jira.view.issue".
(function ($) {
if(localStorageSupported()){
var contentKey;
var expirationMomentKey;
var secondsBetweenSaves = 10;
$(function () {
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (event, $context, reason) {
if (reason === JIRA.CONTENT_ADDED_REASON.pageLoad || reason === JIRA.CONTENT_ADDED_REASON.panelRefreshed) {
restoreCommentDraft();
}
});
// On every change, check if the saved draft is old. If it is, save the new comment draft.
$("textarea#comment").bind('input propertychange', saveIfDraftIsOld);
// After we click the Send button, we don't want this draft anymore.
$("input#issue-comment-add-submit").on("click", deleteDraft);
// Save the draft when we close the window
window.addEventListener("beforeunload", saveCommentDraft);
restoreCommentDraft();
});
function updateKeys(){
issueKey = JIRA.Issue.getIssueKey();
contentKey = "comment.draft.content." + issueKey;
expirationMomentKey = "comment.draft.expires-on." + issueKey;
}
function restoreCommentDraft() {
updateKeys();
var restoredComment = localStorage.getItem(contentKey);
if(restoredComment){
$("textarea#comment").val(restoredComment);
}
}
function saveCommentDraft() {
var commentContent = $("textarea#comment").val();
if(commentContent){
localStorage.setItem(contentKey, commentContent);
var now = new Date();
var expirationMoment = now.getTime() + secondsBetweenSaves * 1000;
localStorage.setItem(expirationMomentKey, expirationMoment);
}
}
function deleteDraft() {
localStorage.removeItem(contentKey);
localStorage.removeItem(expirationMomentKey);
}
function saveIfDraftIsOld() {
var expirationMoment = localStorage.getItem(expirationMomentKey);
var currentMoment = new Date();
if (currentMoment >= expirationMoment){
saveCommentDraft();
}
}
}
function localStorageSupported() {
return typeof(Storage) !== "undefined";
}
})(AJS.$);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment