Skip to content

Commit

Permalink
#2110 new template/paste/merge logic for groups WiP
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Permiakov authored and Stanislav Permiakov committed Feb 6, 2023
1 parent f65b5fd commit 99e7453
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
53 changes: 34 additions & 19 deletions packages/ketcher-react/src/script/editor/tool/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
FunctionalGroup,
SGroup,
ReStruct,
Struct
Struct,
fromPaste
} from 'ketcher-core'

import utils from '../shared/utils'
Expand All @@ -37,6 +38,7 @@ class TemplateTool {
template: any
findItems: Array<string>
dragCtx: any
followAction: any

constructor(editor, tmpl) {
this.editor = editor
Expand Down Expand Up @@ -80,6 +82,11 @@ class TemplateTool {
}

mousedown(event) {
if (this.followAction) {
this.followAction.perform(this.editor.render.ctab)
delete this.followAction
}

const closestItem = this.editor.findItem(event, [
'atoms',
'bonds',
Expand Down Expand Up @@ -165,21 +172,16 @@ class TemplateTool {
this.editor.event.removeFG.dispatch({ fgIds: result })
return
}
// eslint-disable-line max-statements
const editor = this.editor
this.editor.hover(null)

const ci = this.editor.findItem(event, this.findItems)

this.dragCtx = {
xy0: editor.render.page2obj(event),
item: editor.findItem(event, this.findItems)
xy0: this.editor.render.page2obj(event),
...(ci && { item: ci })
}

const dragCtx = this.dragCtx
const ci = dragCtx.item

if (!ci) {
// ci.type == 'Canvas'
delete dragCtx.item
return true
}

Expand Down Expand Up @@ -223,13 +225,13 @@ class TemplateTool {
})
}

dragCtx.v0 = xy0.scaled(1 / count)
this.dragCtx.v0 = xy0.scaled(1 / count)

const sign = getSign(molecule, bond, dragCtx.v0)
const sign = getSign(molecule, bond, this.dragCtx.v0)

// calculate default template flip
dragCtx.sign1 = sign || 1
dragCtx.sign2 = this.template.sign
this.dragCtx.sign1 = sign || 1
this.dragCtx.sign2 = this.template.sign
}

return true
Expand All @@ -239,11 +241,21 @@ class TemplateTool {
const restruct = this.editor.render.ctab

if (!this.dragCtx) {
this.editor.hover(
this.editor.findItem(event, this.findItems),
null,
event
if (this.followAction) {
this.followAction.perform(this.editor.render.ctab)
}

const [followAction /* followItems */] = fromPaste(
this.editor.render.ctab,
this.template.molecule,
this.editor.render.page2obj(event)
)

this.followAction = followAction
this.editor.update(followAction, true)

const ci = this.editor.findItem(event, this.findItems)
this.editor.hover(ci, null, event)
return true
}

Expand Down Expand Up @@ -302,7 +314,6 @@ class TemplateTool {

// calc angle
let angle = utils.calcAngle(pos0, pos1)

if (!event.ctrlKey) {
angle = utils.fracAngle(angle, null)
}
Expand Down Expand Up @@ -489,6 +500,10 @@ class TemplateTool {
}

cancel(e) {
if (this.followAction) {
this.followAction.perform(this.editor.render.ctab)
delete this.followAction
}
this.mouseup(e)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ const TemplateDialog: FC<Props> = (props) => {
const selectTemplate = (template, props, dispatch) => {
dispatch(selectTmpl(null))
if (!template) return
template.mode = 'fg'
dispatch(changeFilter(''))
dispatch(selectTmpl(template))
dispatch(onAction({ tool: 'template', opts: template }))
Expand Down

0 comments on commit 99e7453

Please sign in to comment.