Allow Restricted Notebooks to export text (#6542)

* Refactor string to stream

* add to restricted notebooks and allow for blank users

* forgot to add notebook

* use better types and fix test

* move streamToString

* add export group

* catch blank pages
This commit is contained in:
Scott Bell 2023-04-01 07:08:22 +02:00 committed by GitHub
parent 4f10a93ef5
commit b7a671d392
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 69 additions and 24 deletions

View File

@ -170,5 +170,6 @@ exports.test = base.test.extend({
}
}
});
exports.expect = expect;
exports.waitForAnimations = waitForAnimations;

View File

@ -150,3 +150,17 @@ exports.test = test.extend({
}
});
exports.expect = expect;
/**
* Takes a readable stream and returns a string.
* @param {ReadableStream} readable - the readable stream
* @return {Promise<String>} the stringified stream
*/
exports.streamToString = async function (readable) {
let result = '';
for await (const chunk of readable) {
result += chunk;
}
return result;
};

View File

@ -24,7 +24,7 @@
This test suite is dedicated to tests which verify the basic operations surrounding Notebooks.
*/
const { test, expect } = require('../../../../pluginFixtures');
const { test, expect, streamToString } = require('../../../../pluginFixtures');
const { createDomainObjectWithDefaults } = require('../../../../appActions');
const nbUtils = require('../../../../helper/notebookUtils');
const path = require('path');
@ -395,13 +395,4 @@ test.describe('Notebook entry tests', () => {
test.fixme('can export all notebook entry metdata', async ({ page }) => {});
test.fixme('can export all notebook tags', async ({ page }) => {});
test.fixme('can export all notebook snapshots', async ({ page }) => {});
async function streamToString(readable) {
let result = '';
for await (const chunk of readable) {
result += chunk;
}
return result;
}
});

View File

@ -20,7 +20,7 @@
* at runtime from the About dialog for additional information.
*****************************************************************************/
const { test, expect } = require('../../../../pluginFixtures');
const { test, expect, streamToString } = require('../../../../pluginFixtures');
const { openObjectTreeContextMenu, createDomainObjectWithDefaults } = require('../../../../appActions');
const path = require('path');
const nbUtils = require('../../../../helper/notebookUtils');
@ -169,6 +169,33 @@ test.describe('Restricted Notebook with a page locked and with an embed @addInit
});
test.describe('can export restricted notebook as text', () => {
test.beforeEach(async ({ page }) => {
await startAndAddRestrictedNotebookObject(page);
});
test('basic functionality ', async ({ page }) => {
await nbUtils.enterTextEntry(page, `Foo bar entry`);
// Click on 3 Dot Menu
await page.locator('button[title="More options"]').click();
const downloadPromise = page.waitForEvent('download');
await page.getByRole('menuitem', { name: /Export Notebook as Text/ }).click();
await page.getByRole('button', { name: 'Save' }).click();
const download = await downloadPromise;
const readStream = await download.createReadStream();
const exportedText = await streamToString(readStream);
expect(exportedText).toContain('Foo bar entry');
});
test.fixme('can export multiple notebook entries as text ', async ({ page }) => {});
test.fixme('can export all notebook entry metdata', async ({ page }) => {});
test.fixme('can export all notebook tags', async ({ page }) => {});
test.fixme('can export all notebook snapshots', async ({ page }) => {});
});
/**
* @param {import('@playwright/test').Page} page
*/

View File

