Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4911 - insert image from ket to canvas #4956

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ReStruct } from 'application/render';
import { Scale } from 'domain/helpers';

export class RasterImageMove extends BaseOperation {
constructor(private id: number, private difference: Vec2) {
constructor(private id: number, private offset: Vec2) {
super(OperationType.RASTER_IMAGE_MOVE);
}

Expand All @@ -16,16 +16,16 @@ export class RasterImageMove extends BaseOperation {
if (!item || !renderItem) {
return;
}
const differenceScaled = Scale.modelToCanvas(
this.difference,
const scaledOffset = Scale.modelToCanvas(
this.offset,
reStruct.render.options,
);

renderItem.move(differenceScaled);
item.addPositionOffset(this.difference);
renderItem.move(scaledOffset);
item.addPositionOffset(this.offset);
}

invert(): BaseOperation {
return new RasterImageMove(this.id, this.difference.negated());
return new RasterImageMove(this.id, this.offset.negated());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class RasterImageUpsert extends BaseOperation {
if (!this.id) {
this.id = struct.rasterImages.newId();
}
const id = this.id!;
const id = this.id;
const item = this.rasterImage.clone();
struct.rasterImages.set(id, item);
reStruct.rasterImages.set(id, new ReRasterImage(item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ReRasterImage extends ReObject {

show(restruct: ReStruct, options: RenderOptions) {
if (this.element) {
return;
this.remove();
}

const render = restruct.render;
Expand Down
9 changes: 7 additions & 2 deletions packages/ketcher-core/src/domain/entities/rasterImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ import { Vec2 } from 'domain/entities/vec2';

type Position = [Vec2, Vec2];

export const RASTER_IMAGE_KEY = 'rasterImage';

export class RasterImage extends BaseMicromoleculeEntity {
constructor(public bitmap: string, public position: [Vec2, Vec2]) {
super();
}

clone(): RasterImage {
return new RasterImage(this.bitmap, this.position);
return new RasterImage(
this.bitmap,
this.position.map((item) => item) as Position,
);
Comment on lines +30 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like position.item still sharing between instance and it's clone

}

addPositionOffset(offset: Vec2) {
this.position = this.position.map((item) => item.add(offset)) as Position;
}

scaled(scale: number): void {
rescalePosition(scale: number): void {
this.position = this.position.map((item) => item.scaled(scale)) as Position;
}
}
4 changes: 3 additions & 1 deletion packages/ketcher-core/src/domain/entities/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,9 @@ export class Struct {
simpleObjects.pos = simpleObjects.pos.map((p) => p.scaled(scale));
});

this.rasterImages.forEach((rasterImage) => rasterImage.scaled(scale));
this.rasterImages.forEach((rasterImage) =>
rasterImage.rescalePosition(scale),
);
}

rescale() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { MonomerItemType } from 'domain/types';
import { PolymerBond } from 'domain/entities/PolymerBond';
import { rasterImageToKet } from 'domain/serializers/ket/toKet/rasterImageToKet';
import { rasterImageToStruct } from 'domain/serializers/ket/fromKet/rasterImageToStruct';
import { RASTER_IMAGE_KEY } from 'domain/entities/rasterImage';

function parseNode(node: any, struct: any) {
const type = node.type;
Expand Down Expand Up @@ -106,7 +107,7 @@ function parseNode(node: any, struct: any) {
textToStruct(node, struct);
break;
}
case 'rasterImage': {
case [RASTER_IMAGE_KEY]: {
rasterImageToStruct(node, struct);
break;
}
Expand Down Expand Up @@ -180,7 +181,7 @@ export class KetSerializer implements Serializer<Struct> {
result.root.nodes.push(textToKet(item));
break;
}
case 'rasterImage': {
case RASTER_IMAGE_KEY: {
result.root.nodes.push(rasterImageToKet(item));
break;
}
Expand Down
3 changes: 0 additions & 3 deletions packages/ketcher-core/src/domain/serializers/ket/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,6 @@
},
"y": {
"type": "number"
},
"z": {
"type": "number"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
***************************************************************************/
import { Pile, Pool, SGroup, Struct, Vec2 } from 'domain/entities';
import { RASTER_IMAGE_KEY } from 'domain/entities/rasterImage';

type KetNode = {
type: string;
Expand Down Expand Up @@ -99,7 +100,7 @@ export function prepareStructForKet(struct: Struct) {
struct.rasterImages.forEach((image) => {
const center: Vec2 = Vec2.centre(image.position[0], image.position[1]);
ketNodes.push({
type: 'rasterImage',
type: RASTER_IMAGE_KEY,
center,
data: {
bitmap: image.bitmap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
***************************************************************************/

import { getNodeWithInvertedYCoord } from '../helpers';
import { RASTER_IMAGE_KEY } from 'domain/entities/rasterImage';

export function rasterImageToKet(rasterImageNode) {
return {
type: 'rasterImage',
type: RASTER_IMAGE_KEY,
data: getNodeWithInvertedYCoord(rasterImageNode.data),
selected: rasterImageNode.selected,
};
Expand Down
Loading