Skip to content

Commit

Permalink
Click on the terminal link to open it in your browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Hongshiqiang committed Mar 27, 2023
1 parent 0bca183 commit e1d9e50
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
17 changes: 16 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
"uuid": "^9.0.0",
"watcher": "^2.2.2",
"webpack": "^5.75.0",
"xterm": "^4.19.0"
"xterm": "^4.19.0",
"xterm-addon-web-links": "^0.6.0"
}
}
14 changes: 14 additions & 0 deletions src/components/terminal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useRef } from 'react'
import { Terminal } from 'xterm'
import { FitAddon } from 'xterm-addon-fit'
import { WebLinksAddon } from 'xterm-addon-web-links';
import 'xterm/css/xterm.css'
import { useAppSelector, useAppDispatch } from '../app/hooks'
import { FullState } from '../features/window/state'
Expand All @@ -13,6 +14,12 @@ export function XTermComponent({ height }: { height: number }) {
const terminalRef = useRef<HTMLDivElement>(null)
const terminal = useRef<Terminal | null>(null)
const fitAddon = useRef<FitAddon>(new FitAddon())
const webLinksAddon = useRef<WebLinksAddon>(new WebLinksAddon(
(event: MouseEvent, url: string) => {
event.preventDefault()
connector.terminalClickLink(url)
},
))

const handleIncomingData = (e: any, data: any) => {
terminal.current!.write(data)
Expand All @@ -30,6 +37,7 @@ export function XTermComponent({ height }: { height: number }) {
})

terminal.current.loadAddon(fitAddon.current)
terminal.current.loadAddon(webLinksAddon.current)

if (terminalRef.current) {
terminal.current.open(terminalRef.current)
Expand All @@ -41,6 +49,11 @@ export function XTermComponent({ height }: { height: number }) {
connector.terminalInto(e)
})

terminal.current.onData((e) => {
connector.terminalInto(e)
})


connector.registerIncData(handleIncomingData)

// Make the terminal's size and geometry fit the size of #terminal-container
Expand All @@ -55,6 +68,7 @@ export function XTermComponent({ height }: { height: number }) {
useEffect(() => {
if (terminal.current != null) {
terminal.current.loadAddon(fitAddon.current)
terminal.current.loadAddon(webLinksAddon.current)
fitAddon.current.fit()
}
}, [height, terminal, fitAddon])
Expand Down
6 changes: 6 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,12 @@ const createWindow = () => {
}
return null
})

// click on the terminal link
ipcMain.handle('terminal-click-link', (event, data) => {
shell.openExternal(data);
})

setupLSPs(store)
const projectPathObj = store.get('projectPath')
if (
Expand Down
1 change: 1 addition & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ const electronConnector = {
ipcRenderer.removeListener('terminal-incData', callback)
},
terminalInto: (data: any) => ipcRenderer.invoke('terminal-into', data),
terminalClickLink: (data: any) => ipcRenderer.invoke('terminal-click-link', data),
terminalResize: (data: any) => ipcRenderer.invoke('terminal-resize', data),

registerFileWasAdded: (callback: Callback) =>
Expand Down

0 comments on commit e1d9e50

Please sign in to comment.