Skip to content

Commit

Permalink
#4902 - delete image, fixed image selection, moved text consts
Browse files Browse the repository at this point in the history
  • Loading branch information
daniil-sloboda committed Jul 12, 2024
1 parent 1c19dc9 commit 972f229
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 21 deletions.
6 changes: 6 additions & 0 deletions packages/ketcher-core/src/application/editor/actions/erase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
AtomDelete,
BondDelete,
CalcImplicitH,
RasterImageDelete,
RGroupAttachmentPointRemove,
RxnArrowDelete,
RxnPlusDelete,
Expand All @@ -39,6 +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';

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

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

const actionToDeleteRGroupAttachmentPoints = new Action();
selection.rgroupAttachmentPoints.forEach((id) => {
if (!removedRGroupAttachmentPoints.includes(id)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import {
TextMove,
RasterImageMove,
} from '../operations';
import { Pile, RASTER_IMAGE_KEY, RGroup, Vec2 } from 'domain/entities';
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';

export function fromMultipleMove(restruct, lists, d: Vec2) {
d = new Vec2(d);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ReStruct } from 'application/render';
import { Vec2 } from 'domain/entities';
import { Action, RasterImageUpsert } from 'application/editor';
import {
Action,
RasterImageDelete,
RasterImageUpsert,
} from 'application/editor';
import { RasterImage } from 'domain/entities/rasterImage';

export function fromRasterImageCreation(
Expand All @@ -14,3 +18,9 @@ export function fromRasterImageCreation(
action.addOp(new RasterImageUpsert(rasterImage));
return action.perform(reStruct);
}

export function fromRasterImageDeletion(reStruct: ReStruct, id: number) {
const action = new Action();
action.addOp(new RasterImageDelete(id));
return action.perform(reStruct);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class RasterImageDelete extends BaseOperation {
return;
}

reStruct.clearVisel(reRasterImage.visel);
reRasterImage.remove();
reStruct.markItemRemoved();
reStruct.rasterImages.delete(this.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
***************************************************************************/

import { RASTER_IMAGE_KEY } from 'domain/constants';

export const SgContexts = {
Fragment: 'Fragment',
Multifragment: 'Multifragment',
Expand All @@ -33,6 +35,7 @@ export const selectionKeys = [
'rxnPluses',
'simpleObjects',
'texts',
RASTER_IMAGE_KEY,
] as const;

export const defaultBondThickness = 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ReObject, ReStruct } from 'application/render/restruct';
import { RASTER_IMAGE_KEY, RasterImage } from 'domain/entities/rasterImage';
import { RasterImage } from 'domain/entities/rasterImage';
import { RenderOptions } from 'application/render/render.types';
import { Scale } from 'domain/helpers';
import { RaphaelElement } from 'raphael';
import { Box2Abs, Vec2 } from 'domain/entities';
import draw from 'application/render/draw';
import { RASTER_IMAGE_KEY } from 'domain/constants';

export class ReRasterImage extends ReObject {
private element?: RaphaelElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Visel from './visel';
import util from '../util';
import { ReRGroupAttachmentPoint } from './rergroupAttachmentPoint';
import { ReRasterImage } from 'application/render/restruct/rerasterImage';
import { RASTER_IMAGE_KEY } from 'domain/constants';

class ReStruct {
public static readonly maps = {
Expand All @@ -58,9 +59,7 @@ class ReStruct {
reloops: ReLoop,
simpleObjects: ReSimpleObject,
texts: ReText,
// We cannot use const RASTER_IMAGE_KEY here because this is being executed before initializing the variable
// But it must contain the same value otherwise the code will stop working
rasterImages: ReRasterImage,
[RASTER_IMAGE_KEY]: ReRasterImage,
} as const;

public render: Render;
Expand Down
1 change: 1 addition & 0 deletions packages/ketcher-core/src/domain/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './elementColor';
export * from './elements';
export * from './element.types';
export * from './generics';
export * from './rasterImage';
2 changes: 2 additions & 0 deletions packages/ketcher-core/src/domain/constants/rasterImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const RASTER_IMAGE_KEY = 'rasterImages';
export const RASTER_IMAGE_SERIALIZE_KEY = 'rasterImage';
5 changes: 1 addition & 4 deletions packages/ketcher-core/src/domain/entities/rasterImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
import { BaseMicromoleculeEntity } from 'domain/entities/BaseMicromoleculeEntity';
import { Point, Vec2 } from 'domain/entities/vec2';
import { getNodeWithInvertedYCoord, KetFileNode } from 'domain/serializers';
import { RASTER_IMAGE_SERIALIZE_KEY } from 'domain/constants';

interface KetFileNodeContent {
bitmap: string;
halfSize: Point;
}

// Having the key as plural guarantees that restruct map
export const RASTER_IMAGE_KEY = 'rasterImages';
export const RASTER_IMAGE_SERIALIZE_KEY = 'rasterImage';

export class RasterImage extends BaseMicromoleculeEntity {
constructor(
public bitmap: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {
Atom,
Bond,
RASTER_IMAGE_SERIALIZE_KEY,
SGroupAttachmentPoint,
Struct,
UnresolvedMonomer,
Expand Down Expand Up @@ -80,6 +79,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_SERIALIZE_KEY } from 'domain/constants';

function parseNode(node: any, struct: any) {
const type = node.type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
***************************************************************************/

import { RASTER_IMAGE_KEY } from 'domain/entities/rasterImage';
import { RASTER_IMAGE_SERIALIZE_KEY } from 'domain/constants';

export function rasterImageToKet(rasterImageNode) {
return {
type: RASTER_IMAGE_KEY,
type: RASTER_IMAGE_SERIALIZE_KEY,
center: rasterImageNode.center,
data: rasterImageNode.data,
selected: rasterImageNode.selected,
Expand Down
4 changes: 2 additions & 2 deletions packages/ketcher-react/src/script/editor/shared/closest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,8 @@ function findClosestRasterImage(
reStruct: ReStruct,
cursorPosition: Vec2,
): ClosestReturnType {
return Array.from(reStruct.rasterImages.values()).reduce(
(acc: ClosestReturnType, item, id) => {
return Array.from(reStruct.rasterImages.entries()).reduce(
(acc: ClosestReturnType, [id, item]) => {
const distanceToPoint =
item.rasterImage.calculateDistanceToPoint(cursorPosition);
if (distanceToPoint < SELECTION_DISTANCE_COEFFICIENT) {
Expand Down
5 changes: 5 additions & 0 deletions packages/ketcher-react/src/script/editor/tool/eraser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import {
fromOneAtomDeletion,
fromOneBondDeletion,
fromPlusDeletion,
fromRasterImageDeletion,
fromRGroupAttachmentPointDeletion,
fromSgroupDeletion,
fromSimpleObjectDeletion,
fromTextDeletion,
FunctionalGroup,
SGroup,
RASTER_IMAGE_KEY,
} from 'ketcher-core';

import LassoHelper from './helper/lasso';
Expand All @@ -52,6 +54,7 @@ class EraserTool implements Tool {
'simpleObjects',
'texts',
'rgroupAttachmentPoints',
RASTER_IMAGE_KEY,
];
this.lassoHelper = new LassoHelper(mode || 0, editor, null);

Expand Down Expand Up @@ -368,6 +371,8 @@ class EraserTool implements Tool {
this.editor.update(fromTextDeletion(restruct, ci.id));
} else if (ci.map === 'rgroupAttachmentPoints') {
this.editor.update(fromRGroupAttachmentPointDeletion(restruct, ci.id));
} else if (ci.map === RASTER_IMAGE_KEY) {
this.editor.update(fromRasterImageDeletion(restruct, ci.id));
} else {
// TODO re-factoring needed - should be "map-independent"
console.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
RASTER_IMAGE_KEY,
Struct,
Vec2,
ReRasterImage,
ReStruct,
} from 'ketcher-core';

Expand Down Expand Up @@ -176,8 +175,8 @@ function getElementsInRectangle(restruct: ReStruct, p0, p1) {
}
});

const rerasterImages = Array.from(restruct.rasterImages.values()).reduce(
(acc: Array<number>, item: ReRasterImage, id): Array<number> => {
const rerasterImages = Array.from(restruct.rasterImages.entries()).reduce(
(acc: Array<number>, [id, item]): Array<number> => {
if (
Object.values(item.rasterImage.getReferencePositions()).some((point) =>
point.isInsidePolygon([
Expand Down Expand Up @@ -336,8 +335,8 @@ function getElementsInPolygon(restruct: ReStruct, rr) {
}
});

const rerasterImages = Array.from(restruct.rasterImages.values()).reduce(
(acc: Array<number>, item: ReRasterImage, id) => {
const rerasterImages = Array.from(restruct.rasterImages.entries()).reduce(
(acc: Array<number>, [id, item]) => {
if (
Object.values(item.rasterImage.getReferencePositions()).some((point) =>
isPointInPolygon(r, point),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
import { RASTER_IMAGE_KEY } from 'domain/entities/rasterImage';
import { RASTER_IMAGE_KEY } from 'domain/constants';

type TopGroup = 'document' | 'edit' | 'zoom' | 'process' | 'meta';

Expand Down

0 comments on commit 972f229

Please sign in to comment.