[Notebooks] Don't save images on the object (#3792)

* Create and store image data into new domain object of type 'notebookSnapshotImage'
* Reduced thumbnail size to 30px
* Image migration script for old notebooks.
* Saves thumbnail image on notebook instead of new object.
This commit is contained in:
Nikhil
2021-06-21 15:42:33 -07:00
committed by GitHub
parent fa5aceb7b3
commit 925518c83f
11 changed files with 302 additions and 56 deletions

View File

@ -0,0 +1,78 @@
import uuid from 'uuid';
export const DEFAULT_SIZE = {
width: 30,
height: 30
};
export function createNotebookImageDomainObject(openmct, fullSizeImageURL) {
const identifier = {
key: uuid(),
namespace: ''
};
const viewType = 'notebookSnapshotImage';
const object = {
name: 'Notebook Snapshot Image',
type: viewType,
identifier,
configuration: {
fullSizeImageURL
}
};
return new Promise((resolve, reject) => {
openmct.objects.save(object)
.then(result => {
if (result) {
resolve(object);
}
reject();
})
.catch(e => {
console.error(e);
reject();
});
});
}
export function getThumbnailURLFromCanvas(canvas, size = DEFAULT_SIZE) {
const thumbnailCanvas = document.createElement('canvas');
thumbnailCanvas.setAttribute('width', size.width);
thumbnailCanvas.setAttribute('height', size.height);
const ctx = thumbnailCanvas.getContext('2d');
ctx.globalCompositeOperation = "copy";
ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, size.width, size.height);
return thumbnailCanvas.toDataURL('image/png');
}
export function getThumbnailURLFromimageUrl(imageUrl, size = DEFAULT_SIZE) {
return new Promise(resolve => {
const image = new Image();
const canvas = document.createElement('canvas');
canvas.width = size.width;
canvas.height = size.height;
image.onload = function () {
canvas.getContext('2d')
.drawImage(image, 0, 0, size.width, size.height);
resolve(canvas.toDataURL('image/png'));
};
image.src = imageUrl;
});
}
export function updateNotebookImageDomainObject(openmct, identifier, fullSizeImage) {
openmct.objects.get(identifier)
.then(domainObject => {
const configuration = domainObject.configuration;
configuration.fullSizeImageURL = fullSizeImage.src;
openmct.objects.mutate(domainObject, 'configuration', configuration);
});
}

View File

@ -0,0 +1,43 @@
import { createNotebookImageDomainObject, getThumbnailURLFromimageUrl } from './notebook-image';
import { mutateObject } from './notebook-entries';
export function notebookImageMigration(openmct, domainObject) {
const configuration = domainObject.configuration;
const notebookEntries = configuration.entries;
const imageMigrationVer = configuration.imageMigrationVer;
if (imageMigrationVer && imageMigrationVer === 'v1') {
return;
}
configuration.imageMigrationVer = 'v1';
// to avoid muliple notebookImageMigration calls updating images.
mutateObject(openmct, domainObject, 'configuration', configuration);
configuration.sections.forEach(section => {
const sectionId = section.id;
section.pages.forEach(page => {
const pageId = page.id;
const notebookSection = notebookEntries && notebookEntries[sectionId] || {};
const pageEntries = notebookSection && notebookSection[pageId] || [];
pageEntries.forEach(entry => {
entry.embeds.forEach(async (embed) => {
const snapshot = embed.snapshot;
const fullSizeImageURL = snapshot.src;
if (fullSizeImageURL) {
const thumbnailImageURL = await getThumbnailURLFromimageUrl(fullSizeImageURL);
const notebookImageDomainObject = await createNotebookImageDomainObject(openmct, fullSizeImageURL);
embed.snapshot = {
fullSizeImageObjectIdentifier: notebookImageDomainObject.identifier,
thumbnailImage: { src: thumbnailImageURL || '' }
};
mutateObject(openmct, domainObject, 'configuration.entries', notebookEntries);
}
});
});
});
});
}

View File

@ -1,4 +1,5 @@
import Painterro from 'painterro';
import { getThumbnailURLFromimageUrl } from './notebook-image';
const DEFAULT_CONFIG = {
activeColor: '#ff0000',
@ -25,11 +26,11 @@ const DEFAULT_CONFIG = {
};
export default class PainterroInstance {
constructor(element, saveCallback) {
constructor(element) {
this.elementId = element.id;
this.isSave = false;
this.painterroInstance = null;
this.saveCallback = saveCallback;
this.painterroInstance = undefined;
this.saveCallback = undefined;
}
dismiss() {
@ -46,31 +47,41 @@ export default class PainterroInstance {
this.painterro = Painterro(this.config);
}
save() {
save(callback) {
this.saveCallback = callback;
this.isSave = true;
this.painterroInstance.save();
}
saveHandler(image, done) {
if (this.isSave) {
const self = this;
const url = image.asBlob();
const reader = new window.FileReader();
reader.readAsDataURL(url);
reader.onloadend = () => {
const snapshot = reader.result;
reader.onloadend = async () => {
const fullSizeImageURL = reader.result;
const thumbnailURL = await getThumbnailURLFromimageUrl(fullSizeImageURL);
const snapshotObject = {
src: snapshot,
type: url.type,
size: url.size,
modified: Date.now()
fullSizeImage: {
src: fullSizeImageURL,
type: url.type,
size: url.size,
modified: Date.now()
},
thumbnailImage: {
src: thumbnailURL,
modified: Date.now()
}
};
self.saveCallback(snapshotObject);
};
}
this.saveCallback(snapshotObject);
done(true);
done(true);
};
} else {
done(true);
}
}
show(src) {