Snapshot images should use the namespace of the notebook they are being saved to or LocalStorage (#4020)

* Snapshot images should use the namespace of the notebook they are being saved to, or LocalStorage #4007

Co-authored-by: Shefali Joshi <simplyrender@gmail.com>
This commit is contained in:
Nikhil 2021-09-13 13:44:38 -07:00 committed by GitHub
parent 2131ef2397
commit d7c9c9cb98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 120 additions and 62 deletions

View File

@ -140,6 +140,7 @@ import SearchResults from './SearchResults.vue';
import Sidebar from './Sidebar.vue'; import Sidebar from './Sidebar.vue';
import { clearDefaultNotebook, getDefaultNotebook, setDefaultNotebook, setDefaultNotebookSectionId, setDefaultNotebookPageId } from '../utils/notebook-storage'; import { clearDefaultNotebook, getDefaultNotebook, setDefaultNotebook, setDefaultNotebookSectionId, setDefaultNotebookPageId } from '../utils/notebook-storage';
import { addNotebookEntry, createNewEmbed, getEntryPosById, getNotebookEntries, mutateObject } from '../utils/notebook-entries'; import { addNotebookEntry, createNewEmbed, getEntryPosById, getNotebookEntries, mutateObject } from '../utils/notebook-entries';
import { saveNotebookImageDomainObject, updateNamespaceOfDomainObject } from '../utils/notebook-image';
import { NOTEBOOK_VIEW_TYPE } from '../notebook-constants'; import { NOTEBOOK_VIEW_TYPE } from '../notebook-constants';
import objectUtils from 'objectUtils'; import objectUtils from 'objectUtils';
@ -385,9 +386,13 @@ export default {
const snapshotId = event.dataTransfer.getData('openmct/snapshot/id'); const snapshotId = event.dataTransfer.getData('openmct/snapshot/id');
if (snapshotId.length) { if (snapshotId.length) {
const snapshot = this.snapshotContainer.getSnapshot(snapshotId); const snapshot = this.snapshotContainer.getSnapshot(snapshotId);
this.newEntry(snapshot); this.newEntry(snapshot.embedObject);
this.snapshotContainer.removeSnapshot(snapshotId); this.snapshotContainer.removeSnapshot(snapshotId);
const namespace = this.domainObject.identifier.namespace;
const notebookImageDomainObject = updateNamespaceOfDomainObject(snapshot.notebookImageDomainObject, namespace);
saveNotebookImageDomainObject(this.openmct, notebookImageDomainObject);
return; return;
} }

View File

@ -40,7 +40,7 @@ export default {
components: { components: {
PopupMenu PopupMenu
}, },
inject: ['openmct'], inject: ['openmct', 'snapshotContainer'],
props: { props: {
embed: { embed: {
type: Object, type: Object,
@ -48,6 +48,12 @@ export default {
return {}; return {};
} }
}, },
isSnapshotContainer: {
type: Boolean,
default() {
return false;
}
},
removeActionString: { removeActionString: {
type: String, type: String,
default() { default() {
@ -135,6 +141,14 @@ export default {
return; return;
} }
if (this.isSnapshotContainer) {
const snapshot = this.snapshotContainer.getSnapshot(this.embed.id);
const fullSizeImageURL = snapshot.notebookImageDomainObject.configuration.fullSizeImageURL;
painterroInstance.show(fullSizeImageURL);
return;
}
this.openmct.objects.get(fullSizeImageObjectIdentifier) this.openmct.objects.get(fullSizeImageObjectIdentifier)
.then(object => { .then(object => {
painterroInstance.show(object.configuration.fullSizeImageURL); painterroInstance.show(object.configuration.fullSizeImageURL);
@ -190,6 +204,14 @@ export default {
return; return;
} }
if (this.isSnapshotContainer) {
const snapshot = this.snapshotContainer.getSnapshot(this.embed.id);
const fullSizeImageURL = snapshot.notebookImageDomainObject.configuration.fullSizeImageURL;
this.openSnapshotOverlay(fullSizeImageURL);
return;
}
this.openmct.objects.get(fullSizeImageObjectIdentifier) this.openmct.objects.get(fullSizeImageObjectIdentifier)
.then(object => { .then(object => {
this.openSnapshotOverlay(object.configuration.fullSizeImageURL); this.openSnapshotOverlay(object.configuration.fullSizeImageURL);
@ -259,8 +281,20 @@ export default {
updateSnapshot(snapshotObject) { updateSnapshot(snapshotObject) {
this.embed.snapshot.thumbnailImage = snapshotObject.thumbnailImage; this.embed.snapshot.thumbnailImage = snapshotObject.thumbnailImage;
updateNotebookImageDomainObject(this.openmct, this.embed.snapshot.fullSizeImageObjectIdentifier, snapshotObject.fullSizeImage); this.updateNotebookImageDomainObjectSnapshot(snapshotObject);
this.updateEmbed(this.embed); this.updateEmbed(this.embed);
},
updateNotebookImageDomainObjectSnapshot(snapshotObject) {
if (this.isSnapshotContainer) {
const snapshot = this.snapshotContainer.getSnapshot(this.embed.id);
snapshot.embedObject.snapshot.thumbnailImage = snapshotObject.thumbnailImage;
snapshot.notebookImageDomainObject.configuration.fullSizeImageURL = snapshotObject.fullSizeImage.src;
this.snapshotContainer.updateSnapshot(snapshot);
} else {
updateNotebookImageDomainObject(this.openmct, this.embed.snapshot.fullSizeImageObjectIdentifier, snapshotObject.fullSizeImage);
}
} }
} }
}; };

View File

@ -102,9 +102,11 @@
<script> <script>
import NotebookEmbed from './NotebookEmbed.vue'; import NotebookEmbed from './NotebookEmbed.vue';
import { createNewEmbed } from '../utils/notebook-entries';
import Moment from 'moment';
import TextHighlight from '../../../utils/textHighlight/TextHighlight.vue'; import TextHighlight from '../../../utils/textHighlight/TextHighlight.vue';
import { createNewEmbed } from '../utils/notebook-entries';
import { saveNotebookImageDomainObject, updateNamespaceOfDomainObject } from '../utils/notebook-image';
import Moment from 'moment';
export default { export default {
components: { components: {
@ -210,8 +212,12 @@ export default {
const snapshotId = $event.dataTransfer.getData('openmct/snapshot/id'); const snapshotId = $event.dataTransfer.getData('openmct/snapshot/id');
if (snapshotId.length) { if (snapshotId.length) {
const snapshot = this.snapshotContainer.getSnapshot(snapshotId); const snapshot = this.snapshotContainer.getSnapshot(snapshotId);
this.entry.embeds.push(snapshot.embedObject);
this.snapshotContainer.removeSnapshot(snapshotId); this.snapshotContainer.removeSnapshot(snapshotId);
this.entry.embeds.push(snapshot);
const namespace = this.domainObject.identifier.namespace;
const notebookImageDomainObject = updateNamespaceOfDomainObject(snapshot.notebookImageDomainObject, namespace);
saveNotebookImageDomainObject(this.openmct, notebookImageDomainObject);
} else { } else {
const data = $event.dataTransfer.getData('openmct/domain-object-path'); const data = $event.dataTransfer.getData('openmct/domain-object-path');
const objectPath = JSON.parse(data); const objectPath = JSON.parse(data);

View File

@ -27,15 +27,15 @@
</div><!-- closes l-browse-bar --> </div><!-- closes l-browse-bar -->
<div class="c-snapshots"> <div class="c-snapshots">
<span v-for="snapshot in snapshots" <span v-for="snapshot in snapshots"
:key="snapshot.id" :key="snapshot.embedObject.id"
draggable="true" draggable="true"
@dragstart="startEmbedDrag(snapshot, $event)" @dragstart="startEmbedDrag(snapshot, $event)"
> >
<NotebookEmbed ref="notebookEmbed" <NotebookEmbed ref="notebookEmbed"
:key="snapshot.id" :key="snapshot.embedObject.id"
:embed="snapshot" :embed="snapshot.embedObject"
:is-snapshot-container="true"
:remove-action-string="'Delete Snapshot'" :remove-action-string="'Delete Snapshot'"
@updateEmbed="updateSnapshot"
@removeEmbed="removeSnapshot" @removeEmbed="removeSnapshot"
/> />
</span> </span>
@ -119,11 +119,8 @@ export default {
this.snapshots = this.snapshotContainer.getSnapshots(); this.snapshots = this.snapshotContainer.getSnapshots();
}, },
startEmbedDrag(snapshot, event) { startEmbedDrag(snapshot, event) {
event.dataTransfer.setData('text/plain', snapshot.id); event.dataTransfer.setData('text/plain', snapshot.embedObject.id);
event.dataTransfer.setData('openmct/snapshot/id', snapshot.id); event.dataTransfer.setData('openmct/snapshot/id', snapshot.embedObject.id);
},
updateSnapshot(snapshot) {
this.snapshotContainer.updateSnapshot(snapshot);
} }
} }
}; };

View File

@ -18,13 +18,18 @@ export default class SnapshotContainer extends EventEmitter {
return SnapshotContainer.instance; return SnapshotContainer.instance;
} }
addSnapshot(embedObject) { addSnapshot(notebookImageDomainObject, embedObject) {
const snapshots = this.getSnapshots(); const snapshots = this.getSnapshots();
if (snapshots.length >= NOTEBOOK_SNAPSHOT_MAX_COUNT) { if (snapshots.length >= NOTEBOOK_SNAPSHOT_MAX_COUNT) {
snapshots.pop(); snapshots.pop();
} }
snapshots.unshift(embedObject); const snapshotObject = {
notebookImageDomainObject,
embedObject
};
snapshots.unshift(snapshotObject);
return this.saveSnapshots(snapshots); return this.saveSnapshots(snapshots);
} }
@ -32,7 +37,7 @@ export default class SnapshotContainer extends EventEmitter {
getSnapshot(id) { getSnapshot(id) {
const snapshots = this.getSnapshots(); const snapshots = this.getSnapshots();
return snapshots.find(s => s.id === id); return snapshots.find(s => s.embedObject.id === id);
} }
getSnapshots() { getSnapshots() {
@ -47,7 +52,7 @@ export default class SnapshotContainer extends EventEmitter {
} }
const snapshots = this.getSnapshots(); const snapshots = this.getSnapshots();
const filteredsnapshots = snapshots.filter(snapshot => snapshot.id !== id); const filteredsnapshots = snapshots.filter(snapshot => snapshot.embedObject.id !== id);
return this.saveSnapshots(filteredsnapshots); return this.saveSnapshots(filteredsnapshots);
} }
@ -73,7 +78,7 @@ export default class SnapshotContainer extends EventEmitter {
updateSnapshot(snapshot) { updateSnapshot(snapshot) {
const snapshots = this.getSnapshots(); const snapshots = this.getSnapshots();
const updatedSnapshots = snapshots.map(s => { const updatedSnapshots = snapshots.map(s => {
return s.id === snapshot.id return s.embedObject.id === snapshot.embedObject.id
? snapshot ? snapshot
: s; : s;
}); });

View File

@ -1,7 +1,7 @@
import { addNotebookEntry, createNewEmbed } from './utils/notebook-entries'; import { addNotebookEntry, createNewEmbed } from './utils/notebook-entries';
import { getDefaultNotebook, getNotebookSectionAndPage, getDefaultNotebookLink, setDefaultNotebook } from './utils/notebook-storage'; import { getDefaultNotebook, getNotebookSectionAndPage, getDefaultNotebookLink, setDefaultNotebook } from './utils/notebook-storage';
import { NOTEBOOK_DEFAULT } from '@/plugins/notebook/notebook-constants'; import { NOTEBOOK_DEFAULT } from '@/plugins/notebook/notebook-constants';
import { createNotebookImageDomainObject, DEFAULT_SIZE } from './utils/notebook-image'; import { createNotebookImageDomainObject, saveNotebookImageDomainObject, updateNamespaceOfDomainObject, DEFAULT_SIZE } from './utils/notebook-image';
import SnapshotContainer from './snapshot-container'; import SnapshotContainer from './snapshot-container';
import ImageExporter from '../../exporters/ImageExporter'; import ImageExporter from '../../exporters/ImageExporter';
@ -35,29 +35,28 @@ export default class Snapshot {
* @private * @private
*/ */
_saveSnapShot(notebookType, fullSizeImageURL, thumbnailImageURL, snapshotMeta) { _saveSnapShot(notebookType, fullSizeImageURL, thumbnailImageURL, snapshotMeta) {
createNotebookImageDomainObject(this.openmct, fullSizeImageURL) const object = createNotebookImageDomainObject(fullSizeImageURL);
.then(object => { const thumbnailImage = { src: thumbnailImageURL || '' };
const thumbnailImage = { src: thumbnailImageURL || '' }; const snapshot = {
const snapshot = { fullSizeImageObjectIdentifier: object.identifier,
fullSizeImageObjectIdentifier: object.identifier, thumbnailImage
thumbnailImage };
}; const embed = createNewEmbed(snapshotMeta, snapshot);
const embed = createNewEmbed(snapshotMeta, snapshot); if (notebookType === NOTEBOOK_DEFAULT) {
if (notebookType === NOTEBOOK_DEFAULT) { const notebookStorage = getDefaultNotebook();
this._saveToDefaultNoteBook(embed);
return; this._saveToDefaultNoteBook(notebookStorage, embed);
} const notebookImageDomainObject = updateNamespaceOfDomainObject(object, notebookStorage.identifier.namespace);
saveNotebookImageDomainObject(this.openmct, notebookImageDomainObject);
this._saveToNotebookSnapshots(embed); } else {
}); this._saveToNotebookSnapshots(object, embed);
}
} }
/** /**
* @private * @private
*/ */
_saveToDefaultNoteBook(embed) { _saveToDefaultNoteBook(notebookStorage, embed) {
const notebookStorage = getDefaultNotebook();
this.openmct.objects.get(notebookStorage.identifier) this.openmct.objects.get(notebookStorage.identifier)
.then(async (domainObject) => { .then(async (domainObject) => {
addNotebookEntry(this.openmct, domainObject, notebookStorage, embed); addNotebookEntry(this.openmct, domainObject, notebookStorage, embed);
@ -85,8 +84,8 @@ export default class Snapshot {
/** /**
* @private * @private
*/ */
_saveToNotebookSnapshots(embed) { _saveToNotebookSnapshots(notebookImageDomainObject, embed) {
this.snapshotContainer.addSnapshot(embed); this.snapshotContainer.addSnapshot(notebookImageDomainObject, embed);
} }
_showNotification(msg, url) { _showNotification(msg, url) {

View File

@ -5,14 +5,14 @@ export const DEFAULT_SIZE = {
height: 30 height: 30
}; };
export function createNotebookImageDomainObject(openmct, fullSizeImageURL) { export function createNotebookImageDomainObject(fullSizeImageURL) {
const identifier = { const identifier = {
key: uuid(), key: uuid(),
namespace: '' namespace: ''
}; };
const viewType = 'notebookSnapshotImage'; const viewType = 'notebookSnapshotImage';
const object = { return {
name: 'Notebook Snapshot Image', name: 'Notebook Snapshot Image',
type: viewType, type: viewType,
identifier, identifier,
@ -20,21 +20,6 @@ export function createNotebookImageDomainObject(openmct, fullSizeImageURL) {
fullSizeImageURL 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) { export function getThumbnailURLFromCanvas(canvas, size = DEFAULT_SIZE) {
@ -67,6 +52,23 @@ export function getThumbnailURLFromimageUrl(imageUrl, size = DEFAULT_SIZE) {
}); });
} }
export function saveNotebookImageDomainObject(openmct, object) {
return new Promise((resolve, reject) => {
openmct.objects.save(object)
.then(result => {
if (result) {
resolve(object);
} else {
reject();
}
})
.catch(e => {
console.error(e);
reject();
});
});
}
export function updateNotebookImageDomainObject(openmct, identifier, fullSizeImage) { export function updateNotebookImageDomainObject(openmct, identifier, fullSizeImage) {
openmct.objects.get(identifier) openmct.objects.get(identifier)
.then(domainObject => { .then(domainObject => {
@ -76,3 +78,9 @@ export function updateNotebookImageDomainObject(openmct, identifier, fullSizeIma
openmct.objects.mutate(domainObject, 'configuration', configuration); openmct.objects.mutate(domainObject, 'configuration', configuration);
}); });
} }
export function updateNamespaceOfDomainObject(object, namespace) {
object.identifier.namespace = namespace;
return object;
}

View File

@ -1,16 +1,18 @@
import { createNotebookImageDomainObject, getThumbnailURLFromimageUrl } from './notebook-image'; import { createNotebookImageDomainObject, getThumbnailURLFromimageUrl, saveNotebookImageDomainObject, updateNamespaceOfDomainObject } from './notebook-image';
import { mutateObject } from './notebook-entries'; import { mutateObject } from './notebook-entries';
const IMAGE_MIGRATION_VER = "v1";
export function notebookImageMigration(openmct, domainObject) { export function notebookImageMigration(openmct, domainObject) {
const configuration = domainObject.configuration; const configuration = domainObject.configuration;
const notebookEntries = configuration.entries; const notebookEntries = configuration.entries;
const imageMigrationVer = configuration.imageMigrationVer; const imageMigrationVer = configuration.imageMigrationVer;
if (imageMigrationVer && imageMigrationVer === 'v1') { if (imageMigrationVer && imageMigrationVer === IMAGE_MIGRATION_VER) {
return; return;
} }
configuration.imageMigrationVer = 'v1'; configuration.imageMigrationVer = IMAGE_MIGRATION_VER;
// to avoid muliple notebookImageMigration calls updating images. // to avoid muliple notebookImageMigration calls updating images.
mutateObject(openmct, domainObject, 'configuration', configuration); mutateObject(openmct, domainObject, 'configuration', configuration);
@ -27,14 +29,16 @@ export function notebookImageMigration(openmct, domainObject) {
const fullSizeImageURL = snapshot.src; const fullSizeImageURL = snapshot.src;
if (fullSizeImageURL) { if (fullSizeImageURL) {
const thumbnailImageURL = await getThumbnailURLFromimageUrl(fullSizeImageURL); const thumbnailImageURL = await getThumbnailURLFromimageUrl(fullSizeImageURL);
const notebookImageDomainObject = await createNotebookImageDomainObject(openmct, fullSizeImageURL); const object = createNotebookImageDomainObject(fullSizeImageURL);
const notebookImageDomainObject = updateNamespaceOfDomainObject(object, domainObject.identifier.namespace);
embed.snapshot = { embed.snapshot = {
fullSizeImageObjectIdentifier: notebookImageDomainObject.identifier, fullSizeImageObjectIdentifier: notebookImageDomainObject.identifier,
thumbnailImage: { src: thumbnailImageURL || '' } thumbnailImage: { src: thumbnailImageURL || '' }
}; };
mutateObject(openmct, domainObject, 'configuration.entries', notebookEntries); mutateObject(openmct, domainObject, 'configuration.entries', notebookEntries);
saveNotebookImageDomainObject(openmct, notebookImageDomainObject);
} }
}); });
}); });