-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4539 - Export macromolecules canvas as svg image
- Loading branch information
1 parent
560f410
commit 64dee36
Showing
27 changed files
with
293 additions
and
25 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
ketcher-autotests/tests/Macromolecule-editor/API/set-mode.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { test } from '@playwright/test'; | ||
import { | ||
waitForPageInit, | ||
turnOnMacromoleculesEditor, | ||
openFileAndAddToCanvasMacro, | ||
setMode, | ||
takePageScreenshot, | ||
} from '@utils'; | ||
|
||
test.describe('setMode', () => { | ||
test.beforeEach(async ({ page }) => { | ||
await waitForPageInit(page); | ||
await turnOnMacromoleculesEditor(page); | ||
}); | ||
|
||
test('Should set "sequence" mode', async ({ page }) => { | ||
await openFileAndAddToCanvasMacro('KET/rna-and-peptide.ket', page); | ||
await takePageScreenshot(page); | ||
await setMode(page, 'sequence'); | ||
await takePageScreenshot(page); | ||
}); | ||
}); |
Binary file added
BIN
+40.6 KB
...et-mode.spec.ts-snapshots/setMode-Should-set-sequence-mode-1-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+41.2 KB
...et-mode.spec.ts-snapshots/setMode-Should-set-sequence-mode-2-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions
23
ketcher-autotests/tests/Macromolecule-editor/API/set-zoom.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { test } from '@playwright/test'; | ||
import { | ||
waitForPageInit, | ||
takeEditorScreenshot, | ||
turnOnMacromoleculesEditor, | ||
openFileAndAddToCanvasMacro, | ||
setZoom, | ||
} from '@utils'; | ||
|
||
test.describe('setZoom', () => { | ||
test.beforeEach(async ({ page }) => { | ||
await waitForPageInit(page); | ||
await turnOnMacromoleculesEditor(page); | ||
}); | ||
|
||
test('Should zoom drawn structures', async ({ page }) => { | ||
await openFileAndAddToCanvasMacro('KET/rna-and-peptide.ket', page); | ||
const zoomValue = 2; | ||
await takeEditorScreenshot(page); | ||
await setZoom(page, zoomValue); | ||
await takeEditorScreenshot(page); | ||
}); | ||
}); |
Binary file added
BIN
+6.23 KB
...oom.spec.ts-snapshots/setZoom-Should-zoom-drawn-structures-1-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.29 KB
...oom.spec.ts-snapshots/setZoom-Should-zoom-drawn-structures-2-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions
35
ketcher-autotests/tests/Macromolecule-editor/Import-Saving-Files/saving-svg.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { test } from '@playwright/test'; | ||
import { | ||
TopPanelButton, | ||
openFileAndAddToCanvasMacro, | ||
selectTopPanelButton, | ||
takeEditorScreenshot, | ||
waitForPageInit, | ||
chooseFileFormat, | ||
selectRectangleArea, | ||
} from '@utils'; | ||
import { turnOnMacromoleculesEditor } from '@utils/macromolecules'; | ||
|
||
test.describe('Saving in .svg files', () => { | ||
test.beforeEach(async ({ page }) => { | ||
await waitForPageInit(page); | ||
await turnOnMacromoleculesEditor(page); | ||
}); | ||
|
||
test('Should convert .ket file to .svg format in save modal', async ({ | ||
page, | ||
}) => { | ||
await openFileAndAddToCanvasMacro('KET/rna-and-peptide.ket', page); | ||
// Coordinates for rectangle selection | ||
const startX = 100; | ||
const startY = 100; | ||
const endX = 600; | ||
const endY = 450; | ||
await selectRectangleArea(page, startX, startY, endX, endY); | ||
await takeEditorScreenshot(page); | ||
await selectTopPanelButton(TopPanelButton.Save, page); | ||
await chooseFileFormat(page, 'SVG Document'); | ||
// Should clean up dynamic svg elements (selections...) in drawn structure | ||
await takeEditorScreenshot(page); | ||
}); | ||
}); |
Binary file added
BIN
+9.54 KB
...-files-Should-convert-ket-file-to-svg-format-in-save-modal-1-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23.4 KB
...-files-Should-convert-ket-file-to-svg-format-in-save-modal-2-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Types for 'setMode' | ||
export enum ModeTypes { | ||
flex = 'flex-layout-mode', | ||
snake = 'snake-layout-mode', | ||
sequence = 'sequence-layout-mode', | ||
} | ||
export type SupportedModes = keyof typeof ModeTypes; | ||
|
||
// Types for 'exportImage' | ||
export enum BlobTypes { | ||
svg = 'image/svg+xml', | ||
} | ||
export type SupportedImageFormats = keyof typeof BlobTypes; | ||
export type ExportImageParams = { | ||
margin?: number; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/ketcher-core/src/utilities/getSvgFromDrawnStructures.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { KetcherLogger } from 'utilities'; | ||
|
||
const SVG_NAMESPACE_URI = 'http://www.w3.org/2000/svg'; | ||
const ADDITIONAL_TOP_MARGIN = 54; | ||
const ADDITIONAL_LEFT_MARGIN = 50; | ||
const DEFAULT_MARGIN = 10; | ||
|
||
export const getSvgFromDrawnStructures = ( | ||
canvas: SVGSVGElement, | ||
type: 'preview' | 'file', | ||
margin = DEFAULT_MARGIN, | ||
) => { | ||
// Copy and clean up svg structures before previewing or saving | ||
let svgInnerHTML = canvas?.innerHTML || ''; | ||
const wrapper = document.createElementNS(SVG_NAMESPACE_URI, 'svg'); | ||
wrapper.innerHTML = svgInnerHTML; | ||
// remove #rectangle-selection-area | ||
wrapper.querySelector('#rectangle-selection-area')?.remove(); | ||
// remove dynamic elements (scrolls, highlighters, attachment points...) | ||
wrapper.querySelectorAll('.dynamic-element')?.forEach((el) => el.remove()); | ||
// set default cursor, mostly for sequence mode | ||
wrapper | ||
.querySelectorAll('text') | ||
?.forEach((el) => el.setAttribute('cursor', 'default')); | ||
wrapper.querySelectorAll('rect')?.forEach((el) => { | ||
if (el.getAttribute('cursor') === 'text') el.removeAttribute('cursor'); | ||
}); | ||
// remove opacity of structures, mostly for sequence "edit in RNA builder" mode | ||
wrapper.querySelectorAll('g')?.forEach((el) => { | ||
if (el.hasAttribute('opacity')) el.removeAttribute('opacity'); | ||
}); | ||
svgInnerHTML = wrapper.innerHTML; | ||
// remove "cursor: pointer" style | ||
svgInnerHTML = svgInnerHTML?.replaceAll('cursor: pointer;', ''); | ||
|
||
const drawStructureClientRect = canvas | ||
?.getElementsByClassName('drawn-structures')[0] | ||
.getBoundingClientRect(); | ||
|
||
if (!drawStructureClientRect || !svgInnerHTML) { | ||
const errorMessage = 'Cannot get drawn structures!'; | ||
KetcherLogger.error(errorMessage); | ||
return; | ||
} | ||
|
||
const viewBoxX = drawStructureClientRect.x - ADDITIONAL_LEFT_MARGIN - margin; | ||
const viewBoxY = drawStructureClientRect.y - ADDITIONAL_TOP_MARGIN - margin; | ||
const viewBoxWidth = drawStructureClientRect.width + margin * 2; | ||
const viewBoxHeight = drawStructureClientRect.height + margin * 2; | ||
const viewBox = `${viewBoxX} ${viewBoxY} ${viewBoxWidth} ${viewBoxHeight}`; | ||
|
||
if (type === 'preview') | ||
return `<svg width='100%' height='100%' style='position: absolute' viewBox='${viewBox}'>${svgInnerHTML}</svg>`; | ||
else if (type === 'file') | ||
return `<svg width='${viewBoxWidth}' height='${viewBoxHeight}' viewBox='${viewBox}' xmlns='${SVG_NAMESPACE_URI}'>${svgInnerHTML}</svg>`; | ||
else return `<svg xmlns='${SVG_NAMESPACE_URI}' />`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.