Skip to content

Commit f786b1a

Browse files
committed
opt 移除highlight和codemirror默认配置方式 #132 调整组件安装方式,引入更多配置项 #133
1 parent e37cd7b commit f786b1a

File tree

7 files changed

+187
-173
lines changed

7 files changed

+187
-173
lines changed

src/Terminal.vue

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
_getByteLen,
3333
_getClipboardText,
3434
_getSelection,
35+
_hash,
3536
_html,
3637
_isEmpty,
3738
_isPad,
@@ -43,12 +44,11 @@ import {
4344
_parsePixelFromValue,
4445
_pointInRect,
4546
_screenType,
46-
_hash,
4747
} from "~/common/util.ts";
48-
import api, {getOptions, register, rename, unregister} from "~/common/api";
48+
import api, {getConfiguration, register, rename, unregister} from "~/common/api";
4949
import {DEFAULT_COMMANDS, WINDOW_STYLE} from "~/common/configuration.ts";
5050
import {_parseANSI} from "~/ansi";
51-
import store from "~/common/store";
51+
import {getStore} from "~/common/store";
5252
import THeader from "~/components/THeader.vue";
5353
import TViewerNormal from "~/components/TViewerNormal.vue";
5454
import TViewerJson from "~/components/TViewerJson.vue";
@@ -704,14 +704,14 @@ const getThemeStyleId = (salt: string): string => {
704704
* @param theme
705705
*/
706706
const setTheme = (theme: string) => {
707-
let customThemes = getOptions().themes
708-
let themeStyle
707+
let customThemes = getConfiguration().themes
708+
let themeStyle: string
709709
if (customThemes && customThemes[theme]) {
710710
themeStyle = customThemes[theme]
711711
} else if (theme === 'dark') {
712-
themeStyle = themeDark
712+
themeStyle = themeDark as string
713713
} else if (theme === 'light') {
714-
themeStyle = themeLight
714+
themeStyle = themeLight as string
715715
} else {
716716
console.warn("The specified terminal theme style was not found:", theme)
717717
return
@@ -751,7 +751,7 @@ const changeThemeFlag = (newNameKey: string, oldNameKey: string) => {
751751
752752
const _clearLog = (clearHistory: boolean) => {
753753
if (clearHistory) {
754-
store.clear(getName())
754+
getStore().clear(getName())
755755
} else {
756756
terminalLog.value = [];
757757
logSize.value = 0;
@@ -1304,7 +1304,7 @@ const _jumpToBottom = (enforce: boolean = false) => {
13041304
const _saveCurCommand = () => {
13051305
let cmd = command.value = command.value.trim()
13061306
if (cmd.length > 0) {
1307-
store.push(getName(), cmd)
1307+
getStore().push(getName(), cmd)
13081308
}
13091309
13101310
let group = _newTerminalLogGroup()
@@ -1455,21 +1455,21 @@ const _switchTipsSelectedIdx = (idx: number) => {
14551455
}
14561456
14571457
const _switchPreCmd = () => {
1458-
let cmdLog = store.getLog(getName())
1459-
let cmdIdx = store.getIdx(getName())
1458+
let cmdLog = getStore().getLog(getName())
1459+
let cmdIdx = getStore().getIdx(getName())
14601460
if (cmdLog.length !== 0 && cmdIdx > 0) {
14611461
cmdIdx -= 1;
14621462
command.value = cmdLog[cmdIdx] ? cmdLog[cmdIdx] : '';
14631463
}
14641464
_resetCursorPos()
1465-
store.setIdx(getName(), cmdIdx)
1465+
getStore().setIdx(getName(), cmdIdx)
14661466
_searchCmd()
14671467
_jumpToBottom(true)
14681468
}
14691469
14701470
const _switchNextCmd = () => {
1471-
let cmdLog = store.getLog(getName())
1472-
let cmdIdx = store.getIdx(getName())
1471+
let cmdLog = getStore().getLog(getName())
1472+
let cmdIdx = getStore().getIdx(getName())
14731473
if (cmdLog.length !== 0 && cmdIdx < cmdLog.length - 1) {
14741474
cmdIdx += 1;
14751475
command.value = cmdLog[cmdIdx] ? cmdLog[cmdIdx] : '';
@@ -1478,7 +1478,7 @@ const _switchNextCmd = () => {
14781478
command.value = '';
14791479
}
14801480
_resetCursorPos()
1481-
store.setIdx(getName(), cmdIdx)
1481+
getStore().setIdx(getName(), cmdIdx)
14821482
_searchCmd()
14831483
_jumpToBottom(true)
14841484
}

src/common/api/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Options, TerminalApi, TerminalApiData, TerminalApiListenerFunc} from "~/types";
1+
import {TerminalApi, TerminalApiData, TerminalApiListenerFunc, TerminalConfiguration} from "~/types";
22

33
const data: TerminalApiData = {
44
pool: {},
5-
options: {
6-
highlight: null,
7-
codemirror: null,
5+
configuration: {
6+
maxStoredCommandCountPerInstance: 100,
7+
storeName: 'terminal',
88
themes: {}
99
}
1010
}
@@ -25,14 +25,6 @@ export function rename(newName: string, oldName: string, listener: TerminalApiLi
2525
register(newName, listener);
2626
}
2727

28-
export function configHighlight(config: any) {
29-
data.options.highlight = config
30-
}
31-
32-
export function configCodemirror(config: any) {
33-
data.options.codemirror = config
34-
}
35-
3628
export function configTheme(theme: string, css: string) {
3729
let res = css.match(/^.*\{(.*)}\s*$/s)
3830
if (!res || res.length != 2) {
@@ -44,19 +36,28 @@ export function configTheme(theme: string, css: string) {
4436
}
4537
`)
4638
}
47-
let themes = data.options.themes
39+
let themes = data.configuration.themes
4840
if (!themes) {
49-
data.options.themes = themes = {}
41+
data.configuration.themes = themes = {}
5042
}
5143
themes[theme] = css
5244
}
5345

54-
export function getOptions(): Options {
55-
return data.options as Options
46+
export function configStoreName(name: string) {
47+
data.configuration.storeName = name
48+
console.debug("Configured storeName", name)
49+
}
50+
51+
export function configMaxStoredCommandCountPerInstance(count: number) {
52+
if (count <= 1) {
53+
throw new Error("The value of 'maxStoredLogCountPerInstance' must be a valid positive number")
54+
}
55+
data.configuration.maxStoredCommandCountPerInstance = count
56+
console.debug("Configured maxStoredCommandCountPerInstance", count)
5657
}
5758

58-
export function setOptions(op: Options) {
59-
data.options = {...op}
59+
export function getConfiguration(): TerminalConfiguration {
60+
return data.configuration
6061
}
6162

6263
export default new TerminalApi(data)

src/common/store/index.ts

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,98 @@
1-
import {TerminalStore} from "~/types";
1+
import {CmdHistory} from "~/types";
2+
import {getConfiguration} from "~/common/api";
3+
import {ref} from "vue";
24

3-
export default new TerminalStore()
5+
const store = ref<TerminalStore>()
6+
7+
export class TerminalStore {
8+
storageKey: string
9+
maxStoredCommandCountPerInstance: number
10+
dataMap: Record<string, CmdHistory>
11+
12+
constructor(key: string, maxStoredCommandCountPerInstance: number) {
13+
this.storageKey = key
14+
this.maxStoredCommandCountPerInstance = maxStoredCommandCountPerInstance
15+
let dataMapStr = window.localStorage.getItem(this.storageKey)
16+
if (dataMapStr) {
17+
this.dataMap = JSON.parse(dataMapStr)
18+
} else {
19+
this.dataMap = {}
20+
}
21+
}
22+
23+
push(name: string, cmd: string) {
24+
let data = this.getData(name)
25+
if (data.cmdLog == null) {
26+
data.cmdLog = []
27+
}
28+
if (data.cmdLog.length === 0 || data.cmdLog[data.cmdLog.length - 1] !== cmd) {
29+
data.cmdLog.push(cmd)
30+
31+
console.log(data.cmdLog.length, this.maxStoredCommandCountPerInstance)
32+
33+
if (data.cmdLog.length > this.maxStoredCommandCountPerInstance) {
34+
data.cmdLog.splice(0, data.cmdLog.length - this.maxStoredCommandCountPerInstance)
35+
}
36+
}
37+
38+
data.cmdIdx = data.cmdLog.length
39+
this.store()
40+
}
41+
42+
store() {
43+
window.localStorage.setItem(this.storageKey, JSON.stringify(this.dataMap))
44+
}
45+
46+
getData(name: string): CmdHistory {
47+
let data = this.dataMap[name]
48+
if (data == null) {
49+
data = {
50+
cmdLog: [],
51+
cmdIdx: 0
52+
}
53+
this.dataMap[name] = data
54+
}
55+
return data
56+
}
57+
58+
getLog(name: string) {
59+
let data = this.getData(name)
60+
if (!data.cmdLog) {
61+
data.cmdLog = []
62+
}
63+
return data.cmdLog
64+
}
65+
66+
clear(name: string) {
67+
let data = this.getData(name)
68+
data.cmdLog = []
69+
data.cmdIdx = 0
70+
this.store()
71+
}
72+
73+
clearAll() {
74+
this.dataMap = {}
75+
this.store()
76+
}
77+
78+
getIdx(name: string) {
79+
let data = this.getData(name)
80+
return data.cmdIdx | 0
81+
}
82+
83+
setIdx(name: string, idx: number) {
84+
this.getData(name).cmdIdx = idx
85+
}
86+
}
87+
88+
export function initStore() {
89+
const configuration = getConfiguration();
90+
store.value = new TerminalStore(configuration.storeName, configuration.maxStoredCommandCountPerInstance)
91+
}
92+
93+
export function getStore(): TerminalStore {
94+
if (store.value) {
95+
return store.value
96+
}
97+
throw new Error("The store must be initialized before reading")
98+
}

src/components/TViewerCode.vue

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,16 @@
11
<script setup lang="ts">
2-
import {computed, PropType} from "vue";
2+
import {PropType} from "vue";
33
import {Message} from "~/types";
4-
import {getOptions} from "~/common/api";
54
65
defineProps({
76
message: Object as PropType<Message>
87
})
98
10-
const getHighlight = computed(() => {
11-
let options = getOptions()
12-
if (options) {
13-
return options.highlight
14-
}
15-
})
16-
17-
const getCodemirror = computed(() => {
18-
let options = getOptions()
19-
if (options) {
20-
return options.codemirror
21-
}
22-
})
23-
249
</script>
2510

2611
<template>
27-
<div class="t-code">
28-
<div v-if="getHighlight" class="t-vue-highlight">
29-
<highlightjs ref="highlightjs"
30-
autodetect
31-
:code="message.content"/>
32-
</div>
33-
<div v-else-if="getCodemirror" class="t-vue-codemirror">
34-
<codemirror ref="codemirror"
35-
v-model="message.content"
36-
:options="getCodemirror"/>
37-
</div>
38-
<div v-else class="t-code-default">
39-
<pre style="padding: 1em;margin: 0"><code style="font-size: 15px">{{ message.content }}</code></pre>
40-
</div>
12+
<div class="t-code t-code-default">
13+
<pre style="padding: 1em;margin: 0"><code style="font-size: 15px">{{ message.content }}</code></pre>
4114
</div>
4215
</template>
4316

src/index.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,34 @@ import './css/ansi.css'
33
import './css/style.css'
44
import 'vue-json-viewer/style.css'
55
import type {App} from 'vue'
6-
import TerminalStore from "./common/store"
7-
import TerminalApi, {configCodemirror, configHighlight, configTheme, setOptions} from "./common/api"
6+
import TerminalApi, {configMaxStoredCommandCountPerInstance, configStoreName, configTheme} from "./common/api"
87
import Terminal from "./Terminal.vue"
9-
import {Options, TerminalAsk, TerminalFlash} from "./types"
8+
import {TerminalAsk, TerminalFlash, VueWebTerminal} from "./types"
9+
import {getStore, initStore} from "~/common/store";
1010

11-
const install = (app: App, options?: Options): void => {
12-
if (options) {
13-
setOptions(options)
14-
}
11+
const install = (app: App): void => {
12+
initStore()
1513
app.component(Terminal.__name as string, Terminal)
1614
}
1715

18-
// 兼容老版本
19-
Terminal.install = install
16+
const createTerminal = (): VueWebTerminal => {
17+
return {
18+
install,
19+
configTheme,
20+
configStoreName,
21+
configMaxStoredCommandCountPerInstance
22+
}
23+
}
2024

2125
export * from './types'
2226

2327
export {
2428
Terminal,
25-
TerminalStore,
2629
TerminalApi,
2730
TerminalAsk,
2831
TerminalFlash,
29-
configHighlight,
30-
configCodemirror,
31-
configTheme
32-
}
33-
34-
const VueWebTerminal = {
35-
install
32+
createTerminal,
33+
getStore
3634
}
3735

38-
export default VueWebTerminal
36+
export default Terminal

0 commit comments

Comments
 (0)