-
Notifications
You must be signed in to change notification settings - Fork 187
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './rasterImageUpsertDelete'; | ||
export * from './rasterImageMove'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { BaseOperation } from 'application/editor/operations/base'; | ||
import { Vec2 } from 'domain/entities'; | ||
import { OperationType } from 'application/editor'; | ||
import { ReStruct } from 'application/render'; | ||
import { Scale } from 'domain/helpers'; | ||
|
||
export class RasterImageMove extends BaseOperation { | ||
constructor(private id: number, private offset: Vec2) { | ||
super(OperationType.RASTER_IMAGE_MOVE); | ||
} | ||
|
||
execute(reStruct: ReStruct) { | ||
const renderItem = reStruct.rasterImages.get(this.id); | ||
const item = reStruct.molecule.rasterImages.get(this.id); | ||
|
||
if (!item || !renderItem) { | ||
return; | ||
} | ||
const scaledOffset = Scale.modelToCanvas( | ||
this.offset, | ||
reStruct.render.options, | ||
); | ||
|
||
renderItem.move(scaledOffset); | ||
item.addPositionOffset(this.offset); | ||
} | ||
|
||
invert(): BaseOperation { | ||
return new RasterImageMove(this.id, this.offset.negated()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/**************************************************************************** | ||
* 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. | ||
***************************************************************************/ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
/* 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 { ReStruct } from 'application/render'; | ||
import { ReRasterImage } from 'application/render/restruct/rerasterImage'; | ||
|
||
export class RasterImageUpsert extends BaseOperation { | ||
constructor(private readonly rasterImage: RasterImage, private id?: number) { | ||
super(OperationType.RASTER_IMAGE_UPSERT); | ||
} | ||
|
||
execute(reStruct: ReStruct) { | ||
const struct = reStruct.molecule; | ||
|
||
if (!this.id) { | ||
this.id = struct.rasterImages.newId(); | ||
} | ||
const id = this.id; | ||
const item = this.rasterImage.clone(); | ||
struct.rasterImages.set(id, item); | ||
reStruct.rasterImages.set(id, new ReRasterImage(item)); | ||
|
||
BaseOperation.invalidateItem(reStruct, 'rasterImages', id, 1); | ||
} | ||
|
||
invert(): BaseOperation { | ||
return new RasterImageDelete(this.id!); | ||
} | ||
} | ||
|
||
export class RasterImageDelete extends BaseOperation { | ||
private rasterImage?: RasterImage; | ||
constructor(private id: number) { | ||
super(OperationType.RASTER_IMAGE_DELETE); | ||
} | ||
|
||
execute(reStruct: ReStruct) { | ||
const reRasterImage = reStruct.rasterImages.get(this.id); | ||
|
||
if (!reRasterImage) { | ||
return; | ||
} | ||
|
||
reRasterImage.remove(); | ||
reStruct.markItemRemoved(); | ||
reStruct.rasterImages.delete(this.id); | ||
reStruct.molecule.rasterImages.delete(this.id); | ||
} | ||
|
||
invert(): BaseOperation { | ||
return new RasterImageUpsert(this.rasterImage!, this.id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ReObject, ReStruct } from 'application/render/restruct'; | ||
import { RasterImage } from 'domain/entities/rasterImage'; | ||
import { RenderOptions } from 'application/render/render.types'; | ||
import { Scale } from 'domain/helpers'; | ||
import { RaphaelElement } from 'raphael'; | ||
import { Vec2 } from 'domain/entities'; | ||
|
||
export class ReRasterImage extends ReObject { | ||
private element?: RaphaelElement; | ||
|
||
static isSelectable(): boolean { | ||
return true; | ||
} | ||
|
||
constructor(private item: RasterImage) { | ||
super('image'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create constant or object with constants to reuse for all such scenarios in future |
||
} | ||
|
||
show(restruct: ReStruct, options: RenderOptions) { | ||
if (this.element) { | ||
this.remove(); | ||
} | ||
|
||
const render = restruct.render; | ||
const [topLeft, bottomRight] = this.item.position; | ||
const scaledTopLeft = Scale.modelToCanvas(topLeft, options); | ||
const scaledBottomRight = Scale.modelToCanvas(bottomRight, options); | ||
const topLeftWithOffset = scaledTopLeft.add(options.offset); | ||
|
||
const width = scaledBottomRight.x - scaledTopLeft.x; | ||
const height = scaledBottomRight.y - scaledTopLeft.y; | ||
|
||
this.element = render.paper.image( | ||
this.item.bitmap, | ||
topLeftWithOffset.x, | ||
topLeftWithOffset.y, | ||
width, | ||
height, | ||
); | ||
} | ||
|
||
remove() { | ||
if (this.element) { | ||
this.element.remove(); | ||
} | ||
} | ||
|
||
move(diff: Vec2) { | ||
if (this.element) { | ||
this.element.translate(diff.x, diff.y); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/**************************************************************************** | ||
* 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 { BaseMicromoleculeEntity } from 'domain/entities/BaseMicromoleculeEntity'; | ||
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.map((item) => item) as Position, | ||
); | ||
Comment on lines
+30
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
rescalePosition(scale: number): void { | ||
this.position = this.position.map((item) => item.scaled(scale)) as Position; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move 'rasterImages' to constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of better to object with constants to use it for all such invalidation items names in future (including existing ones)