mirror of
https://we.phorge.it/source/phorge.git
synced 2025-02-02 01:48:23 +01:00
1352be827e
Summary: Ref T2009. Inline comments have "scaffolding", which is basically some empty table cells/rows around them to get the layout correct. The scaffolding depends on the renderer, since the cells are different for side-by-side vs unified diffs. This is currently duplicated all over the place: - Edit view has 1up/2up. - Detail view has 1up/2up. - 1up renderer has 1up. - 2up renderer has four separate copies of the 2up logic. These all have subtle differences, which are mostly bugs. Start making the scaffolding more composable so we can get rid of that mess. Test Plan: Added, edited, and removed inline comments on unified and side-by-side diffs. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T2009 Differential Revision: https://secure.phabricator.com/D11997
268 lines
7.9 KiB
PHP
268 lines
7.9 KiB
PHP
<?php
|
|
|
|
abstract class PhabricatorInlineCommentController
|
|
extends PhabricatorController {
|
|
|
|
abstract protected function createComment();
|
|
abstract protected function loadComment($id);
|
|
abstract protected function loadCommentForEdit($id);
|
|
abstract protected function deleteComment(
|
|
PhabricatorInlineCommentInterface $inline);
|
|
abstract protected function saveComment(
|
|
PhabricatorInlineCommentInterface $inline);
|
|
|
|
private $changesetID;
|
|
private $isNewFile;
|
|
private $isOnRight;
|
|
private $lineNumber;
|
|
private $lineLength;
|
|
private $commentText;
|
|
private $operation;
|
|
private $commentID;
|
|
private $renderer;
|
|
|
|
public function getCommentID() {
|
|
return $this->commentID;
|
|
}
|
|
|
|
public function getOperation() {
|
|
return $this->operation;
|
|
}
|
|
|
|
public function getCommentText() {
|
|
return $this->commentText;
|
|
}
|
|
|
|
public function getLineLength() {
|
|
return $this->lineLength;
|
|
}
|
|
|
|
public function getLineNumber() {
|
|
return $this->lineNumber;
|
|
}
|
|
|
|
public function getIsOnRight() {
|
|
return $this->isOnRight;
|
|
}
|
|
|
|
public function getChangesetID() {
|
|
return $this->changesetID;
|
|
}
|
|
|
|
public function getIsNewFile() {
|
|
return $this->isNewFile;
|
|
}
|
|
|
|
public function setRenderer($renderer) {
|
|
$this->renderer = $renderer;
|
|
return $this;
|
|
}
|
|
|
|
public function getRenderer() {
|
|
return $this->renderer;
|
|
}
|
|
|
|
public function processRequest() {
|
|
$request = $this->getRequest();
|
|
$user = $request->getUser();
|
|
|
|
$this->readRequestParameters();
|
|
|
|
switch ($this->getOperation()) {
|
|
case 'delete':
|
|
$inline = $this->loadCommentForEdit($this->getCommentID());
|
|
|
|
if ($request->isFormPost()) {
|
|
$this->deleteComment($inline);
|
|
return $this->buildEmptyResponse();
|
|
}
|
|
|
|
$dialog = new AphrontDialogView();
|
|
$dialog->setUser($user);
|
|
$dialog->setSubmitURI($request->getRequestURI());
|
|
|
|
$dialog->setTitle(pht('Really delete this comment?'));
|
|
$dialog->addHiddenInput('id', $this->getCommentID());
|
|
$dialog->addHiddenInput('op', 'delete');
|
|
$dialog->appendChild(
|
|
phutil_tag('p', array(), pht('Delete this inline comment?')));
|
|
|
|
$dialog->addCancelButton('#');
|
|
$dialog->addSubmitButton(pht('Delete'));
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
case 'edit':
|
|
$inline = $this->loadCommentForEdit($this->getCommentID());
|
|
|
|
$text = $this->getCommentText();
|
|
|
|
if ($request->isFormPost()) {
|
|
if (strlen($text)) {
|
|
$inline->setContent($text);
|
|
$this->saveComment($inline);
|
|
return $this->buildRenderedCommentResponse(
|
|
$inline,
|
|
$this->getIsOnRight());
|
|
} else {
|
|
$this->deleteComment($inline);
|
|
return $this->buildEmptyResponse();
|
|
}
|
|
}
|
|
|
|
$edit_dialog = $this->buildEditDialog();
|
|
$edit_dialog->setTitle(pht('Edit Inline Comment'));
|
|
|
|
$edit_dialog->addHiddenInput('id', $this->getCommentID());
|
|
$edit_dialog->addHiddenInput('op', 'edit');
|
|
$edit_dialog->addHiddenInput('renderer', $this->getRenderer());
|
|
|
|
$edit_dialog->appendChild(
|
|
$this->renderTextArea(
|
|
nonempty($text, $inline->getContent())));
|
|
|
|
return id(new AphrontAjaxResponse())
|
|
->setContent($edit_dialog->render());
|
|
case 'create':
|
|
$text = $this->getCommentText();
|
|
|
|
if (!$request->isFormPost() || !strlen($text)) {
|
|
return $this->buildEmptyResponse();
|
|
}
|
|
|
|
$inline = $this->createComment()
|
|
->setChangesetID($this->getChangesetID())
|
|
->setAuthorPHID($user->getPHID())
|
|
->setLineNumber($this->getLineNumber())
|
|
->setLineLength($this->getLineLength())
|
|
->setIsNewFile($this->getIsNewFile())
|
|
->setContent($text);
|
|
$this->saveComment($inline);
|
|
|
|
return $this->buildRenderedCommentResponse(
|
|
$inline,
|
|
$this->getIsOnRight());
|
|
case 'reply':
|
|
default:
|
|
$edit_dialog = $this->buildEditDialog();
|
|
|
|
if ($this->getOperation() == 'reply') {
|
|
$inline = $this->loadComment($this->getCommentID());
|
|
|
|
$edit_dialog->setTitle(pht('Reply to Inline Comment'));
|
|
$changeset = $inline->getChangesetID();
|
|
$is_new = $inline->getIsNewFile();
|
|
$number = $inline->getLineNumber();
|
|
$length = $inline->getLineLength();
|
|
} else {
|
|
$edit_dialog->setTitle(pht('New Inline Comment'));
|
|
$changeset = $this->getChangesetID();
|
|
$is_new = $this->getIsNewFile();
|
|
$number = $this->getLineNumber();
|
|
$length = $this->getLineLength();
|
|
}
|
|
|
|
$edit_dialog->addHiddenInput('op', 'create');
|
|
$edit_dialog->addHiddenInput('changeset', $changeset);
|
|
$edit_dialog->addHiddenInput('is_new', $is_new);
|
|
$edit_dialog->addHiddenInput('number', $number);
|
|
$edit_dialog->addHiddenInput('length', $length);
|
|
$edit_dialog->addHiddenInput('renderer', $this->getRenderer());
|
|
|
|
$text_area = $this->renderTextArea($this->getCommentText());
|
|
$edit_dialog->appendChild($text_area);
|
|
|
|
return id(new AphrontAjaxResponse())
|
|
->setContent($edit_dialog->render());
|
|
}
|
|
}
|
|
|
|
private function readRequestParameters() {
|
|
$request = $this->getRequest();
|
|
|
|
// NOTE: This isn't necessarily a DifferentialChangeset ID, just an
|
|
// application identifier for the changeset. In Diffusion, it's a Path ID.
|
|
$this->changesetID = $request->getInt('changeset');
|
|
|
|
$this->isNewFile = (int)$request->getBool('is_new');
|
|
$this->isOnRight = $request->getBool('on_right');
|
|
$this->lineNumber = $request->getInt('number');
|
|
$this->lineLength = $request->getInt('length');
|
|
$this->commentText = $request->getStr('text');
|
|
$this->commentID = $request->getInt('id');
|
|
$this->operation = $request->getStr('op');
|
|
$this->renderer = $request->getStr('renderer');
|
|
}
|
|
|
|
private function buildEditDialog() {
|
|
$request = $this->getRequest();
|
|
$user = $request->getUser();
|
|
|
|
$edit_dialog = id(new PHUIDiffInlineCommentEditView())
|
|
->setUser($user)
|
|
->setSubmitURI($request->getRequestURI())
|
|
->setOnRight($this->getIsOnRight())
|
|
->setNumber($this->getLineNumber())
|
|
->setLength($this->getLineLength())
|
|
->setRenderer($this->getRenderer());
|
|
|
|
return $edit_dialog;
|
|
}
|
|
|
|
private function buildEmptyResponse() {
|
|
return id(new AphrontAjaxResponse())
|
|
->setContent(
|
|
array(
|
|
'markup' => '',
|
|
));
|
|
}
|
|
|
|
private function buildRenderedCommentResponse(
|
|
PhabricatorInlineCommentInterface $inline,
|
|
$on_right) {
|
|
|
|
$request = $this->getRequest();
|
|
$user = $request->getUser();
|
|
|
|
$engine = new PhabricatorMarkupEngine();
|
|
$engine->setViewer($user);
|
|
$engine->addObject(
|
|
$inline,
|
|
PhabricatorInlineCommentInterface::MARKUP_FIELD_BODY);
|
|
$engine->process();
|
|
|
|
$phids = array($user->getPHID());
|
|
|
|
$handles = $this->loadViewerHandles($phids);
|
|
|
|
$view = id(new PHUIDiffInlineCommentDetailView())
|
|
->setInlineComment($inline)
|
|
->setOnRight($on_right)
|
|
->setMarkupEngine($engine)
|
|
->setHandles($handles)
|
|
->setEditable(true);
|
|
|
|
$renderer = DifferentialChangesetHTMLRenderer::getHTMLRendererByKey(
|
|
$this->getRenderer());
|
|
|
|
$view = $renderer->getRowScaffoldForInline($view);
|
|
$view = id(new PHUIDiffInlineCommentTableScaffold())
|
|
->addRowScaffold($view);
|
|
|
|
return id(new AphrontAjaxResponse())
|
|
->setContent(
|
|
array(
|
|
'inlineCommentID' => $inline->getID(),
|
|
'markup' => $view->render(),
|
|
));
|
|
}
|
|
|
|
private function renderTextArea($text) {
|
|
return id(new PhabricatorRemarkupControl())
|
|
->setUser($this->getRequest()->getUser())
|
|
->setSigil('differential-inline-comment-edit-textarea')
|
|
->setName('text')
|
|
->setValue($text)
|
|
->setDisableFullScreen(true);
|
|
}
|
|
|
|
}
|