-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2246 Multiple bond editing changes bond types to all selected bonds
- Loading branch information
1 parent
b721f6e
commit b95abe8
Showing
11 changed files
with
135 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/ketcher-react/src/script/ui/state/editor/utils/generateCommonProperties.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Atom, Bond } from 'ketcher-core' | ||
|
||
type partialPropertiesOfElement = Partial<{ | ||
[attribute in keyof (Atom | Bond)]: string | number | boolean | ||
}> | ||
|
||
// If all elements have the same value, then this value is used as a baseline | ||
// otherwise it is set to an empty string. Afterwards, empty string denotes that the value is not changed | ||
export function generateCommonProperties( | ||
selectedElements: Atom[] | Bond[], | ||
normalizedElement | ||
): partialPropertiesOfElement { | ||
const properties = Object.getOwnPropertyNames(normalizedElement) | ||
const resultElementAttributes: partialPropertiesOfElement = {} | ||
properties.forEach((property) => { | ||
const uniqueValues = new Set() | ||
selectedElements.forEach((element) => { | ||
uniqueValues.add(element[property]) | ||
}) | ||
const allElementsHaveTheSameValue = uniqueValues.size === 1 | ||
if (allElementsHaveTheSameValue) { | ||
resultElementAttributes[property] = normalizedElement[property] | ||
} else { | ||
resultElementAttributes[property] = '' | ||
} | ||
}) | ||
return resultElementAttributes | ||
} |
1 change: 1 addition & 0 deletions
1
packages/ketcher-react/src/script/ui/state/editor/utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './generateCommonProperties' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Action, Bond, fromBondsAttrs } from 'ketcher-core' | ||
import { updateOnlyChangedProperties } from './utils' | ||
|
||
export function updateSelectedBonds({ | ||
bonds, | ||
changeBondPromise, | ||
editor | ||
}: { | ||
bonds: number[] | ||
changeBondPromise: Promise<Bond> | ||
editor | ||
}) { | ||
const action = new Action() | ||
const struct = editor.render.ctab | ||
const { molecule } = struct | ||
if (bonds) { | ||
Promise.resolve(changeBondPromise) | ||
.then((userChangedBond) => { | ||
bonds.forEach((bondId) => { | ||
const unchangedBond = molecule.bonds.get(bondId) | ||
const bondWithChangedProperties = updateOnlyChangedProperties( | ||
unchangedBond, | ||
userChangedBond | ||
) | ||
action.mergeWith( | ||
fromBondsAttrs( | ||
struct, | ||
bondId, | ||
bondWithChangedProperties as Bond, | ||
false | ||
) | ||
) | ||
}) | ||
editor.update(action) | ||
}) | ||
.catch(() => null) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/ketcher-react/src/script/ui/state/modal/utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './updateOnlyChangedProperties' |
17 changes: 17 additions & 0 deletions
17
packages/ketcher-react/src/script/ui/state/modal/utils/updateOnlyChangedProperties.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export function updateOnlyChangedProperties( | ||
unchangedElement, | ||
userChangedElement | ||
) { | ||
const updatedKeys = Object.getOwnPropertyNames(userChangedElement).filter( | ||
(key) => userChangedElement[key] !== '' | ||
) | ||
return Object.getOwnPropertyNames(unchangedElement).reduce( | ||
(updatedElement, key) => { | ||
updatedElement[key] = updatedKeys.includes(key) | ||
? userChangedElement[key] | ||
: unchangedElement[key] | ||
return updatedElement | ||
}, | ||
{} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters