This plugin provides some JS APIs that can actively trigger or capture the behavior of the plug-in, and can also simulate some user behaviors through these APIs.
Calling MethodsTo call the API interface, you need to specify a Terminal instance first, and then call the corresponding API method. There may be multiple Terminal instances on the same page. The plugin distinguishes these different instances through the name attribute, so the name attribute should be globally unique.
There are two ways to call the interface of the corresponding instance: Global Call
and Ref Call
This method can call any Terminal with a given name anywhere. Compared with Ref Call
, it does not need to rely on Vue's reference passing and is relatively more flexible. However, the prerequisite for calling it is to specify the Terminal's name.
The method of use is to introduce the global API TerminalApi
to call the interface. The first parameter of all interface inputs is the name value of Terminal, and the subsequent parameters are the parameter values of the corresponding interface.
<script setup lang="ts">
import {TerminalApi} from 'vue-web-terminal';
import {onMounted} from "vue";
onMounted(() => {
TerminalApi.pushMessage('my-terminal', "hello world!")
})
</script>
<template>
<terminal name="my-terminal"></terminal>
</template>
Ref Call
This calling method relies on the reference in Vue. Once the Ref of the specified Dom is obtained, the plug-in API can be called without passing the name value of the Terminal.
Vue3
<script setup lang="ts">
import {onMounted, ref} from "vue";
import {Terminal} from 'vue-web-terminal'
const myTerminalRef = ref<InstanceType<typeof Terminal>>()
onMounted(() => {
myTerminalRef.value!.pushMessage("hello world!")
})
</script>
<template>
<terminal name="my-terminal" ref="myTerminalRef"></terminal>
</template>
Vue2
<template>
<terminal name="my-terminal" ref="myTerminalRef"></terminal>
</template>
<script>
export default {
methods: {
invokeApi() {
this.$refs.myTerminalRef.pushMessage("hello world!")
}
}
}
</script>
Interfaces pushMessage
type pushMessage = (message: string | Message | Message[]) => void;
TerminalApi.pushMessage('my-terminal', 'Hello world!')
TerminalApi.pushMessage('my-terminal', {
class: 'warning',
tag: 'WARN',
content: 'This is warning message!'
})
TerminalApi.pushMessage('my-terminal', [
{content: 'message 1'},
{content: 'message 2', class: 'info'},
{content: 'message 3', class: 'success'},
])
normal
, ansi
, code
, html
, otherwise a new message will be pushed.type appendMessage = (msg: string) => void;
TerminalApi.appendMessage('my-terminal', "This is additional content")
fullscreen
type fullscreen = () => void;
TerminalApi.fullscreen('my-terminal')
isFullscreen
type isFullscreen = () => boolean;
let isFullscreen = TerminalApi.isFullscreen('my-terminal')
console.log(isFullscreen)
dragging
x
is the distance from the left border of the terminal to the left border of the browser's visible range, and y
is the distance from the top border of the terminal to the top border of the browser's visible range, in px.type dragging = (pos: Position) => void;
TerminalApi.dragging('my-terminal', { x: 100, y: 200 })
type execute = (cmd: string) => void;
TerminalApi.execute('my-terminal', 'help :local')
focus
type focus = (enforceFocus?: boolean | MouseEvent) => void;
TerminalApi.focus('my-terminal', true)
elementInfo
Notice
If your window has been created but not displayed on the page (for example, if v-show is used to control the display), some information may become invalid.
type elementInfo = () => TerminalElementInfo;
let info = TerminalApi.elementInfo('my-terminal')
console.log(info)
{
"pos": {
"x": 100,
"y": 100
},
"screenWidth": 700,
"screenHeight": 500,
"clientWidth": 690,
"clientHeight": 490,
"charWidth": {
"en": 7.2,
"cn": 14
}
}
The following diagram clearly describes what these values mean:
textEditorOpencontent
is the preset content when opening the editor. If you don't want to preset any content, you can leave this parameter blank. When the user clicks Close or actively calls the textEditorClose()
method, the onClose
callback will be triggered. The parameter value is the text content in the current editor and the passed parameter options.type textEditorOpen = (setting: EditorSetting) => any;
TerminalApi.textEditorOpen('my-terminal', {
content: 'This is the preset content',
onClose: (value, options) => {
console.log('Final content: ', value, "options:", options)
}
})
type textEditorClose = (options: any) => string;
TerminalApi.textEditorClose('my-terminal', true)
TerminalApi.textEditorClose('my-terminal', 'hello! this is close options')
clearLog
type clearLog = (clearCommandHistory?: boolean) => void;
TerminalApi.clearLog('my-terminal')
TerminalApi.clearLog('my-terminal', true)
getCommand
type getCommand = () => string;
TerminalApi.getCommand('my-terminal')
setCommand
type setCommand = (command: string) => void;
TerminalApi.setCommand('my-terminal', "customCmd -a hello")
switchAllFoldState
type switchAllFoldState = (name: string, state: boolean) => number;
TerminalApi.switchAllFoldState('my-terminal', true)
TerminalApi.switchAllFoldState('my-terminal', false)
jumpToBottom
type jumpToBottom = (name: string, force: boolean) => void;
TerminalApi.jumpToBottom('my-terminal', false)
TerminalApi.jumpToBottom('my-terminal', true)
getOutputs
type getOutputs = (name: string) => MessageGroup[];
const outputs = TerminalApi.getOutputs('my-terminal')
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4