-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make changes required for Desktop FS updates (#1099)
Make a set of changes required for Desktop FS improvements, see gristlabs/grist-desktop#42 --------- Co-authored-by: Spoffy <contact@spoffy.net> Co-authored-by: Spoffy <4805393+Spoffy@users.noreply.github.com>
- Loading branch information
1 parent
938bb06
commit 02cfcee
Showing
29 changed files
with
552 additions
and
447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {homeImports} from 'app/client/ui/HomeImports'; | ||
import {docUrl, urlState} from 'app/client/models/gristUrlState'; | ||
import {HomeModel} from 'app/client/models/HomeModel'; | ||
import {ImportSourceElement} from 'app/client/lib/ImportSourceElement'; | ||
import {reportError} from 'app/client/models/AppModel'; | ||
|
||
export async function createDocAndOpen(home: HomeModel) { | ||
const destWS = home.newDocWorkspace.get(); | ||
if (!destWS) { return; } | ||
try { | ||
const docId = await home.createDoc("Untitled document", destWS === "unsaved" ? "unsaved" : destWS.id); | ||
// Fetch doc information including urlId. | ||
// TODO: consider changing API to return same response as a GET when creating an | ||
// object, which is a semi-standard. | ||
const doc = await home.app.api.getDoc(docId); | ||
await urlState().pushUrl(docUrl(doc)); | ||
} catch (err) { | ||
reportError(err); | ||
} | ||
} | ||
|
||
export async function importDocAndOpen(home: HomeModel) { | ||
const destWS = home.newDocWorkspace.get(); | ||
if (!destWS) { return; } | ||
const docId = await homeImports.docImport(home.app, destWS === "unsaved" ? "unsaved" : destWS.id); | ||
if (docId) { | ||
const doc = await home.app.api.getDoc(docId); | ||
await urlState().pushUrl(docUrl(doc)); | ||
} | ||
} | ||
|
||
export async function importFromPluginAndOpen(home: HomeModel, source: ImportSourceElement) { | ||
try { | ||
const destWS = home.newDocWorkspace.get(); | ||
if (!destWS) { return; } | ||
const docId = await homeImports.importFromPlugin( | ||
home.app, | ||
destWS === "unsaved" ? "unsaved" : destWS.id, | ||
source); | ||
if (docId) { | ||
const doc = await home.app.api.getDoc(docId); | ||
await urlState().pushUrl(docUrl(doc)); | ||
} | ||
} catch (err) { | ||
reportError(err); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {IProgress} from 'app/client/models/NotifyModel'; | ||
import {Disposable} from 'grainjs'; | ||
|
||
export class ImportProgress extends Disposable { | ||
// Import does upload first, then import. We show a single indicator, estimating which fraction | ||
// of the time should be given to upload (whose progress we can report well), and which to the | ||
// subsequent import (whose progress indicator is mostly faked). | ||
private _uploadFraction: number; | ||
private _estImportSeconds: number; | ||
|
||
private _importTimer: null | ReturnType<typeof setInterval> = null; | ||
private _importStart: number = 0; | ||
|
||
constructor(private _progressUI: IProgress, file: File) { | ||
super(); | ||
// We'll assume that for .grist files, the upload takes 90% of the total time, and for other | ||
// files, 40%. | ||
this._uploadFraction = file.name.endsWith(".grist") ? 0.9 : 0.4; | ||
|
||
// TODO: Import step should include a progress callback, to be combined with upload progress. | ||
// Without it, we estimate import to take 2s per MB (non-scientific unreliable estimate), and | ||
// use an asymptotic indicator which keeps moving without ever finishing. Not terribly useful, | ||
// but does slow down for larger files, and is more comforting than a stuck indicator. | ||
this._estImportSeconds = file.size / 1024 / 1024 * 2; | ||
|
||
this._progressUI.setProgress(0); | ||
this.onDispose(() => this._importTimer && clearInterval(this._importTimer)); | ||
} | ||
|
||
// Once this reaches 100, the import stage begins. | ||
public setUploadProgress(percentage: number) { | ||
this._progressUI.setProgress(percentage * this._uploadFraction); | ||
if (percentage >= 100 && !this._importTimer) { | ||
this._importStart = Date.now(); | ||
this._importTimer = setInterval(() => this._onImportTimer(), 100); | ||
} | ||
} | ||
|
||
public finish() { | ||
if (this._importTimer) { | ||
clearInterval(this._importTimer); | ||
} | ||
this._progressUI.setProgress(100); | ||
} | ||
|
||
/** | ||
* Calls _progressUI.setProgress(percent) with percentage increasing from 0 and asymptotically | ||
* approaching 100, reaching 50% after estSeconds. It's intended to look reasonable when the | ||
* estimate is good, and to keep showing slowing progress even if it's not. | ||
*/ | ||
private _onImportTimer() { | ||
const elapsedSeconds = (Date.now() - this._importStart) / 1000; | ||
const importProgress = elapsedSeconds / (elapsedSeconds + this._estImportSeconds); | ||
const progress = this._uploadFraction + importProgress * (1 - this._uploadFraction); | ||
this._progressUI.setProgress(100 * progress); | ||
} | ||
} | ||
|
Oops, something went wrong.