From b68061ebe77b210539b8a4f2f6198ba3367eeb61 Mon Sep 17 00:00:00 2001 From: valentine195 <38669521+valentine195@users.noreply.github.com> Date: Thu, 19 Aug 2021 10:05:48 -0400 Subject: [PATCH] 6.2.5 - better codemirror import - icon chooser now prefers font-awesome first - added icon module source to chooser - explicitly selecting an icon will now select that exact icon if duplicate names exist (closes #77) --- package.json | 1 + src/codemirror/codemirror.js | 1 - src/main.ts | 26 ++++++++++++++++++-------- src/modal/index.ts | 18 ++++++++++++++---- src/settings.ts | 12 ++++-------- src/util/icons.ts | 12 ++++++++++-- tsconfig.json | 2 +- 7 files changed, 48 insertions(+), 24 deletions(-) delete mode 100644 src/codemirror/codemirror.js diff --git a/package.json b/package.json index a09224f..3bebab3 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@rollup/plugin-typescript": "^6.0.0", "@types/node": "^14.14.2", "@types/object.fromentries": "^2.0.0", + "codemirror": "^5.62.2", "object.fromentries": "^2.0.4", "obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master", "rollup": "^2.32.1", diff --git a/src/codemirror/codemirror.js b/src/codemirror/codemirror.js deleted file mode 100644 index 2c9986b..0000000 --- a/src/codemirror/codemirror.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = CodeMirror; diff --git a/src/main.ts b/src/main.ts index 109aa7d..d7f24b5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,7 +32,14 @@ import { REMOVE_COMMAND_NAME } from "./util"; -import * as CodeMirror from "./codemirror/codemirror"; +/* import * as CodeMirror from "./codemirror/codemirror"; */ +import type codemirror from "codemirror"; + +declare global { + interface Window { + CodeMirror: typeof codemirror; + } +} //add commands to app interface declare module "obsidian" { @@ -81,7 +88,7 @@ Object.fromEntries = import "./assets/main.css"; import AdmonitionSetting from "./settings"; -import { IconName, COPY_BUTTON_ICON, iconNames } from "./util/icons"; +import { IconName, COPY_BUTTON_ICON, iconDefinitions } from "./util/icons"; import { InsertAdmonitionModal } from "./modal"; const DEFAULT_APP_SETTINGS: ISettingsData = { @@ -623,9 +630,12 @@ title: types.forEach((type) => { if (this.data.syntaxHighlight) { /** Process from @deathau's syntax highlight plugin */ - CodeMirror.defineMode(`ad-${type}`, (config, options) => { - return CodeMirror.getMode(config, "hypermd"); - }); + window.CodeMirror.defineMode( + `ad-${type}`, + (config, options) => { + return window.CodeMirror.getMode({}, "hypermd"); + } + ); } }); @@ -639,8 +649,8 @@ title: turnOffSyntaxHighlighting(types: string[] = Object.keys(this.admonitions)) { types.forEach((type) => { - if (CodeMirror.modes.hasOwnProperty(`ad-${type}`)) { - delete CodeMirror.modes[`ad-${type}`]; + if (window.CodeMirror.modes.hasOwnProperty(`ad-${type}`)) { + delete window.CodeMirror.modes[`ad-${type}`]; } }); this.app.workspace.layoutReady @@ -721,7 +731,7 @@ title: let admonitionElement = getAdmonitionElement( type, title, - iconNames.get(icon as RPGIconName | IconName) ?? + iconDefinitions.find(({ name }) => icon === name) ?? this.admonitions[type].icon, color ?? this.admonitions[type].color, collapse, diff --git a/src/modal/index.ts b/src/modal/index.ts index 11aaf35..f3def28 100644 --- a/src/modal/index.ts +++ b/src/modal/index.ts @@ -12,7 +12,12 @@ import { } from "obsidian"; import { createPopper, Instance as PopperInstance } from "@popperjs/core"; -import { getAdmonitionElement, getIconNode, iconNames } from "../util"; +import { + getAdmonitionElement, + getIconModuleName, + getIconNode, + iconDefinitions +} from "../util"; import { Admonition, AdmonitionIconDefinition, @@ -238,9 +243,9 @@ export class IconSuggestionModal extends SuggestionModal) { this.text.setValue(item.name); + this.icon = item; this.onClose(); this.close(); @@ -301,6 +307,10 @@ export class IconSuggestionModal extends SuggestionModal = new Map( }) ); -export const iconNames = new Map([...RPGIconNames, ...FontAwesomeIconNames]); +export const iconDefinitions = [ + ...FontAwesomeIconNames.values(), + ...RPGIconNames.values() +]; export function getIconType(str: string): "rpg" | "font-awesome" { - if (RPG[str as RPGIconName]) return "rpg"; if (findIconDefinition({ iconName: str as IconName, prefix: "fas" })) return "font-awesome"; + if (RPG[str as RPGIconName]) return "rpg"; +} + +export function getIconModuleName(icon: AdmonitionIconDefinition) { + if (icon.type === "rpg") return "RPG Awesome"; + if (icon.type === "font-awesome") return "Font Awesome"; } export function getIconNode(item: AdmonitionIconDefinition): Element { diff --git a/tsconfig.json b/tsconfig.json index 1f566b2..302dc10 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ "importHelpers": true, "lib": ["dom", "es5", "scripthost", "es2019"] }, - "include": ["**/*.ts", "src/codemirror/codemirror.js"] + "include": ["**/*.ts"] }