2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-29 01:37:13 +02:00
|
|
|
const Handlebars = require("handlebars/runtime");
|
2020-07-28 00:26:58 +02:00
|
|
|
const XDate = require("xdate");
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const render_draft_table_body = require("../templates/draft_table_body.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = require("./people");
|
2020-07-24 06:02:07 +02:00
|
|
|
const util = require("./util");
|
|
|
|
|
2020-06-08 19:49:56 +02:00
|
|
|
function set_count(count) {
|
|
|
|
const draft_count = count.toString();
|
2020-07-20 22:18:43 +02:00
|
|
|
const text = i18n.t("Drafts (__draft_count__)", {draft_count});
|
2020-07-02 16:17:42 +02:00
|
|
|
$(".compose_drafts_button").text(text);
|
2020-06-08 19:49:56 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_model = (function () {
|
|
|
|
const exports = {};
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
// the key that the drafts are stored under.
|
2019-11-02 00:06:25 +01:00
|
|
|
const KEY = "drafts";
|
|
|
|
const ls = localstorage();
|
2017-02-22 02:34:05 +01:00
|
|
|
ls.version = 1;
|
|
|
|
|
|
|
|
function getTimestamp() {
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get() {
|
|
|
|
return ls.get(KEY) || {};
|
|
|
|
}
|
|
|
|
exports.get = get;
|
|
|
|
|
|
|
|
exports.getDraft = function (id) {
|
|
|
|
return get()[id] || false;
|
|
|
|
};
|
|
|
|
|
|
|
|
function save(drafts) {
|
|
|
|
ls.set(KEY, drafts);
|
2020-06-08 19:49:56 +02:00
|
|
|
set_count(Object.keys(drafts).length);
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.addDraft = function (draft) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const drafts = get();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
// use the base16 of the current time + a random string to reduce
|
|
|
|
// collisions to essentially zero.
|
2019-11-02 00:06:25 +01:00
|
|
|
const id = getTimestamp().toString(16) + "-" + Math.random().toString(16).split(/\./).pop();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
draft.updatedAt = getTimestamp();
|
|
|
|
drafts[id] = draft;
|
|
|
|
save(drafts);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.editDraft = function (id, draft) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const drafts = get();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
if (drafts[id]) {
|
|
|
|
draft.updatedAt = getTimestamp();
|
|
|
|
drafts[id] = draft;
|
|
|
|
save(drafts);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.deleteDraft = function (id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const drafts = get();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
delete drafts[id];
|
|
|
|
save(drafts);
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
exports.draft_model = draft_model;
|
|
|
|
|
2017-03-29 08:54:26 +02:00
|
|
|
exports.snapshot_message = function () {
|
2018-08-21 23:04:30 +02:00
|
|
|
if (!compose_state.composing() || compose_state.message_content().length <= 2) {
|
2017-03-29 08:54:26 +02:00
|
|
|
// If you aren't in the middle of composing the body of a
|
2018-08-21 23:04:30 +02:00
|
|
|
// message or the message is shorter than 2 characters long, don't try to snapshot.
|
2017-03-29 08:54:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save what we can.
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = {
|
2017-04-24 20:35:26 +02:00
|
|
|
type: compose_state.get_message_type(),
|
2017-04-15 01:15:59 +02:00
|
|
|
content: compose_state.message_content(),
|
2017-03-29 08:54:26 +02:00
|
|
|
};
|
|
|
|
if (message.type === "private") {
|
2019-12-02 17:53:55 +01:00
|
|
|
const recipient = compose_state.private_message_recipient();
|
2017-03-29 08:54:26 +02:00
|
|
|
message.reply_to = recipient;
|
|
|
|
message.private_message_recipient = recipient;
|
|
|
|
} else {
|
2017-04-15 01:15:59 +02:00
|
|
|
message.stream = compose_state.stream_name();
|
2018-12-16 17:36:41 +01:00
|
|
|
message.topic = compose_state.topic();
|
2017-03-29 08:54:26 +02:00
|
|
|
}
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2018-12-16 16:52:27 +01:00
|
|
|
exports.restore_message = function (draft) {
|
|
|
|
// This is kinda the inverse of snapshot_message, and
|
|
|
|
// we are essentially making a deep copy of the draft,
|
|
|
|
// being explicit about which fields we send to the compose
|
|
|
|
// system.
|
2019-11-02 00:06:25 +01:00
|
|
|
let compose_args;
|
2018-12-16 16:52:27 +01:00
|
|
|
|
|
|
|
if (draft.type === "stream") {
|
|
|
|
compose_args = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-12-16 16:52:27 +01:00
|
|
|
stream: draft.stream,
|
2018-11-15 19:14:16 +01:00
|
|
|
topic: util.get_draft_topic(draft),
|
2018-12-16 16:52:27 +01:00
|
|
|
content: draft.content,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
compose_args = {
|
|
|
|
type: draft.type,
|
|
|
|
private_message_recipient: draft.private_message_recipient,
|
|
|
|
content: draft.content,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return compose_args;
|
|
|
|
};
|
|
|
|
|
2017-12-11 17:04:42 +01:00
|
|
|
function draft_notify() {
|
|
|
|
$(".alert-draft").css("display", "inline-block");
|
2020-04-07 22:13:33 +02:00
|
|
|
$(".alert-draft").delay(1000).fadeOut(500);
|
2017-12-11 17:04:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 02:34:05 +01:00
|
|
|
exports.update_draft = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft = exports.snapshot_message();
|
2020-04-07 20:44:53 +02:00
|
|
|
|
|
|
|
if (draft === undefined) {
|
|
|
|
// The user cleared the compose box, which means
|
|
|
|
// there is nothing to save here. Don't obliterate
|
|
|
|
// the existing draft yet--the user may have mistakenly
|
|
|
|
// hit delete after select-all or something.
|
|
|
|
// Just do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_id = $("#compose-textarea").data("draft-id");
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
if (draft_id !== undefined) {
|
2020-04-07 20:44:53 +02:00
|
|
|
// We don't save multiple drafts of the same message;
|
|
|
|
// just update the existing draft.
|
|
|
|
draft_model.editDraft(draft_id, draft);
|
|
|
|
draft_notify();
|
|
|
|
return;
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
2020-04-07 20:44:53 +02:00
|
|
|
|
|
|
|
// We have never saved a draft for this message, so add
|
|
|
|
// one.
|
|
|
|
const new_draft_id = draft_model.addDraft(draft);
|
|
|
|
$("#compose-textarea").data("draft-id", new_draft_id);
|
|
|
|
draft_notify();
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.delete_draft_after_send = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_id = $("#compose-textarea").data("draft-id");
|
2017-02-22 02:34:05 +01:00
|
|
|
if (draft_id) {
|
|
|
|
draft_model.deleteDraft(draft_id);
|
|
|
|
}
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").removeData("draft-id");
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.restore_draft = function (draft_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft = draft_model.getDraft(draft_id);
|
2017-02-22 02:34:05 +01:00
|
|
|
if (!draft) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const compose_args = exports.restore_message(draft);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2018-12-16 16:52:27 +01:00
|
|
|
if (compose_args.type === "stream") {
|
2017-06-24 23:36:27 +02:00
|
|
|
if (draft.stream !== "") {
|
2018-05-07 01:38:14 +02:00
|
|
|
narrow.activate(
|
|
|
|
[
|
2018-12-16 16:52:27 +01:00
|
|
|
{operator: "stream", operand: compose_args.stream},
|
2018-11-15 19:14:16 +01:00
|
|
|
{operator: "topic", operand: compose_args.topic},
|
2018-05-07 01:38:14 +02:00
|
|
|
],
|
2020-07-02 02:16:03 +02:00
|
|
|
{trigger: "restore draft"},
|
2018-12-16 16:52:27 +01:00
|
|
|
);
|
2017-06-24 23:36:27 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-16 16:52:27 +01:00
|
|
|
if (compose_args.private_message_recipient !== "") {
|
|
|
|
narrow.activate(
|
2020-07-15 00:34:28 +02:00
|
|
|
[{operator: "pm-with", operand: compose_args.private_message_recipient}],
|
2020-07-02 02:16:03 +02:00
|
|
|
{trigger: "restore draft"},
|
2018-12-16 16:52:27 +01:00
|
|
|
);
|
2017-06-24 23:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
overlays.close_overlay("drafts");
|
2017-02-22 02:34:05 +01:00
|
|
|
compose_fade.clear_compose();
|
2017-12-19 03:34:55 +01:00
|
|
|
compose.clear_preview_area();
|
|
|
|
|
2017-02-22 02:34:05 +01:00
|
|
|
if (draft.type === "stream" && draft.stream === "") {
|
2018-11-15 19:14:16 +01:00
|
|
|
compose_args.topic = "";
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
2018-12-16 16:52:27 +01:00
|
|
|
compose_actions.start(compose_args.type, compose_args);
|
2017-04-23 08:51:26 +02:00
|
|
|
compose_ui.autosize_textarea();
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").data("draft-id", draft_id);
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const DRAFT_LIFETIME = 30;
|
2018-02-15 17:38:31 +01:00
|
|
|
|
2019-02-20 15:45:17 +01:00
|
|
|
exports.remove_old_drafts = function () {
|
2020-07-16 23:29:01 +02:00
|
|
|
const old_date = new Date().setDate(new Date().getDate() - DRAFT_LIFETIME);
|
2019-11-02 00:06:25 +01:00
|
|
|
const drafts = draft_model.get();
|
2020-02-06 04:25:39 +01:00
|
|
|
for (const [id, draft] of Object.entries(drafts)) {
|
2018-02-15 17:38:31 +01:00
|
|
|
if (draft.updatedAt < old_date) {
|
|
|
|
draft_model.deleteDraft(id);
|
|
|
|
}
|
2020-02-06 04:25:39 +01:00
|
|
|
}
|
2019-02-20 15:45:17 +01:00
|
|
|
};
|
2018-12-16 22:01:59 +01:00
|
|
|
|
|
|
|
exports.format_draft = function (draft) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const id = draft.id;
|
|
|
|
let formatted;
|
|
|
|
const time = new XDate(draft.updatedAt);
|
|
|
|
let time_stamp = timerender.render_now(time).time_str;
|
2018-12-16 22:01:59 +01:00
|
|
|
if (time_stamp === i18n.t("Today")) {
|
|
|
|
time_stamp = timerender.stringify_time(time);
|
|
|
|
}
|
|
|
|
if (draft.type === "stream") {
|
|
|
|
// In case there is no stream for the draft, we need a
|
|
|
|
// single space char for proper rendering of the stream label
|
2019-11-02 00:06:25 +01:00
|
|
|
const space_string = new Handlebars.SafeString(" ");
|
|
|
|
const stream = draft.stream.length > 0 ? draft.stream : space_string;
|
|
|
|
let draft_topic = util.get_draft_topic(draft);
|
|
|
|
const draft_stream_color = stream_data.get_color(draft.stream);
|
2018-12-16 22:01:59 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (draft_topic === "") {
|
2018-12-16 22:01:59 +01:00
|
|
|
draft_topic = compose.empty_topic_placeholder();
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted = {
|
|
|
|
draft_id: draft.id,
|
|
|
|
is_stream: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
stream,
|
2019-02-14 16:47:27 +01:00
|
|
|
stream_color: draft_stream_color,
|
|
|
|
dark_background: stream_color.get_color_class(draft_stream_color),
|
2018-12-16 22:01:59 +01:00
|
|
|
topic: draft_topic,
|
|
|
|
raw_content: draft.content,
|
2020-07-20 22:18:43 +02:00
|
|
|
time_stamp,
|
2018-12-16 22:01:59 +01:00
|
|
|
};
|
|
|
|
} else {
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = util.extract_pm_recipients(draft.private_message_recipient);
|
2020-07-15 00:34:28 +02:00
|
|
|
const recipients = emails
|
|
|
|
.map((email) => {
|
|
|
|
email = email.trim();
|
|
|
|
const person = people.get_by_email(email);
|
|
|
|
if (person !== undefined) {
|
|
|
|
return person.full_name;
|
|
|
|
}
|
|
|
|
return email;
|
|
|
|
})
|
|
|
|
.join(", ");
|
2018-12-16 22:01:59 +01:00
|
|
|
|
|
|
|
formatted = {
|
|
|
|
draft_id: draft.id,
|
|
|
|
is_stream: false,
|
2020-07-20 22:18:43 +02:00
|
|
|
recipients,
|
2018-12-16 22:01:59 +01:00
|
|
|
raw_content: draft.content,
|
2020-07-20 22:18:43 +02:00
|
|
|
time_stamp,
|
2018-12-16 22:01:59 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
markdown.apply_markdown(formatted);
|
|
|
|
} catch (error) {
|
|
|
|
// In the unlikely event that there is syntax in the
|
2020-08-11 01:47:49 +02:00
|
|
|
// draft content which our Markdown processor is
|
2018-12-16 22:01:59 +01:00
|
|
|
// unable to process, we delete the draft, so that the
|
|
|
|
// drafts overlay can be opened without any errors.
|
|
|
|
// We also report the exception to the server so that
|
|
|
|
// the bug can be fixed.
|
|
|
|
draft_model.deleteDraft(id);
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.error(
|
|
|
|
"Error in rendering draft.",
|
|
|
|
{
|
|
|
|
draft_content: draft.content,
|
|
|
|
},
|
|
|
|
error.stack,
|
|
|
|
);
|
2018-12-16 22:01:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
};
|
|
|
|
|
2019-01-31 13:42:37 +01:00
|
|
|
function row_with_focus() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const focused_draft = $(".draft-info-box:focus")[0];
|
2019-01-31 13:42:37 +01:00
|
|
|
return $(focused_draft).parent(".draft-row");
|
|
|
|
}
|
|
|
|
|
|
|
|
function row_before_focus() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const focused_row = row_with_focus();
|
2019-01-31 13:42:37 +01:00
|
|
|
return focused_row.prev(".draft-row:visible");
|
|
|
|
}
|
|
|
|
|
|
|
|
function row_after_focus() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const focused_row = row_with_focus();
|
2019-01-31 13:42:37 +01:00
|
|
|
return focused_row.next(".draft-row:visible");
|
|
|
|
}
|
|
|
|
|
2019-01-29 18:59:18 +01:00
|
|
|
function remove_draft(draft_row) {
|
|
|
|
// Deletes the draft and removes it from the list
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_id = draft_row.data("draft-id");
|
2019-01-29 18:59:18 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.draft_model.deleteDraft(draft_id);
|
2019-01-29 18:59:18 +01:00
|
|
|
|
|
|
|
draft_row.remove();
|
|
|
|
|
|
|
|
if ($("#drafts_table .draft-row").length === 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#drafts_table .no-drafts").show();
|
2019-01-29 18:59:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 21:10:20 +01:00
|
|
|
exports.launch = function () {
|
2017-02-22 02:34:05 +01:00
|
|
|
function format_drafts(data) {
|
2020-02-06 04:25:39 +01:00
|
|
|
for (const [id, draft] of Object.entries(data)) {
|
2018-12-16 21:15:32 +01:00
|
|
|
draft.id = id;
|
2020-02-06 04:25:39 +01:00
|
|
|
}
|
2018-12-16 21:15:32 +01:00
|
|
|
|
2020-02-09 04:59:25 +01:00
|
|
|
const unsorted_raw_drafts = Object.values(data);
|
2018-12-16 21:15:32 +01:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
const sorted_raw_drafts = unsorted_raw_drafts.sort(
|
2020-07-02 02:16:03 +02:00
|
|
|
(draft_a, draft_b) => draft_b.updatedAt - draft_a.updatedAt,
|
2020-07-02 01:45:54 +02:00
|
|
|
);
|
2018-12-16 21:15:32 +01:00
|
|
|
|
2020-02-08 03:33:46 +01:00
|
|
|
const sorted_formatted_drafts = sorted_raw_drafts.map(exports.format_draft).filter(Boolean);
|
2018-12-16 21:15:32 +01:00
|
|
|
|
|
|
|
return sorted_formatted_drafts;
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 22:11:17 +01:00
|
|
|
function render_widgets(drafts) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#drafts_table").empty();
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered = render_draft_table_body({
|
2020-07-20 22:18:43 +02:00
|
|
|
drafts,
|
2018-05-06 21:43:17 +02:00
|
|
|
draft_lifetime: DRAFT_LIFETIME,
|
2018-02-15 17:38:31 +01:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#drafts_table").append(rendered);
|
2017-02-22 02:34:05 +01:00
|
|
|
if ($("#drafts_table .draft-row").length > 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#drafts_table .no-drafts").hide();
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
2018-12-16 20:56:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setup_event_handlers() {
|
|
|
|
$(".restore-draft").on("click", function (e) {
|
2020-04-10 13:57:52 +02:00
|
|
|
if (document.getSelection().type === "Range") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:56:35 +01:00
|
|
|
e.stopPropagation();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_row = $(this).closest(".draft-row");
|
|
|
|
const draft_id = draft_row.data("draft-id");
|
2018-12-16 20:56:35 +01:00
|
|
|
exports.restore_draft(draft_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".draft_controls .delete-draft").on("click", function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_row = $(this).closest(".draft-row");
|
2018-12-16 20:56:35 +01:00
|
|
|
|
2019-01-29 18:59:18 +01:00
|
|
|
remove_draft(draft_row);
|
2018-12-16 20:56:35 +01:00
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 15:45:17 +01:00
|
|
|
exports.remove_old_drafts();
|
2019-11-02 00:06:25 +01:00
|
|
|
const drafts = format_drafts(draft_model.get());
|
2018-12-16 22:11:17 +01:00
|
|
|
render_widgets(drafts);
|
2019-03-08 04:12:34 +01:00
|
|
|
|
|
|
|
// We need to force a style calculation on the newly created
|
|
|
|
// element in order for the CSS transition to take effect.
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#draft_overlay").css("opacity");
|
2019-03-08 04:12:34 +01:00
|
|
|
|
2020-05-09 16:11:34 +02:00
|
|
|
exports.open_overlay();
|
2018-12-16 22:24:27 +01:00
|
|
|
exports.set_initial_element(drafts);
|
2018-12-16 20:56:35 +01:00
|
|
|
setup_event_handlers();
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
|
2018-12-16 20:17:17 +01:00
|
|
|
function activate_element(elem) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".draft-info-box").removeClass("active");
|
|
|
|
$(elem).expectOne().addClass("active");
|
2020-07-22 04:21:43 +02:00
|
|
|
elem.focus();
|
2018-12-16 20:17:17 +01:00
|
|
|
}
|
|
|
|
|
2017-04-14 21:12:52 +02:00
|
|
|
function drafts_initialize_focus(event_name) {
|
|
|
|
// If a draft is not focused in draft modal, then focus the last draft
|
|
|
|
// if up_arrow is clicked or the first draft if down_arrow is clicked.
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
(event_name !== "up_arrow" && event_name !== "down_arrow") ||
|
|
|
|
$(".draft-info-box:focus")[0]
|
|
|
|
) {
|
2017-04-14 21:12:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_arrow = draft_model.get();
|
|
|
|
const draft_id_arrow = Object.getOwnPropertyNames(draft_arrow);
|
2020-07-15 00:34:28 +02:00
|
|
|
if (draft_id_arrow.length === 0) {
|
|
|
|
// empty drafts modal
|
2017-04-14 21:12:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let draft_element;
|
2017-04-14 21:12:52 +02:00
|
|
|
if (event_name === "up_arrow") {
|
2020-07-15 00:34:28 +02:00
|
|
|
draft_element = document.querySelectorAll(
|
|
|
|
'[data-draft-id="' + draft_id_arrow[draft_id_arrow.length - 1] + '"]',
|
|
|
|
);
|
2017-04-14 21:12:52 +02:00
|
|
|
} else if (event_name === "down_arrow") {
|
|
|
|
draft_element = document.querySelectorAll('[data-draft-id="' + draft_id_arrow[0] + '"]');
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const focus_element = draft_element[0].children[0];
|
2018-12-16 20:17:17 +01:00
|
|
|
|
|
|
|
activate_element(focus_element);
|
2017-04-14 21:12:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 21:49:40 +02:00
|
|
|
function drafts_scroll(next_focus_draft_row) {
|
|
|
|
if (next_focus_draft_row[0] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (next_focus_draft_row[0].children[0] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-16 20:17:17 +01:00
|
|
|
activate_element(next_focus_draft_row[0].children[0]);
|
2017-04-14 21:49:40 +02:00
|
|
|
|
|
|
|
// If focused draft is first draft, scroll to the top.
|
2019-06-06 05:50:36 +02:00
|
|
|
if ($(".draft-info-box").first()[0].parentElement === next_focus_draft_row[0]) {
|
2017-04-14 21:49:40 +02:00
|
|
|
$(".drafts-list")[0].scrollTop = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If focused draft is the last draft, scroll to the bottom.
|
2019-06-06 05:50:36 +02:00
|
|
|
if ($(".draft-info-box").last()[0].parentElement === next_focus_draft_row[0]) {
|
2020-07-15 00:34:28 +02:00
|
|
|
$(".drafts-list")[0].scrollTop =
|
|
|
|
$(".drafts-list")[0].scrollHeight - $(".drafts-list").height();
|
2017-04-14 21:49:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If focused draft is cut off from the top, scroll up halfway in draft modal.
|
|
|
|
if (next_focus_draft_row.position().top < 55) {
|
|
|
|
// 55 is the minimum distance from the top that will require extra scrolling.
|
|
|
|
$(".drafts-list")[0].scrollTop -= $(".drafts-list")[0].clientHeight / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If focused draft is cut off from the bottom, scroll down halfway in draft modal.
|
2019-11-02 00:06:25 +01:00
|
|
|
const dist_from_top = next_focus_draft_row.position().top;
|
|
|
|
const total_dist = dist_from_top + next_focus_draft_row[0].clientHeight;
|
|
|
|
const dist_from_bottom = $(".drafts-container")[0].clientHeight - total_dist;
|
2017-04-14 21:49:40 +02:00
|
|
|
if (dist_from_bottom < -4) {
|
|
|
|
//-4 is the min dist from the bottom that will require extra scrolling.
|
|
|
|
$(".drafts-list")[0].scrollTop += $(".drafts-list")[0].clientHeight / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:26:24 +01:00
|
|
|
exports.drafts_handle_events = function (e, event_key) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_arrow = draft_model.get();
|
|
|
|
const draft_id_arrow = Object.getOwnPropertyNames(draft_arrow);
|
2017-04-14 21:12:52 +02:00
|
|
|
drafts_initialize_focus(event_key);
|
2017-04-14 08:57:41 +02:00
|
|
|
|
2017-03-18 21:26:24 +01:00
|
|
|
// This detects up arrow key presses when the draft overlay
|
|
|
|
// is open and scrolls through the drafts.
|
|
|
|
if (event_key === "up_arrow") {
|
2019-01-31 13:42:37 +01:00
|
|
|
drafts_scroll(row_before_focus());
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
2017-04-04 20:48:08 +02:00
|
|
|
|
2017-03-18 21:26:24 +01:00
|
|
|
// This detects down arrow key presses when the draft overlay
|
|
|
|
// is open and scrolls through the drafts.
|
|
|
|
if (event_key === "down_arrow") {
|
2019-01-31 13:42:37 +01:00
|
|
|
drafts_scroll(row_after_focus());
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
2017-04-04 20:48:08 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const focused_draft_id = row_with_focus().data("draft-id");
|
2020-08-11 02:09:14 +02:00
|
|
|
// Allows user to delete drafts with Backspace
|
2018-03-07 21:27:55 +01:00
|
|
|
if (event_key === "backspace" || event_key === "delete") {
|
2019-01-31 13:42:37 +01:00
|
|
|
if (focused_draft_id !== undefined) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_row = row_with_focus();
|
|
|
|
const next_draft_row = row_after_focus();
|
|
|
|
const prev_draft_row = row_before_focus();
|
|
|
|
let draft_to_be_focused_id;
|
2019-01-29 18:59:18 +01:00
|
|
|
|
|
|
|
// Try to get the next draft in the list and 'focus' it
|
|
|
|
// Use previous draft as a fallback
|
2019-01-31 13:42:37 +01:00
|
|
|
if (next_draft_row[0] !== undefined) {
|
|
|
|
draft_to_be_focused_id = next_draft_row.data("draft-id");
|
|
|
|
} else if (prev_draft_row[0] !== undefined) {
|
|
|
|
draft_to_be_focused_id = prev_draft_row.data("draft-id");
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
2019-01-29 18:59:18 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const new_focus_element = document.querySelectorAll(
|
|
|
|
'[data-draft-id="' + draft_to_be_focused_id + '"]',
|
|
|
|
);
|
2017-03-18 21:26:24 +01:00
|
|
|
if (new_focus_element[0] !== undefined) {
|
2018-12-16 20:17:17 +01:00
|
|
|
activate_element(new_focus_element[0].children[0]);
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
2019-01-29 18:59:18 +01:00
|
|
|
|
|
|
|
remove_draft(draft_row);
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-14 08:57:41 +02:00
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
// This handles when pressing Enter while looking at drafts.
|
2017-04-14 08:57:41 +02:00
|
|
|
// It restores draft that is focused.
|
|
|
|
if (event_key === "enter") {
|
|
|
|
if (document.activeElement.parentElement.hasAttribute("data-draft-id")) {
|
2019-01-31 13:42:37 +01:00
|
|
|
exports.restore_draft(focused_draft_id);
|
2017-04-14 08:57:41 +02:00
|
|
|
} else {
|
2019-11-02 00:06:25 +01:00
|
|
|
const first_draft = draft_id_arrow[draft_id_arrow.length - 1];
|
2017-04-14 08:57:41 +02:00
|
|
|
exports.restore_draft(first_draft);
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 21:26:24 +01:00
|
|
|
};
|
|
|
|
|
2020-05-09 16:11:34 +02:00
|
|
|
exports.open_overlay = function () {
|
2018-12-16 21:10:20 +01:00
|
|
|
overlays.open_overlay({
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "drafts",
|
|
|
|
overlay: $("#draft_overlay"),
|
2020-07-20 22:18:43 +02:00
|
|
|
on_close() {
|
2018-12-16 21:10:20 +01:00
|
|
|
hashchange.exit_overlay();
|
|
|
|
},
|
2017-02-22 02:34:05 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-16 22:24:27 +01:00
|
|
|
exports.set_initial_element = function (drafts) {
|
|
|
|
if (drafts.length > 0) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const curr_draft_id = drafts[0].draft_id;
|
|
|
|
const selector = '[data-draft-id="' + curr_draft_id + '"]';
|
|
|
|
const curr_draft_element = document.querySelectorAll(selector);
|
|
|
|
const focus_element = curr_draft_element[0].children[0];
|
2018-12-16 21:10:20 +01:00
|
|
|
activate_element(focus_element);
|
2018-12-16 22:56:35 +01:00
|
|
|
$(".drafts-list")[0].scrollTop = 0;
|
2018-12-16 21:10:20 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-02 23:12:15 +02:00
|
|
|
exports.initialize = function () {
|
2020-07-02 01:45:54 +02:00
|
|
|
window.addEventListener("beforeunload", () => {
|
2017-02-22 02:34:05 +01:00
|
|
|
exports.update_draft();
|
|
|
|
});
|
|
|
|
|
2020-06-08 19:49:56 +02:00
|
|
|
set_count(Object.keys(draft_model.get()).length);
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$("#compose-textarea").on("focusout", exports.update_draft);
|
2018-12-16 20:17:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("body").on("focus", ".draft-info-box", (e) => {
|
2018-12-16 20:17:17 +01:00
|
|
|
activate_element(e.target);
|
|
|
|
});
|
2017-06-02 23:12:15 +02:00
|
|
|
};
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.drafts = exports;
|