mirror of
https://github.com/GNS3/gns3-web-ui.git
synced 2025-02-04 18:21:56 +00:00
Fix error when taking PNG screenshots
This commit is contained in:
parent
180c65351b
commit
b510f3dc8f
@ -98,25 +98,42 @@ export class ProjectMapMenuComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
private async saveImage(screenshotProperties: Screenshot) {
|
private async saveImage(screenshotProperties: Screenshot) {
|
||||||
if (screenshotProperties.filetype === 'png') {
|
if (screenshotProperties.filetype === 'png') {
|
||||||
let splittedSvg = document.getElementsByTagName('svg')[0].outerHTML.split('image');
|
try {
|
||||||
let i = 1;
|
// Get the SVG element and clone it to avoid modifying the original
|
||||||
|
const originalSvg = document.getElementsByTagName('svg')[0];
|
||||||
|
const svgClone = originalSvg.cloneNode(true) as SVGElement;
|
||||||
|
|
||||||
while (i < splittedSvg.length) {
|
// Process any embedded images
|
||||||
let splittedImage = splittedSvg[i].split('"');
|
const images = svgClone.getElementsByTagName('image');
|
||||||
let splittedUrl = splittedImage[1].split('/');
|
for (let i = 0; i < images.length; i++) {
|
||||||
|
const image = images[i];
|
||||||
let elem = await this.symbolService.raw(this.controller, splittedUrl[7]).toPromise();
|
const href = image.getAttribute('href') || image.getAttribute('xlink:href');
|
||||||
let splittedElement = elem.split('-->');
|
if (href) {
|
||||||
splittedSvg[i] = splittedElement[1].substring(2);
|
const urlParts = href.split('/');
|
||||||
i += 2;
|
const symbolId = urlParts[urlParts.length - 1];
|
||||||
|
try {
|
||||||
|
const rawSvg = await this.symbolService.raw(this.controller, symbolId).toPromise();
|
||||||
|
if (rawSvg) {
|
||||||
|
// Extract SVG content, fallback to raw content if parsing fails
|
||||||
|
const svgContent = rawSvg.includes('-->') ?
|
||||||
|
rawSvg.split('-->')[1].trim() :
|
||||||
|
rawSvg.trim();
|
||||||
|
image.outerHTML = svgContent;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(`Failed to process embedded image: ${symbolId}`, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let svgString = splittedSvg.join();
|
|
||||||
|
|
||||||
let placeholder = document.createElement('div');
|
// Create a temporary container and save as PNG
|
||||||
placeholder.innerHTML = svgString;
|
const container = document.createElement('div');
|
||||||
let element = placeholder.firstChild;
|
container.appendChild(svgClone);
|
||||||
|
svg.saveSvgAsPng(svgClone, `${screenshotProperties.name}.png`);
|
||||||
svg.saveSvgAsPng(element, `${screenshotProperties.name}.png`);
|
} catch (err) {
|
||||||
|
console.error('Failed to save PNG:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var svg_el = select('svg').attr('version', 1.1).attr('xmlns', 'http://www.w3.org/2000/svg').node();
|
var svg_el = select('svg').attr('version', 1.1).attr('xmlns', 'http://www.w3.org/2000/svg').node();
|
||||||
downloadSvg(select('svg').node(), `${screenshotProperties.name}`);
|
downloadSvg(select('svg').node(), `${screenshotProperties.name}`);
|
||||||
|
@ -25,7 +25,7 @@ export class ScreenshotDialogComponent implements OnInit {
|
|||||||
this.nameForm = this.formBuilder.group({
|
this.nameForm = this.formBuilder.group({
|
||||||
screenshotName: new UntypedFormControl(`screenshot-${Date.now()}`, [Validators.required]),
|
screenshotName: new UntypedFormControl(`screenshot-${Date.now()}`, [Validators.required]),
|
||||||
});
|
});
|
||||||
this.isPngAvailable = this.electronService.isWindows || this.deviceService.getDeviceInfo().os === 'Windows';
|
this.isPngAvailable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {}
|
ngOnInit() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user