From eb89a1205671f714083a1af8a3aef02dda6d9c83 Mon Sep 17 00:00:00 2001 From: Pedro Lamas Date: Tue, 5 Jul 2022 10:30:38 +0100 Subject: [PATCH] fix: codelens links for moonraker-telegram-bot Signed-off-by: Pedro Lamas --- .../widgets/filesystem/setupMonaco.ts | 48 +++++++++++-------- src/locales/en.yaml | 1 + 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/components/widgets/filesystem/setupMonaco.ts b/src/components/widgets/filesystem/setupMonaco.ts index 0ac7af66c5..0aa445d2a9 100644 --- a/src/components/widgets/filesystem/setupMonaco.ts +++ b/src/components/widgets/filesystem/setupMonaco.ts @@ -6,15 +6,24 @@ import getVueApp from '@/util/get-vue-app' import themeDark from '@/monaco/theme/editor.dark.theme.json' import themeLight from '@/monaco/theme/editor.light.theme.json' -const extruderRegExp = /^extruder[0-9]+$/ - -const getDocsSection = (sectionName: string) => { - if (sectionName.startsWith('stepper_')) { - return 'stepper' - } +type CodeLensSupportedService = 'klipper' | 'moonraker' | 'moonraker-telegram-bot' + +const isCodeLensSupportedService = (service: string) : service is CodeLensSupportedService => [ + 'klipper', + 'moonraker', + 'moonraker-telegram-bot' +].includes(service) + +const getDocsSection = (service: CodeLensSupportedService, sectionName: string) => { + switch (service) { + case 'klipper': + if (sectionName.startsWith('stepper_')) { + return 'stepper' + } - if (extruderRegExp.test(sectionName)) { - return 'extruder' + if (/^extruder[0-9]+$/.test(sectionName)) { + return 'extruder' + } } return sectionName @@ -66,19 +75,20 @@ async function setupMonaco () { const app = getVueApp() - monaco.editor.registerCommand('fluidd_open_docs', (_, isMoonrakerConfig, hash) => { - if (isMoonrakerConfig) { - const url = app.$t('app.file_system.url.moonraker_config', { hash }).toString() - window.open(url) - } else { - const url = app.$t('app.file_system.url.klipper_config', { hash }).toString() - window.open(url) - } + monaco.editor.registerCommand('fluidd_open_docs', (_, service: CodeLensSupportedService, hash: string) => { + const serviceKey = service.replace(/-/g, '_') + const url = app.$t(`app.file_system.url.${serviceKey}_config`, { hash }).toString() + + window.open(url) }) monaco.languages.registerCodeLensProvider('klipper-config', { provideCodeLenses: (model) => { - const isMoonrakerConfig = model.uri.path.toLowerCase().endsWith('/moonraker.conf') + const { service } = app.$store.getters['server/getConfigMapByFilename'](model.uri.path.split('/').pop()) + + if (!isCodeLensSupportedService(service)) { + return null + } const linesContent = model.getLinesContent() @@ -87,7 +97,7 @@ async function setupMonaco () { if (section) { const [sectionName] = section[1].split(' ') - const referenceSection = getDocsSection(sectionName) + const referenceSection = getDocsSection(service, sectionName) return ranges.concat({ referenceSection, @@ -110,7 +120,7 @@ async function setupMonaco () { command: { id: 'fluidd_open_docs', title: app.$t('app.file_system.label.view_section_documentation', { section: section.referenceSection }).toString(), - arguments: [isMoonrakerConfig, section.referenceSection] + arguments: [service, section.referenceSection] } }) ), diff --git a/src/locales/en.yaml b/src/locales/en.yaml index 622092e4b6..7b73689506 100644 --- a/src/locales/en.yaml +++ b/src/locales/en.yaml @@ -103,6 +103,7 @@ app: url: klipper_config: 'https://www.klipper3d.org/Config_Reference.html#%{hash}' moonraker_config: 'https://moonraker.readthedocs.io/en/latest/configuration/#%{hash}' + moonraker_telegram_bot_config: 'https://github.com/nlef/moonraker-telegram-bot/wiki/Sample-config#%{hash}' gcode: btn: load_current_file: Load Current File