2021-02-28 00:37:57 +01:00
|
|
|
import {subDays} from "date-fns";
|
|
|
|
import Handlebars from "handlebars/runtime";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-11-30 17:52:16 +01:00
|
|
|
import _ from "lodash";
|
2021-05-11 23:56:59 +02:00
|
|
|
import tippy from "tippy.js";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-11-12 11:09:12 +01:00
|
|
|
import render_confirm_delete_all_drafts from "../templates/confirm_dialog/confirm_delete_all_drafts.hbs";
|
2021-02-28 00:37:57 +01:00
|
|
|
import render_draft_table_body from "../templates/draft_table_body.hbs";
|
2020-07-28 00:26:58 +02:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-03-22 16:09:12 +01:00
|
|
|
import * as browser_history from "./browser_history";
|
2021-03-24 17:54:23 +01:00
|
|
|
import * as color_class from "./color_class";
|
2021-02-28 00:51:57 +01:00
|
|
|
import * as compose from "./compose";
|
2021-02-28 21:32:22 +01:00
|
|
|
import * as compose_actions from "./compose_actions";
|
2021-02-28 00:49:06 +01:00
|
|
|
import * as compose_fade from "./compose_fade";
|
2021-02-28 00:50:52 +01:00
|
|
|
import * as compose_state from "./compose_state";
|
2021-02-28 01:26:48 +01:00
|
|
|
import * as compose_ui from "./compose_ui";
|
2021-11-12 11:09:12 +01:00
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-02-28 00:37:57 +01:00
|
|
|
import {localstorage} from "./localstorage";
|
2021-02-28 00:49:36 +01:00
|
|
|
import * as markdown from "./markdown";
|
2021-02-28 21:31:57 +01:00
|
|
|
import * as narrow from "./narrow";
|
2021-02-28 01:03:46 +01:00
|
|
|
import * as overlays from "./overlays";
|
2021-02-28 00:37:57 +01:00
|
|
|
import * as people from "./people";
|
2021-04-15 11:11:46 +02:00
|
|
|
import * as rendered_markdown from "./rendered_markdown";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2020-06-06 23:57:20 +02:00
|
|
|
import * as sub_store from "./sub_store";
|
2021-02-28 01:14:36 +01:00
|
|
|
import * as timerender from "./timerender";
|
2021-07-04 23:17:03 +02:00
|
|
|
import * as ui_util from "./ui_util";
|
2021-02-28 00:37:57 +01:00
|
|
|
import * as util from "./util";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2020-06-08 19:49:56 +02:00
|
|
|
function set_count(count) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $drafts_li = $(".top_left_drafts");
|
|
|
|
ui_util.update_unread_count_in_dom($drafts_li, count);
|
2020-06-08 19:49:56 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export const draft_model = (function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
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() {
|
2020-12-22 11:54:49 +01:00
|
|
|
return Date.now();
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-05-19 20:32:26 +02:00
|
|
|
exports.editDraft = function (id, draft, update_timestamp = true) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const drafts = get();
|
2021-11-30 17:52:16 +01:00
|
|
|
let changed = false;
|
|
|
|
|
|
|
|
function check_if_equal(draft_a, draft_b) {
|
2021-12-01 21:12:29 +01:00
|
|
|
return _.isEqual(_.omit(draft_a, ["updatedAt"]), _.omit(draft_b, ["updatedAt"]));
|
2021-11-30 17:52:16 +01:00
|
|
|
}
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
if (drafts[id]) {
|
2021-11-30 17:52:16 +01:00
|
|
|
changed = !check_if_equal(drafts[id], draft);
|
2022-05-19 20:32:26 +02:00
|
|
|
if (update_timestamp) {
|
|
|
|
draft.updatedAt = getTimestamp();
|
|
|
|
}
|
2017-02-22 02:34:05 +01:00
|
|
|
drafts[id] = draft;
|
|
|
|
save(drafts);
|
|
|
|
}
|
2021-11-30 17:52:16 +01:00
|
|
|
return changed;
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-11-12 11:09:12 +01:00
|
|
|
export function delete_all_drafts() {
|
|
|
|
const drafts = draft_model.get();
|
|
|
|
for (const [id] of Object.entries(drafts)) {
|
|
|
|
draft_model.deleteDraft(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function confirm_delete_all_drafts() {
|
|
|
|
const html_body = render_confirm_delete_all_drafts();
|
|
|
|
|
|
|
|
confirm_dialog.launch({
|
|
|
|
html_heading: $t_html({defaultMessage: "Delete all drafts"}),
|
|
|
|
html_body,
|
|
|
|
on_click: delete_all_drafts,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:32:26 +02:00
|
|
|
export function rename_topic(stream_id, old_topic, new_topic) {
|
|
|
|
const current_drafts = draft_model.get();
|
|
|
|
for (const draft_id of Object.keys(current_drafts)) {
|
|
|
|
const draft = current_drafts[draft_id];
|
|
|
|
if (util.same_stream_and_topic(draft, {stream_id, topic: old_topic})) {
|
|
|
|
draft.topic = new_topic;
|
|
|
|
draft_model.editDraft(draft_id, draft, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function snapshot_message() {
|
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.
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-03-29 08:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
2020-06-06 23:57:20 +02:00
|
|
|
const sub = stream_data.get_sub(message.stream);
|
|
|
|
if (sub) {
|
|
|
|
message.stream_id = sub.stream_id;
|
|
|
|
}
|
2018-12-16 17:36:41 +01:00
|
|
|
message.topic = compose_state.topic();
|
2017-03-29 08:54:26 +02:00
|
|
|
}
|
|
|
|
return message;
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
2017-03-29 08:54:26 +02:00
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function restore_message(draft) {
|
2018-12-16 16:52:27 +01:00
|
|
|
// 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;
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
2018-12-16 16:52:27 +01:00
|
|
|
|
2017-12-11 17:04:42 +01:00
|
|
|
function draft_notify() {
|
2021-05-11 23:56:59 +02:00
|
|
|
// Display a tooltip to notify the user about the saved draft.
|
2021-07-05 00:06:50 +02:00
|
|
|
const instance = tippy(".top_left_drafts .unread_count", {
|
2021-05-11 23:56:59 +02:00
|
|
|
content: $t({defaultMessage: "Saved as draft"}),
|
|
|
|
arrow: true,
|
2021-07-05 00:06:50 +02:00
|
|
|
placement: "right",
|
2021-05-11 23:56:59 +02:00
|
|
|
})[0];
|
|
|
|
instance.show();
|
|
|
|
function remove_instance() {
|
|
|
|
instance.destroy();
|
|
|
|
}
|
2021-07-05 00:13:44 +02:00
|
|
|
setTimeout(remove_instance, 3000);
|
2017-12-11 17:04:42 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 19:27:11 +01:00
|
|
|
function maybe_notify(no_notify) {
|
|
|
|
if (!no_notify) {
|
|
|
|
draft_notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 02:59:08 +02:00
|
|
|
export function update_draft(opts = {}) {
|
|
|
|
const no_notify = opts.no_notify || false;
|
2021-02-28 00:37:57 +01:00
|
|
|
const draft = 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.
|
2021-06-14 11:08:54 +02:00
|
|
|
return undefined;
|
2020-04-07 20:44:53 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2021-11-30 17:52:16 +01:00
|
|
|
const changed = draft_model.editDraft(draft_id, draft);
|
|
|
|
if (changed) {
|
|
|
|
maybe_notify(no_notify);
|
|
|
|
}
|
2021-06-14 11:08:54 +02:00
|
|
|
return draft_id;
|
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);
|
2021-11-30 19:27:11 +01:00
|
|
|
maybe_notify(no_notify);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2021-06-14 11:08:54 +02:00
|
|
|
return new_draft_id;
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function restore_draft(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;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
const compose_args = restore_message(draft);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2018-12-16 16:52:27 +01:00
|
|
|
if (compose_args.type === "stream") {
|
2021-04-13 18:43:42 +02:00
|
|
|
if (draft.stream !== "" && draft.topic !== "") {
|
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();
|
2018-12-16 16:52:27 +01:00
|
|
|
compose_actions.start(compose_args.type, compose_args);
|
2020-09-04 23:49:49 +02:00
|
|
|
compose_ui.autosize_textarea($("#compose-textarea"));
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").data("draft-id", draft_id);
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
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
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function remove_old_drafts() {
|
2021-02-05 23:05:21 +01:00
|
|
|
const old_date = subDays(new Date(), DRAFT_LIFETIME).getTime();
|
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
|
|
|
}
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
2018-12-16 22:01:59 +01:00
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function format_draft(draft) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const id = draft.id;
|
|
|
|
let formatted;
|
2021-02-05 21:20:14 +01:00
|
|
|
const time = new Date(draft.updatedAt);
|
2019-11-02 00:06:25 +01:00
|
|
|
let time_stamp = timerender.render_now(time).time_str;
|
2021-04-13 06:51:54 +02:00
|
|
|
if (time_stamp === $t({defaultMessage: "Today"})) {
|
2018-12-16 22:01:59 +01:00
|
|
|
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(" ");
|
2020-10-24 21:24:21 +02:00
|
|
|
let stream_name = draft.stream.length > 0 ? draft.stream : space_string;
|
2020-06-06 23:57:20 +02:00
|
|
|
if (draft.stream_id) {
|
|
|
|
const sub = sub_store.get(draft.stream_id);
|
2020-10-24 21:24:21 +02:00
|
|
|
if (sub && sub.name !== stream_name) {
|
|
|
|
stream_name = sub.name;
|
|
|
|
draft.stream = stream_name;
|
2020-06-06 23:57:20 +02:00
|
|
|
draft_model.editDraft(id, draft);
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
let draft_topic = util.get_draft_topic(draft);
|
2020-10-24 21:24:21 +02:00
|
|
|
const draft_stream_color = stream_data.get_color(stream_name);
|
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-10-24 21:24:21 +02:00
|
|
|
stream_name,
|
2019-02-14 16:47:27 +01:00
|
|
|
stream_color: draft_stream_color,
|
2021-03-24 17:54:23 +01:00
|
|
|
dark_background: color_class.get_css_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,
|
|
|
|
);
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-12-16 22:01:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return formatted;
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
2018-12-16 22:01:59 +01:00
|
|
|
|
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() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $focused_row = row_with_focus();
|
|
|
|
return $focused_row.prev(".draft-row:visible");
|
2019-01-31 13:42:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function row_after_focus() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $focused_row = row_with_focus();
|
|
|
|
return $focused_row.next(".draft-row:visible");
|
2019-01-31 13:42:37 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function remove_draft($draft_row) {
|
2019-01-29 18:59:18 +01:00
|
|
|
// Deletes the draft and removes it from the list
|
2022-01-25 11:36:19 +01:00
|
|
|
const draft_id = $draft_row.data("draft-id");
|
2019-01-29 18:59:18 +01:00
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
draft_model.deleteDraft(draft_id);
|
2019-01-29 18:59:18 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$draft_row.remove();
|
2019-01-29 18:59:18 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function launch() {
|
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
|
|
|
|
2021-01-23 02:36:54 +01:00
|
|
|
const sorted_formatted_drafts = sorted_raw_drafts
|
2021-02-28 00:37:57 +01:00
|
|
|
.map((draft_row) => format_draft(draft_row))
|
2021-01-23 02:36:54 +01:00
|
|
|
.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
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
const $drafts_table = $("#drafts_table");
|
|
|
|
$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();
|
2021-04-15 11:11:46 +02:00
|
|
|
// Update possible dynamic elements.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $rendered_drafts = $drafts_table.find(
|
2021-04-15 11:11:46 +02:00
|
|
|
".message_content.rendered_markdown.restore-draft",
|
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
$rendered_drafts.each(function () {
|
2021-04-15 11:11:46 +02:00
|
|
|
rendered_markdown.update_elements($(this));
|
|
|
|
});
|
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
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $draft_row = $(this).closest(".draft-row");
|
|
|
|
const $draft_id = $draft_row.data("draft-id");
|
|
|
|
restore_draft($draft_id);
|
2018-12-16 20:56:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$(".draft_controls .delete-draft").on("click", function () {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $draft_row = $(this).closest(".draft-row");
|
2018-12-16 20:56:35 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
remove_draft($draft_row);
|
2018-12-16 20:56:35 +01:00
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
open_overlay();
|
|
|
|
set_initial_element(drafts);
|
2018-12-16 20:56:35 +01:00
|
|
|
setup_event_handlers();
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
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(
|
2022-01-24 09:05:06 +01:00
|
|
|
'[data-draft-id="' + draft_id_arrow.at(-1) + '"]',
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function drafts_scroll($next_focus_draft_row) {
|
|
|
|
if ($next_focus_draft_row[0] === undefined) {
|
2017-04-14 21:49:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($next_focus_draft_row[0].children[0] === undefined) {
|
2017-04-14 21:49:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-01-25 11:36:19 +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.
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($(".draft-info-box").first()[0].parentElement === $next_focus_draft_row[0]) {
|
2022-03-01 00:25:41 +01:00
|
|
|
$(".drafts-list")[0].scrollTop = 0;
|
2017-04-14 21:49:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If focused draft is the last draft, scroll to the bottom.
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($(".draft-info-box").last()[0].parentElement === $next_focus_draft_row[0]) {
|
2022-03-01 00:25:41 +01: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.
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($next_focus_draft_row.position().top < 55) {
|
2017-04-14 21:49:40 +02:00
|
|
|
// 55 is the minimum distance from the top that will require extra scrolling.
|
2022-03-01 00:25:41 +01:00
|
|
|
$(".drafts-list")[0].scrollTop -= $(".drafts-list")[0].clientHeight / 2;
|
2017-04-14 21:49:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If focused draft is cut off from the bottom, scroll down halfway in draft modal.
|
2022-01-25 11:36:19 +01:00
|
|
|
const dist_from_top = $next_focus_draft_row.position().top;
|
|
|
|
const total_dist = dist_from_top + $next_focus_draft_row[0].clientHeight;
|
2019-11-02 00:06:25 +01:00
|
|
|
const dist_from_bottom = $(".drafts-container")[0].clientHeight - total_dist;
|
2017-04-14 21:49:40 +02:00
|
|
|
if (dist_from_bottom < -4) {
|
2021-03-26 20:38:59 +01:00
|
|
|
// -4 is the min dist from the bottom that will require extra scrolling.
|
2022-03-01 00:25:41 +01:00
|
|
|
$(".drafts-list")[0].scrollTop += $(".drafts-list")[0].clientHeight / 2;
|
2017-04-14 21:49:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function drafts_handle_events(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.
|
2021-05-08 15:22:44 +02:00
|
|
|
if (event_key === "up_arrow" || event_key === "vim_up") {
|
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.
|
2021-05-08 15:22:44 +02:00
|
|
|
if (event_key === "down_arrow" || event_key === "vim_down") {
|
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
|
2020-12-22 11:26:39 +01:00
|
|
|
if ((event_key === "backspace" || event_key === "delete") && focused_draft_id !== undefined) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $draft_row = row_with_focus();
|
|
|
|
const $next_draft_row = row_after_focus();
|
|
|
|
const $prev_draft_row = row_before_focus();
|
2020-12-22 11:26:39 +01:00
|
|
|
let draft_to_be_focused_id;
|
|
|
|
|
|
|
|
// Try to get the next draft in the list and 'focus' it
|
|
|
|
// Use previous draft as a fallback
|
2022-01-25 11:36:19 +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");
|
2020-12-22 11:26:39 +01:00
|
|
|
}
|
2019-01-29 18:59:18 +01:00
|
|
|
|
2020-12-22 11:26:39 +01:00
|
|
|
const new_focus_element = document.querySelectorAll(
|
|
|
|
'[data-draft-id="' + draft_to_be_focused_id + '"]',
|
|
|
|
);
|
|
|
|
if (new_focus_element[0] !== undefined) {
|
|
|
|
activate_element(new_focus_element[0].children[0]);
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
2020-12-22 11:26:39 +01:00
|
|
|
|
2022-01-25 11:36:19 +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") {
|
2022-01-24 23:48:18 +01:00
|
|
|
if (Object.hasOwn(document.activeElement.parentElement.dataset, "draftId")) {
|
2021-02-28 00:37:57 +01:00
|
|
|
restore_draft(focused_draft_id);
|
2017-04-14 08:57:41 +02:00
|
|
|
} else {
|
2022-01-24 09:05:06 +01:00
|
|
|
const first_draft = draft_id_arrow.at(-1);
|
2021-02-28 00:37:57 +01:00
|
|
|
restore_draft(first_draft);
|
2017-04-14 08:57:41 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
2017-03-18 21:26:24 +01:00
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function open_overlay() {
|
2018-12-16 21:10:20 +01:00
|
|
|
overlays.open_overlay({
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "drafts",
|
2022-01-25 11:36:19 +01:00
|
|
|
$overlay: $("#draft_overlay"),
|
2020-07-20 22:18:43 +02:00
|
|
|
on_close() {
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.exit_overlay();
|
2018-12-16 21:10:20 +01:00
|
|
|
},
|
2017-02-22 02:34:05 +01:00
|
|
|
});
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function set_initial_element(drafts) {
|
2018-12-16 22:24:27 +01:00
|
|
|
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
|
|
|
}
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|
2018-12-16 21:10:20 +01:00
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
export function initialize() {
|
2021-07-16 21:40:28 +02:00
|
|
|
remove_old_drafts();
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
window.addEventListener("beforeunload", () => {
|
2021-02-28 00:37:57 +01:00
|
|
|
update_draft();
|
2017-02-22 02:34:05 +01:00
|
|
|
});
|
|
|
|
|
2020-06-08 19:49:56 +02:00
|
|
|
set_count(Object.keys(draft_model.get()).length);
|
|
|
|
|
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);
|
|
|
|
});
|
2021-02-28 00:37:57 +01:00
|
|
|
}
|