2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {createSessionObs} from 'app/client/lib/sessionObs';
|
|
|
|
import {DocPageModel} from 'app/client/models/DocPageModel';
|
|
|
|
import {reportError} from 'app/client/models/errors';
|
|
|
|
import {urlState} from 'app/client/models/gristUrlState';
|
|
|
|
import {getTimeFromNow} from 'app/client/models/HomeModel';
|
|
|
|
import {buildConfigContainer} from 'app/client/ui/RightPanel';
|
|
|
|
import {buttonSelect} from 'app/client/ui2018/buttonSelect';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {testId, theme, vars} from 'app/client/ui2018/cssVars';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
2020-12-09 05:59:42 +00:00
|
|
|
import {menu, menuAnnotate, menuItemLink} from 'app/client/ui2018/menus';
|
|
|
|
import {buildUrlId, parseUrlId} from 'app/common/gristUrls';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {StringUnion} from 'app/common/StringUnion';
|
|
|
|
import {DocSnapshot} from 'app/common/UserAPI';
|
|
|
|
import {Disposable, dom, IDomComponent, MultiHolder, Observable, styled} from 'grainjs';
|
2022-07-04 14:14:55 +00:00
|
|
|
import moment from 'moment';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('DocHistory');
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const DocHistorySubTab = StringUnion("activity", "snapshots");
|
|
|
|
|
|
|
|
export class DocHistory extends Disposable implements IDomComponent {
|
2020-12-09 05:59:42 +00:00
|
|
|
private _subTab = createSessionObs(this, "docHistorySubTab", "snapshots", DocHistorySubTab.guard);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
constructor(private _docPageModel: DocPageModel, private _actionLog: IDomComponent) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom() {
|
|
|
|
const tabs = [
|
2022-10-28 16:11:08 +00:00
|
|
|
{value: 'activity', label: t('Activity')},
|
|
|
|
{value: 'snapshots', label: t('Snapshots')},
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
return [
|
|
|
|
cssSubTabs(
|
|
|
|
buttonSelect(this._subTab, tabs, {}, testId('doc-history-tabs')),
|
|
|
|
),
|
|
|
|
dom.domComputed(this._subTab, (subTab) =>
|
|
|
|
buildConfigContainer(
|
|
|
|
subTab === 'activity' ? this._actionLog.buildDom() :
|
|
|
|
subTab === 'snapshots' ? dom.create(this._buildSnapshots.bind(this)) :
|
|
|
|
null
|
|
|
|
)
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private _buildSnapshots(owner: MultiHolder) {
|
|
|
|
// Fetch snapshots, and render.
|
|
|
|
const doc = this._docPageModel.currentDoc.get();
|
|
|
|
if (!doc) { return null; }
|
|
|
|
|
2020-12-09 05:59:42 +00:00
|
|
|
// origUrlId is the snapshot-less URL, which we use to fetch snapshot history, and for
|
|
|
|
// snapshot comparisons.
|
|
|
|
const origUrlId = buildUrlId({...doc.idParts, snapshotId: undefined});
|
|
|
|
|
|
|
|
// If comparing one snapshot to another, get the other ID, so that we can highlight it too.
|
|
|
|
const compareUrlId = urlState().state.get().params?.compare;
|
|
|
|
const compareSnapshotId = compareUrlId && parseUrlId(compareUrlId).snapshotId;
|
|
|
|
|
|
|
|
// Helper to set a link to open a snapshot, optionally comparing it with a docId.
|
|
|
|
// We include urlState().state to preserve the currently selected page.
|
|
|
|
function setLink(snapshot: DocSnapshot, compareDocId?: string) {
|
|
|
|
return dom.attr('href', (use) => urlState().makeUrl({
|
2020-12-09 22:45:28 +00:00
|
|
|
...use(urlState().state), doc: snapshot.docId,
|
|
|
|
params: (compareDocId ? {compare: compareDocId} : {})
|
|
|
|
}));
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const snapshots = Observable.create<DocSnapshot[]>(owner, []);
|
|
|
|
const userApi = this._docPageModel.appModel.api;
|
2020-12-09 05:59:42 +00:00
|
|
|
const docApi = userApi.getDocAPI(origUrlId);
|
2020-12-11 19:04:06 +00:00
|
|
|
docApi.getSnapshots().then(result =>
|
|
|
|
snapshots.isDisposed() || snapshots.set(result.snapshots)).catch(reportError);
|
2020-10-02 15:10:00 +00:00
|
|
|
return dom('div',
|
2020-12-09 05:59:42 +00:00
|
|
|
// Note that most recent snapshots are first.
|
|
|
|
dom.domComputed(snapshots, (snapshotList) => snapshotList.map((snapshot, index) => {
|
2020-10-02 15:10:00 +00:00
|
|
|
const modified = moment(snapshot.lastModified);
|
2020-12-09 05:59:42 +00:00
|
|
|
const prevSnapshot = snapshotList[index + 1] || null;
|
2020-10-02 15:10:00 +00:00
|
|
|
return cssSnapshot(
|
|
|
|
cssSnapshotTime(getTimeFromNow(snapshot.lastModified)),
|
|
|
|
cssSnapshotCard(
|
2020-12-09 05:59:42 +00:00
|
|
|
cssSnapshotCard.cls('-current', Boolean(
|
|
|
|
snapshot.snapshotId === doc.idParts.snapshotId ||
|
|
|
|
(compareSnapshotId && snapshot.snapshotId === compareSnapshotId)
|
|
|
|
)),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom('div',
|
|
|
|
cssDatePart(modified.format('ddd ll')), ' ',
|
|
|
|
cssDatePart(modified.format('LT'))
|
|
|
|
),
|
|
|
|
cssMenuDots(icon('Dots'),
|
2020-12-09 05:59:42 +00:00
|
|
|
menu(() => [
|
2022-10-28 16:11:08 +00:00
|
|
|
menuItemLink(setLink(snapshot), t('OpenSnapshot')),
|
|
|
|
menuItemLink(setLink(snapshot, origUrlId), t('CompareToCurrent'),
|
|
|
|
menuAnnotate(t('Beta'))),
|
|
|
|
prevSnapshot && menuItemLink(setLink(prevSnapshot, snapshot.docId), t('CompareToPrevious'),
|
|
|
|
menuAnnotate(t('Beta'))),
|
2020-12-09 05:59:42 +00:00
|
|
|
],
|
|
|
|
{placement: 'bottom-end', parentSelectorToMark: '.' + cssSnapshotCard.className}
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('doc-history-snapshot-menu'),
|
|
|
|
),
|
2020-12-09 05:59:42 +00:00
|
|
|
testId('doc-history-card'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
testId('doc-history-snapshot'),
|
|
|
|
);
|
2020-12-09 05:59:42 +00:00
|
|
|
})),
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssSubTabs = styled('div', `
|
|
|
|
padding: 16px;
|
2022-09-06 01:51:57 +00:00
|
|
|
border-bottom: 1px solid ${theme.pagePanelsBorder};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSnapshot = styled('div', `
|
|
|
|
margin: 8px 16px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSnapshotTime = styled('div', `
|
|
|
|
text-align: right;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.lightText};
|
2020-10-02 15:10:00 +00:00
|
|
|
font-size: ${vars.smallFontSize};
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSnapshotCard = styled('div', `
|
2022-09-06 01:51:57 +00:00
|
|
|
border: 1px solid ${theme.documentHistorySnapshotBorder};
|
2020-10-02 15:10:00 +00:00
|
|
|
padding: 8px;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.documentHistorySnapshotFg};
|
|
|
|
background: ${theme.documentHistorySnapshotBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
border-radius: 8px;
|
|
|
|
overflow: hidden;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.controlSecondaryFg};
|
2020-12-09 05:59:42 +00:00
|
|
|
|
|
|
|
&-current {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.documentHistorySnapshotSelectedBg};
|
|
|
|
color: ${theme.documentHistorySnapshotSelectedFg};
|
|
|
|
--icon-color: ${theme.documentHistorySnapshotSelectedFg};
|
2020-12-09 05:59:42 +00:00
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssDatePart = styled('span', `
|
|
|
|
display: inline-block;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenuDots = styled('div', `
|
|
|
|
flex: none;
|
|
|
|
margin: 0 4px 0 auto;
|
|
|
|
height: 24px;
|
|
|
|
width: 24px;
|
|
|
|
padding: 4px;
|
|
|
|
line-height: 0px;
|
|
|
|
border-radius: 3px;
|
|
|
|
cursor: default;
|
|
|
|
&:hover, &.weasel-popup-open {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.hover};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|