Skip to content

Commit

Permalink
#5172 - updated ket image format
Browse files Browse the repository at this point in the history
  • Loading branch information
daniil-sloboda committed Jul 29, 2024
1 parent 9003ae9 commit 5fe455d
Show file tree
Hide file tree
Showing 39 changed files with 309 additions and 308 deletions.
8 changes: 4 additions & 4 deletions packages/ketcher-core/src/application/editor/actions/erase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
AtomDelete,
BondDelete,
CalcImplicitH,
RasterImageDelete,
ImageDelete,
RGroupAttachmentPointRemove,
RxnArrowDelete,
RxnPlusDelete,
Expand All @@ -40,7 +40,7 @@ import { fromFragmentSplit } from './fragment';
import { fromRGroupAttachmentPointDeletion } from './rgroupAttachmentPoint';
import { ReStruct } from 'application/render';
import { isNumber } from 'lodash';
import { RASTER_IMAGE_KEY } from 'domain/constants';
import { IMAGE_KEY } from 'domain/constants';

export function fromOneAtomDeletion(restruct, atomId: number) {
return fromFragmentDeletion(restruct, { atoms: [atomId] });
Expand Down Expand Up @@ -218,8 +218,8 @@ export function fromFragmentDeletion(restruct, rawSelection) {
action.addOp(new TextDelete(id));
});

