feat: complate websocket plugin
This commit is contained in:
54
packages/plugins/public/js/editor.js
Normal file
54
packages/plugins/public/js/editor.js
Normal file
@@ -0,0 +1,54 @@
|
||||
let editor
|
||||
let codeStorageKey = "MiaoScript:code";
|
||||
|
||||
let monaco_path = 'https://cdn.jsdelivr.net/npm/monaco-editor@0.18.1/min'
|
||||
require.config({ paths: { 'vs': monaco_path + '/vs' } });
|
||||
window.MonacoEnvironment = { getWorkerUrl: () => proxy };
|
||||
let proxy = URL.createObjectURL(new Blob([`
|
||||
self.MonacoEnvironment = {
|
||||
baseUrl: '${monaco_path}/'
|
||||
};
|
||||
importScripts('${monaco_path}/vs/base/worker/workerMain.js');
|
||||
`], { type: 'text/javascript' }));
|
||||
|
||||
require(["vs/editor/editor.main"], function() {
|
||||
if (main.type !== 'unknow') {
|
||||
let ts_d_src = `https://cdn.jsdelivr.net/gh/circlecloud/ms@master/packages/${main.type}/src/typings`
|
||||
$.get(`${ts_d_src}/index.ts`, (res) => {
|
||||
monaco.languages.typescript.javascriptDefaults.addExtraLib(res, 'file:///src/typings/index.ts')
|
||||
let classes = res.split('\n').map(line => line.match(/.*\.\/(.*)".*/)).filter(line => line).map(dts => dts[1])
|
||||
main.classes.total = classes.length
|
||||
main.classes.loaded = 0
|
||||
classes.forEach(fname => {
|
||||
$.get(`${ts_d_src}/${fname}`, content => {
|
||||
monaco.languages.typescript.javascriptDefaults.addExtraLib(content, `file:///src/typings/${fname}`)
|
||||
main.classes.loaded++
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
editor = monaco.editor.create(document.getElementById('editor'), {
|
||||
value: window.localStorage.getItem(codeStorageKey) || 'org.bukkit.Bukkit.server.version',
|
||||
language: 'javascript',
|
||||
automaticLayout: true,
|
||||
scrollBeyondLastLine: false,
|
||||
theme: 'vs-dark'
|
||||
});
|
||||
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, function() {
|
||||
window.localStorage.setItem(codeStorageKey, editor.getValue())
|
||||
showMessenger('代码保存成功!')
|
||||
})
|
||||
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_R, function() {
|
||||
main.send('execCode', getSelectContent(editor) || editor.getValue())
|
||||
})
|
||||
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_E, function() {
|
||||
main.send('execCommand', getSelectContent(editor))
|
||||
})
|
||||
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_Q, function() {
|
||||
console.log('switch')
|
||||
})
|
||||
});
|
||||
function getSelectContent(editor) {
|
||||
let selInfo = editor.getSelection();
|
||||
return editor.getModel().getLineContent(selInfo.startLineNumber).substr(selInfo.startColumn - 1, selInfo.endColumn - selInfo.startColumn);
|
||||
}
|
||||
62
packages/plugins/public/js/main.js
Normal file
62
packages/plugins/public/js/main.js
Normal file
@@ -0,0 +1,62 @@
|
||||
var ws
|
||||
var SPLIT_LINE = '\\M\\W\\S|T|S|S/L/T/'
|
||||
let serverKey = 'MiaoScript:server'
|
||||
var main = avalon.define({
|
||||
$id: 'main',
|
||||
server: window.localStorage.getItem(serverKey) || location.host,
|
||||
type: 'unknow',
|
||||
logs: '',
|
||||
classes: {
|
||||
total: 1,
|
||||
loaded: 0,
|
||||
},
|
||||
precent: () => {
|
||||
return classes.total
|
||||
},
|
||||
log: (info) => {
|
||||
info.split("\n").forEach((line) => { term.writeln(mcColor2ANSI(line + '§r')) })
|
||||
},
|
||||
send: (type, content) => {
|
||||
if (!ws || ws.readyState != 1) { term.writeln('Please Connect to Server first!'); return; }
|
||||
ws.send(`${type}${SPLIT_LINE}${content}`);
|
||||
},
|
||||
connect: (event = { key: 'Enter' }) => {
|
||||
if (event.key !== "Enter") {
|
||||
return;
|
||||
}
|
||||
if (ws && ws.readyState == 1) {
|
||||
ws.close()
|
||||
}
|
||||
window.localStorage.setItem(serverKey, main.server)
|
||||
ws = new WebSocket(`${location.protocol == 'http:' ? 'ws' : 'wss'}://${main.server}/ws`)
|
||||
ws.onmessage = (event) => {
|
||||
const [type, obj] = event.data.split(SPLIT_LINE)
|
||||
switch (type) {
|
||||
case "log":
|
||||
main.log(obj)
|
||||
break;
|
||||
case "type":
|
||||
main.type = obj;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ws.onopen = () => {
|
||||
main.send("execDetect", "type");
|
||||
}
|
||||
ws.onclose = (ev) => {
|
||||
main.log(`Remote Server Close Connection... ${ev.code}`)
|
||||
if (ev.code == 1006) {
|
||||
setTimeout(() => {
|
||||
main.connect()
|
||||
}, 1000)
|
||||
}
|
||||
}
|
||||
},
|
||||
init: () => {
|
||||
if (main.server) {
|
||||
main.connect()
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
main.init()
|
||||
10
packages/plugins/public/js/message.js
Normal file
10
packages/plugins/public/js/message.js
Normal file
@@ -0,0 +1,10 @@
|
||||
$._messengerDefaults = {
|
||||
extraClasses: 'messenger-fixed messenger-theme-future messenger-on-top messenger-on-right'
|
||||
};
|
||||
var showMessenger = function(message, type) {
|
||||
return Messenger().post({
|
||||
message: message,
|
||||
type: type || 'info',
|
||||
showClo6seButton: true
|
||||
});
|
||||
};
|
||||
54
packages/plugins/public/js/term.js
Normal file
54
packages/plugins/public/js/term.js
Normal file
@@ -0,0 +1,54 @@
|
||||
Terminal.applyAddon(fit);
|
||||
var term = new Terminal();
|
||||
avalon.ready(() => {
|
||||
term.open(document.getElementById('terminal'));
|
||||
window.onresize = () => {
|
||||
term.fit();
|
||||
}
|
||||
window.onresize()
|
||||
})
|
||||
term.on('data', (data) => {
|
||||
if (data == '\r') {
|
||||
term.writeln(data)
|
||||
}
|
||||
});
|
||||
term.attachCustomKeyEventHandler(e => {
|
||||
if (e.ctrlKey && e.key == 'c' && term.hasSelection()) {
|
||||
showMessenger('内容已复制到粘贴板')
|
||||
return false;
|
||||
}
|
||||
if (e.ctrlKey && e.key == 'v') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
var colorMap = []
|
||||
colorMap['0'] = '38;5;0'
|
||||
colorMap['1'] = '38;5;4'
|
||||
colorMap['2'] = '38;5;2'
|
||||
colorMap['3'] = '38;5;6'
|
||||
colorMap['4'] = '38;5;1'
|
||||
colorMap['5'] = '38;5;5'
|
||||
colorMap['6'] = '38;5;3'
|
||||
colorMap['7'] = '38;5;7'
|
||||
colorMap['8'] = '38;5;8'
|
||||
colorMap['9'] = '38;5;12'
|
||||
colorMap['a'] = '38;5;10'
|
||||
colorMap['b'] = '38;5;14'
|
||||
colorMap['c'] = '38;5;9'
|
||||
colorMap['d'] = '38;5;13'
|
||||
colorMap['e'] = '38;5;11'
|
||||
colorMap['f'] = '38;5;15'
|
||||
colorMap['r'] = '0'
|
||||
colorMap['l'] = '1'
|
||||
colorMap['n'] = '4'
|
||||
var regexMap = []
|
||||
for (const c in colorMap) {
|
||||
regexMap[colorMap[c]] = new RegExp(`§${c}`, "g")
|
||||
}
|
||||
function mcColor2ANSI(str) {
|
||||
for (const regex in regexMap) {
|
||||
str = str.replace(regexMap[regex], `\u001b[${regex}m`)
|
||||
}
|
||||
return str;
|
||||
}
|
||||
0
packages/plugins/public/js/websocket.js
Normal file
0
packages/plugins/public/js/websocket.js
Normal file
Reference in New Issue
Block a user