drafts: Convert .data("draft-id") to a module variable.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-05-07 10:15:04 -07:00 committed by Tim Abbott
parent 3cad639a82
commit 49e1c919c3
4 changed files with 12 additions and 6 deletions

View File

@ -132,7 +132,7 @@ export function clear_compose_box() {
$("textarea#compose-textarea").val("").trigger("focus"); $("textarea#compose-textarea").val("").trigger("focus");
compose_validate.check_overflow_text(); compose_validate.check_overflow_text();
compose_validate.clear_topic_resolved_warning(); compose_validate.clear_topic_resolved_warning();
$("textarea#compose-textarea").removeData("draft-id"); drafts.set_compose_draft_id(undefined);
compose_ui.autosize_textarea($("textarea#compose-textarea")); compose_ui.autosize_textarea($("textarea#compose-textarea"));
compose_banner.clear_errors(); compose_banner.clear_errors();
compose_banner.clear_warnings(); compose_banner.clear_warnings();

View File

@ -124,7 +124,7 @@ function clear_box(): void {
compose_state.set_recipient_edited_manually(false); compose_state.set_recipient_edited_manually(false);
clear_textarea(); clear_textarea();
compose_validate.check_overflow_text(); compose_validate.check_overflow_text();
$("textarea#compose-textarea").removeData("draft-id"); drafts.set_compose_draft_id(undefined);
$("textarea#compose-textarea").toggleClass("invalid", false); $("textarea#compose-textarea").toggleClass("invalid", false);
compose_ui.autosize_textarea($("textarea#compose-textarea")); compose_ui.autosize_textarea($("textarea#compose-textarea"));
compose_banner.clear_errors(); compose_banner.clear_errors();
@ -338,7 +338,7 @@ export function start(raw_opts: ComposeActionsStartOpts): void {
show_compose_box(opts); show_compose_box(opts);
if (opts.draft_id) { if (opts.draft_id) {
$("textarea#compose-textarea").data("draft-id", opts.draft_id); drafts.set_compose_draft_id(opts.draft_id);
} }
const $clear_topic_button = $("#recipient_box_clear_topic_button"); const $clear_topic_button = $("#recipient_box_clear_topic_button");

View File

@ -382,6 +382,12 @@ function maybe_notify(no_notify: boolean): void {
} }
} }
export let compose_draft_id: string | undefined;
export function set_compose_draft_id(draft_id: string | undefined): void {
compose_draft_id = draft_id;
}
type UpdateDraftOptions = { type UpdateDraftOptions = {
no_notify?: boolean; no_notify?: boolean;
update_count?: boolean; update_count?: boolean;
@ -389,8 +395,7 @@ type UpdateDraftOptions = {
}; };
export function update_draft(opts: UpdateDraftOptions = {}): string | undefined { export function update_draft(opts: UpdateDraftOptions = {}): string | undefined {
const draft_id: unknown = $("textarea#compose-textarea").data("draft-id"); const draft_id = compose_draft_id;
assert(draft_id === undefined || typeof draft_id === "string");
const old_draft = draft_id === undefined ? undefined : draft_model.getDraft(draft_id); const old_draft = draft_id === undefined ? undefined : draft_model.getDraft(draft_id);
const no_notify = opts.no_notify ?? false; const no_notify = opts.no_notify ?? false;
@ -428,7 +433,7 @@ export function update_draft(opts: UpdateDraftOptions = {}): string | undefined
// We have never saved a draft for this message, so add one. // We have never saved a draft for this message, so add one.
const update_count = opts.update_count ?? true; const update_count = opts.update_count ?? true;
const new_draft_id = draft_model.addDraft(draft, update_count); const new_draft_id = draft_model.addDraft(draft, update_count);
$("textarea#compose-textarea").data("draft-id", new_draft_id); compose_draft_id = new_draft_id;
maybe_notify(no_notify); maybe_notify(no_notify);
return new_draft_id; return new_draft_id;

View File

@ -46,6 +46,7 @@ mock_esm("../src/drafts", {
update_draft: noop, update_draft: noop,
update_compose_draft_count: noop, update_compose_draft_count: noop,
get_last_restorable_draft_based_on_compose_state: noop, get_last_restorable_draft_based_on_compose_state: noop,
set_compose_draft_id: noop,
}); });
mock_esm("../src/unread_ops", { mock_esm("../src/unread_ops", {
notify_server_message_read: noop, notify_server_message_read: noop,