Similar to above: it'd be nice to reuse this code.  Could this while block be replaced by a call to `handleFileSave({doc: doc})`?