From a5aff7d0c969adb78338e35a1d736ad80462b34b Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Wed, 4 Apr 2018 15:22:38 -0400 Subject: Post body and title are auto-saved --- Zotlabs/Module/Autosavetext.php | 37 +++++++++++++++++++++++++++++++++++++ Zotlabs/Module/Item.php | 4 ++++ include/conversation.php | 16 ++++++++++++---- view/tpl/jot.tpl | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 Zotlabs/Module/Autosavetext.php diff --git a/Zotlabs/Module/Autosavetext.php b/Zotlabs/Module/Autosavetext.php new file mode 100644 index 000000000..f6bffd2a2 --- /dev/null +++ b/Zotlabs/Module/Autosavetext.php @@ -0,0 +1,37 @@ + true); + if(array_key_exists('body',$_REQUEST) && array_key_exists('type',$_REQUEST)) { + $body = escape_tags($_REQUEST['body']); + $title = (array_key_exists('title',$_REQUEST) ? escape_tags($_REQUEST['title']) : ''); + $type = $_REQUEST['type']; + + if($body && $type === 'post') { + set_pconfig(local_channel(),'autosavetext_post','body',$body); + set_pconfig(local_channel(),'autosavetext_post','title',$title); + } + + logger('post saved.', LOGGER_DEBUG); + } + + // // push updates to channel clones + // + // if((argc() > 1) && (argv(1) === 'sync')) { + // require_once('include/zot.php'); + // build_sync_packet(); + // } + + json_return_and_die($ret); + + } + +} diff --git a/Zotlabs/Module/Item.php b/Zotlabs/Module/Item.php index fba2ef7a4..ed35fc20d 100644 --- a/Zotlabs/Module/Item.php +++ b/Zotlabs/Module/Item.php @@ -1050,6 +1050,10 @@ class Item extends \Zotlabs\Web\Controller { logger('post_complete'); + // Remove auto-saved post text if the post was successfully posted + del_pconfig(local_channel(),'autosavetext_post','body'); + del_pconfig(local_channel(),'autosavetext_post','title'); + if($moderated) { info(t('Your comment is awaiting approval.') . EOL); } diff --git a/include/conversation.php b/include/conversation.php index 64beb1b0e..22eb2f6ad 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -1361,8 +1361,16 @@ function status_editor($a, $x, $popup = false) { } $sharebutton = (x($x,'button') ? $x['button'] : t('Share')); - $placeholdtext = (x($x,'content_label') ? $x['content_label'] : $sharebutton); - + $content = ((x($x,'body')) ? htmlspecialchars($x['body'], ENT_COMPAT,'UTF-8') : ''); + if(!$content) { + $content = get_pconfig(local_channel(),'autosavetext_post','body'); + } + $title = ((x($x, 'title')) ? htmlspecialchars($x['title'], ENT_COMPAT,'UTF-8') : ''); + if(!$title) { + $title = get_pconfig(local_channel(),'autosavetext_post','title'); + } + $placeholdtext = (x($x,'content_label') ? $x['content_label'] : $sharebutton); + $o .= replace_macros($tpl, array( '$return_path' => ((x($x, 'return_path')) ? $x['return_path'] : App::$query_string), '$action' => z_root() . '/item', @@ -1394,14 +1402,14 @@ function status_editor($a, $x, $popup = false) { '$feature_nocomment' => $feature_nocomment, '$nocomment' => ((array_key_exists('item',$x)) ? $x['item']['item_nocomment'] : 0), '$clearloc' => $clearloc, - '$title' => ((x($x, 'title')) ? htmlspecialchars($x['title'], ENT_COMPAT,'UTF-8') : ''), + '$title' => $title, '$placeholdertitle' => ((x($x, 'placeholdertitle')) ? $x['placeholdertitle'] : t('Title (optional)')), '$catsenabled' => $catsenabled, '$category' => ((x($x, 'category')) ? $x['category'] : ''), '$placeholdercategory' => t('Categories (optional, comma-separated list)'), '$permset' => t('Permission settings'), '$ptyp' => ((x($x, 'ptyp')) ? $x['ptyp'] : ''), - '$content' => ((x($x,'body')) ? htmlspecialchars($x['body'], ENT_COMPAT,'UTF-8') : ''), + '$content' => $content, '$attachment' => ((x($x, 'attachment')) ? $x['attachment'] : ''), '$post_id' => ((x($x, 'post_id')) ? $x['post_id'] : ''), '$defloc' => $x['default_location'], diff --git a/view/tpl/jot.tpl b/view/tpl/jot.tpl index bc9339d4c..90058ab30 100755 --- a/view/tpl/jot.tpl +++ b/view/tpl/jot.tpl @@ -40,6 +40,38 @@ {{/if}} + +
-- cgit v1.2.3 From 43cafcc761bc12f442c30ab952164736aa18f9d8 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 8 Apr 2018 14:18:10 -0400 Subject: Auto-save post and comment entry using localStorage in browser. --- Zotlabs/Module/Item.php | 4 ---- include/conversation.php | 16 ++++---------- view/js/main.js | 29 +++++++++++++++++++++++++ view/tpl/jot-header.tpl | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ view/tpl/jot.tpl | 32 ---------------------------- 5 files changed, 88 insertions(+), 48 deletions(-) diff --git a/Zotlabs/Module/Item.php b/Zotlabs/Module/Item.php index 083ad50f7..bba1dc02d 100644 --- a/Zotlabs/Module/Item.php +++ b/Zotlabs/Module/Item.php @@ -1051,10 +1051,6 @@ class Item extends \Zotlabs\Web\Controller { logger('post_complete'); - // Remove auto-saved post text if the post was successfully posted - del_pconfig(local_channel(),'autosavetext_post','body'); - del_pconfig(local_channel(),'autosavetext_post','title'); - if($moderated) { info(t('Your comment is awaiting approval.') . EOL); } diff --git a/include/conversation.php b/include/conversation.php index 93f7e11a7..ce0467770 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -1361,16 +1361,8 @@ function status_editor($a, $x, $popup = false) { } $sharebutton = (x($x,'button') ? $x['button'] : t('Share')); - $content = ((x($x,'body')) ? htmlspecialchars($x['body'], ENT_COMPAT,'UTF-8') : ''); - if(!$content) { - $content = get_pconfig(local_channel(),'autosavetext_post','body'); - } - $title = ((x($x, 'title')) ? htmlspecialchars($x['title'], ENT_COMPAT,'UTF-8') : ''); - if(!$title) { - $title = get_pconfig(local_channel(),'autosavetext_post','title'); - } - $placeholdtext = (x($x,'content_label') ? $x['content_label'] : $sharebutton); - + $placeholdtext = (x($x,'content_label') ? $x['content_label'] : $sharebutton); + $o .= replace_macros($tpl, array( '$return_path' => ((x($x, 'return_path')) ? $x['return_path'] : App::$query_string), '$action' => z_root() . '/item', @@ -1402,14 +1394,14 @@ function status_editor($a, $x, $popup = false) { '$feature_nocomment' => $feature_nocomment, '$nocomment' => ((array_key_exists('item',$x)) ? $x['item']['item_nocomment'] : 0), '$clearloc' => $clearloc, - '$title' => $title, + '$title' => ((x($x, 'title')) ? htmlspecialchars($x['title'], ENT_COMPAT,'UTF-8') : ''), '$placeholdertitle' => ((x($x, 'placeholdertitle')) ? $x['placeholdertitle'] : t('Title (optional)')), '$catsenabled' => $catsenabled, '$category' => ((x($x, 'category')) ? $x['category'] : ''), '$placeholdercategory' => t('Categories (optional, comma-separated list)'), '$permset' => t('Permission settings'), '$ptyp' => ((x($x, 'ptyp')) ? $x['ptyp'] : ''), - '$content' => $content, + '$content' => ((x($x,'body')) ? htmlspecialchars($x['body'], ENT_COMPAT,'UTF-8') : ''), '$attachment' => ((x($x, 'attachment')) ? $x['attachment'] : ''), '$post_id' => ((x($x, 'post_id')) ? $x['post_id'] : ''), '$defloc' => $x['default_location'], diff --git a/view/js/main.js b/view/js/main.js index e700c4e6e..d09615f3c 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -166,6 +166,12 @@ function handle_comment_form(e) { $('#' + commentElm).addClass('expanded').removeAttr('placeholder'); $('#' + commentElm).attr('tabindex','9'); $('#' + submitElm).attr('tabindex','10'); + + var commentBody = localStorage.getItem("comment_body"); + if(commentBody && $('#' + commentElm).val() === '') { + $('#' + commentElm).val(commentBody); + } + form.find(':not(:visible)').show(); } @@ -185,6 +191,28 @@ function handle_comment_form(e) { form.find(':not(.comment-edit-text)').hide(); } }); + + var commentSaveTimer = null; + var emptyCommentElm = form.find('.comment-edit-text').attr('id'); + $(document).on('focusout','#' + emptyCommentElm,function(e){ + if(commentSaveTimer) + clearTimeout(commentSaveTimer); + commentSaveChanges(true); + commentSaveTimer = null; + }); + + $(document).on('focusin','#' + emptyCommentElm,function(e){ + commentSaveTimer = setTimeout(function () { + commentSaveChanges(false); + },10000); + }); + + function commentSaveChanges(isFinal = false, type) { + localStorage.setItem("comment_body", $('#' + emptyCommentElm).val()); + if( !isFinal) { + commentSaveTimer = setTimeout(commentSaveChanges,10000); + } + } } function commentClose(obj, id) { @@ -1106,6 +1134,7 @@ function post_comment(id) { $("#comment-edit-form-" + id).serialize(), function(data) { if(data.success) { + localStorage.removeItem("comment_body"); $("#comment-edit-preview-" + id).hide(); $("#comment-edit-wrapper-" + id).hide(); $("#comment-edit-text-" + id).val(''); diff --git a/view/tpl/jot-header.tpl b/view/tpl/jot-header.tpl index c1dab52d5..2b4284ade 100755 --- a/view/tpl/jot-header.tpl +++ b/view/tpl/jot-header.tpl @@ -560,3 +560,58 @@ $( document ).on( "click", ".wall-item-delete-link,.page-delete-link,.layout-del } }); + + + \ No newline at end of file diff --git a/view/tpl/jot.tpl b/view/tpl/jot.tpl index 90058ab30..bc9339d4c 100755 --- a/view/tpl/jot.tpl +++ b/view/tpl/jot.tpl @@ -40,38 +40,6 @@ {{/if}} - -
-- cgit v1.2.3 From 4760dc9bcb7e43be9c27cf83ba5273344384813d Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 8 Apr 2018 14:25:04 -0400 Subject: removed Autosavetext.php file since it is not used with localStorage --- Zotlabs/Module/Autosavetext.php | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 Zotlabs/Module/Autosavetext.php diff --git a/Zotlabs/Module/Autosavetext.php b/Zotlabs/Module/Autosavetext.php deleted file mode 100644 index f6bffd2a2..000000000 --- a/Zotlabs/Module/Autosavetext.php +++ /dev/null @@ -1,37 +0,0 @@ - true); - if(array_key_exists('body',$_REQUEST) && array_key_exists('type',$_REQUEST)) { - $body = escape_tags($_REQUEST['body']); - $title = (array_key_exists('title',$_REQUEST) ? escape_tags($_REQUEST['title']) : ''); - $type = $_REQUEST['type']; - - if($body && $type === 'post') { - set_pconfig(local_channel(),'autosavetext_post','body',$body); - set_pconfig(local_channel(),'autosavetext_post','title',$title); - } - - logger('post saved.', LOGGER_DEBUG); - } - - // // push updates to channel clones - // - // if((argc() > 1) && (argv(1) === 'sync')) { - // require_once('include/zot.php'); - // build_sync_packet(); - // } - - json_return_and_die($ret); - - } - -} -- cgit v1.2.3 From f9ec3c66ff1305ca0647454d27793ac5365f7f4a Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 8 Apr 2018 19:44:21 -0400 Subject: Added feature setting for auto-save, defaulting to enabled. --- Zotlabs/Lib/ThreadItem.php | 5 +++- include/conversation.php | 5 +++- include/features.php | 9 +++++++ view/js/main.js | 20 +++++++++------ view/tpl/comment_item.tpl | 3 +++ view/tpl/jot-header.tpl | 62 ++++++++++++++++++++++++++-------------------- 6 files changed, 68 insertions(+), 36 deletions(-) diff --git a/Zotlabs/Lib/ThreadItem.php b/Zotlabs/Lib/ThreadItem.php index d35d4732a..c2aa920b9 100644 --- a/Zotlabs/Lib/ThreadItem.php +++ b/Zotlabs/Lib/ThreadItem.php @@ -733,6 +733,8 @@ class ThreadItem { $arr = array('comment_buttons' => '','id' => $this->get_id()); call_hooks('comment_buttons',$arr); $comment_buttons = $arr['comment_buttons']; + + $feature_auto_save_draft = ((feature_enabled($conv->get_profile_owner(), 'auto_save_draft')) ? "true" : "false"); $comment_box = replace_macros($template,array( '$return_path' => '', @@ -768,7 +770,8 @@ class ThreadItem { '$anoncomments' => ((($conv->get_mode() === 'channel' || $conv->get_mode() === 'display') && perm_is_allowed($conv->get_profile_owner(),'','post_comments')) ? true : false), '$anonname' => [ 'anonname', t('Your full name (required)') ], '$anonmail' => [ 'anonmail', t('Your email address (required)') ], - '$anonurl' => [ 'anonurl', t('Your website URL (optional)') ] + '$anonurl' => [ 'anonurl', t('Your website URL (optional)') ], + '$auto_save_draft' => $feature_auto_save_draft, )); return $comment_box; diff --git a/include/conversation.php b/include/conversation.php index ce0467770..3834d9866 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -1301,7 +1301,9 @@ function status_editor($a, $x, $popup = false) { $id_select = ''; $webpage = ((x($x,'webpage')) ? $x['webpage'] : ''); - + + $feature_auto_save_draft = ((feature_enabled($x['profile_uid'], 'auto_save_draft')) ? "true" : "false"); + $tpl = get_markup_template('jot-header.tpl'); App::$page['htmlhead'] .= replace_macros($tpl, array( @@ -1323,6 +1325,7 @@ function status_editor($a, $x, $popup = false) { '$modalerroralbum' => t('Error getting album'), '$nocomment_enabled' => t('Comments enabled'), '$nocomment_disabled' => t('Comments disabled'), + '$auto_save_draft' => $feature_auto_save_draft, )); $tpl = get_markup_template('jot.tpl'); diff --git a/include/features.php b/include/features.php index 4859a01db..36f4f0433 100644 --- a/include/features.php +++ b/include/features.php @@ -350,6 +350,15 @@ function get_features($filtered = true) { feature_level('suppress_duplicates',1), ], + [ + 'auto_save_draft', + t('Auto-save drafts of posts and comments'), + t('Automatically saves post and comment drafts in local browser storage to help prevent accidental loss of compositions'), + true, + get_config('feature_lock','auto_save_draft'), + feature_level('auto_save_draft',1), + ], + ], // Network Tools diff --git a/view/js/main.js b/view/js/main.js index d09615f3c..c3c2c850f 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -167,9 +167,13 @@ function handle_comment_form(e) { $('#' + commentElm).attr('tabindex','9'); $('#' + submitElm).attr('tabindex','10'); - var commentBody = localStorage.getItem("comment_body"); - if(commentBody && $('#' + commentElm).val() === '') { - $('#' + commentElm).val(commentBody); + if(auto_save_draft) { + var commentBody = localStorage.getItem("comment_body"); + if(commentBody && $('#' + commentElm).val() === '') { + $('#' + commentElm).val(commentBody); + } + } else { + localStorage.removeItem("comment_body"); } form.find(':not(:visible)').show(); @@ -207,10 +211,12 @@ function handle_comment_form(e) { },10000); }); - function commentSaveChanges(isFinal = false, type) { - localStorage.setItem("comment_body", $('#' + emptyCommentElm).val()); - if( !isFinal) { - commentSaveTimer = setTimeout(commentSaveChanges,10000); + function commentSaveChanges(isFinal = false) { + if(auto_save_draft) { + localStorage.setItem("comment_body", $('#' + emptyCommentElm).val()); + if( !isFinal) { + commentSaveTimer = setTimeout(commentSaveChanges,10000); + } } } } diff --git a/view/tpl/comment_item.tpl b/view/tpl/comment_item.tpl index 3b51971ec..23594677c 100755 --- a/view/tpl/comment_item.tpl +++ b/view/tpl/comment_item.tpl @@ -1,3 +1,6 @@ + {{if $threaded}}
{{else}} diff --git a/view/tpl/jot-header.tpl b/view/tpl/jot-header.tpl index 2b4284ade..7af344681 100755 --- a/view/tpl/jot-header.tpl +++ b/view/tpl/jot-header.tpl @@ -580,37 +580,45 @@ $( document ).on( "click", ".wall-item-delete-link,.page-delete-link,.layout-del }); function postSaveChanges(isFinal = false, type) { - localStorage.setItem("post_title", $("#jot-title").val()); - localStorage.setItem("post_body", $("#profile-jot-text").val()); - localStorage.setItem("post_category", $("#jot-category").val()); - if( !isFinal) { - postSaveTimer = setTimeout(postSaveChanges,10000); - } + if({{$auto_save_draft}}) { + localStorage.setItem("post_title", $("#jot-title").val()); + localStorage.setItem("post_body", $("#profile-jot-text").val()); + localStorage.setItem("post_category", $("#jot-category").val()); + if( !isFinal) { + postSaveTimer = setTimeout(postSaveChanges,10000); + } + } } $(document).ready(function() { - var postTitle = localStorage.getItem("post_title"); - var postBody = localStorage.getItem("post_body"); - var postCategory = localStorage.getItem("post_category"); - var openEditor = false; - if(postTitle) { - $('#jot-title').val(postTitle); - openEditor = true; - } - if(postBody) { - $('#profile-jot-text').val(postBody); - openEditor = true; - } - if(postCategory) { - var categories = postCategory.split(','); - categories.forEach(function(cat) { - $('#jot-category').tagsinput('add', cat); - }); - openEditor = true; - } - if(openEditor) { - initEditor(); + if({{$auto_save_draft}}) { + var postTitle = localStorage.getItem("post_title"); + var postBody = localStorage.getItem("post_body"); + var postCategory = localStorage.getItem("post_category"); + var openEditor = false; + if(postTitle) { + $('#jot-title').val(postTitle); + openEditor = true; + } + if(postBody) { + $('#profile-jot-text').val(postBody); + openEditor = true; + } + if(postCategory) { + var categories = postCategory.split(','); + categories.forEach(function(cat) { + $('#jot-category').tagsinput('add', cat); + }); + openEditor = true; + } + if(openEditor) { + initEditor(); + } + } else { + localStorage.removeItem("post_title"); + localStorage.removeItem("post_body"); + localStorage.removeItem("post_category"); } }); -- cgit v1.2.3