mirror of
https://github.com/nasa/openmct.git
synced 2025-01-11 07:23:16 +00:00
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:
parent
4f10a93ef5
commit
b7a671d392
@ -170,5 +170,6 @@ exports.test = base.test.extend({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
exports.expect = expect;
|
exports.expect = expect;
|
||||||
exports.waitForAnimations = waitForAnimations;
|
exports.waitForAnimations = waitForAnimations;
|
||||||
|
@ -150,3 +150,17 @@ exports.test = test.extend({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
exports.expect = expect;
|
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;
|
||||||
|
};
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
This test suite is dedicated to tests which verify the basic operations surrounding Notebooks.
|
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 { createDomainObjectWithDefaults } = require('../../../../appActions');
|
||||||
const nbUtils = require('../../../../helper/notebookUtils');
|
const nbUtils = require('../../../../helper/notebookUtils');
|
||||||
const path = require('path');
|
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 entry metdata', async ({ page }) => {});
|
||||||
test.fixme('can export all notebook tags', async ({ page }) => {});
|
test.fixme('can export all notebook tags', async ({ page }) => {});
|
||||||
test.fixme('can export all notebook snapshots', 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;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
* at runtime from the About dialog for additional information.
|
* 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 { openObjectTreeContextMenu, createDomainObjectWithDefaults } = require('../../../../appActions');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const nbUtils = require('../../../../helper/notebookUtils');
|
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
|
* @param {import('@playwright/test').Page} page
|
||||||
*/
|
*/
|
||||||
|
@ -31,7 +31,7 @@ class ActionsAPI extends EventEmitter {
|
|||||||
this._actionCollections = new WeakMap();
|
this._actionCollections = new WeakMap();
|
||||||
this._openmct = openmct;
|
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.register = this.register.bind(this);
|
||||||
this.getActionsCollection = this.getActionsCollection.bind(this);
|
this.getActionsCollection = this.getActionsCollection.bind(this);
|
||||||
|
@ -32,7 +32,7 @@ export default class ExportAsJSONAction {
|
|||||||
this.key = 'export.JSON';
|
this.key = 'export.JSON';
|
||||||
this.description = '';
|
this.description = '';
|
||||||
this.cssClass = "icon-export";
|
this.cssClass = "icon-export";
|
||||||
this.group = "json";
|
this.group = "export";
|
||||||
this.priority = 1;
|
this.priority = 1;
|
||||||
|
|
||||||
this.externalIdentifiers = [];
|
this.externalIdentifiers = [];
|
||||||
|
@ -29,7 +29,7 @@ export default class ImportAsJSONAction {
|
|||||||
this.key = 'import.JSON';
|
this.key = 'import.JSON';
|
||||||
this.description = '';
|
this.description = '';
|
||||||
this.cssClass = "icon-import";
|
this.cssClass = "icon-import";
|
||||||
this.group = "json";
|
this.group = "import";
|
||||||
this.priority = 2;
|
this.priority = 2;
|
||||||
|
|
||||||
this.openmct = openmct;
|
this.openmct = openmct;
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import {saveAs} from 'saveAs';
|
import {saveAs} from 'saveAs';
|
||||||
import Moment from 'moment';
|
import Moment from 'moment';
|
||||||
|
import {NOTEBOOK_TYPE, RESTRICTED_NOTEBOOK_TYPE} from '../notebook-constants';
|
||||||
const UNKNOWN_USER = 'Unknown';
|
const UNKNOWN_USER = 'Unknown';
|
||||||
const UNKNOWN_TIME = 'Unknown';
|
const UNKNOWN_TIME = 'Unknown';
|
||||||
|
const ALLOWED_TYPES = [NOTEBOOK_TYPE, RESTRICTED_NOTEBOOK_TYPE];
|
||||||
|
|
||||||
export default class ExportNotebookAsTextAction {
|
export default class ExportNotebookAsTextAction {
|
||||||
|
|
||||||
@ -11,10 +12,9 @@ export default class ExportNotebookAsTextAction {
|
|||||||
|
|
||||||
this.cssClass = 'icon-export';
|
this.cssClass = 'icon-export';
|
||||||
this.description = 'Exports notebook contents as a text file';
|
this.description = 'Exports notebook contents as a text file';
|
||||||
this.group = "action";
|
this.group = "export";
|
||||||
this.key = 'exportNotebookAsText';
|
this.key = 'exportNotebookAsText';
|
||||||
this.name = 'Export Notebook as Text';
|
this.name = 'Export Notebook as Text';
|
||||||
this.priority = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
invoke(objectPath) {
|
invoke(objectPath) {
|
||||||
@ -56,9 +56,8 @@ export default class ExportNotebookAsTextAction {
|
|||||||
|
|
||||||
appliesTo(objectPath) {
|
appliesTo(objectPath) {
|
||||||
const domainObject = objectPath[0];
|
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) {
|
async onSave(changes, objectPath) {
|
||||||
@ -75,8 +74,8 @@ export default class ExportNotebookAsTextAction {
|
|||||||
|
|
||||||
if (changes.exportMetaData) {
|
if (changes.exportMetaData) {
|
||||||
const createdTimestamp = domainObject.created;
|
const createdTimestamp = domainObject.created;
|
||||||
const createdBy = domainObject.createdBy ?? UNKNOWN_USER;
|
const createdBy = this.getUserName(domainObject.createdBy);
|
||||||
const modifiedBy = domainObject.modifiedBy ?? UNKNOWN_USER;
|
const modifiedBy = this.getUserName(domainObject.modifiedBy);
|
||||||
const modifiedTimestamp = domainObject.modified ?? domainObject.created;
|
const modifiedTimestamp = domainObject.modified ?? domainObject.created;
|
||||||
notebookAsText += `Created on ${this.formatTimeStamp(createdTimestamp)} by user ${createdBy}\n\n`;
|
notebookAsText += `Created on ${this.formatTimeStamp(createdTimestamp)} by user ${createdBy}\n\n`;
|
||||||
notebookAsText += `Updated on ${this.formatTimeStamp(modifiedTimestamp)} by user ${modifiedBy}\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`;
|
notebookAsText += `### ${page.name}\n\n`;
|
||||||
|
|
||||||
const notebookPageEntries = notebookEntries[section.id]?.[page.id];
|
const notebookPageEntries = notebookEntries[section.id]?.[page.id];
|
||||||
|
if (!notebookPageEntries) {
|
||||||
|
// blank page
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
notebookPageEntries.forEach(entry => {
|
notebookPageEntries.forEach(entry => {
|
||||||
if (changes.exportMetaData) {
|
if (changes.exportMetaData) {
|
||||||
const createdTimestamp = entry.createdOn;
|
const createdTimestamp = entry.createdOn;
|
||||||
const createdBy = entry.createdBy ?? UNKNOWN_USER;
|
const createdBy = this.getUserName(entry.createdBy);
|
||||||
const modifiedBy = entry.modifiedBy ?? UNKNOWN_USER;
|
const modifiedBy = this.getUserName(entry.modifiedBy);
|
||||||
const modifiedTimestamp = entry.modified ?? entry.created;
|
const modifiedTimestamp = entry.modified ?? entry.created;
|
||||||
notebookAsText += `Created on ${this.formatTimeStamp(createdTimestamp)} by user ${createdBy}\n\n`;
|
notebookAsText += `Created on ${this.formatTimeStamp(createdTimestamp)} by user ${createdBy}\n\n`;
|
||||||
notebookAsText += `Updated on ${this.formatTimeStamp(modifiedTimestamp)} by user ${modifiedBy}\n\n`;
|
notebookAsText += `Updated on ${this.formatTimeStamp(modifiedTimestamp)} by user ${modifiedBy}\n\n`;
|
||||||
@ -123,6 +127,14 @@ export default class ExportNotebookAsTextAction {
|
|||||||
saveAs(blob, fileName);
|
saveAs(blob, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getUserName(userId) {
|
||||||
|
if (userId && userId.length) {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
return UNKNOWN_USER;
|
||||||
|
}
|
||||||
|
|
||||||
async showForm(objectPath) {
|
async showForm(objectPath) {
|
||||||
const formStructure = {
|
const formStructure = {
|
||||||
title: "Export Notebook Text",
|
title: "Export Notebook Text",
|
||||||
|
@ -2,7 +2,7 @@ import objectLink from '../../../ui/mixins/object-link';
|
|||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
|
|
||||||
async function getUsername(openmct) {
|
async function getUsername(openmct) {
|
||||||
let username = '';
|
let username = null;
|
||||||
|
|
||||||
if (openmct.user.hasProvider()) {
|
if (openmct.user.hasProvider()) {
|
||||||
const user = await openmct.user.getCurrentUser();
|
const user = await openmct.user.getCurrentUser();
|
||||||
|
Loading…
Reference in New Issue
Block a user