Allow submitting comment with ctrl+enter

Fixes #427
This commit is contained in:
rubenwardy 2023-08-26 13:38:34 +01:00
parent 798679ca44
commit 8f52c67f0f
6 changed files with 21 additions and 11 deletions

@ -178,7 +178,7 @@ def delete_reply(id):
class CommentForm(FlaskForm): class CommentForm(FlaskForm):
comment = TextAreaField(lazy_gettext("Comment"), [InputRequired(), Length(2, 2000)]) comment = TextAreaField(lazy_gettext("Comment"), [InputRequired(), Length(2, 2000)])
submit = SubmitField(lazy_gettext("Comment")) btn_submit = SubmitField(lazy_gettext("Comment"))
@bp.route("/threads/<int:id>/edit/", methods=["GET", "POST"]) @bp.route("/threads/<int:id>/edit/", methods=["GET", "POST"])
@ -280,7 +280,7 @@ class ThreadForm(FlaskForm):
title = StringField(lazy_gettext("Title"), [InputRequired(), Length(3,100)]) title = StringField(lazy_gettext("Title"), [InputRequired(), Length(3,100)])
comment = TextAreaField(lazy_gettext("Comment"), [InputRequired(), Length(10, 2000)]) comment = TextAreaField(lazy_gettext("Comment"), [InputRequired(), Length(10, 2000)])
private = BooleanField(lazy_gettext("Private")) private = BooleanField(lazy_gettext("Private"))
submit = SubmitField(lazy_gettext("Open Thread")) btn_submit = SubmitField(lazy_gettext("Open Thread"))
@bp.route("/threads/new/", methods=["GET", "POST"]) @bp.route("/threads/new/", methods=["GET", "POST"])

@ -55,4 +55,14 @@ document.querySelectorAll("textarea.markdown").forEach((element) => {
return preview.innerHTML; return preview.innerHTML;
} }
}); });
// Ctrl+enter to submit
if (element.getAttribute("data-enter-submit")) {
element.easy_mde.codemirror.on("keyup", (mirror, e) => {
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
element.form?.submit();
e.preventDefault();
}
});
}
}) })

@ -280,7 +280,7 @@
<script src="/static/libs/easymde.min.js"></script> <script src="/static/libs/easymde.min.js"></script>
<link rel="stylesheet" type="text/css" href="/static/libs/easymde.min.css"> <link rel="stylesheet" type="text/css" href="/static/libs/easymde.min.css">
<link href="/static/fa/css/all.css" rel="stylesheet"> <link href="/static/fa/css/all.css" rel="stylesheet">
<script src="/static/js/markdowntextarea.js"></script> <script src="/static/js/markdowntextarea.js?v=2"></script>
{% block scriptextra %}{% endblock %} {% block scriptextra %}{% endblock %}
</body> </body>

@ -166,14 +166,14 @@
{% from "macros/forms.html" import render_field, render_submit_field %} {% from "macros/forms.html" import render_field, render_submit_field %}
<form method="post" action="{{ url_for('threads.view', id=thread.id) }}" class="card-body"> <form method="post" action="{{ url_for('threads.view', id=thread.id) }}" class="card-body">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field(form.comment, fieldclass="form-control markdown", label="") }} {{ render_field(form.comment, fieldclass="form-control markdown", label="", data_enter_submit="1") }}
{{ render_submit_field(form.submit) }} {{ render_submit_field(form.btn_submit) }}
</form> </form>
{% else %} {% else %}
<form method="post" action="{{ url_for('threads.view', id=thread.id)}}" class="card-body"> <form method="post" action="{{ url_for('threads.view', id=thread.id)}}" class="card-body">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" /> <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<textarea class="form-control markdown" required maxlength=2000 name="comment"></textarea><br /> <textarea class="form-control markdown" required maxlength=2000 name="comment" data-enter-submit="1"></textarea><br />
<input class="btn btn-primary" type="submit" value="Comment" /> <input class="btn btn-primary" name="btn_submit" type="submit" value="Comment" />
</form> </form>
{% endif %} {% endif %}
{% if thread.private %} {% if thread.private %}

@ -11,7 +11,7 @@
<form method="POST" action="" enctype="multipart/form-data"> <form method="POST" action="" enctype="multipart/form-data">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field(form.comment, label="", class_="m-0", fieldclass="form-control markdown") }} <br /> {{ render_field(form.comment, label="", class_="m-0", fieldclass="form-control markdown", data_enter_submit="1") }} <br />
{{ render_submit_field(form.submit) }} {{ render_submit_field(form.btn_submit) }}
</form> </form>
{% endblock %} {% endblock %}

@ -29,7 +29,7 @@
<a name="reply"></a> <a name="reply"></a>
</div> </div>
<div class="card-body"> <div class="card-body">
{{ render_field(form.comment, label="", class_="m-0", fieldclass="form-control markdown") }} {{ render_field(form.comment, label="", class_="m-0", fieldclass="form-control markdown", data_enter_submit="1") }}
</div> </div>
</div> </div>
</div> </div>
@ -48,7 +48,7 @@
</p> </p>
{% endif %} {% endif %}
{{ render_submit_field(form.submit) }} {{ render_submit_field(form.btn_submit) }}
</form> </form>