mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-23 04:41:19 +00:00
Making sure new comments don't clear out your current textarea.
- Making a better random string generator. - Doing better incoming comment checking. - Fixes #769
This commit is contained in:
parent
43905a041b
commit
4c582cf1b6
2 changed files with 47 additions and 22 deletions
44
ui/src/components/comment-form.tsx
vendored
44
ui/src/components/comment-form.tsx
vendored
|
@ -245,18 +245,32 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleFinished() {
|
handleFinished(data: CommentResponse) {
|
||||||
this.state.previewMode = false;
|
let isReply =
|
||||||
this.state.loading = false;
|
this.props.node !== undefined && data.comment.parent_id !== null;
|
||||||
this.state.commentForm.content = '';
|
let xor =
|
||||||
this.setState(this.state);
|
+!(data.comment.parent_id !== null) ^ +(this.props.node !== undefined);
|
||||||
let form: any = document.getElementById(this.formId);
|
|
||||||
form.reset();
|
if (
|
||||||
if (this.props.node) {
|
(data.comment.creator_id == UserService.Instance.user.id &&
|
||||||
this.props.onReplyCancel();
|
// If its a reply, make sure parent child match
|
||||||
|
isReply &&
|
||||||
|
data.comment.parent_id == this.props.node.comment.id) ||
|
||||||
|
// Otherwise, check the XOR of the two
|
||||||
|
(!isReply && xor)
|
||||||
|
) {
|
||||||
|
this.state.previewMode = false;
|
||||||
|
this.state.loading = false;
|
||||||
|
this.state.commentForm.content = '';
|
||||||
|
this.setState(this.state);
|
||||||
|
let form: any = document.getElementById(this.formId);
|
||||||
|
form.reset();
|
||||||
|
if (this.props.node) {
|
||||||
|
this.props.onReplyCancel();
|
||||||
|
}
|
||||||
|
autosize.update(form);
|
||||||
|
this.setState(this.state);
|
||||||
}
|
}
|
||||||
autosize.update(document.querySelector('textarea'));
|
|
||||||
this.setState(this.state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleCommentSubmit(i: CommentForm, event: any) {
|
handleCommentSubmit(i: CommentForm, event: any) {
|
||||||
|
@ -359,14 +373,10 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
|
||||||
if (UserService.Instance.user) {
|
if (UserService.Instance.user) {
|
||||||
if (res.op == UserOperation.CreateComment) {
|
if (res.op == UserOperation.CreateComment) {
|
||||||
let data = res.data as CommentResponse;
|
let data = res.data as CommentResponse;
|
||||||
if (data.comment.creator_id == UserService.Instance.user.id) {
|
this.handleFinished(data);
|
||||||
this.handleFinished();
|
|
||||||
}
|
|
||||||
} else if (res.op == UserOperation.EditComment) {
|
} else if (res.op == UserOperation.EditComment) {
|
||||||
let data = res.data as CommentResponse;
|
let data = res.data as CommentResponse;
|
||||||
if (data.comment.creator_id == UserService.Instance.user.id) {
|
this.handleFinished(data);
|
||||||
this.handleFinished();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
25
ui/src/utils.ts
vendored
25
ui/src/utils.ts
vendored
|
@ -114,11 +114,26 @@ export const emojiPicker = new EmojiButton({
|
||||||
// TODO i18n
|
// TODO i18n
|
||||||
});
|
});
|
||||||
|
|
||||||
export function randomStr() {
|
const DEFAULT_ALPHABET =
|
||||||
return Math.random()
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||||
.toString(36)
|
|
||||||
.replace(/[^a-z]+/g, '')
|
function getRandomCharFromAlphabet(alphabet: string): string {
|
||||||
.substr(2, 10);
|
return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function randomStr(
|
||||||
|
idDesiredLength: number = 20,
|
||||||
|
alphabet = DEFAULT_ALPHABET
|
||||||
|
): string {
|
||||||
|
/**
|
||||||
|
* Create n-long array and map it to random chars from given alphabet.
|
||||||
|
* Then join individual chars as string
|
||||||
|
*/
|
||||||
|
return Array.from({ length: idDesiredLength })
|
||||||
|
.map(() => {
|
||||||
|
return getRandomCharFromAlphabet(alphabet);
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
|
export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
|
||||||
|
|
Loading…
Reference in a new issue