Skip to content

Commit

Permalink
#3666 make opening structure in center of the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlaStarla committed Dec 23, 2023
1 parent 6aba3da commit b7738bf
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Coordinates from 'application/editor/shared/coordinates';
import { Coordinates } from 'application/editor/shared/coordinates';
import ZoomTool from 'application/editor/tools/Zoom';
import { ZoomTransform } from 'd3';
import { Vec2 } from 'domain/entities';
Expand Down
2 changes: 1 addition & 1 deletion packages/ketcher-core/src/application/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { MonomerItemType } from 'domain/types';
import { RenderersManager } from 'application/render/renderers/RenderersManager';
import { DrawingEntitiesManager } from 'domain/entities/DrawingEntitiesManager';
import ZoomTool from './tools/Zoom';
import Coordinates from './shared/coordinates';
import { Coordinates } from './shared/coordinates';
import {
editorEvents,
renderersEvents,
Expand Down
1 change: 1 addition & 0 deletions packages/ketcher-core/src/application/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const vectorUtils = {
export * from './operations';
export * from './actions';
export * from './shared/constants';
export * from './shared/coordinates';
export * from './editor.types';
export * from './Editor';
export * from './EditorHistory';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ZoomTool from '../tools/Zoom';
* `canvas` -- The real coordinates used to draw entities
* `view` -- The zoomed canvas coordinates
*/
class Coordinates {
export class Coordinates {
static canvasToModel(position: Vec2) {
const settings = provideEditorSettings();
return position.scaled(1 / settings.scale);
Expand Down Expand Up @@ -38,5 +38,3 @@ class Coordinates {
return ZoomTool.instance.invertZoom(position);
}
}

export default Coordinates;
2 changes: 1 addition & 1 deletion packages/ketcher-core/src/application/editor/tools/Bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Peptide } from 'domain/entities/Peptide';
import { Sugar } from 'domain/entities/Sugar';
import { RNABase } from 'domain/entities/RNABase';
import { Phosphate } from 'domain/entities/Phosphate';
import Coordinates from 'application/editor/shared/coordinates';
import { Coordinates } from '../shared/coordinates';

class PolymerBond implements BaseTool {
private bondRenderer?: PolymerBondRenderer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { BaseMonomerRenderer } from 'application/render/renderers';
import { MonomerItemType } from 'domain/types';
import { monomerFactory } from '../operations/monomer/monomerFactory';
import assert from 'assert';
import Coordinates from '../shared/coordinates';
import { Coordinates } from '../shared/coordinates';

class MonomerTool implements BaseTool {
private monomerPreview:
Expand Down
82 changes: 82 additions & 0 deletions packages/ketcher-core/src/application/editor/tools/Paste.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// /****************************************************************************
// * Copyright 2021 EPAM Systems
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// ***************************************************************************/
// import { Coordinates, CoreEditor, EditorHistory } from 'application/editor';
// import { BaseTool } from 'application/editor/tools/Tool';
// import { SupportedFormat, identifyStructFormat } from 'application/formatters';
// import assert from 'assert';
// import { IndigoProvider } from 'ketcher-react';
// import { KetSerializer } from 'domain/serializers';
// import { ChemicalMimeType, StructService } from 'domain/services';
// import { Vec2 } from 'domain/entities';

// class PasteTool implements BaseTool {
// private history: EditorHistory;
// private readonly struct: string;

// constructor(private editor: CoreEditor, struct: string) {
// this.editor = editor;
// this.struct = struct;
// this.history = new EditorHistory(editor);
// this.convertStruct();
// }

// async convertStruct() {
// const isKet = identifyStructFormat(this.struct) === SupportedFormat.ket;
// const ketSerializer = new KetSerializer();
// const editor = CoreEditor.provideEditorInstance();
// if (isKet) {
// this.addToCanvas({ struct: this.struct, ketSerializer, editor });
// return;
// }
// const indigo = IndigoProvider.getIndigo() as StructService;
// const ketStruct = await indigo.convert({
// struct: this.struct,
// output_format: ChemicalMimeType.KET,
// });
// this.addToCanvas({ struct: ketStruct.struct, ketSerializer, editor });
// }

// addToCanvas({
// ketSerializer,
// editor,
// struct,
// }: {
// ketSerializer: KetSerializer;
// editor: CoreEditor;
// struct: string;
// }) {
// const centerPointOfCanvas = Coordinates.canvasToModel(
// new Vec2(
// this.editor.canvasOffset.width / 2,
// this.editor.canvasOffset.height / 2,
// ),
// );
// const deserialisedKet = ketSerializer.deserializeToDrawingEntities(
// struct,
// centerPointOfCanvas,
// );
// assert(deserialisedKet);
// deserialisedKet.drawingEntitiesManager.mergeInto(
// editor.drawingEntitiesManager,
// );
// this.history.update(deserialisedKet.modelChanges);
// editor.renderersContainer.update(deserialisedKet.modelChanges);
// }

// destroy() {}
// }

// export { PasteTool };
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { MonomerItemType } from 'domain/types';
import { monomerFactory } from '../operations/monomer/monomerFactory';
import { RNABase } from 'domain/entities/RNABase';
import { Phosphate } from 'domain/entities/Phosphate';
import Coordinates from 'application/editor/shared/coordinates';
import { Coordinates } from '../shared/coordinates';

class RnaPresetTool implements Tool {
rnaBase: MonomerItemType | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { brush as d3Brush, select } from 'd3';
import { BaseRenderer } from 'application/render/renderers/BaseRenderer';
import { Command } from 'domain/entities/Command';
import { BaseTool } from 'application/editor/tools/Tool';
import Coordinates from '../shared/coordinates';
import { Coordinates } from '../shared/coordinates';

class SelectRectangle implements BaseTool {
private brush;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
checkFor0and360,
} from 'domain/helpers/attachmentPointCalculations';
import { AttachmentPoint } from 'domain/AttachmentPoint';
import Coordinates from 'application/editor/shared/coordinates';
import { Vec2 } from 'domain/entities';
import {
AttachmentPointConstructorParams,
AttachmentPointName,
} from 'domain/types';
import { Coordinates } from 'application/editor/shared/coordinates';

export abstract class BaseMonomerRenderer extends BaseRenderer {
private editorEvents: typeof editorEvents;
Expand Down
2 changes: 1 addition & 1 deletion packages/ketcher-core/src/domain/entities/DrawingEntity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Vec2 } from 'domain/entities/vec2';
import { BaseRenderer } from 'application/render/renderers/BaseRenderer';
import assert from 'assert';
import Coordinates from 'application/editor/shared/coordinates';
import { Coordinates } from 'application/editor/shared/coordinates';
let id = 0;

export abstract class DrawingEntity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CoordinatesTool from 'application/editor/shared/coordinates';
import { Coordinates as CoordinatesTool } from 'application/editor/shared/coordinates';
import { BaseMonomer } from 'domain/entities';
import { Vec2 } from 'domain/entities/vec2';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import {
} from 'application/formatters/types/ket';
import { Struct, Vec2 } from 'domain/entities';
import { DrawingEntitiesManager } from 'domain/entities/DrawingEntitiesManager';
import { switchIntoChemistryCoordSystem } from 'domain/serializers/ket/helpers';

export function monomerToDrawingEntity(
node: IKetMonomerNode,
template: IKetMonomerTemplate,
struct: Struct,
drawingEntitiesManager: DrawingEntitiesManager,
offset?: Vec2,
) {
const position: Vec2 = switchIntoChemistryCoordSystem(
new Vec2(node.position.x, node.position.y),
);
const position: Vec2 = offset
? new Vec2(node.position).add(offset)
: new Vec2(node.position);
return drawingEntitiesManager.addMonomer(
{
struct,
Expand Down
35 changes: 31 additions & 4 deletions packages/ketcher-core/src/domain/serializers/ket/ketSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ export class KetSerializer implements Serializer<Struct> {
return { error: true };
}
let error = false;
let xmin = 1e50;
let ymin = xmin;
let xmax = -xmin;
let ymax = -ymin;

parsedFileContent.root.nodes.forEach((node) => {
const nodeDefinition = parsedFileContent[node.$ref];

Expand All @@ -224,6 +229,13 @@ export class KetSerializer implements Serializer<Struct> {
parsedFileContent,
editor,
);
nodeDefinition.position = switchIntoChemistryCoordSystem(

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
user controlled input
.
new Vec2(nodeDefinition.position.x, nodeDefinition.position.y),
);
xmin = Math.min(xmin, nodeDefinition.position.x);
ymin = Math.min(ymin, nodeDefinition.position.y);
xmax = Math.max(xmax, nodeDefinition.position.x);
ymax = Math.max(ymax, nodeDefinition.position.y);
}
});
if (error) {
Expand All @@ -234,7 +246,11 @@ export class KetSerializer implements Serializer<Struct> {
this.validateConnectionTypeAndEndpoints(connection, editor);
},
);
return { error, parsedFileContent };
return {
error,
parsedFileContent,
structCenter: new Vec2((xmin + xmax) / 2, (ymin + ymax) / 2),
};
}

deserializeToStruct(fileContent: string) {
Expand Down Expand Up @@ -284,10 +300,20 @@ export class KetSerializer implements Serializer<Struct> {
return fileContentForMicromolecules;
}

deserializeToDrawingEntities(fileContent: string) {
const { error: hasValidationErrors, parsedFileContent } =
this.parseAndValidateMacromolecules(fileContent);
deserializeToDrawingEntities(
fileContent: string,
centerPointOfCanvas?: Vec2,
) {
const {
error: hasValidationErrors,
parsedFileContent,
structCenter,
} = this.parseAndValidateMacromolecules(fileContent);
if (hasValidationErrors || !parsedFileContent) return;
const offset = Vec2.diff(
centerPointOfCanvas || new Vec2(0, 0),
structCenter,
);
const command = new Command();
const drawingEntitiesManager = new DrawingEntitiesManager();
const monomerIdsMap = {};
Expand All @@ -313,6 +339,7 @@ export class KetSerializer implements Serializer<Struct> {
template,
struct,
drawingEntitiesManager,
offset,
);
const monomer = monomerAdditionCommand.operations[0]
.monomer as BaseMonomer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
CoreEditor,
KetcherLogger,
EditorHistory,
Vec2,
Coordinates,
} from 'ketcher-core';
import { IndigoProvider } from 'ketcher-react';
import assert from 'assert';
Expand Down Expand Up @@ -60,7 +62,13 @@ const addToCanvas = ({
editor: CoreEditor;
struct: string;
}) => {
const deserialisedKet = ketSerializer.deserializeToDrawingEntities(struct);
const centerPointOfCanvas = Coordinates.canvasToModel(
new Vec2(editor.canvasOffset.width / 2, editor.canvasOffset.height / 2),
);
const deserialisedKet = ketSerializer.deserializeToDrawingEntities(
struct,
centerPointOfCanvas,
);
assert(deserialisedKet);
deserialisedKet.drawingEntitiesManager.mergeInto(
editor.drawingEntitiesManager,
Expand Down

0 comments on commit b7738bf

Please sign in to comment.