From 281b4da52b81fa5ceef0463b20643a56808b58f1 Mon Sep 17 00:00:00 2001 From: C Jack Date: Tue, 13 Aug 2024 09:49:30 +0800 Subject: [PATCH] feat: Add select message at cursor command --- src/commands/select.ts | 19 +++++++++++++++++++ src/lang/locale/en.ts | 4 ++++ src/lang/locale/zh-cn.ts | 4 ++++ src/main.ts | 2 ++ 4 files changed, 29 insertions(+) create mode 100644 src/commands/select.ts diff --git a/src/commands/select.ts b/src/commands/select.ts new file mode 100644 index 0000000..13f5911 --- /dev/null +++ b/src/commands/select.ts @@ -0,0 +1,19 @@ +import { App, Command, Editor, MarkdownView, Notice } from 'obsidian' +import { buildRunEnv, getMsgPositionByLine } from 'src/editor' +import { t } from 'src/lang/helper' +import { PluginSettings } from 'src/settings' + +export const selectMsgAtCursorCmd = (app: App, settings: PluginSettings): Command => ({ + id: 'select-message-at-cursor', + name: t('Select message at cursor'), + editorCallback: async (editor: Editor, view: MarkdownView) => { + const env = await buildRunEnv(app, settings) + const currentLine = editor.getCursor('to').line + const [startOffset, endOffset] = getMsgPositionByLine(env, currentLine) + if (startOffset === -1 || endOffset === -1) { + new Notice(t('No message found at cursor')) + return + } + editor.setSelection(editor.offsetToPos(startOffset), editor.offsetToPos(endOffset)) + } +}) diff --git a/src/lang/locale/en.ts b/src/lang/locale/en.ts index 5261dd0..0d95204 100644 --- a/src/lang/locale/en.ts +++ b/src/lang/locale/en.ts @@ -17,6 +17,10 @@ export default { 'Replace the names of the two most frequently occurring speakers with tag format.', Replace: 'Replace', + // commands/select.ts + 'Select message at cursor': 'Select message at cursor', + 'No message found at cursor': 'No message found at cursor', + // providers 'API key is required': 'API key is required', 'API secret is required': 'API secret is required', diff --git a/src/lang/locale/zh-cn.ts b/src/lang/locale/zh-cn.ts index e7651d2..20b171b 100644 --- a/src/lang/locale/zh-cn.ts +++ b/src/lang/locale/zh-cn.ts @@ -17,6 +17,10 @@ export default { '用标签格式替换两个最常出现的说话者的名字', Replace: '替换', + // commands/select.ts + 'Select message at cursor': '选择光标处的消息', + 'No message found at cursor': '光标处没有找到消息', + // providers 'API key is required': '请配置对应的 API key', 'API secret is required': '请配置对应的 API secret', diff --git a/src/main.ts b/src/main.ts index c1733f5..c03fa67 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import { Plugin } from 'obsidian' import { exportCmd } from './commands/export' import { replaceCmd } from './commands/replaceTag' +import { selectMsgAtCursorCmd } from './commands/select' import { TarsSettingTab } from './settingTab' import { DEFAULT_SETTINGS, PluginSettings } from './settings' import { TagEditorSuggest } from './suggest' @@ -17,6 +18,7 @@ export default class TarsPlugin extends Plugin { this.addCommand(replaceCmd(this.app)) this.addCommand(exportCmd(this.app, this.settings)) + this.addCommand(selectMsgAtCursorCmd(this.app, this.settings)) this.addSettingTab(new TarsSettingTab(this.app, this)) }