Skip to content

Commit

Permalink
#1057 tools that modify FGs refer canvas correctly (#1064)
Browse files Browse the repository at this point in the history
* Renamed tools for refactoring to .ts extension

* Refactor Eraser tool

* Refactor Bond tool

* Refactor Chain tool

* Refactor Charge tool

* Refactor Rotate tool

* Refactor Atom tool

* Refactor APoint tool

* Refactored template tool into class with correct reference to canvas

* Refactored sgroup tool into class with correct reference to canvas

* Refactored RGroupFragment tool into class with correct reference to canvas

* Refactored RGroupAtom tool into class with correct reference to canvas

* Refactored Paste tool into class with correct reference to canvas

* Refactored Attach tool into class with correct reference to canvas

Co-authored-by: Viktoriia Fedotkina <[email protected]>
  • Loading branch information
karen-sarkisyan and ViktoriiaFedotkina authored Dec 9, 2021
1 parent a11c128 commit 8e9e05e
Show file tree
Hide file tree
Showing 27 changed files with 3,010 additions and 2,910 deletions.
18 changes: 17 additions & 1 deletion packages/ketcher-react/src/script/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,23 @@ class Editor implements KetcherEditor {
// TODO: when all tools are refactored to classes, remove this check
// and use new keyword for every tool
let tool
if (name === 'select') {
const toolsAsClasses = [
'select',
'eraser',
'bond',
'chain',
'charge',
'rotate',
'atom',
'apoint',
'template',
'sgroup',
'rgroupfragment',
'rgroupatom',
'paste',
'attach'
]
if (toolsAsClasses.includes(name)) {
tool = new toolMap[name](this, opts)
} else {
tool = toolMap[name](this, opts)
Expand Down
88 changes: 0 additions & 88 deletions packages/ketcher-react/src/script/editor/tool/apoint.js

This file was deleted.

93 changes: 93 additions & 0 deletions packages/ketcher-react/src/script/editor/tool/apoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/****************************************************************************
* 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 { fromAtomsAttrs, FunctionalGroup } from 'ketcher-core'
import Editor from '../Editor'

class APointTool {
editor: Editor

constructor(editor) {
this.editor = editor
this.editor.selection(null)
}

mousemove(event) {
const struct = this.editor.render.ctab.molecule
const ci = this.editor.findItem(event, ['atoms'], null)
if (ci) {
const atom = struct.atoms.get(ci.id)
if (atom?.label !== 'R#' && atom?.rglabel === null) this.editor.hover(ci)
} else {
this.editor.hover(null)
}
}

click(event) {
const editor = this.editor
const struct = editor.render.ctab.molecule
const functionalGroups = struct.functionalGroups
const ci = editor.findItem(event, ['atoms'], null)
const atomResult: Array<any> = []
const result: Array<any> = []
if (ci && functionalGroups.size && ci.map === 'atoms') {
const atomId = FunctionalGroup.atomsInFunctionalGroup(
functionalGroups,
ci.id
)
if (atomId !== null) atomResult.push(atomId)
}
if (atomResult.length > 0) {
for (let id of atomResult) {
const fgId = FunctionalGroup.findFunctionalGroupByAtom(
functionalGroups,
id
)
if (fgId !== null && !result.includes(fgId)) {
result.push(fgId)
}
}
this.editor.event.removeFG.dispatch({ fgIds: result })
return
}

if (ci && ci.map === 'atoms') {
this.editor.hover(null)
const atom = struct.atoms.get(ci.id)
if (atom?.label === 'R#' && atom.rglabel !== null) return
const res = editor.event.elementEdit.dispatch({
attpnt: atom?.attpnt
})
Promise.resolve(res)
.then(newatom => {
if (atom?.attpnt !== newatom.attpnt) {
const action = fromAtomsAttrs(
editor.render.ctab,
ci.id,
newatom,
null
)
editor.update(action)
}
})
.catch(() => null) // w/o changes
return true
}
return true
}
}

export default APointTool
Loading

0 comments on commit 8e9e05e

Please sign in to comment.