Skip to content

Commit 732d87e

Browse files
authored
Revert "#1072 refactor all editor tools to classes and ts (#1075)" (#1114)
This reverts commit bacda27.
1 parent 23a5ab5 commit 732d87e

12 files changed

+592
-614
lines changed

packages/ketcher-react/src/script/editor/Editor.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,30 @@ class Editor implements KetcherEditor {
186186
this._tool.cancel()
187187
}
188188

189-
let tool = new toolMap[name](this, opts)
190-
189+
// TODO: when all tools are refactored to classes, remove this check
190+
// and use new keyword for every tool
191+
let tool
192+
const toolsAsClasses = [
193+
'select',
194+
'eraser',
195+
'bond',
196+
'chain',
197+
'charge',
198+
'rotate',
199+
'atom',
200+
'apoint',
201+
'template',
202+
'sgroup',
203+
'rgroupfragment',
204+
'rgroupatom',
205+
'paste',
206+
'attach'
207+
]
208+
if (toolsAsClasses.includes(name)) {
209+
tool = new toolMap[name](this, opts)
210+
} else {
211+
tool = toolMap[name](this, opts)
212+
}
191213
if (!tool) {
192214
return null
193215
}

packages/ketcher-react/src/script/editor/tool/enhanced-stereo.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,25 @@ import {
2323

2424
import Editor from '../Editor'
2525

26-
class EnhancedStereoTool {
27-
editor: Editor | undefined
28-
29-
constructor(editor) {
26+
function EnhancedStereoTool(
27+
this: typeof EnhancedStereoTool,
28+
editor: Editor
29+
): null | void {
30+
if (!(this instanceof EnhancedStereoTool)) {
3031
const selection = editor.selection()
32+
3133
const stereoAtoms = findStereoAtoms(
3234
editor.struct(),
3335
selection
3436
? selection.atoms || []
3537
: Array.from(editor.struct().atoms.keys())
3638
)
3739

38-
if (stereoAtoms.length !== 0) {
39-
changeAtomsStereoAction(editor, stereoAtoms).then(
40-
action => action && editor.update(action)
41-
)
42-
}
40+
if (stereoAtoms.length === 0) return null
41+
42+
changeAtomsStereoAction(editor, stereoAtoms).then(
43+
action => action && editor.update(action)
44+
)
4345
}
4446
}
4547

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/****************************************************************************
2+
* Copyright 2021 EPAM Systems
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
***************************************************************************/
16+
17+
import {
18+
Vec2,
19+
fromArrowAddition,
20+
fromArrowDeletion,
21+
fromArrowResizing,
22+
fromMultipleMove
23+
} from 'ketcher-core'
24+
25+
function ReactionArrowTool(editor, mode) {
26+
if (!(this instanceof ReactionArrowTool))
27+
return new ReactionArrowTool(editor, mode)
28+
29+
this.mode = mode
30+
this.editor = editor
31+
this.editor.selection(null)
32+
}
33+
34+
ReactionArrowTool.prototype.mousedown = function (event) {
35+
var rnd = this.editor.render
36+
const p0 = rnd.page2obj(event)
37+
this.dragCtx = { p0 }
38+
39+
var ci = this.editor.findItem(event, ['rxnArrows'])
40+
if (ci && ci.map === 'rxnArrows') {
41+
this.editor.hover(null)
42+
this.editor.selection({ rxnArrows: [ci.id] })
43+
this.dragCtx.ci = ci
44+
} else {
45+
this.dragCtx.isNew = true
46+
this.editor.selection(null)
47+
}
48+
}
49+
50+
ReactionArrowTool.prototype.mousemove = function (event) {
51+
var rnd = this.editor.render
52+
if (this.dragCtx) {
53+
const current = rnd.page2obj(event)
54+
const diff = current.sub(this.dragCtx.p0)
55+
this.dragCtx.previous = current
56+
if (this.dragCtx.ci) {
57+
if (this.dragCtx.action) this.dragCtx.action.perform(rnd.ctab)
58+
if (!this.dragCtx.ci.ref) {
59+
this.dragCtx.action = fromMultipleMove(
60+
rnd.ctab,
61+
this.editor.selection() || {},
62+
diff
63+
)
64+
} else {
65+
this.dragCtx.action = fromArrowResizing(
66+
rnd.ctab,
67+
this.dragCtx.ci.id,
68+
diff,
69+
current,
70+
this.dragCtx.ci.ref
71+
)
72+
}
73+
this.editor.update(this.dragCtx.action, true)
74+
} else {
75+
if (!this.dragCtx.action) {
76+
const action = fromArrowAddition(
77+
rnd.ctab,
78+
[this.dragCtx.p0, this.dragCtx.p0],
79+
this.mode
80+
)
81+
//TODO: need to rework actions/operations logic
82+
const addOperation = action.operations[0]
83+
const itemId = addOperation.data.id
84+
this.dragCtx.itemId = itemId
85+
this.dragCtx.action = action
86+
this.editor.update(this.dragCtx.action, true)
87+
} else {
88+
this.dragCtx.action.perform(rnd.ctab)
89+
}
90+
91+
this.dragCtx.action = fromArrowResizing(
92+
rnd.ctab,
93+
this.dragCtx.itemId,
94+
diff,
95+
current,
96+
null
97+
)
98+
this.editor.update(this.dragCtx.action, true)
99+
}
100+
} else {
101+
const items = this.editor.findItem(event, ['rxnArrows'])
102+
this.editor.hover(items)
103+
}
104+
}
105+
106+
ReactionArrowTool.prototype.mouseup = function (event) {
107+
if (!this.dragCtx) return true
108+
const rnd = this.editor.render
109+
110+
const p0 = this.dragCtx.p0
111+
const p1 = getDefaultLengthPos(p0, this.dragCtx.previous)
112+
113+
if (this.dragCtx.action) {
114+
if (this.dragCtx.isNew) {
115+
this.editor.update(fromArrowDeletion(rnd.ctab, this.dragCtx.itemId), true)
116+
this.dragCtx.action = fromArrowAddition(rnd.ctab, [p0, p1], this.mode)
117+
}
118+
this.editor.update(this.dragCtx.action)
119+
}
120+
delete this.dragCtx
121+
return true
122+
}
123+
124+
ReactionArrowTool.prototype.click = function (event) {
125+
const rnd = this.editor.render
126+
const ci = this.editor.findItem(event, ['rxnArrows'])
127+
const p0 = rnd.page2obj(event)
128+
if (!ci) {
129+
this.editor.update(
130+
fromArrowAddition(rnd.ctab, [p0, getDefaultLengthPos(p0)], this.mode)
131+
)
132+
}
133+
}
134+
135+
function getArrowParams(x1, y1, x2, y2) {
136+
const length = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
137+
const angle = calcAngle(x2, y2, x1, y1)
138+
return { length, angle }
139+
}
140+
141+
function getDefaultLengthPos(pos1, pos2) {
142+
const minLength = 1.5
143+
const defaultLength = 2
144+
if (!pos2) {
145+
return new Vec2(pos1.x + defaultLength, pos1.y)
146+
}
147+
const arrowParams = getArrowParams(pos1.x, pos1.y, pos2.x, pos2.y)
148+
if (arrowParams.length <= minLength) {
149+
const newPos = new Vec2()
150+
newPos.x =
151+
pos1.x + defaultLength * Math.cos((Math.PI * arrowParams.angle) / 180)
152+
newPos.y =
153+
pos1.y + defaultLength * Math.sin((Math.PI * arrowParams.angle) / 180)
154+
return newPos
155+
}
156+
return pos2
157+
}
158+
159+
function calcAngle(x1, y1, x2, y2) {
160+
const x = x1 - x2,
161+
y = y1 - y2
162+
if (!x && !y) {
163+
return 0
164+
}
165+
return (180 + (Math.atan2(-y, -x) * 180) / Math.PI + 360) % 360
166+
}
167+
168+
export default ReactionArrowTool

0 commit comments

Comments
 (0)