Browse Source

add publish date field to editor

allows backdating and future-dating posts
pull/82/head
Aaron Parecki 4 years ago
parent
commit
8aa73596e8
No known key found for this signature in database GPG Key ID: 276C2817346D6056
4 changed files with 71 additions and 3 deletions
  1. +33
    -0
      controllers/editor.php
  2. +31
    -3
      public/editor-files/editor.js
  3. +3
    -0
      public/editor-files/style.css
  4. +4
    -0
      views/editor.php

+ 33
- 0
controllers/editor.php View File

@ -35,6 +35,10 @@ $app->post('/editor/publish', function() use($app) {
$micropub_request['post-status'] = $params['status'];
}
if(array_key_exists('publish', $params) && $params['publish'] != 'now') {
$micropub_request['published'] = $params['publish'];
}
$r = micropub_post_for_user($user, $micropub_request);
$app->response()['Content-type'] = 'application/json';
@ -72,6 +76,35 @@ $app->post('/editor/upload', function() use($app) {
}
});
$app->post('/editor/parse-date', function() use($app) {
$date = false;
$params = $app->request()->params();
if(isset($params['date'])) {
if($params['date'] == 'now') {
$date = 'now';
} else {
try {
// Check if the provided date has a timezone offset
$has_timezone = preg_match('/[-+]\d\d:?\d\d$/', $params['date']);
if(!$has_timezone && $params['tzoffset']) {
$s = (-60) * $params['tzoffset'];
$h = $params['tzoffset'] / (-60);
$tz = new DateTimeZone($h);
$d = new DateTime($params['date'], $tz);
} else {
$d = new DateTime($params['date']);
}
$date = $d->format('c');
} catch(Exception $e) {
}
}
}
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(['date'=>$date]));
});
$app->post('/editor/delete-file', function() use($app) {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(['result'=>'deleted']));

+ 31
- 3
public/editor-files/editor.js View File

@ -77,7 +77,8 @@ $(function() {
body: editor.serialize().content.value,
category: csv_to_array($("#post-tags").val()),
slug: $("#post-slug").val(),
status: $("#post-status").val()
status: $("#post-status").val(),
publish: $("#post-publish-date").val()
}, function(response) {
if(response.location) {
reset_page().then(function(){
@ -109,6 +110,24 @@ $(function() {
$("#published-status-warning").removeClass("hidden");
});
$("#post-publish-date").change(function(){
if($("#post-publish-date").val() == "") {
$("#post-publish-date").val("now");
} else {
// Parse and verify the publish date when it's changed
$.post('/editor/parse-date', {
date: $("#post-publish-date").val(),
tzoffset: (new Date().getTimezoneOffset())
}, function(response) {
if(response.date) {
$("#post-publish-date").val(response.date);
} else {
$("#post-publish-date").val("now");
}
});
}
});
$.getJSON('/settings/html-content', function(data){
if(data.html == '0') {
$('.micropub-html-warning').show();
@ -121,6 +140,7 @@ function reset_page() {
$("#post-slug").val('');
$("#post-tags").val('');
$("#post-status").val('published');
$("#post-publish-date").val('now');
$("#content").html('');
$("#draft-status").text("New");
$("#publish-confirm").hide();
@ -147,7 +167,11 @@ function doAutoSave() {
autosaveTimeout = false;
var savedData = {
title: $("#post-name").val(),
body: editor.serialize().content.value
body: editor.serialize().content.value,
tags: $("#post-tags").val(),
slug: $("#post-slug").val(),
status: $("#post-status").val(),
publish: $("#post-publish-date").val()
}
localforage.setItem('currentdraft', savedData).then(function(){
$("#draft-status").text("Saved");
@ -160,6 +184,10 @@ $(function(){
$("#post-name").val(val.title);
$("#content").html(val.body);
$("#draft-status").text("Restored");
$("#post-tags").val(val.tags);
$("#post-slug").val(val.slug);
$("#post-status").val(val.status);
$("#post-publish-date").val(val.publish);
// drop the cursor into the editor which clears the placeholder text
$("#content").focus().click();
}
@ -178,5 +206,5 @@ editor.on(document.getElementById('content'), 'input', function(){
contentChanged();
});
$(function(){
$('#post-name').on('keyup', contentChanged);
$('#post-name, #post-tags, #post-slug, #post-publish-date').on('keyup', contentChanged);
});

+ 3
- 0
public/editor-files/style.css View File

@ -183,6 +183,9 @@ select.form-select-small {
.publish-dropdown .dropdown-content a {
color: #51a1a8;
}
.publish-dropdown .dropdown-content table#publish-fields td {
vertical-align: middle;
}
.publish-dropdown input {
font-family: sans-serif;
}

+ 4
- 0
views/editor.php View File

@ -90,6 +90,10 @@
<a href="/docs/post-status" class="small hidden" target="_blank" id="published-status-warning">read this first!</a>
</td>
</tr>
<tr>
<td>Publish:</td>
<td><input type="text" class="form-field-small" id="post-publish-date" value="now" placeholder="YYYY-MM-DD hh:mm:ss"></td>
</tr>
</table>

Loading…
Cancel
Save