Skip to content
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

feat: implement addFragment method #1759

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface Editor {
isDitrty: () => boolean
setOrigin: () => void
struct: (struct?: Struct) => Struct
structToAddFragment: (struct: Struct) => Struct
subscribe: (eventName: string, handler: (data?: any) => any) => any
unsubscribe: (eventName: string, subscriber: any) => void
selection: (arg?: Selection | 'all' | null) => Selection | null
Expand Down
34 changes: 26 additions & 8 deletions packages/ketcher-core/src/application/ketcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ import { MolfileFormat } from 'domain/serializers'
import { Struct } from 'domain/entities'
import assert from 'assert'

async function prepareStructToRender(
structStr: string,
structService: StructService
): Promise<Struct> {
const struct: Struct = await parseStruct(structStr, structService)
struct.initHalfBonds()
struct.initNeighbors()
struct.setImplicitHydrogen()
struct.markFragments()

return struct
}

function parseStruct(structStr: string, structService: StructService) {
const format = identifyStructFormat(structStr)
const factory = new FormatterFactory(structService)
Expand Down Expand Up @@ -150,18 +163,23 @@ export class Ketcher {
async setMolecule(structStr: string): Promise<void> {
assert(typeof structStr === 'string')

const struct: Struct = await parseStruct(structStr, this.#structService)
struct.initHalfBonds()
struct.initNeighbors()
struct.setImplicitHydrogen()
struct.markFragments()
const struct: Struct = await prepareStructToRender(
structStr,
this.#structService
)

this.#editor.struct(struct)
}

async addFragment(fragment: string): Promise<void> {
assert(typeof fragment === 'string')
async addFragment(structStr: string): Promise<void> {
assert(typeof structStr === 'string')

const struct: Struct = await prepareStructToRender(
structStr,
this.#structService
)

throw Error('not implemented yet')
this.#editor.structToAddFragment(struct)
}

recognize(image: Blob, version?: string): Promise<Struct> {
Expand Down
25 changes: 20 additions & 5 deletions packages/ketcher-react/src/script/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ interface Selection {
rxnPluses?: Array<number>
rxnArrows?: Array<number>
}

class Editor implements KetcherEditor {
#origin?: any
render: Render
Expand Down Expand Up @@ -146,6 +147,8 @@ class Editor implements KetcherEditor {
this.historyPtr = 0
this.errorHandler = null
this.highlights = new Highlighter(this)
this.renderAndRecoordinateStruct =
this.renderAndRecoordinateStruct.bind(this)

this.event = {
message: new Subscription(),
Expand Down Expand Up @@ -209,19 +212,31 @@ class Editor implements KetcherEditor {
this.struct(undefined)
}

renderAndRecoordinateStruct(struct: Struct) {
const action = fromNewCanvas(this.render.ctab, struct)
this.update(action)

const structCenter = getStructCenter(this.render.ctab)
recoordinate(this, structCenter)
return this.render.ctab.molecule
}

struct(value?: Struct): Struct {
if (arguments.length === 0) {
return this.render.ctab.molecule
}

this.selection(null)
const struct = value || new Struct()
const action = fromNewCanvas(this.render.ctab, struct)
this.update(action)

const structCenter = getStructCenter(this.render.ctab)
recoordinate(this, structCenter)
return this.render.ctab.molecule
return this.renderAndRecoordinateStruct(struct)
}

// this is used by API addFragment method
structToAddFragment(value: Struct): Struct {
const superStruct = value.mergeInto(this.render.ctab.molecule)

return this.renderAndRecoordinateStruct(superStruct)
}

options(value?: any) {
Expand Down