Skip to content

Commit

Permalink
docs: add reset button to demo page
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jul 24, 2024
1 parent c2a515c commit acb6d45
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 24 deletions.
4 changes: 4 additions & 0 deletions examples/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ button {
@apply bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white py-1 px-2 rounded;
}

select {
@apply bg-white border border-gray-400 hover:border-gray-500 py-1 px-2 rounded;
}

h1 {
@apply text-3xl font-bold mb-3;
}
Expand Down
14 changes: 12 additions & 2 deletions examples/generic_editor/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>Demo - Generic editor</h1>
class="grid gap-2"
style="
grid-template-rows: 30px 600px 30px;
grid-template-columns: 100px 600px;
grid-template-columns: 150px 600px;
grid-template-areas:
'. top-actions . '
'left-actions editor result'
Expand All @@ -24,6 +24,16 @@ <h1>Demo - Generic editor</h1>
<button id="loadFragment">Load fragment</button>
<button id="loadReaction">Load reaction</button>
</div>
<div class="flex flex-col gap-1" style="grid-area: left-actions">
<label>
Mode:
<select id="modeSelect" class="w-full">
<option value="molecule">Molecule</option>
<option value="reaction">Reaction</option>
</select>
</label>
<button id="resetButton">Reset</button>
</div>
<div
id="editor"
class="border border-blue-300"
Expand All @@ -34,7 +44,7 @@ <h1>Demo - Generic editor</h1>
style="grid-area: result; grid-template-columns: fit-content(100%) 1fr"
>
<div>Change count</div>
<div id="changeCount"></div>
<div id="changeCount">0</div>
<div>ID Code</div>
<div id="idcode"></div>
<div>Molfile</div>
Expand Down
33 changes: 11 additions & 22 deletions examples/generic_editor/demo/demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import OCL from '../../../distesm/full.pretty';
import { getEditor, resetEditor } from './editor.ts';

const { CanvasEditor, Molecule, Reaction } = OCL;
const { Molecule, Reaction } = OCL;

const rxn = `$RXN
Expand Down Expand Up @@ -167,32 +168,20 @@ const molfile = `446220
22 43 1 0 0 0 0
M END`;

const changeCountDiv = document.getElementById('changeCount') as HTMLElement;
const idcodeDiv = document.getElementById('idcode') as HTMLElement;
const molfileDiv = document.getElementById('molfile') as HTMLElement;
let changeCount = 0;

const editorElement = document.getElementById('editor') as HTMLElement;
const editor = new CanvasEditor(editorElement, {
initialMode: 'molecule',
});

editor.setOnChangeListener(({ type, isUserEvent }) => {
if (isUserEvent && type === 'molecule') {
changeCountDiv.innerText = String(++changeCount);
const idcodeAndCoords = editor.getMolecule().getIDCodeAndCoordinates();
idcodeDiv.innerText = `${idcodeAndCoords.idCode} ${idcodeAndCoords.coordinates}`;
molfileDiv.innerText = editor.getMolecule().toMolfileV3();
}
});
resetEditor();

const resetButton = document.getElementById('resetButton') as HTMLButtonElement;
resetButton.onclick = () => {
resetEditor();
};

const loadMolecule = document.getElementById(
'loadMolecule',
) as HTMLButtonElement;
loadMolecule.onclick = () => {
// const molecule = Molecule.fromMolfile(molfile);
const molecule = Molecule.fromSmiles('c1ccccc1CO');
editor.setMolecule(molecule);
getEditor().setMolecule(molecule);
};

const loadFragment = document.getElementById(
Expand All @@ -202,7 +191,7 @@ loadFragment.onclick = () => {
// const molecule = Molecule.fromMolfile(molfile);
const molecule = Molecule.fromSmiles('CCC');
molecule.setFragment(true);
editor.setMolecule(molecule);
getEditor().setMolecule(molecule);
};

const loadReaction = document.getElementById(
Expand All @@ -213,5 +202,5 @@ loadReaction.onclick = () => {
// const reaction = Reaction.fromRxn(rxn);
// reaction.addCatalyst(Molecule.fromSmiles('CO'));
console.log(reaction.toSmiles());
editor.setReaction(reaction);
getEditor().setReaction(reaction);
};
43 changes: 43 additions & 0 deletions examples/generic_editor/demo/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import OCL from '../../../distesm/full.pretty';
import {
incrementChangeCount,
resetChangeCount,
updateIDCode,
updateMolfile,
} from './result.ts';

let editor: OCL.CanvasEditor | undefined;

export function resetEditor() {
if (editor) {
editor.destroy();
}
const modeSelect = document.getElementById('modeSelect') as HTMLSelectElement;
const newEditor = new OCL.CanvasEditor(
document.getElementById('editor') as HTMLElement,
{
initialMode: modeSelect.value as OCL.CanvasEditorMode,
},
);

editor = newEditor;

resetChangeCount();

editor.setOnChangeListener(({ type, isUserEvent }) => {
if (type === 'molecule') {
if (isUserEvent) {
incrementChangeCount();
}
updateIDCode(newEditor.getMolecule());
updateMolfile(newEditor.getMolecule());
}
});
}

export function getEditor(): OCL.CanvasEditor {
if (!editor) {
throw new Error('Editor not initialized');
}
return editor;
}
25 changes: 25 additions & 0 deletions examples/generic_editor/demo/result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import OCL from '../../../distesm/full.pretty';

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

export function incrementChangeCount() {
const currentChangeCount = parseInt(changeCountDiv.innerText, 10);
changeCountDiv.innerText = String(currentChangeCount + 1);
}

export function resetChangeCount() {
changeCountDiv.innerText = '0';
}

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

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

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

export function updateMolfile(molecule: OCL.Molecule) {
molfileDiv.innerText = molecule.toMolfileV3();
}
1 change: 1 addition & 0 deletions full.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
OnChangeEventType,
OnChangeEvent,
OnChangeListenerCallback,
CanvasEditorMode,
CanvasEditorOptions,
CanvasEditor,
} from './types';

0 comments on commit acb6d45

Please sign in to comment.