Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
FelisDiligens committed Aug 1, 2023
1 parent 07aaae2 commit 741d779
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
27 changes: 21 additions & 6 deletions src/cmoptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@
This module exports a CodeMirror plugin that applies the user's options.
*/

import { PluginSettings } from "./settings";

module.exports = {
default: function(context) {
default: function (context) {
return {
plugin: function (CodeMirror) {
// Define a CM option, so we can get a CodeMirror.Editor instance (cm) that we can manipulate:
CodeMirror.defineOption('enableMoreCMOptions', false, async function(cm, val, old) {
// Get all CM options that the user has set:
let cmoptions = await context.postMessage({ name: 'getCMOptions' });
CodeMirror.defineOption('enableMoreCMOptions', false, async function (cm, val, old) {
// Get the plugin settings:
const settings: PluginSettings = await context.postMessage({ name: 'getPluginSettings' });

// A list of all valid CodeMirror 5 options the user can set.
// See https://codemirror.net/5/doc/manual.html#config for all options.
const cmoptions = [
"lineWrapping",
"lineNumbers",
"showCursorWhenSelecting",
"cursorBlinkRate",
"resetSelectionOnContextMenu"
];

// Set each CM option:
for (const [key, value] of Object.entries(cmoptions)) {
cm.setOption(key, value);
for (const [key, value] of Object.entries(settings)) {
// Filter out invalid keys:
if (cmoptions.includes(key)) {
cm.setOption(key, value);
}
}
});
},
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ContentScriptType } from 'api/types';

import {
registerAllSettings,
getCMOptions,
getPluginSettings
} from './settings';

Expand All @@ -24,8 +23,8 @@ joplin.plugins.register({

// Register a message, so our CodeMirror plugin can retrieve the user's preferences:
await joplin.contentScripts.onMessage(CONTENT_SCRIPT_ID, async (message: any) => {
if (message.name === 'getCMOptions') {
return await getCMOptions();
if (message.name === 'getPluginSettings') {
return await getPluginSettings();
}
return "Error: " + message + " is not a valid message";
});
Expand Down
29 changes: 14 additions & 15 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import joplin from 'api';
import { SettingItemType } from 'api/types';

/**
* Returns all CodeMirror options the user can/has set.
* See https://codemirror.net/5/doc/manual.html#config for all options.
*/
export async function getCMOptions() {
return {
"lineWrapping": await joplin.settings.value('cmoptions_lineWrapping') as boolean,
"lineNumbers": await joplin.settings.value('cmoptions_lineNumbers') as boolean,
"showCursorWhenSelecting": await joplin.settings.value('cmoptions_showCursorWhenSelecting') as boolean,
"cursorBlinkRate": await joplin.settings.value('cmoptions_cursorBlinkRate') as number,
"resetSelectionOnContextMenu": await joplin.settings.value('cmoptions_resetSelectionOnContextMenu') as boolean,
}
export interface PluginSettings {
lineWrapping: boolean;
lineNumbers: boolean;
showCursorWhenSelecting: boolean;
cursorBlinkRate: number;
resetSelectionOnContextMenu: boolean;
fixLineNumbersCSS: boolean;
}

/**
* Return all settings that the plugin has registered.
*/
export async function getPluginSettings() {
export async function getPluginSettings(): Promise<PluginSettings> {
return {
...(await getCMOptions()),
"fixLineNumbersCSS": await joplin.settings.value('cmoptions_fixLineNumbersCSS') as boolean,
"lineWrapping": await joplin.settings.value('cmoptions_lineWrapping'),
"lineNumbers": await joplin.settings.value('cmoptions_lineNumbers'),
"showCursorWhenSelecting": await joplin.settings.value('cmoptions_showCursorWhenSelecting'),
"cursorBlinkRate": await joplin.settings.value('cmoptions_cursorBlinkRate'),
"resetSelectionOnContextMenu": await joplin.settings.value('cmoptions_resetSelectionOnContextMenu'),
"fixLineNumbersCSS": await joplin.settings.value('cmoptions_fixLineNumbersCSS'),
}
}

Expand Down

0 comments on commit 741d779

Please sign in to comment.