mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-27 01:02:42 +01:00
1308a5555f
Summary: Ref T13559. Substantially correct the client logic for "Save" and "Cancel" actions to handle unusual cases. Test Plan: Quoting behavior: - Quoted a comment. - Cancelled the quoted comment without modifying anything. - Reloaded page. - Before changes: quoted comment still exists. - After changes: quoted comment is deleted. - Looked at comment count in header, saw consistent behavior (before: weird behavior). Empty suggestion behavior: - Created a new comment on a suggestable file. - Clicked "Suggest Edit" to enable suggestions. - Without making any text or suggestion changes, clicked "Save". - Before changes: comment saves, but is empty. - After changes: comment deletes itself without undo. General behavior: - Created and saved an empty comment (deletes itself). - Created and saved a nonempty comment (saves as draft). - Created and saved an empty comment with an edit suggestion (saves). - Created and saved an empty comment with a suggestion to entirely delete lines -- that is, no suggestion text (saves). - Edited a comment, saved without changes (save). - Edited a comment, save deleting all text (saves -- note that this is intentionally without undo, since this is a lot of steps to do by accident). - Cancel editing an unchanged comment (cancels without undo). - Cancel editing a changed comment (cancels with undo). - Undo'd, got text back. - Cancel new comment with no text (deletes without undo). - Cancel new comment with text (deletes with undo). - Undo'd, got text back. - Saved a quoted comment with no changes (saves -- note that this is intentionally not a "delete", since just quoting someone seems fine if you click "Save" -- maybe you want to come back to it later). Maniphest Tasks: T13559 Differential Revision: https://secure.phabricator.com/D21654
137 lines
2.7 KiB
JavaScript
137 lines
2.7 KiB
JavaScript
/**
|
|
* @provides phabricator-diff-inline-content-state
|
|
* @requires javelin-dom
|
|
* @javelin
|
|
*/
|
|
|
|
JX.install('DiffInlineContentState', {
|
|
|
|
construct : function() {
|
|
|
|
},
|
|
|
|
properties: {
|
|
text: null,
|
|
suggestionText: null,
|
|
hasSuggestion: false
|
|
},
|
|
|
|
members: {
|
|
readForm: function(row) {
|
|
var node;
|
|
|
|
try {
|
|
node = JX.DOM.find(row, 'textarea', 'inline-content-text');
|
|
this.setText(node.value);
|
|
} catch (ex) {
|
|
this.setText(null);
|
|
}
|
|
|
|
node = this._getSuggestionNode(row);
|
|
if (node) {
|
|
this.setSuggestionText(node.value);
|
|
} else {
|
|
this.setSuggestionText(null);
|
|
}
|
|
|
|
return this;
|
|
},
|
|
|
|
getWireFormat: function() {
|
|
return {
|
|
text: this.getText(),
|
|
suggestionText: this.getSuggestionText(),
|
|
hasSuggestion: this.getHasSuggestion()
|
|
};
|
|
},
|
|
|
|
readWireFormat: function(map) {
|
|
this.setText(map.text || null);
|
|
this.setSuggestionText(map.suggestionText || null);
|
|
this.setHasSuggestion(!!map.hasSuggestion);
|
|
|
|
return this;
|
|
},
|
|
|
|
getTextForQuote: function() {
|
|
var text = this.getText();
|
|
text = '> ' + text.replace(/\n/g, '\n> ') + '\n\n';
|
|
return text;
|
|
},
|
|
|
|
isStateEmpty: function() {
|
|
return (this.isTextEmpty() && this.isSuggestionEmpty());
|
|
},
|
|
|
|
isTextEmpty: function() {
|
|
var text = this.getText();
|
|
if (text === null) {
|
|
return true;
|
|
}
|
|
|
|
if (this._isStringSimilar(text, '')) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
},
|
|
|
|
isSuggestionEmpty: function() {
|
|
if (!this.getHasSuggestion()) {
|
|
return true;
|
|
}
|
|
|
|
var suggestion = this.getSuggestionText();
|
|
if (suggestion === null) {
|
|
return true;
|
|
}
|
|
|
|
if (this._isStringSimilar(suggestion, '')) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
},
|
|
|
|
isTextSimilar: function(v) {
|
|
if (!v) {
|
|
return false;
|
|
}
|
|
|
|
var us = this.getText();
|
|
var vs = v.getText();
|
|
|
|
return this._isStringSimilar(us, vs);
|
|
},
|
|
|
|
isSuggestionSimilar: function(v) {
|
|
// If we don't have a comparison state, treat them as dissimilar. This
|
|
// is expected to occur in old inline comments that did not save an
|
|
// initial state.
|
|
|
|
if (!v) {
|
|
return false;
|
|
}
|
|
|
|
var us = this.getSuggestionText();
|
|
var vs = v.getSuggestionText();
|
|
|
|
return this._isStringSimilar(us, vs);
|
|
},
|
|
|
|
_isStringSimilar: function(u, v) {
|
|
u = u || '';
|
|
v = v || '';
|
|
return (u === v);
|
|
},
|
|
|
|
_getSuggestionNode: function(row) {
|
|
try {
|
|
return JX.DOM.find(row, 'textarea', 'inline-content-suggestion');
|
|
} catch (ex) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
});
|