selection[RASTER_IMAGE_KEY].forEach((id) => {
action.addOp(new RasterImageDelete(id));
selection[IMAGE_KEY].forEach((id) => {
action.addOp(new ImageDelete(id));
});

const actionToDeleteRGroupAttachmentPoints = new Action();
Expand Down
10 changes: 5 additions & 5 deletions packages/ketcher-core/src/application/editor/actions/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ import {
SGroupDataMove,
SimpleObjectMove,
TextMove,
RasterImageMove,
ImageMove,
} from '../operations';
import { Pile, RGroup, Vec2 } from 'domain/entities';
import { fromRGroupFragment, fromUpdateIfThen } from './rgroup';

import { Action } from './action';
import { fromAtomsFragmentAttr } from './atom';
import { getRelSGroupsBySelection } from './utils';
import { RASTER_IMAGE_KEY } from 'domain/constants';
import { IMAGE_KEY } from 'domain/constants';

export function fromMultipleMove(restruct, lists, d: Vec2) {
d = new Vec2(d);
Expand Down Expand Up @@ -129,9 +129,9 @@ export function fromMultipleMove(restruct, lists, d: Vec2) {
});
}

if (lists[RASTER_IMAGE_KEY]) {
lists[RASTER_IMAGE_KEY].forEach((rasterImage) => {
action.addOp(new RasterImageMove(rasterImage, d));
if (lists[IMAGE_KEY]) {
lists[IMAGE_KEY].forEach((image) => {
action.addOp(new ImageMove(image, d));
});
}

Expand Down
48 changes: 48 additions & 0 deletions packages/ketcher-core/src/application/editor/actions/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ReStruct } from 'application/render';
import { ImageReferencePositionInfo, Vec2 } from 'domain/entities';
import {
Action,
ImageDelete,
ImageMove,
ImageResize,
ImageUpsert,
} from 'application/editor';
import { Image } from 'domain/entities/image';

export function fromImageCreation(
reStruct: ReStruct,
bitmap: string,
center: Vec2,
halfSize: Vec2,
) {
const action = new Action();
const image = new Image(bitmap, center, halfSize);
action.addOp(new ImageUpsert(image));
return action.perform(reStruct);
}

export function fromImageDeletion(reStruct: ReStruct, id: number) {
const action = new Action();
action.addOp(new ImageDelete(id));
return action.perform(reStruct);
}

export function fromImageMove(reStruct: ReStruct, id: number, offset: Vec2) {
const action = new Action();
action.addOp(new ImageMove(id, offset));
return action.perform(reStruct);
}

export function fromImageResize(
reStruct: ReStruct,
id: number,
position: Vec2,
referencePositionInfo: ImageReferencePositionInfo,
) {
const action = new Action();
const positionWithOffset = position.add(referencePositionInfo.offset);
action.addOp(
new ImageResize(id, positionWithOffset, referencePositionInfo.name),
);
return action.perform(reStruct);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export * from './closelyFusing';
export * from './erase';
export * from './fragment';
export * from './paste';
export * from './rasterImage';
export * from './image';
export * from './reaction';
export * from './rgroup';
export * from './rgroupAttachmentPoint';
Expand Down
12 changes: 6 additions & 6 deletions packages/ketcher-core/src/application/editor/actions/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
FragmentSetProperties,
BondAttr,
AtomAttr,
RasterImageUpsert,
ImageUpsert,
} from '../operations';
import { fromRGroupAttrs, fromUpdateIfThen } from './rgroup';

Expand All @@ -37,7 +37,7 @@ import { SGroup, Struct, Vec2 } from 'domain/entities';
import { fromSgroupAddition } from './sgroup';
import { fromRGroupAttachmentPointAddition } from './rgroupAttachmentPoint';
import { MonomerMicromolecule } from 'domain/entities/monomerMicromolecule';
import { RasterImage } from 'domain/entities/rasterImage';
import { Image } from 'domain/entities/image';

export function fromPaste(
restruct,
Expand Down Expand Up @@ -190,11 +190,11 @@ export function fromPaste(
);
});

pstruct.rasterImages.forEach((rasterImage: RasterImage) => {
const clonedImage = rasterImage.clone();
pstruct.images.forEach((image: Image) => {
const clonedImage = image.clone();
clonedImage.addPositionOffset(offset);

action.addOp(new RasterImageUpsert(clonedImage).perform(restruct));
action.addOp(new ImageUpsert(clonedImage).perform(restruct));
});

pstruct.rgroups.forEach((rg, rgid) => {
Expand Down Expand Up @@ -247,7 +247,7 @@ function getStructCenter(struct: Struct): Vec2 {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (struct.texts.size > 0) return struct.texts.get(0)!.position;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (struct.rasterImages.size > 0) return struct.rasterImages.get(0)!.center();
if (struct.images.size > 0) return struct.images.get(0)!.center();

return new Vec2(0, 0);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ export const OperationType = Object.freeze({
DRAWING_ENTITY_SELECT: 'Select drawing entity',
DRAWING_ENTITY_HOVER: 'Hover drawing entity',
SHOW_POLYMER_BOND_INFORMATION: 'Show polymer bond information',
RASTER_IMAGE_UPSERT: 'Upsert raster image',
RASTER_IMAGE_DELETE: 'Delete raster image',
RASTER_IMAGE_MOVE: 'Move raster image',
RASTER_IMAGE_RESIZE: 'Resize raster image',
IMAGE_UPSERT: 'Upsert image',
IMAGE_DELETE: 'Delete image',
IMAGE_MOVE: 'Move image',
IMAGE_RESIZE: 'Resize image',
});

export enum OperationPriority {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { OperationType } from 'application/editor';
import { ReStruct } from 'application/render';
import { Scale } from 'domain/helpers';

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

execute(reStruct: ReStruct) {
const renderItem = reStruct.rasterImages.get(this.id);
const item = reStruct.molecule.rasterImages.get(this.id);
const renderItem = reStruct.images.get(this.id);
const item = reStruct.molecule.images.get(this.id);

if (!item || !renderItem) {
return;
Expand All @@ -26,6 +26,6 @@ export class RasterImageMove extends BaseOperation {
}

invert(): BaseOperation {
return new RasterImageMove(this.id, this.offset.negated());
return new ImageMove(this.id, this.offset.negated());
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import { BaseOperation } from 'application/editor/operations/base';
import { RasterImageReferenceName, Vec2 } from 'domain/entities';
import { ImageReferenceName, Vec2 } from 'domain/entities';
import { ReStruct } from 'application/render';
import { OperationType } from 'application/editor';

const moveLeftPositions: Array<RasterImageReferenceName> = [
const moveLeftPositions: Array<ImageReferenceName> = [
'topLeftPosition',
'leftMiddlePosition',
'bottomLeftPosition',
];

const moveRightPositions: Array<RasterImageReferenceName> = [
const moveRightPositions: Array<ImageReferenceName> = [
'topRightPosition',
'rightMiddlePosition',
'bottomRightPosition',
];

const moveTopPositions: Array<RasterImageReferenceName> = [
const moveTopPositions: Array<ImageReferenceName> = [
'topLeftPosition',
'topMiddlePosition',
'topRightPosition',
];

const moveBottomPositions: Array<RasterImageReferenceName> = [
const moveBottomPositions: Array<ImageReferenceName> = [
'bottomLeftPosition',
'bottomMiddlePosition',
'bottomRightPosition',
];

export class RasterImageResize extends BaseOperation {
export class ImageResize extends BaseOperation {
private previousPosition: Vec2 | null = null;
constructor(
private id: number,
private position: Vec2,
private referencePositionName: RasterImageReferenceName,
private referencePositionName: ImageReferenceName,
) {
super(OperationType.RASTER_IMAGE_RESIZE);
super(OperationType.IMAGE_RESIZE);
}

execute(reStruct: ReStruct) {
const item = reStruct.molecule.rasterImages.get(this.id);
const renderItem = reStruct.rasterImages.get(this.id);
const item = reStruct.molecule.images.get(this.id);
const renderItem = reStruct.images.get(this.id);

if (!item || !renderItem) {
return;
Expand Down Expand Up @@ -69,7 +69,7 @@ export class RasterImageResize extends BaseOperation {
}

invert(): BaseOperation {
return new RasterImageResize(
return new ImageResize(
this.id,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.previousPosition!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,56 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { BaseOperation } from 'application/editor/operations/base';
import { OperationType } from 'application/editor';
import { RasterImage } from 'domain/entities/rasterImage';
import { Image } from 'domain/entities/image';
import { ReStruct } from 'application/render';
import { ReRasterImage } from 'application/render/restruct/rerasterImage';
import { ReImage } from 'application/render/restruct/reImage';
import { IMAGE_KEY } from 'domain/constants';

export class RasterImageUpsert extends BaseOperation {
constructor(private readonly rasterImage: RasterImage, private id?: number) {
super(OperationType.RASTER_IMAGE_UPSERT);
export class ImageUpsert extends BaseOperation {
constructor(private readonly image: Image, private id?: number) {
super(OperationType.IMAGE_UPSERT);
}

execute(reStruct: ReStruct) {
const struct = reStruct.molecule;

if (!this.id) {
this.id = struct.rasterImages.newId();
if (this.id === undefined) {
this.id = struct.images.newId();
}
const id = this.id;
const item = this.rasterImage.clone();
struct.rasterImages.set(id, item);
reStruct.rasterImages.set(id, new ReRasterImage(item));
const item = this.image.clone();
struct.images.set(id, item);
reStruct.images.set(id, new ReImage(item));

BaseOperation.invalidateItem(reStruct, 'rasterImages', id, 1);
BaseOperation.invalidateItem(reStruct, IMAGE_KEY, id, 1);
}

invert(): BaseOperation {
return new RasterImageDelete(this.id!);
return new ImageDelete(this.id!);
}
}

export class RasterImageDelete extends BaseOperation {
private rasterImage?: RasterImage;
export class ImageDelete extends BaseOperation {
private image?: Image;
constructor(private id: number) {
super(OperationType.RASTER_IMAGE_DELETE);
super(OperationType.IMAGE_DELETE);
}

execute(reStruct: ReStruct) {
const reRasterImage = reStruct.rasterImages.get(this.id);
const reImage = reStruct.images.get(this.id);

if (!reRasterImage) {
if (!reImage) {
return;
}

this.rasterImage = reRasterImage.rasterImage.clone();
reStruct.clearVisel(reRasterImage.visel);
this.image = reImage.image.clone();
reStruct.clearVisel(reImage.visel);
reStruct.markItemRemoved();
reStruct.rasterImages.delete(this.id);
reStruct.molecule.rasterImages.delete(this.id);
reStruct.images.delete(this.id);
reStruct.molecule.images.delete(this.id);
}

invert(): BaseOperation {
return new RasterImageUpsert(this.rasterImage!, this.id);
return new ImageUpsert(this.image!, this.id);
}
}
Loading

0 comments on commit 5fe455d

Please sign in to comment.