Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/generic-editor' into generic-editor
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisseau committed Jul 25, 2024
2 parents 1e542ac + f7eda57 commit a3c6b70
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 16 deletions.
4 changes: 4 additions & 0 deletions __tests__/__snapshots__/library.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ exports[`prototype properties of Transformer 1`] = `

exports[`prototype properties of Util 1`] = `[]`;

exports[`prototype properties of registerCustomElement 1`] = `[]`;

exports[`static properties of CanonizerUtil 1`] = `
[
"BACKBONE",
Expand Down Expand Up @@ -780,3 +782,5 @@ exports[`static properties of Util 1`] = `
"getHoseCodesFromDiastereotopicID",
]
`;

exports[`static properties of registerCustomElement 1`] = `[]`;
1 change: 1 addition & 0 deletions __tests__/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const coreAPI = [

const fullAPI = [
'CanvasEditor',
'registerCustomElement',
'StructureView',
'StructureEditor',
'SVGRenderer',
Expand Down
4 changes: 2 additions & 2 deletions examples/generic_editor/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ <h1>Generic editor - Demo</h1>
<div id="changeCount">0</div>
<div>ID Code</div>
<div id="idcode"></div>
<div>Molfile</div>
<div id="molfile"></div>
<label for="molfile">Molfile or RXN</label>
<textarea class="font-mono p-2" rows="12" id="molfile"></textarea>
</div>
</section>
</body>
Expand Down
16 changes: 13 additions & 3 deletions examples/generic_editor/demo/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
incrementChangeCount,
resetChangeCount,
updateIDCode,
updateMolfile,
updateMolfileOrRxn,
} from './result.ts';

let editor: OCL.CanvasEditor | undefined;
Expand All @@ -23,14 +23,24 @@ export function resetEditor() {
editor = newEditor;

resetChangeCount();
updateIDCode('');
updateMolfileOrRxn('');

editor.setOnChangeListener(({ type, isUserEvent }) => {
if (type === 'molecule') {
if (isUserEvent) {
incrementChangeCount();
}
updateIDCode(newEditor.getMolecule());
updateMolfile(newEditor.getMolecule());
const mode = newEditor.getMode();
if (mode === 'molecule') {
const molecule = newEditor.getMolecule();
updateIDCode(molecule.getIDCode());
updateMolfileOrRxn(molecule.toMolfileV3());
} else {
const reaction = newEditor.getReaction();
updateIDCode(OCL.ReactionEncoder.encode(reaction));
updateMolfileOrRxn(reaction.toRxnV3());
}
}
});
}
Expand Down
13 changes: 5 additions & 8 deletions examples/generic_editor/demo/result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import OCL from '../../../distesm/full.pretty';

const changeCountDiv = document.getElementById('changeCount') as HTMLElement;

export function incrementChangeCount() {
Expand All @@ -13,13 +11,12 @@ export function resetChangeCount() {

const idcodeDiv = document.getElementById('idcode') as HTMLElement;

export function updateIDCode(molecule: OCL.Molecule) {
const idcodeAndCoords = molecule.getIDCodeAndCoordinates();
idcodeDiv.innerText = `${idcodeAndCoords.idCode} ${idcodeAndCoords.coordinates}`;
export function updateIDCode(idCode: string) {
idcodeDiv.innerText = idCode;
}

const molfileDiv = document.getElementById('molfile') as HTMLElement;
const molfileArea = document.getElementById('molfile') as HTMLTextAreaElement;

export function updateMolfile(molecule: OCL.Molecule) {
molfileDiv.innerText = molecule.toMolfileV3();
export function updateMolfileOrRxn(value: string) {
molfileArea.value = value;
}
2 changes: 1 addition & 1 deletion full.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const OCL = require('./dist/openchemlib-full.js');
require('./lib/canvas_editor/init')(OCL);

exports.CanvasEditor = OCL.CanvasEditor;
exports.registerCanvasEditor = OCL.registerCanvasEditor;
exports.registerCustomElement = OCL.registerCustomElement;

exports.default = OCL;
exports.CanonizerUtil = OCL.CanonizerUtil;
Expand Down
2 changes: 1 addition & 1 deletion full.pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const OCL = require('./dist/openchemlib-full.pretty.js');
require('./lib/canvas_editor/init')(OCL);

exports.CanvasEditor = OCL.CanvasEditor;
exports.registerCanvasEditor = OCL.registerCanvasEditor;
exports.registerCustomElement = OCL.registerCustomElement;

exports.default = OCL;
exports.CanonizerUtil = OCL.CanonizerUtil;
Expand Down
8 changes: 7 additions & 1 deletion lib/canvas_editor/init/canvas_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ function initCanvasEditor(JavaEditorArea, JavaEditorToolbar, JavaUIHelper) {
this.#destroy = destroy;
}

getMode() {
const mode = this.#editorArea.getMode();
const isReaction = mode & (JavaEditorArea.MODE_REACTION !== 0);
return isReaction ? 'reaction' : 'molecule';
}

setMolecule(molecule) {
this.#checkNotDestroyed();

Expand All @@ -60,7 +66,7 @@ function initCanvasEditor(JavaEditorArea, JavaEditorToolbar, JavaUIHelper) {
getReaction() {
this.#checkNotDestroyed();

this.#editorArea.getReaction();
return this.#editorArea.getReaction();
}

setOnChangeListener(onChange) {
Expand Down
4 changes: 4 additions & 0 deletions src/com/actelion/research/gwt/gui/generic/JSEditorArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,8 @@ public native void repaint()
public void clearAll() {
mDrawArea.clearAll();
}

public int getMode() {
return mDrawArea.getMode();
}
}
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3729,6 +3729,11 @@ export declare class CanvasEditor {
*/
constructor(element: HTMLElement, options?: CanvasEditorOptions);

/**
* Get the current editor mode.
*/
getMode(): CanvasEditorMode;

/**
* Set the molecule to be edited.
* Actions in the editor will mutate the molecule object directly.
Expand Down

0 comments on commit a3c6b70

Please sign in to comment.