diff --git a/src/applications/phriction/controller/documentpreview/PhrictionDocumentPreviewController.php b/src/applications/phriction/controller/documentpreview/PhrictionDocumentPreviewController.php index 412440eaad..c7297fdf2c 100644 --- a/src/applications/phriction/controller/documentpreview/PhrictionDocumentPreviewController.php +++ b/src/applications/phriction/controller/documentpreview/PhrictionDocumentPreviewController.php @@ -27,6 +27,19 @@ class PhrictionDocumentPreviewController $request = $this->getRequest(); $document = $request->getStr('document'); + $draft_key = $request->getStr('draftkey'); + if ($draft_key) { + $table = new PhabricatorDraft(); + queryfx( + $table->establishConnection('w'), + 'INSERT INTO %T (authorPHID, draftKey, draft) VALUES (%s, %s, %s) + ON DUPLICATE KEY UPDATE draft = VALUES(draft)', + $table->getTableName(), + $request->getUser()->getPHID(), + $draft_key, + $document); + } + $content_obj = new PhrictionContent(); $content_obj->setContent($document); diff --git a/src/applications/phriction/controller/documentpreview/__init__.php b/src/applications/phriction/controller/documentpreview/__init__.php index b3287c8d5a..8a284077a5 100644 --- a/src/applications/phriction/controller/documentpreview/__init__.php +++ b/src/applications/phriction/controller/documentpreview/__init__.php @@ -7,9 +7,11 @@ phutil_require_module('phabricator', 'aphront/response/ajax'); +phutil_require_module('phabricator', 'applications/draft/storage/draft'); phutil_require_module('phabricator', 'applications/markup/engine'); phutil_require_module('phabricator', 'applications/phriction/controller/base'); phutil_require_module('phabricator', 'applications/phriction/storage/content'); +phutil_require_module('phabricator', 'storage/queryfx'); phutil_require_module('phutil', 'utils'); diff --git a/src/applications/phriction/controller/edit/PhrictionEditController.php b/src/applications/phriction/controller/edit/PhrictionEditController.php index f462eafa08..e3ffa7a943 100644 --- a/src/applications/phriction/controller/edit/PhrictionEditController.php +++ b/src/applications/phriction/controller/edit/PhrictionEditController.php @@ -1,7 +1,7 @@ getBool('nodraft')) { + $draft = null; + $draft_key = null; + } else { + if ($document->getPHID()) { + $draft_key = $document->getPHID().':'.$content->getVersion(); + } else { + $draft_key = 'phriction:'.$content->getSlug(); + } + $draft = id(new PhabricatorDraft())->loadOneWhere( + 'authorPHID = %s AND draftKey = %s', + $user->getPHID(), + $draft_key); + } + require_celerity_resource('phriction-document-css'); $e_title = true; @@ -101,6 +116,10 @@ class PhrictionEditController $editor->save(); + if ($draft) { + $draft->delete(); + } + $uri = PhrictionDocument::getSlugURI($document->getSlug()); return id(new AphrontRedirectResponse())->setURI($uri); } @@ -144,10 +163,33 @@ class PhrictionEditController $cancel_uri = PhrictionDocument::getSlugURI($document->getSlug()); + if ($draft && + strlen($draft->getDraft()) && + ($draft->getDraft() != $content->getContent())) { + $content_text = $draft->getDraft(); + + $discard = phutil_render_tag( + 'a', + array( + 'href' => $request->getRequestURI()->alter('nodraft', true), + ), + 'discard this draft'); + + $draft_note = new AphrontErrorView(); + $draft_note->setSeverity(AphrontErrorView::SEVERITY_NOTICE); + $draft_note->setTitle('Recovered Draft'); + $draft_note->appendChild( + '
Showing a saved draft of your edits, you can '.$discard.'.
'); + } else { + $content_text = $content->getContent(); + $draft_note = null; + } + $form = id(new AphrontFormView()) ->setUser($user) ->setAction($request->getRequestURI()->getPath()) ->addHiddenInput('slug', $document->getSlug()) + ->addHiddenInput('nodraft', $request->getBool('nodraft')) ->appendChild( id(new AphrontFormTextControl()) ->setLabel('Title') @@ -161,7 +203,7 @@ class PhrictionEditController ->appendChild( id(new AphrontFormTextAreaControl()) ->setLabel('Content') - ->setValue($content->getContent()) + ->setValue($content_text) ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL) ->setName('content') ->setID('document-textarea') @@ -204,11 +246,12 @@ class PhrictionEditController array( 'preview' => 'document-preview', 'textarea' => 'document-textarea', - 'uri' => '/phriction/preview/', + 'uri' => '/phriction/preview/?draftkey='.$draft_key, )); return $this->buildStandardPageResponse( array( + $draft_note, $error_view, $panel, $preview_panel, diff --git a/src/applications/phriction/controller/edit/__init__.php b/src/applications/phriction/controller/edit/__init__.php index a29a88bcb4..b2d470df71 100644 --- a/src/applications/phriction/controller/edit/__init__.php +++ b/src/applications/phriction/controller/edit/__init__.php @@ -8,6 +8,7 @@ phutil_require_module('phabricator', 'aphront/response/404'); phutil_require_module('phabricator', 'aphront/response/redirect'); +phutil_require_module('phabricator', 'applications/draft/storage/draft'); phutil_require_module('phabricator', 'applications/phriction/controller/base'); phutil_require_module('phabricator', 'applications/phriction/editor/document'); phutil_require_module('phabricator', 'applications/phriction/storage/content');