Skip to content

Commit

Permalink
Rename smilesDrawerOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Acylation committed Feb 11, 2024
1 parent 06f35a8 commit b0eb899
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 38 deletions.
8 changes: 5 additions & 3 deletions src/SmilesBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class SmilesBlock extends MarkdownRenderChild {
this.renderCell(rows[0], div, this.theme);
} else {
const table = this.el.createDiv({ cls: 'chem-table' });
const maxWidth = this.settings.options.moleculeOptions.width ?? 300;
const maxWidth =
this.settings.smilesDrawerOptions.moleculeOptions.width ?? 300;

rows.forEach((row) => {
const cell = table.createDiv({ cls: 'chem-cell' });
Expand All @@ -64,7 +65,8 @@ export class SmilesBlock extends MarkdownRenderChild {
});

table.style.gridTemplateColumns = `repeat(auto-fill, minmax(${
this.settings.options.moleculeOptions.width?.toString() ?? '300'
this.settings.smilesDrawerOptions.moleculeOptions.width?.toString() ??
'300'
}px, 1fr)`;
}
}
Expand Down Expand Up @@ -148,7 +150,7 @@ export class SmilesBlock extends MarkdownRenderChild {
errorCb(error, target.createEl('div'));
}
);
if (this.settings.options.moleculeOptions.scale == 0)
if (this.settings.smilesDrawerOptions.moleculeOptions.scale == 0)
svg.style.width = `${this.settings.imgWidth.toString()}px`;
return svg;
};
Expand Down
2 changes: 1 addition & 1 deletion src/SmilesInline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function inlinePlugin(settings: ChemPluginSettings) {
errorCb(error, target.createEl('div'));
}
);
if (settings.options.moleculeOptions.scale == 0)
if (settings.smilesDrawerOptions.moleculeOptions.scale == 0)
svg.style.width = `${settings.imgWidth.toString()}px`;
return svg;
};
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default class ChemPlugin extends Plugin {

// initialize global variables
setDrawer(
this.settings.options.moleculeOptions,
this.settings.options.reactionOptions
this.settings.smilesDrawerOptions.moleculeOptions,
this.settings.smilesDrawerOptions.reactionOptions
);
setBlocks();
setObserver();
Expand Down
19 changes: 11 additions & 8 deletions src/settings/LivePreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class LivePreview {
this.settings.darkTheme
);

if (this.settings.options.moleculeOptions.scale == 0)
if (this.settings.smilesDrawerOptions.moleculeOptions.scale == 0)
this.container.style.gridTemplateColumns = `repeat(auto-fill, minmax(${(
this.settings?.imgWidth ?? 300
).toString()}px, 1fr)`;
Expand Down Expand Up @@ -83,16 +83,18 @@ export class LivePreview {
container.style.display = `grid`;
container.style.alignContent = `center`;

if (this.settings.options.moleculeOptions.scale == 0)
if (this.settings.smilesDrawerOptions.moleculeOptions.scale == 0)
container.style.width = `${(
this.settings?.imgWidth ?? 300
).toString()}px`;
else if (
container.offsetWidth >
(this.settings.options.moleculeOptions?.width ?? 300)
(this.settings.smilesDrawerOptions.moleculeOptions?.width ??
300)
) {
container.style.width = `${(
this.settings.options.moleculeOptions?.width ?? 300
this.settings.smilesDrawerOptions.moleculeOptions?.width ??
300
).toString()}px`;
}
};
Expand All @@ -107,21 +109,22 @@ export class LivePreview {
errorCb(error, target.createEl('div'));
}
);
if (this.settings.options.moleculeOptions.scale == 0)
if (this.settings.smilesDrawerOptions.moleculeOptions.scale == 0)
svg.style.width = `${(
this.settings?.imgWidth ?? 300
).toString()}px`;
else if (
parseFloat(svg.style.width) >
(this.settings.options.moleculeOptions?.width ?? 300)
(this.settings.smilesDrawerOptions.moleculeOptions?.width ?? 300)
) {
const r =
parseFloat(svg.style.width) / parseFloat(svg.style.height);
svg.style.width = `${(
this.settings.options.moleculeOptions?.width ?? 300
this.settings.smilesDrawerOptions.moleculeOptions?.width ?? 300
).toString()}px`;
svg.style.height = `${(
(this.settings.options.moleculeOptions?.width ?? 300) / r
(this.settings.smilesDrawerOptions.moleculeOptions?.width ??
300) / r
).toString()}px`;
}
return parseFloat(svg.style.width);
Expand Down
39 changes: 24 additions & 15 deletions src/settings/SettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ChemSettingTab extends PluginSettingTab {
.setIcon('rotate-ccw')
.setTooltip(i18n.t('settings.scale.description'))
.onClick(async () => {
this.plugin.settings.options.moleculeOptions.scale = 1;
this.plugin.settings.smilesDrawerOptions.moleculeOptions.scale = 1;
scaleSlider.setValue(50);
await this.plugin.saveSettings();
this.updateDrawer();
Expand All @@ -46,18 +46,24 @@ export class ChemSettingTab extends PluginSettingTab {

const scaleLabel = scaleSetting.controlEl.createDiv('slider-readout');
scaleLabel.setText(
(this.plugin.settings.options.moleculeOptions.scale ?? 1.0)
(
this.plugin.settings.smilesDrawerOptions.moleculeOptions
.scale ?? 1.0
)
.toFixed(2)
.toString()
);

const scaleSlider = new SliderComponent(scaleSetting.controlEl)
.setValue(
50 * (this.plugin.settings.options.moleculeOptions.scale ?? 1.0)
50 *
(this.plugin.settings.smilesDrawerOptions.moleculeOptions
.scale ?? 1.0)
)
.setLimits(0.0, 100, 0.5)
.onChange(async (value) => {
this.plugin.settings.options.moleculeOptions.scale = value / 50;
this.plugin.settings.smilesDrawerOptions.moleculeOptions.scale =
value / 50;
scaleLabel.setText((value / 50).toFixed(2).toString());
await this.plugin.saveSettings();
this.updateDrawer();
Expand Down Expand Up @@ -142,11 +148,11 @@ export class ChemSettingTab extends PluginSettingTab {
.addToggle((toggle) =>
toggle
.setValue(
this.plugin.settings.options.moleculeOptions
this.plugin.settings.smilesDrawerOptions.moleculeOptions
.compactDrawing ?? false
)
.onChange(async (value) => {
this.plugin.settings.options.moleculeOptions.compactDrawing =
this.plugin.settings.smilesDrawerOptions.moleculeOptions.compactDrawing =
value;
await this.plugin.saveSettings();
this.updateDrawer();
Expand All @@ -160,11 +166,11 @@ export class ChemSettingTab extends PluginSettingTab {
.addToggle((toggle) =>
toggle
.setValue(
this.plugin.settings.options.moleculeOptions
this.plugin.settings.smilesDrawerOptions.moleculeOptions
.terminalCarbons ?? false
)
.onChange(async (value) => {
this.plugin.settings.options.moleculeOptions.terminalCarbons =
this.plugin.settings.smilesDrawerOptions.moleculeOptions.terminalCarbons =
value;
await this.plugin.saveSettings();
this.updateDrawer();
Expand All @@ -178,11 +184,11 @@ export class ChemSettingTab extends PluginSettingTab {
.addToggle((toggle) =>
toggle
.setValue(
this.plugin.settings.options.moleculeOptions
this.plugin.settings.smilesDrawerOptions.moleculeOptions
.explicitHydrogens ?? false
)
.onChange(async (value) => {
this.plugin.settings.options.moleculeOptions.explicitHydrogens =
this.plugin.settings.smilesDrawerOptions.moleculeOptions.explicitHydrogens =
value;
await this.plugin.saveSettings();
this.updateDrawer();
Expand Down Expand Up @@ -302,14 +308,14 @@ export class ChemSettingTab extends PluginSettingTab {
.addText((text) =>
text
.setValue(
this.plugin.settings.options.moleculeOptions.width?.toString() ??
this.plugin.settings.smilesDrawerOptions.moleculeOptions.width?.toString() ??
'300'
)
.onChange(async (value) => {
if (value == '') {
value = '300';
}
this.plugin.settings.options.moleculeOptions.width =
this.plugin.settings.smilesDrawerOptions.moleculeOptions.width =
parseInt(value);
await this.plugin.saveSettings();
this.updateDrawer();
Expand Down Expand Up @@ -341,7 +347,10 @@ export class ChemSettingTab extends PluginSettingTab {

// Initialize
preview.render();
if ((this.plugin.settings.options.moleculeOptions.scale ?? 1) == 0)
if (
(this.plugin.settings.smilesDrawerOptions.moleculeOptions.scale ??
1) == 0
)
unifyImageWidth();
else unifyBondLength();
}
Expand All @@ -354,11 +363,11 @@ export class ChemSettingTab extends PluginSettingTab {
setDrawer(
{
...DEFAULT_SD_OPTIONS.moleculeOptions,
...this.plugin.settings.options.moleculeOptions,
...this.plugin.settings.smilesDrawerOptions.moleculeOptions,
},
{
...DEFAULT_SD_OPTIONS.reactionOptions,
...this.plugin.settings.options.reactionOptions,
...this.plugin.settings.smilesDrawerOptions.reactionOptions,
}
);
}
17 changes: 10 additions & 7 deletions src/settings/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ const migrate_2_3 = (v2: ChemPluginSettingsV2): ChemPluginSettingsV3 => {
dataview: v2.dataview,
inlineSmiles: v2.inlineSmiles,
inlineSmilesPrefix: v2.inlineSmilesPrefix,
options: {
smilesDrawerOptions: {
moleculeOptions: v2.options,
reactionOptions: {},
},
};
};

export const migrateSettings = (draft: any) => {
const migrate_3_3 = (draft: ChemPluginSettingsV3): ChemPluginSettingsV3 => {
return { ...DEFAULT_SETTINGS, ...draft };
};

export const migrateSettings = (draft: any): ChemPluginSettingsV3 => {

Check warning on line 59 in src/settings/base.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
if (Object.keys(draft).length === 0) return DEFAULT_SETTINGS;
else if (!('version' in draft)) return migrate_1_3(draft);
else if ('version' in draft && draft.version === 'v2')
return migrate_2_3({ ...draft, ...DEFAULT_SETTINGS_V2 });
else if ('version' in draft && draft.version === SETTINGS_VERSION)
return { ...DEFAULT_SETTINGS, ...draft };
if (!('version' in draft)) return migrate_1_3(draft); // v1
else if (draft.version === 'v2') return migrate_2_3(draft);
else if (draft.version === 'v3') return migrate_3_3(draft); // current
return DEFAULT_SETTINGS; // consider branch coverage rate
};
6 changes: 4 additions & 2 deletions src/settings/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export interface ChemPluginSettingsV3 {
dataview: boolean;
inlineSmiles: boolean;
inlineSmilesPrefix: string;
options: {
smilesDrawerOptions: {
moleculeOptions: Partial<SD_MoleculeOptions>;
reactionOptions: Partial<SD_ReactionOptions>;
};
rdkitJsOptions: {};
}
export const DEFAULT_SETTINGS_V3: ChemPluginSettingsV3 = {
version: 'v3',
Expand All @@ -36,8 +37,9 @@ export const DEFAULT_SETTINGS_V3: ChemPluginSettingsV3 = {
dataview: false,
inlineSmiles: false,
inlineSmilesPrefix: '$smiles=',
options: {
smilesDrawerOptions: {
moleculeOptions: {},
reactionOptions: {},
},
rdkitJsOptions: {},
};

0 comments on commit b0eb899

Please sign in to comment.