Skip to content

Commit

Permalink
refactor: move readOnly to an options object
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jul 23, 2024
1 parent 9c0eb44 commit 581bba4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions examples/generic_editor_poc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import OCL from '../distesm/full.pretty';

const { CanvasEditor, Molecule, Reaction } = OCL;

const rxn = `$RXN
Expand Down
1 change: 1 addition & 0 deletions full.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export {
OnChangeEventType,
OnChangeEvent,
OnChangeListenerCallback,
CanvasEditorOptions,
CanvasEditor,
} from './types';
7 changes: 4 additions & 3 deletions lib/canvas_editor/create_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const UIHelper = require('./ui_helper');

function createEditor(
rootElement,
options,
onChange,
isReadOnly,
JavaEditorArea,
JavaEditorToolbar,
JavaUIHelper,
) {
const { readOnly = false } = options;
const childElement = document.createElement('div');
childElement.setAttribute(
'style',
Expand Down Expand Up @@ -47,7 +48,7 @@ function createEditor(

editorArea.draw();

const toolbar = isReadOnly
const toolbar = readOnly
? null
: new Toolbar(toolbarCanvas, editorArea, JavaEditorArea, JavaEditorToolbar);

Expand All @@ -58,7 +59,7 @@ function createEditor(
});
resizeObserver.observe(editorContainer);

if (isReadOnly) {
if (readOnly) {
toolbarCanvas.remove();
} else {
addMouseListeners(editorCanvas, editorArea, JavaEditorArea);
Expand Down
7 changes: 4 additions & 3 deletions lib/canvas_editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ function createCanvasEditor(JavaEditorArea, JavaEditorToolbar, JavaUIHelper) {
#changeEventMap;
#isReadOnly;

constructor(rootElement, isReadOnly = false) {
this.#isReadOnly = isReadOnly;
constructor(rootElement, options = {}) {
const { readOnly = false } = options;
this.#isReadOnly = readOnly;
const { editorArea, toolbar, uiHelper } = createEditor(
rootElement,
options,
(event) => this.#handleChange(event),
isReadOnly,
JavaEditorArea,
JavaEditorToolbar,
JavaUIHelper,
Expand Down
28 changes: 18 additions & 10 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
export interface IMoleculeFromSmilesOptions {
/**
* Disable coordinate invention.
* @default `false`
* @default false
*/
noCoordinates?: boolean;

/**
* Disable stereo features parsing.
* @default `false`
* @default false
*/
noStereo?: boolean;
}
Expand Down Expand Up @@ -115,17 +115,17 @@ export interface IHoseCodesOptions {
export interface ISmilesGeneratorOptions {
/**
* Whether to create SMILES with SMARTS capabilities.
* @default `false`
* @default false
*/
createSmarts?: boolean;
/**
* Whether to include mapping information (atomMapNo) in the SMILES.
* @default `false`
* @default false
*/
includeMapping?: boolean;
/**
* Should localisation of double bonds be attempted?
* @default `false`
* @default false
*/
kekulizedOutput?: boolean;
}
Expand Down Expand Up @@ -2674,7 +2674,7 @@ export declare class Molecule {
export interface ISmilesParserOptions {
/**
* Enable SMARTS parsing with `'smarts'` or `'guess'`.
* @default `'smiles'`
* @default 'smiles'
*/
smartsMode?: 'smiles' | 'smarts' | 'guess';

Expand Down Expand Up @@ -2703,13 +2703,13 @@ export interface ISmilesParserParseMoleculeOptions {

/**
* Disable coordinate invention.
* @default `false`
* @default false
*/
noCoordinates?: boolean;

/**
* Disable stereo features parsing.
* @default `false`
* @default false
*/
noStereo?: boolean;
}
Expand Down Expand Up @@ -3706,13 +3706,21 @@ export interface OnChangeEvent {

export type OnChangeListenerCallback = (event: OnChangeEvent) => void;

export interface CanvasEditorOptions {
/**
* No toolbar and user interactions are ignored.
* @default false
*/
readOnly?: boolean;
}

export declare class CanvasEditor {
/**
* Create a new canvas-based editor.
* @param element - The DOM element in which to create the editor.
* @param isReadOnly - No toolbar and user interactions are ignored.
* @param options
*/
constructor(element: HTMLElement, isReadOnly = false);
constructor(element: HTMLElement, options?: CanvasEditorOptions);

/**
* Set the molecule to be edited.
Expand Down

0 comments on commit 581bba4

Please sign in to comment.