2019-08-05 15:54:05 +02:00
{{define "pad"}}<!DOCTYPE HTML>
<html>
<head>
<title>{{if .Editing}}Editing {{if .Post.Title}}{{.Post.Title}}{{else}}{{.Post.Id}}{{end}}{{else}}New Post{{end}} — {{.SiteName}}</title>
<link rel="stylesheet" type="text/css" href="/css/write.css" />
2022-02-01 04:24:56 +01:00
{{if .CustomCSS}}<link rel="stylesheet" type="text/css" href="/local/custom.css" />{{end}}
2019-08-05 15:54:05 +02:00
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="google" value="notranslate">
</head>
<body id="pad" class="light">
<div id="overlay"></div>
2023-11-16 19:46:38 +01:00
<textarea dir="auto" id="writer" placeholder="Write..." class="{{.Post.Font}}" autofocus>{{if .Post.Title}} # {{.Post.Title}}
2019-08-05 15:54:05 +02:00
{{end}}{{.Post.Content}}</textarea>
2020-06-11 17:45:12 +02:00
<div class="alert success hidden" id="edited-elsewhere">This post has been updated elsewhere since you last published! <a href=" #" id="erase-edit">Delete draft and reload</a>.</div>
2019-08-05 15:54:05 +02:00
<header id="tools">
<div id="clip">
2019-08-15 05:14:34 +02:00
{{if not .SingleUser}}<h1>{{if .Chorus}}<a href="/" title="Home">{{else}}<a href="/me/c/" title="View blogs">{{end}}{{.SiteName}}</a></h1>{{end}}
2019-08-05 15:54:05 +02:00
<nav id="target" {{if .SingleUser}}style="margin-left:0"{{end}}><ul>
<li>{{if .Blogs}}<a href="{{ $c := index .Blogs 0}}{{ $ c . CanonicalURL }}">My Posts</a>{{else}}<a>Draft</a>{{end}}</li>
</ul></nav>
<span id="wc" class="hidden if-room room-4">0 words</span>
</div>
2023-03-05 08:12:36 +01:00
<noscript style="margin-left: 2em;"><strong>NOTE</strong>: for now, you'll need JavaScript enabled to post.</noscript>
2019-08-05 15:54:05 +02:00
<div id="belt">
{{if .Editing}}<div class="tool hidden if-room"><a href="{{if .EditCollection}}{{.EditCollection.CanonicalURL}}{{.Post.Slug}}/edit/meta{{else}}/{{if .SingleUser}}d/{{end}}{{.Post.Id}}/meta{{end}}" title="Edit post metadata" id="edit-meta"><img class="ic-24dp" src="/img/ic_info_dark@2x.png" /></a></div>{{end}}
<div class="tool"><button title="Publish your writing" id="publish" style="font-weight: bold">Post</button></div>
</div>
</header>
<script src="/js/h.js"></script>
<script>
var $ writer = H.getEl('writer');
var $ btnPublish = H.getEl('publish');
2020-06-11 17:45:12 +02:00
var $ btnEraseEdit = H.getEl('edited-elsewhere');
2019-08-05 15:54:05 +02:00
var $ wc = H.getEl("wc");
var updateWordCount = function() {
var words = 0;
var val = $ writer . el . value . trim ();
if (val != '') {
words = $ writer . el . value . trim ().replace(/\s+/gi, ' ').split(' ').length;
}
$ wc . el . innerText = words + " word" + (words != 1 ? "s" : "");
};
var setButtonStates = function() {
if (!canPublish) {
$ btnPublish . el . className = 'disabled';
return;
}
if ( $ writer . el . value . length === 0 || (draftDoc != 'lastDoc' && $ writer . el . value == origDoc)) {
$ btnPublish . el . className = 'disabled';
} else {
$ btnPublish . el . className = '';
}
};
{{if .Post.Id}}var draftDoc = 'draft{{.Post.Id}}';
var origDoc = '{{.Post.Content}}';{{else}}var draftDoc = 'lastDoc';{{end}}
2020-06-11 17:45:12 +02:00
var updatedStr = '{{.Post.Updated8601}}';
var updated = null;
if (updatedStr != '') {
updated = new Date(updatedStr);
}
var ok = H.load( $ writer , draftDoc, true, updated);
if (!ok) {
// Show "edited elsewhere" warning
$ btnEraseEdit . el . classList . remove ('hidden');
}
var defaultTimeSet = false;
2019-08-05 15:54:05 +02:00
updateWordCount();
var typingTimer;
var doneTypingInterval = 200;
var posts;
{{if and .Post.Id (not .Post.Slug)}}
var token = null;
var curPostIdx;
posts = JSON.parse(H.get('posts', '[]'));
for (var i=0; i<posts.length; i++) {
if (posts[i].id == "{{.Post.Id}}") {
token = posts[i].token;
break;
}
}
var canPublish = token != null;
{{else}}var canPublish = true;{{end}}
var publishing = false;
var justPublished = false;
var publish = function(content, font) {
{{if and (and .Post.Id (not .Post.Slug)) (not .User)}}
if (!token) {
alert("You don't have permission to update this post.");
return;
}
{{end}}
publishing = true;
$ btnPublish . el . textContent = 'Posting...';
$ btnPublish . el . disabled = true;
var http = new XMLHttpRequest();
var post = H.getTitleStrict(content);
var params = {
body: post.content,
title: post.title,
2021-04-06 18:38:45 +02:00
font: font
2019-08-05 15:54:05 +02:00
};
{{ if .Post.Slug }}
var url = "/api/collections/{{.EditCollection.Alias}}/posts/{{.Post.Id}}";
{{ else if .Post.Id }}
var url = "/api/posts/{{.Post.Id}}";
if (typeof token === 'undefined' || !token) {
token = "";
}
params.token = token;
{{ else }}
2021-04-06 18:38:45 +02:00
var lang = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage);
lang = lang.substring(0, 2);
params.lang = lang;
2019-08-05 15:54:05 +02:00
var url = "/api/posts";
var postTarget = '{{if .Blogs}}{{ $c := index .Blogs 0}}{{ $ c . Alias }}{{else}}anonymous{{end}}';
if (postTarget != 'anonymous') {
url = "/api/collections/" + postTarget + "/posts";
}
{{ end }}
http.open("POST", url, true);
// Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/json");
http.onreadystatechange = function() {
if (http.readyState == 4) {
publishing = false;
if (http.status == 200 || http.status == 201) {
data = JSON.parse(http.responseText);
id = data.data.id;
nextURL = '{{if .SingleUser}}/d{{end}}/'+id;
2020-06-11 17:45:12 +02:00
localStorage.setItem('draft'+id+'-published', new Date().toISOString());
2019-08-05 15:54:05 +02:00
{{ if not .Post.Id }}
// Post created
if (postTarget != 'anonymous') {
nextURL = {{if not .SingleUser}}'/'+postTarget+{{end}}'/'+data.data.slug;
}
editToken = data.data.token;
{{ if not .User }}if (postTarget == 'anonymous') {
// Save the data
var posts = JSON.parse(H.get('posts', '[]'));
{{if .Post.Id}}var newPost = H.createPost("{{.Post.Id}}", token, content);
for (var i=0; i<posts.length; i++) {
if (posts[i].id == "{{.Post.Id}}") {
posts[i].title = newPost.title;
posts[i].summary = newPost.summary;
break;
}
}
nextURL = "/pad/posts";{{else}}posts.push(H.createPost(id, editToken, content));{{end}}
H.set('posts', JSON.stringify(posts));
}
{{ end }}
{{ end }}
justPublished = true;
if (draftDoc != 'lastDoc') {
H.remove(draftDoc);
{{if .Editing}}H.remove('draft{{.Post.Id}}font');{{end}}
} else {
H.set(draftDoc, '');
}
{{if .EditCollection}}
window.location = '{{.EditCollection.CanonicalURL}}{{.Post.Slug}}';
{{else}}
window.location = nextURL;
{{end}}
} else {
$ btnPublish . el . textContent = 'Post';
alert("Failed to post. Please try again.");
}
}
}
http.send(JSON.stringify(params));
};
setButtonStates();
$ writer . on ('keyup input', function() {
setButtonStates();
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
}, false);
$ writer . on ('keydown', function(e) {
clearTimeout(typingTimer);
if (e.keyCode == 13 && (e.metaKey || e.ctrlKey)) {
$ btnPublish . el . click ();
}
});
$ btnPublish . on ('click', function(e) {
e.preventDefault();
if (!publishing && $ writer . el . value ) {
var content = $ writer . el . value ;
publish(content, selectedFont);
}
});
2020-06-11 17:45:12 +02:00
H.getEl('erase-edit').on('click', function(e) {
e.preventDefault();
H.remove(draftDoc);
H.remove(draftDoc+'-published');
justPublished = true; // Block auto-save
location.reload();
});
2019-08-05 15:54:05 +02:00
WebFontConfig = {
custom: { families: [ 'Lora:400,700:latin' ], urls: [ '/css/fonts.css' ] }
};
var selectedFont = H.get('{{if .Editing}}draft{{.Post.Id}}font{{else}}padFont{{end}}', '{{.Post.Font}}');
var doneTyping = function() {
if (draftDoc == 'lastDoc' || $ writer . el . value != origDoc) {
H.save( $ writer , draftDoc);
2020-06-11 17:45:12 +02:00
if (!defaultTimeSet) {
var lastLocalPublishStr = localStorage.getItem(draftDoc+'-published');
if (lastLocalPublishStr == null || lastLocalPublishStr == '') {
localStorage.setItem(draftDoc+'-published', updatedStr);
}
defaultTimeSet = true;
}
2019-08-05 15:54:05 +02:00
updateWordCount();
}
};
window.addEventListener('beforeunload', function(e) {
if (draftDoc != 'lastDoc' && $ writer . el . value == origDoc) {
H.remove(draftDoc);
2020-06-11 17:45:12 +02:00
H.remove(draftDoc+'-published');
2019-08-05 15:54:05 +02:00
} else if (!justPublished) {
doneTyping();
}
});
try {
(function() {
var wf=document.createElement('script');
wf.src = '/js/webfont.js';
wf.type='text/javascript';
wf.async='true';
var s=document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
} catch (e) {
// whatevs
}
</script>
</body>
</html>{{end}}