Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose: Add autosave for drafts while typing.
CerealNotFoundac190151ec7f
diff --git a/web/src/compose_setup.ts b/web/src/compose_setup.tsindex 77e1dac2b88..d196dcc2579 100644--- a/web/src/compose_setup.ts+++ b/web/src/compose_setup.ts@@ -48,6 +48,8 @@ import * as user_topics from "./user_topics.ts";import * as util from "./util.ts";import * as widget_modal from "./widget_modal.ts";+const AUTOSAVE_DRAFT_THROTTLE_IN_MILLISECONDS = 60 * 1000;+export function abort_xhr(): void {upload.compose_upload_cancel();}@@ -63,6 +65,30 @@ function setup_compose_actions_hooks(): void {});}+// This autosave callback does not track the compose_draft_id that was+// active when scheduled; it simply calls update_draft() on whatever+// compose state is current when it fires, as long as compose is still+// open.+//+// This is safe because compose context transitions (send, close,+// switching/restoring drafts) already perform explicit draft saves, and+// compose_draft_id is only changed through those controlled flows.+//+// At worst, a delayed call results in an extra save of the current draft,+// which is harmless. Adding draft_id tracking here would introduce+// significant complexity without meaningful benefit.+export function update_draft_if_composing(): void {+ if (compose_state.composing()) {+ drafts.update_draft({no_notify: true});+ }+}++const throttled_update_draft = _.throttle(+ update_draft_if_composing,+ AUTOSAVE_DRAFT_THROTTLE_IN_MILLISECONDS,+ {leading: false, trailing: true},+);+export function initialize(): void {// Register hooks for compose_actions.setup_compose_actions_hooks();@@ -125,6 +151,9 @@ export function initialize(): void {if (compose_state.get_is_content_unedited_restored_draft()) {compose_state.set_is_content_unedited_restored_draft(false);}++ // We occasionally update the saved draft while the user is typing to minimize data loss risk.+ throttled_update_draft();}, 25),);diff --git a/web/tests/compose.test.cjs b/web/tests/compose.test.cjsindex 0416c71dfca..777ceee1624 100644--- a/web/tests/compose.test.cjs+++ b/web/tests/compose.test.cjs@@ -616,6 +616,28 @@ test_ui("initialize", ({override}) => {})();});+test_ui("update_draft_if_composing", ({override_rewire}) => {+ let update_draft_call_count = 0;+ override_rewire(drafts, "update_draft", (opts) => {+ assert.deepEqual(opts, {no_notify: true});+ update_draft_call_count += 1;+ return "draft-id";+ });++ // Autosave does nothing once the compose box has closed, so a+ // delayed call can't resurrect a draft the user is done with.+ compose_state.set_message_type(undefined);+ assert.ok(!compose_state.composing());+ compose_setup.update_draft_if_composing();+ assert.equal(update_draft_call_count, 0);++ // While the user is still composing, autosave persists the draft.+ compose_state.set_message_type("stream");+ assert.ok(compose_state.composing());+ compose_setup.update_draft_if_composing();+ assert.equal(update_draft_call_count, 1);+});+test_ui("update_fade", ({override, override_rewire}) => {mock_banners();initialize_handlers({override});