本插件提供了一些JS API,可以主动触发或捕获插件的行为,也可以通过这些API模拟用户的部分行为。
调用方法要调用API接口你需要先指定到某一个Terminal实例,再调用相应的API方法,在同一个页面中可能会有多个Terminal实例, 插件区分这些不同的实例就是通过name属性来实现的,因此 name 属性应该是全局唯一的。
有两种方法可以调用对应实例的接口:全局调用、Ref调用
全局调用这种方法可以在任何地方调用到任何一个 name 的Terminal,相比于Ref调用
它不需要依赖于Vue的引用传递,相对来说更灵活,但调用它的前提是需要有指定Terminal的name。
使用方法是通过引入全局API TerminalApi
调用接口,所有接口入参的第一个都是Terminal的name值,之后的参数就是对应接口的参数值。
<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调用
这种调用方式依赖于Vue中的 Ref 引用,获取到指定Dom的Ref即可调用插件的API,且无需传递Terminal的name值
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>
接口方法 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
其中一个时才会追加,否则推送一条新消息。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
是terminal左边框到浏览器可视范围左边框的距离,y
是terminal上边框到浏览器可视范围上边框的距离,单位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
注意
如果你的窗口已创建但未显示在页面(比如用了v-show控制显示),可能会出现部分信息失效的问题。
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
}
}
下面这张图清晰地描述了这些值的含义:
textEditorOpencontent
是打开编辑器时预置的内容,如果你不想预置任何内容可以不填此参数,当用户点击Close或主动调用 textEditorClose()
方法时会触发onClose
回调,参数value为当前编辑器内的文本内容和传入参数选项。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