@ -31,7 +31,7 @@ class ActionsAPI extends EventEmitter {
this._actionCollections = new WeakMap();
this._openmct = openmct;
this._groupOrder = ['windowing', 'undefined', 'view', 'action', 'json'];
this._groupOrder = ['windowing', 'undefined', 'view', 'action', 'export', 'import'];
this.register = this.register.bind(this);
this.getActionsCollection = this.getActionsCollection.bind(this);

View File

@ -32,7 +32,7 @@ export default class ExportAsJSONAction {
this.key = 'export.JSON';
this.description = '';
this.cssClass = "icon-export";
this.group = "json";
this.group = "export";
this.priority = 1;
this.externalIdentifiers = [];

View File

@ -29,7 +29,7 @@ export default class ImportAsJSONAction {
this.key = 'import.JSON';
this.description = '';
this.cssClass = "icon-import";
this.group = "json";
this.group = "import";
this.priority = 2;
this.openmct = openmct;

View File

@ -1,8 +1,9 @@
import {saveAs} from 'saveAs';
import Moment from 'moment';
import {NOTEBOOK_TYPE, RESTRICTED_NOTEBOOK_TYPE} from '../notebook-constants';
const UNKNOWN_USER = 'Unknown';
const UNKNOWN_TIME = 'Unknown';
const ALLOWED_TYPES = [NOTEBOOK_TYPE, RESTRICTED_NOTEBOOK_TYPE];
export default class ExportNotebookAsTextAction {
@ -11,10 +12,9 @@ export default class ExportNotebookAsTextAction {
this.cssClass = 'icon-export';
this.description = 'Exports notebook contents as a text file';
this.group = "action";
this.group = "export";
this.key = 'exportNotebookAsText';
this.name = 'Export Notebook as Text';
this.priority = 1;
}
invoke(objectPath) {
@ -56,9 +56,8 @@ export default class ExportNotebookAsTextAction {
appliesTo(objectPath) {
const domainObject = objectPath[0];
const type = this.openmct.types.get(domainObject.type);
return type?.definition?.name === 'Notebook';
return ALLOWED_TYPES.includes(domainObject.type);
}
async onSave(changes, objectPath) {
@ -75,8 +74,8 @@ export default class ExportNotebookAsTextAction {
if (changes.exportMetaData) {
const createdTimestamp = domainObject.created;
const createdBy = domainObject.createdBy ?? UNKNOWN_USER;
const modifiedBy = domainObject.modifiedBy ?? UNKNOWN_USER;
const createdBy = this.getUserName(domainObject.createdBy);
const modifiedBy = this.getUserName(domainObject.modifiedBy);
const modifiedTimestamp = domainObject.modified ?? domainObject.created;
notebookAsText += `Created on ${this.formatTimeStamp(createdTimestamp)} by user ${createdBy}\n\n`;
notebookAsText += `Updated on ${this.formatTimeStamp(modifiedTimestamp)} by user ${modifiedBy}\n\n`;
@ -94,11 +93,16 @@ export default class ExportNotebookAsTextAction {
notebookAsText += `### ${page.name}\n\n`;
const notebookPageEntries = notebookEntries[section.id]?.[page.id];
if (!notebookPageEntries) {
// blank page
return;
}
notebookPageEntries.forEach(entry => {
if (changes.exportMetaData) {
const createdTimestamp = entry.createdOn;
const createdBy = entry.createdBy ?? UNKNOWN_USER;
const modifiedBy = entry.modifiedBy ?? UNKNOWN_USER;
const createdBy = this.getUserName(entry.createdBy);
const modifiedBy = this.getUserName(entry.modifiedBy);
const modifiedTimestamp = entry.modified ?? entry.created;
notebookAsText += `Created on ${this.formatTimeStamp(createdTimestamp)} by user ${createdBy}\n\n`;
notebookAsText += `Updated on ${this.formatTimeStamp(modifiedTimestamp)} by user ${modifiedBy}\n\n`;
@ -123,6 +127,14 @@ export default class ExportNotebookAsTextAction {
saveAs(blob, fileName);
}
getUserName(userId) {
if (userId && userId.length) {
return userId;
}
return UNKNOWN_USER;
}
async showForm(objectPath) {
const formStructure = {
title: "Export Notebook Text",

View File

@ -2,7 +2,7 @@ import objectLink from '../../../ui/mixins/object-link';
import { v4 as uuid } from 'uuid';
async function getUsername(openmct) {
let username = '';
let username = null;
if (openmct.user.hasProvider()) {
const user = await openmct.user.getCurrentUser();