Skip to content

Commit

Permalink
fix: external link modifier for local files
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Oct 17, 2024
1 parent 10857b1 commit a2abecc
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 44 deletions.
10 changes: 5 additions & 5 deletions addons/explorer/src/renderer/FileExplorer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ const files = $computed(() => {
return result
})
function selectFile(file: FileEntity) {
function selectFile(event: MouseEvent, file: FileEntity) {
if (file.isDirectory) {
modelValue = file.path
} else {
commas.remote.openFile(file.path)
commas.ui.openItem(file.path, event)
}
}
Expand Down Expand Up @@ -88,7 +88,7 @@ function goBack() {
function openExternal(file: FileEntity) {
if (file.isDirectory) {
commas.remote.openDirectoryExternally(file.path)
commas.remote.openFileExternally(file.path)
} else {
commas.remote.showFileExternally(file.path)
}
Expand All @@ -108,7 +108,7 @@ function openExternalExplorer() {
}
function openDirectory() {
commas.remote.openDirectoryExternally(modelValue)
commas.remote.openFileExternally(modelValue)
}
function openNewTab() {
Expand Down Expand Up @@ -212,7 +212,7 @@ function autoselect(event: FocusEvent) {
:key="file.name"
draggable="true"
:class="['file', { directory: file.isDirectory }]"
@click="selectFile(file)"
@click="selectFile($event, file)"
@dragstart.prevent="startDragging($event, file)"
>
<VisualIcon
Expand Down
4 changes: 2 additions & 2 deletions addons/launcher/src/renderer/LauncherLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import * as commas from 'commas:api/renderer'
const { vI18n } = commas.ui.vueAssets
function openLaunchers() {
function openLaunchers(event: MouseEvent) {
const examplePath = path.join(__dirname, '../../examples/launchers.yaml')
commas.remote.openUserFile('launchers.yaml', examplePath)
commas.remote.openUserFile('launchers.yaml', examplePath, event)
}
</script>

Expand Down
2 changes: 1 addition & 1 deletion addons/launcher/src/renderer/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export async function startLauncherExternally(launcher: Launcher) {
)
if (!explorer) {
if (launcher.remote) return
return commas.remote.openDirectory(directory)
return commas.ui.openFolder(directory)
}
explorer = explorer
.replace(/\$\{directory\}/g, directory)
Expand Down
28 changes: 14 additions & 14 deletions addons/preference/src/renderer/PreferencePane.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ const aboutItems = $computed(() => getItems('about'))
const version = commas.app.getVersion()
function openUserDirectory() {
commas.remote.openUserDirectory()
function openUserDirectory(event: MouseEvent) {
commas.remote.openUserDirectory(event)
}
function openDefaultSettings() {
commas.remote.openDefaultSettings()
function openDefaultSettings(event: MouseEvent) {
commas.remote.openDefaultSettings(event)
}
function openSettingsFile() {
commas.remote.openSettingsFile()
function openSettingsFile(event: MouseEvent) {
commas.remote.openSettingsFile(event)
}
const language = $(commas.remote.useLanguage())
Expand All @@ -44,20 +44,20 @@ const supportedLanguages = $computed(() => {
return languages
})
function openKeyBindings() {
commas.remote.openUserFile('keybindings.yaml')
function openKeyBindings(event: MouseEvent) {
commas.remote.openUserFile('keybindings.yaml', undefined, event)
}
function openTranslation() {
commas.remote.openUserFile('translation.yaml')
function openTranslation(event: MouseEvent) {
commas.remote.openUserFile('translation.yaml', undefined, event)
}
function openCustomJS() {
commas.remote.openUserFile('custom.js')
function openCustomJS(event: MouseEvent) {
commas.remote.openUserFile('custom.js', undefined, event)
}
function openCustomCSS() {
commas.remote.openUserFile('custom.css')
function openCustomCSS(event: MouseEvent) {
commas.remote.openUserFile('custom.css', undefined, event)
}
function openWebsite(event: MouseEvent) {
Expand Down
3 changes: 2 additions & 1 deletion addons/settings/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"Use ${directory} or ${remote} to interpolate these variables at runtime#!settings.comments.1.terminal.external.remoteExplorer": "使用 ${directory} 或 ${remote} 以在运行时插入这些变量",
"Skip by default#!settings.comments.2.terminal.external.remoteExplorer": "默认跳过",
"Extra External Link Modifier#!settings.label.terminal.external.extraLinkModifier": "外部链接额外修饰键",
"Extra keyboard modifier for opening external link by click#!settings.comments.0.terminal.external.extraLinkModifier": "点击打开外部链接时的额外键盘修饰键",
"Extra keyboard modifier for opening external link (including local file) by click#!settings.comments.0.terminal.external.extraLinkModifier": "点击打开外部链接(包括本地文件)时的额外键盘修饰键",
"any#!settings.options.0.terminal.external.extraLinkModifier": "任意",
"Tab#!settings.group.terminal.tab": "标签页",
"Enable Live CWD#!settings.label.terminal.tab.liveCwd": "启用实时工作路径",
"Support dynamic \\w and \\W in formatting#!settings.comments.0.terminal.tab.liveCwd": "在格式中支持动态的 \\w 和 \\W",
Expand Down
6 changes: 3 additions & 3 deletions addons/sync/src/renderer/SyncPane.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ async function downloadSyncPlan(plan: SyncPlan) {
}
}
function openSyncPlanDirectory(plan: SyncPlan) {
commas.remote.openDirectory(plan.directory)
function openSyncPlanDirectory(event: MouseEvent, plan: SyncPlan) {
commas.ui.openFolder(plan.directory, event)
}
function formatTime(time: string | null) {
Expand Down Expand Up @@ -144,7 +144,7 @@ function removeSyncPlan(index: number) {
<VisualIcon name="lucide-package-minus" />
</span>
<input v-model.lazy="plan.name" class="immersive-control plan-name">
<span class="plan-directory" @click="openSyncPlanDirectory(plan)">
<span class="plan-directory" @click="openSyncPlanDirectory($event, plan)">
<VisualIcon name="lucide-folder-output" class="directory-icon" />
<span class="directory-path">{{ omitHome(plan.directory) }}</span>
</span>
Expand Down
2 changes: 1 addition & 1 deletion resources/settings.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
"key": "terminal.external.extraLinkModifier",
"label": "Extra External Link Modifier",
"comments": [
"Extra keyboard modifier for opening external link by click"
"Extra keyboard modifier for opening external link (including local file) by click"
],
"schema": {
"type": "string",
Expand Down
21 changes: 11 additions & 10 deletions src/api/modules/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@ import {
useSettings,
useSettingsSpecs,
} from '../../renderer/compositions/settings'
import { openDirectory, openDirectoryExternally, openFile, openURL, openURLExternally, showDirectory, showFileExternally } from '../../renderer/compositions/shell'
import { openDirectory, openFile, openFileExternally, openURL, openURLExternally, showDirectory, showFileExternally } from '../../renderer/compositions/shell'
import { useIsLightTheme, useTheme } from '../../renderer/compositions/theme'
import { translate } from '../../renderer/utils/i18n'
import type { RendererAPIContext } from '../types'

export * from '../shim'

async function openUserDirectory() {
async function openUserDirectory(this: RendererAPIContext, event?: MouseEvent) {
const directory = await ipcRenderer.invoke('prepare-user-directory')
openDirectory(directory)
return this.$.ui.openFolder(directory, event)
}

async function openDefaultSettings() {
async function openDefaultSettings(this: RendererAPIContext, event?: MouseEvent) {
const filePath = await ipcRenderer.invoke('prepare-default-settings')
openFile(filePath)
return this.$.ui.openItem(filePath, event)
}

async function openSettingsFile() {
async function openSettingsFile(this: RendererAPIContext, event?: MouseEvent) {
const filePath = await ipcRenderer.invoke('prepare-settings-file')
openFile(filePath)
return this.$.ui.openItem(filePath, event)
}

async function openUserFile(file: string, example?: string) {
async function openUserFile(this: RendererAPIContext, file: string, example?: string, event?: MouseEvent) {
const filePath = await ipcRenderer.invoke('prepare-user-file', file, example)
openFile(filePath)
return this.$.ui.openItem(filePath, event)
}

function writeUserFile(file: string, content?: string) {
Expand All @@ -54,7 +55,7 @@ export {
openFile,
showFileExternally,
openDirectory,
openDirectoryExternally,
openFileExternally,
showDirectory,
openURL,
openURLExternally,
Expand Down
4 changes: 3 additions & 1 deletion src/api/modules/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ObjectEditor from '../../renderer/components/basic/ObjectEditor.vue'
import SwitchControl from '../../renderer/components/basic/SwitchControl.vue'
import ValueSelector from '../../renderer/components/basic/ValueSelector.vue'
import VisualIcon from '../../renderer/components/basic/VisualIcon.vue'
import { openLink } from '../../renderer/compositions/terminal'
import { openFolder, openItem, openLink } from '../../renderer/compositions/terminal'
import { RendererWebContentsView } from '../../renderer/compositions/web-contents'
import { createContextMenu, openContextMenu, withContextMenuSeparator } from '../../renderer/utils/frame'
import { vI18n } from '../../renderer/utils/i18n'
Expand Down Expand Up @@ -63,4 +63,6 @@ export {
extractClosestEdge,
RendererWebContentsView,
openLink,
openFolder,
openItem,
}
4 changes: 2 additions & 2 deletions src/renderer/components/TerminalTitle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ipcRenderer } from '@commas/electron-ipc'
import * as commas from '../../api/core-renderer'
import { omitHome } from '../../shared/terminal'
import { useSettings } from '../compositions/settings'
import { openDirectoryExternally, showDirectory, showFileExternally } from '../compositions/shell'
import { openFileExternally, showDirectory, showFileExternally } from '../compositions/shell'
import { useCurrentTerminal } from '../compositions/terminal'
import { translate } from '../utils/i18n'
import { getPrompt } from '../utils/terminal'
Expand Down Expand Up @@ -104,7 +104,7 @@ function openDirectory(event: MouseEvent) {
}
function openFile() {
openDirectoryExternally(fileOrDirectory)
openFileExternally(fileOrDirectory)
}
function startDraggingDirectory(event: DragEvent) {
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/compositions/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function openDirectory(directory: string) {
return globalHandler.invoke('global-renderer:open-directory', directory)
}

export function openDirectoryExternally(directory: string) {
export function openFileExternally(directory: string) {
return ipcRenderer.invoke('open-path', directory)
}

Expand Down Expand Up @@ -107,10 +107,10 @@ export function handleShellMessages() {
return showFileExternally(file)
})
globalHandler.handle('global-renderer:open-directory', (directory) => {
return openDirectoryExternally(directory)
return openFileExternally(directory)
})
globalHandler.handle('global-renderer:show-directory', (directory) => {
return openDirectoryExternally(directory)
return openFileExternally(directory)
})
globalHandler.handle('global-renderer:open-url', (url) => {
return openURLExternally(url)
Expand Down
30 changes: 29 additions & 1 deletion src/renderer/compositions/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { getProcessName, getPrompt, getTerminalTabID, getWindowsProcessInfo } fr
import { useA11yEnabled } from './a11y'
import { useKeyBindings } from './keybinding'
import { useSettings } from './settings'
import { openURL, openURLExternally } from './shell'
import { openDirectory, openFile, openFileExternally, openURL, openURLExternally, showFileExternally } from './shell'
import { useTheme } from './theme'

declare module '@commas/api/modules/app' {
Expand Down Expand Up @@ -214,6 +214,34 @@ export async function openLink(uri: string, event?: MouseEvent) {
}
}

export async function openFolder(folder: string, event?: MouseEvent) {
let modifier: boolean
if (event) {
modifier = isMatchExternalLinkModifier(event)
} else {
modifier = externalLinkModifier
}
if (modifier) {
return openFileExternally(folder)
} else {
return openDirectory(folder)
}
}

export async function openItem(file: string, event?: MouseEvent) {
let modifier: boolean
if (event) {
modifier = isMatchExternalLinkModifier(event)
} else {
modifier = externalLinkModifier
}
if (modifier) {
return showFileExternally(file)
} else {
return openFile(file)
}
}

function handleTerminalLink(event: MouseEvent, uri: string) {
if (isMatchLinkModifier(event)) {
return openLink(uri, event)
Expand Down

0 comments on commit a2abecc

Please sign in to comment.