mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
20d8124f45
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
49 lines
1.6 KiB
TypeScript
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();
|