import molecule from "@dtinsight/molecule"; import Welcome from "../../pages/welcome"; import {store} from "@/redux/store"; import {loadCurrentQuestion, loadQuestion} from "@/redux/action/action"; import * as monaco from '@dtinsight/molecule/esm/monaco'; import { CloseAction, createConnection, ErrorAction, MonacoLanguageClient, MonacoServices, MessageConnection } from "@codingame/monaco-languageclient"; import {listen} from "@codingame/monaco-jsonrpc"; import normalizeUrl from "normalize-url"; export function setEntry() { molecule.editor.setEntry() } export function activateEditCallback() { molecule.editor.onUpdateTab((tab) => { const nextFileValue = molecule.editor.editorInstance?.getModel()?.getValue() const tabId = molecule.editor.getState().current?.tab?.id if (nextFileValue && tabId) { const node = molecule.folderTree.get(tabId) if (node){ const prevFileValue = node.data.value const location = node.location if (prevFileValue !== nextFileValue) { node.data.value = nextFileValue molecule.folderTree.update(node) const question = store.getState().loadCurrentQuestion if (question && question.editable && location) { question.editable[location] = nextFileValue store.dispatch(loadCurrentQuestion(question)) } if (location != null) { const el = window.document.querySelector(`div[data-key='${node.id}'].mo-tree__treenode span.mo-tree__treenode__title`) as HTMLElement | null; if (el) el.style.color = '#2491f7' // console.log(molecule.editor.editorInstance.getModel()) // console.log(monaco.editor.getModel(monaco.Uri.parse(location))) } console.log('value update') } } } }) } export function activateLanguageService() { function createUrl(hostname: string, port: number, path: string): string { return normalizeUrl(`ws://${hostname}:${port}${path}`); } MonacoServices.install(monaco); // create the web socket const url = createUrl('localhost', 8086, '') const webSocket = new WebSocket(url); // listen when the web socket is opened listen({ webSocket, onConnection: connection => { // create and start the language client const languageClient = createLanguageClient(connection); const disposable = languageClient.start(); connection.onClose(() => disposable.dispose()); console.log(`Connected to "${url}" and started the language client.`); } }); function createLanguageClient(connection: MessageConnection): MonacoLanguageClient { return new MonacoLanguageClient({ name: "Sample Language Client", clientOptions: { // use a language id as a document selector documentSelector: ['java'], // disable the default error handler errorHandler: { error: () => ErrorAction.Continue, closed: () => CloseAction.DoNotRestart } }, // create a language client connection from the JSON RPC connection on demand connectionProvider: { get: (errorHandler, closeHandler) => { return Promise.resolve(createConnection(connection, errorHandler, closeHandler)) } } }); } }