gristlabs_grist-core/app/client/components/UnsavedChanges.ts
Paul Fitzpatrick 20d8124f45 (core) support ?embed=true and &style=light for a clean embed experience
Summary:
This adds query parameters useful for tailoring the Grist experience, with an eye to embedding.

Setting `style=light` removes side and top bars, as a first pass at a focused view of a single document page (this would benefit from refining).

Setting `embed=true` has no significant effect just yet other than it restricts document access to viewer at most (this can be overridden by specifying `/m/default`).

Test Plan: added tests

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2585
2020-08-14 13:34:38 -04:00

49 lines
1.6 KiB
TypeScript

/**
* Module to help deal with unsaved changes when closing a page.
*/
import {Disposable} from 'grainjs';
/**
* Create an UnsavedChanges object to indicate there are UnsavedChanges. Dispose it when this is
* no longer the case. The optional callback will be called to confirm there are indeed unsaved
* changes. If omitted, it is assumed that there are.
*/
export class UnsavedChange extends Disposable {
constructor(
// If given, saveChanges() will call it to save changes.
private _saveCB?: () => Promise<void>,
// If given, it may return false to indicate that actually nothing has changed.
private _haveChanges?: () => boolean,
) {
super();
unsavedChanges.add(this);
this.onDispose(() => unsavedChanges.delete(this));
}
public haveUnsavedChanges() { return !this._haveChanges || this._haveChanges(); }
public async save(): Promise<void> { return this._saveCB && this._saveCB(); }
}
export class UnsavedChangeSet {
private _changes = new Set<UnsavedChange>();
/**
* Check if there are any unsaved changes out there.
*/
public haveUnsavedChanges(): boolean {
return Array.from(this._changes).some((c) => c.haveUnsavedChanges());
}
/**
* Save any unsaved changes out there.
*/
public async saveChanges(): Promise<void> {
await Promise.all(Array.from(this._changes).map((c) => c.save()));
}
public add(unsaved: UnsavedChange) { this._changes.add(unsaved); }
public delete(unsaved: UnsavedChange) { this._changes.delete(unsaved); }
}
// Global set of UnsavedChanges, checked on page unload.
export const unsavedChanges = new UnsavedChangeSet();