Skip to content

Commit

Permalink
feat: Add select message at cursor command
Browse files Browse the repository at this point in the history
  • Loading branch information
ae86jack committed Aug 13, 2024
1 parent a25a53d commit 281b4da
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/commands/select.ts
Original file line number Diff line number Diff line change
@@ -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))
}
})
4 changes: 4 additions & 0 deletions src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions src/lang/locale/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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))
}
Expand Down

0 comments on commit 281b4da

Please sign in to comment.