1
0

editorController.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import molecule from "@dtinsight/molecule";
  2. import Welcome from "../../pages/welcome";
  3. import {store} from "@/redux/store";
  4. import {loadCurrentQuestion, loadQuestion} from "@/redux/action/action";
  5. import * as monaco from '@dtinsight/molecule/esm/monaco';
  6. import {
  7. CloseAction,
  8. createConnection,
  9. ErrorAction,
  10. MonacoLanguageClient,
  11. MonacoServices,
  12. MessageConnection
  13. } from "@codingame/monaco-languageclient";
  14. import {listen} from "@codingame/monaco-jsonrpc";
  15. import normalizeUrl from "normalize-url";
  16. export function setEntry() {
  17. molecule.editor.setEntry(<Welcome/>)
  18. }
  19. export function activateEditCallback() {
  20. molecule.editor.onUpdateTab((tab) => {
  21. const nextFileValue = molecule.editor.editorInstance?.getModel()?.getValue()
  22. const tabId = molecule.editor.getState().current?.tab?.id
  23. if (nextFileValue && tabId) {
  24. const node = molecule.folderTree.get(tabId)
  25. if (node){
  26. const prevFileValue = node.data.value
  27. const location = node.location
  28. if (prevFileValue !== nextFileValue) {
  29. node.data.value = nextFileValue
  30. molecule.folderTree.update(node)
  31. const question = store.getState().loadCurrentQuestion
  32. if (question && question.editable && location) {
  33. question.editable[location] = nextFileValue
  34. store.dispatch(loadCurrentQuestion(question))
  35. }
  36. if (location != null) {
  37. const el = window.document.querySelector(`div[data-key='${node.id}'].mo-tree__treenode span.mo-tree__treenode__title`) as HTMLElement | null;
  38. if (el) el.style.color = '#2491f7'
  39. // console.log(molecule.editor.editorInstance.getModel())
  40. // console.log(monaco.editor.getModel(monaco.Uri.parse(location)))
  41. }
  42. console.log('value update')
  43. }
  44. }
  45. }
  46. })
  47. }
  48. export function activateLanguageService() {
  49. function createUrl(hostname: string, port: number, path: string): string {
  50. return normalizeUrl(`ws://${hostname}:${port}${path}`);
  51. }
  52. MonacoServices.install(monaco);
  53. // create the web socket
  54. const url = createUrl('localhost', 8086, '')
  55. const webSocket = new WebSocket(url);
  56. // listen when the web socket is opened
  57. listen({
  58. webSocket,
  59. onConnection: connection => {
  60. // create and start the language client
  61. const languageClient = createLanguageClient(connection);
  62. const disposable = languageClient.start();
  63. connection.onClose(() => disposable.dispose());
  64. console.log(`Connected to "${url}" and started the language client.`);
  65. }
  66. });
  67. function createLanguageClient(connection: MessageConnection): MonacoLanguageClient {
  68. return new MonacoLanguageClient({
  69. name: "Sample Language Client",
  70. clientOptions: {
  71. // use a language id as a document selector
  72. documentSelector: ['java'],
  73. // disable the default error handler
  74. errorHandler: {
  75. error: () => ErrorAction.Continue,
  76. closed: () => CloseAction.DoNotRestart
  77. }
  78. },
  79. // create a language client connection from the JSON RPC connection on demand
  80. connectionProvider: {
  81. get: (errorHandler, closeHandler) => {
  82. return Promise.resolve(createConnection(connection, errorHandler, closeHandler))
  83. }
  84. }
  85. });
  86. }
  87. }