2020-10-02 15:10:00 +00:00
|
|
|
import { GristDoc } from 'app/client/components/GristDoc';
|
|
|
|
import { ViewFieldRec, ViewSectionRec } from 'app/client/models/DocModel';
|
(core) Speed up and upgrade build.
Summary:
- Upgrades to build-related packages:
- Upgrade typescript, related libraries and typings.
- Upgrade webpack, eslint; add tsc-watch, node-dev, eslint_d.
- Build organization changes:
- Build webpack from original typescript, transpiling only; with errors still
reported by a background tsc watching process.
- Typescript-related changes:
- Reduce imports of AWS dependencies (very noticeable speedup)
- Avoid auto-loading global @types
- Client code is now built with isolatedModules flag (for safe transpilation)
- Use allowJs to avoid copying JS files manually.
- Linting changes
- Enhance Arcanist ESLintLinter to run before/after commands, and set up to use eslint_d
- Update eslint config, and include .eslintignore to avoid linting generated files.
- Include a bunch of eslint-prompted and eslint-generated fixes
- Add no-unused-expression rule to eslint, and fix a few warnings about it
- Other items:
- Refactor cssInput to avoid circular dependency
- Remove a bit of unused code, libraries, dependencies
Test Plan: No behavior changes, all existing tests pass. There are 30 tests fewer reported because `test_gpath.py` was removed (it's been unused for years)
Reviewers: paulfitz
Reviewed By: paulfitz
Subscribers: paulfitz
Differential Revision: https://phab.getgrist.com/D3498
2022-06-27 20:09:41 +00:00
|
|
|
import { cssInput } from 'app/client/ui/cssInput';
|
|
|
|
import { cssField, cssLabel } from 'app/client/ui/MakeCopyMenu';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { IPageWidget, toPageWidget } from 'app/client/ui/PageWidgetPicker';
|
|
|
|
import { confirmModal } from 'app/client/ui2018/modals';
|
2022-01-04 11:54:06 +00:00
|
|
|
import { BulkColValues, getColValues, RowRecord, UserAction } from 'app/common/DocActions';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { arrayRepeat } from 'app/common/gutil';
|
|
|
|
import { schema } from 'app/common/schema';
|
|
|
|
import { dom } from 'grainjs';
|
|
|
|
import cloneDeepWith = require('lodash/cloneDeepWith');
|
|
|
|
import flatten = require('lodash/flatten');
|
|
|
|
import forEach = require('lodash/forEach');
|
|
|
|
import zip = require('lodash/zip');
|
|
|
|
import zipObject = require('lodash/zipObject');
|
2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
|
|
|
|
|
|
|
const t = makeT('components.duplicatePage');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Duplicate page with pageId. Starts by prompting user for a new name.
|
|
|
|
export async function duplicatePage(gristDoc: GristDoc, pageId: number) {
|
|
|
|
const pagesTable = gristDoc.docModel.pages;
|
|
|
|
const pageName = pagesTable.rowModels[pageId].view.peek().name.peek();
|
|
|
|
let inputEl: HTMLInputElement;
|
2021-05-23 17:43:11 +00:00
|
|
|
setTimeout(() => { inputEl.focus(); inputEl.select(); }, 100);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
confirmModal('Duplicate page', 'Save', () => makeDuplicate(gristDoc, pageId, inputEl.value), (
|
|
|
|
dom('div', [
|
|
|
|
cssField(
|
|
|
|
cssLabel("Name"),
|
|
|
|
inputEl = cssInput({value: pageName + ' (copy)'}),
|
2021-09-10 03:26:43 +00:00
|
|
|
),
|
2022-10-28 16:11:08 +00:00
|
|
|
t("DoesNotCopyData"),
|
2020-10-02 15:10:00 +00:00
|
|
|
])
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function makeDuplicate(gristDoc: GristDoc, pageId: number, pageName: string = '') {
|
|
|
|
const sourceView = gristDoc.docModel.pages.rowModels[pageId].view.peek();
|
|
|
|
pageName = pageName || `${sourceView.name.peek()} (copy)`;
|
|
|
|
const viewSections = sourceView.viewSections.peek().peek();
|
|
|
|
let viewRef = 0;
|
|
|
|
await gristDoc.docData.bundleActions(
|
2022-10-28 16:11:08 +00:00
|
|
|
t("DuplicatePageName", {pageName}),
|
2020-10-02 15:10:00 +00:00
|
|
|
async () => {
|
|
|
|
// create new view and new sections
|
|
|
|
const results = await createNewViewSections(gristDoc.docData, viewSections);
|
|
|
|
viewRef = results[0].viewRef;
|
|
|
|
|
|
|
|
// give it a better name
|
|
|
|
await gristDoc.docModel.views.rowModels[viewRef].name.saveOnly(pageName);
|
|
|
|
|
|
|
|
// create a map from source to target section ids
|
|
|
|
const viewSectionIdMap = zipObject(
|
|
|
|
viewSections.map(vs => vs.getRowId()),
|
|
|
|
results.map(res => res.sectionRef)
|
|
|
|
) as {[id: number]: number};
|
|
|
|
|
|
|
|
// update the view fields
|
|
|
|
const destViewSections = viewSections.map((vs) => (
|
|
|
|
gristDoc.docModel.viewSections.rowModels[viewSectionIdMap[vs.getRowId()]]
|
|
|
|
));
|
|
|
|
const newViewFieldIds = await updateViewFields(gristDoc, destViewSections, viewSections);
|
|
|
|
|
|
|
|
// create map for mapping from a src field's id to its corresponding dest field's id
|
|
|
|
const viewFieldsIdMap = zipObject(
|
|
|
|
flatten(viewSections.map((vs) => vs.viewFields.peek().peek().map((field) => field.getRowId()))),
|
|
|
|
flatten(newViewFieldIds)) as {[id: number]: number};
|
|
|
|
|
2022-01-04 11:54:06 +00:00
|
|
|
// update layout spec
|
|
|
|
const viewLayoutSpec = patchLayoutSpec(sourceView.layoutSpecObj.peek(), viewSectionIdMap);
|
|
|
|
await Promise.all([
|
|
|
|
gristDoc.docData.sendAction(
|
|
|
|
['UpdateRecord', '_grist_Views', viewRef, { layoutSpec: JSON.stringify(viewLayoutSpec)}]
|
|
|
|
),
|
|
|
|
updateViewSections(gristDoc, destViewSections, viewSections, viewFieldsIdMap, viewSectionIdMap),
|
|
|
|
copyFilters(gristDoc, viewSections, viewSectionIdMap)
|
|
|
|
]);
|
2020-10-02 15:10:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Give copy focus
|
|
|
|
await gristDoc.openDocPage(viewRef);
|
|
|
|
}
|
|
|
|
|
2022-01-04 11:54:06 +00:00
|
|
|
/**
|
|
|
|
* Copies _grist_Filters from source sections.
|
|
|
|
*/
|
|
|
|
async function copyFilters(
|
|
|
|
gristDoc: GristDoc,
|
|
|
|
srcViewSections: ViewSectionRec[],
|
|
|
|
viewSectionMap: {[id: number]: number}) {
|
|
|
|
|
|
|
|
// Get all filters for selected sections.
|
|
|
|
const filters: RowRecord[] = [];
|
|
|
|
const table = gristDoc.docData.getMetaTable('_grist_Filters');
|
|
|
|
for (const srcViewSection of srcViewSections) {
|
|
|
|
const sectionFilters = table
|
|
|
|
.filterRecords({ viewSectionRef : srcViewSection.id.peek()})
|
|
|
|
.map(filter => ({
|
|
|
|
// Replace section ref with destination ref.
|
|
|
|
...filter, viewSectionRef : viewSectionMap[srcViewSection.id.peek()]
|
|
|
|
}));
|
|
|
|
filters.push(...sectionFilters);
|
|
|
|
}
|
|
|
|
if (filters.length) {
|
|
|
|
const filterInfo = getColValues(filters);
|
|
|
|
await gristDoc.docData.sendAction(['BulkAddRecord', '_grist_Filters',
|
|
|
|
new Array(filters.length).fill(null), filterInfo]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Update all of destViewSections with srcViewSections, use fieldsMap to patch the section layout
|
|
|
|
* (for detail/cardlist sections), use viewSectionMap to patch the sections ids for linking.
|
|
|
|
*/
|
|
|
|
async function updateViewSections(gristDoc: GristDoc, destViewSections: ViewSectionRec[],
|
|
|
|
srcViewSections: ViewSectionRec[], fieldsMap: {[id: number]: number},
|
|
|
|
viewSectionMap: {[id: number]: number}) {
|
|
|
|
|
|
|
|
// collect all the records for the src view sections
|
|
|
|
const records: RowRecord[] = [];
|
|
|
|
for (const srcViewSection of srcViewSections) {
|
|
|
|
const viewSectionLayoutSpec = patchLayoutSpec(srcViewSection.layoutSpecObj.peek(), fieldsMap);
|
2021-12-07 11:21:16 +00:00
|
|
|
const record = gristDoc.docData.getMetaTable('_grist_Views_section').getRecord(srcViewSection.getRowId())!;
|
2020-10-02 15:10:00 +00:00
|
|
|
records.push({
|
|
|
|
...record,
|
|
|
|
layoutSpec: JSON.stringify(viewSectionLayoutSpec),
|
|
|
|
linkSrcSectionRef: viewSectionMap[srcViewSection.linkSrcSectionRef.peek()],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// transpose data
|
2022-01-04 11:54:06 +00:00
|
|
|
const sectionsInfo = getColValues(records);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-01-04 11:54:06 +00:00
|
|
|
// ditch column parentId
|
2020-10-02 15:10:00 +00:00
|
|
|
delete sectionsInfo.parentId;
|
|
|
|
|
|
|
|
// send action
|
|
|
|
const rowIds = destViewSections.map((vs) => vs.getRowId());
|
|
|
|
await gristDoc.docData.sendAction(['BulkUpdateRecord', '_grist_Views_section', rowIds, sectionsInfo]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateViewFields(gristDoc: GristDoc, destViewSections: ViewSectionRec[],
|
|
|
|
srcViewSections: ViewSectionRec[]) {
|
|
|
|
const actions: UserAction[] = [];
|
|
|
|
const docData = gristDoc.docData;
|
|
|
|
|
|
|
|
// First, remove all existing fields. Needed because `CreateViewSections` adds some by default.
|
|
|
|
const toRemove = flatten(destViewSections.map((vs) => vs.viewFields.peek().peek().map((field) => field.getRowId())));
|
|
|
|
actions.push(['BulkRemoveRecord', '_grist_Views_section_field', toRemove]);
|
|
|
|
|
|
|
|
// collect all the fields to add
|
|
|
|
const fieldsToAdd: RowRecord[] = [];
|
|
|
|
for (const [destViewSection, srcViewSection] of zip(destViewSections, srcViewSections)) {
|
|
|
|
const srcViewFields: ViewFieldRec[] = srcViewSection!.viewFields.peek().peek();
|
|
|
|
const parentId = destViewSection!.getRowId();
|
|
|
|
for (const field of srcViewFields) {
|
2021-12-07 11:21:16 +00:00
|
|
|
const record = docData.getMetaTable('_grist_Views_section_field').getRecord(field.getRowId())!;
|
2020-10-02 15:10:00 +00:00
|
|
|
fieldsToAdd.push({...record, parentId});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// transpose data
|
|
|
|
const fieldsInfo = {} as BulkColValues;
|
|
|
|
forEach(schema._grist_Views_section_field, (val, key) => fieldsInfo[key] = fieldsToAdd.map(rec => rec[key]));
|
|
|
|
const rowIds = arrayRepeat(fieldsInfo.parentId.length, null);
|
|
|
|
actions.push(['BulkAddRecord', '_grist_Views_section_field', rowIds, fieldsInfo]);
|
|
|
|
|
|
|
|
const results = await gristDoc.docData.sendActions(actions);
|
|
|
|
return results[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new view containing all of the viewSections. Note that it doesn't copy view fields, for
|
|
|
|
* which you can use `updateViewFields`.
|
|
|
|
*/
|
|
|
|
async function createNewViewSections(docData: GristDoc['docData'], viewSections: ViewSectionRec[]) {
|
|
|
|
const [first, ...rest] = viewSections.map(toPageWidget);
|
|
|
|
|
|
|
|
// Passing a viewId of 0 will create a new view.
|
|
|
|
const firstResult = await docData.sendAction(newViewSectionAction(first, 0));
|
|
|
|
|
|
|
|
const otherResult = await docData.sendActions(
|
|
|
|
// other view section are added to the newly created view
|
|
|
|
rest.map((widget) => newViewSectionAction(widget, firstResult.viewRef))
|
|
|
|
);
|
|
|
|
return [firstResult, ...otherResult];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to create an action that add widget to the view with viewId.
|
|
|
|
function newViewSectionAction(widget: IPageWidget, viewId: number) {
|
2022-05-04 09:54:30 +00:00
|
|
|
return ['CreateViewSection', widget.table, viewId, widget.type, widget.summarize ? widget.columns : null, null];
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces each `leaf` id in layoutSpec by its corresponding id in mapIds. Leave unchanged if id is
|
|
|
|
* missing from mapIds.
|
|
|
|
*/
|
|
|
|
export function patchLayoutSpec(layoutSpec: any, mapIds: {[id: number]: number}) {
|
|
|
|
return cloneDeepWith(layoutSpec, (val) => {
|
|
|
|
if (typeof val === 'object') {
|
|
|
|
if (mapIds[val.leaf]) {
|
|
|
|
return {...val, leaf: mapIds[val.leaf]};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|