init.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import molecule from "@dtinsight/molecule";
  2. import {ColorThemeMode, IActivityMenuItemProps, IEditorGroup} from "@dtinsight/molecule/esm/model";
  3. import {
  4. DRAWER_MENU_ENUM,
  5. FOLDERTREE_SUBMIT_TOOLBAR,
  6. ID_COLLECTIONS,
  7. OPEN_IN_NEW_GROUP,
  8. QUESTION_LIST_PANEL
  9. } from "../constant";
  10. import {UniqueId} from "@dtinsight/molecule/esm/common/types";
  11. import {IExtension} from "@dtinsight/molecule/esm/model/extension";
  12. import LogPane from "@/components/logPane";
  13. import {store} from "@/redux/store";
  14. import {jumpToLogin} from "@/services/auth";
  15. import Api from "@/api";
  16. import {history} from "umi";
  17. import notification from "@/components/notification";
  18. import {deleteCookie, getQuestion, goToLogin, randomId, submitQuestion, updateAccountContext} from "@/utils";
  19. import {loadUser} from "@/redux/action/action";
  20. import {QuickTogglePanelAction} from "@dtinsight/molecule/esm/monaco/quickTogglePanelAction";
  21. import {Button} from "antd";
  22. import {jumpToCoder} from "@/services/exam";
  23. import classNames from "classnames";
  24. import {transformToEditorTab} from "@/common";
  25. import {QuestionList} from "@/components/questionListPanel";
  26. function loadStyles(url: string) {
  27. const link = document.createElement('link');
  28. link.rel = 'stylesheet';
  29. link.type = 'text/css';
  30. link.href = url;
  31. link.id = 'antd_dark';
  32. if (!document.getElementById('antd_dark')) {
  33. const head = document.getElementsByTagName('head')[0];
  34. head.appendChild(link);
  35. }
  36. }
  37. function removeStyles() {
  38. const darkStyle = document.querySelector('#antd_dark');
  39. darkStyle?.remove();
  40. }
  41. export default class InitializeExtension implements IExtension {
  42. id: UniqueId = 'initialize';
  43. name: string = 'initialize';
  44. activate(): void {
  45. const { CONTEXT_MENU_SEARCH } = molecule.builtin.getConstants();
  46. molecule.activityBar.remove([CONTEXT_MENU_SEARCH!]);
  47. initializeColorTheme();
  48. initMenuBar();
  49. initLogin();
  50. initializeEntry();
  51. initExpandCollapse();
  52. initExplorer();
  53. // 默认不展示 Panel
  54. }
  55. dispose(): void {
  56. throw new Error('Method not implemented.');
  57. }
  58. }
  59. function initializeColorTheme() {
  60. const defaultThemeId = localStorage.getItem(ID_COLLECTIONS.COLOR_THEME_ID);
  61. const defaultTheme = defaultThemeId && molecule.colorTheme.getThemeById(defaultThemeId);
  62. if (defaultTheme) {
  63. molecule.colorTheme.setTheme(defaultTheme.id);
  64. } else {
  65. molecule.colorTheme.setTheme('Default Light+');
  66. }
  67. const currentThemeMode = molecule.colorTheme.getColorThemeMode();
  68. if (currentThemeMode === ColorThemeMode.dark) {
  69. loadStyles('https://unpkg.com/antd@4.20.3/dist/antd.dark.css');
  70. }
  71. document.documentElement.setAttribute('data-prefers-color', currentThemeMode);
  72. molecule.colorTheme.onChange((_, nextTheme, themeMode) => {
  73. localStorage.setItem(ID_COLLECTIONS.COLOR_THEME_ID, nextTheme.id);
  74. document.documentElement.setAttribute('data-prefers-color', themeMode);
  75. if (themeMode === ColorThemeMode.dark) {
  76. loadStyles('https://unpkg.com/antd@4.20.3/dist/antd.dark.css');
  77. } else {
  78. removeStyles();
  79. }
  80. });
  81. }
  82. function initMenuBar() {
  83. molecule.menuBar.remove('Run');
  84. molecule.menuBar.remove('Help');
  85. molecule.menuBar.remove('workbench.action.quickCreateFile')
  86. molecule.menuBar.remove(QuickTogglePanelAction.ID)
  87. molecule.layout.setMenuBarMode('horizontal');
  88. const state = molecule.menuBar.getState();
  89. const nextData = state.data.concat();
  90. nextData.splice(1, 0, {
  91. id: 'seecoder',
  92. name: 'Seecoder',
  93. data: [
  94. {
  95. id: DRAWER_MENU_ENUM.RESULT,
  96. name: 'Result'
  97. },
  98. {
  99. id: DRAWER_MENU_ENUM.SUBMIT,
  100. name: 'Submit'
  101. }
  102. ],
  103. });
  104. molecule.menuBar.setState({
  105. data: nextData,
  106. });
  107. }
  108. function initExpandCollapse() {
  109. const { SAMPLE_FOLDER_PANEL_ID } = molecule.builtin.getConstants();
  110. molecule.explorer.setExpandedPanels([SAMPLE_FOLDER_PANEL_ID!, QUESTION_LIST_PANEL.id]);
  111. }
  112. function initExplorer() {
  113. molecule.explorer.addPanel({
  114. ...QUESTION_LIST_PANEL,
  115. renderPanel:() => (
  116. <QuestionList store={store}/>
  117. ),
  118. })
  119. const explorerData = molecule.explorer.getState().data?.concat() || [];
  120. const { SAMPLE_FOLDER_PANEL_ID } = molecule.builtin.getConstants();
  121. const folderTreePane = explorerData.find((item) => item.id === SAMPLE_FOLDER_PANEL_ID);
  122. molecule.explorer.onPanelToolbarClick((panel, toolbarId)=>{
  123. switch (toolbarId) {
  124. case FOLDERTREE_SUBMIT_TOOLBAR.id:
  125. submitQuestion()
  126. break;
  127. default:
  128. break;
  129. }
  130. })
  131. if (folderTreePane?.toolbar) {
  132. folderTreePane.toolbar[0] = folderTreePane.toolbar[2]
  133. folderTreePane.toolbar[1] = folderTreePane.toolbar[3]
  134. folderTreePane.toolbar[2] = FOLDERTREE_SUBMIT_TOOLBAR
  135. folderTreePane.toolbar.pop()
  136. }
  137. molecule.explorer.setState({
  138. data: explorerData,
  139. });
  140. molecule.explorer.onPanelToolbarClick((panel, toolbarId) => {
  141. if (toolbarId === "refresh" && store.getState().loadQuestion.examId) {
  142. const groups = molecule.editor.getState().groups
  143. groups?.forEach((group) => {
  144. molecule.editor.closeAll(group.id)
  145. })
  146. getQuestion(store.getState().loadQuestion.examId as unknown as string, "");
  147. }
  148. })
  149. molecule.explorer.onCollapseAllFolders(() => {
  150. molecule.folderTree.setExpandKeys([]);
  151. });
  152. molecule.editorTree.onCloseSaved((groupId) => {
  153. molecule.editor.closeAll(groupId);
  154. });
  155. molecule.folderTree.setFileContextMenu([OPEN_IN_NEW_GROUP])
  156. molecule.folderTree.onContextMenu((contextMenu, treeNode) => {
  157. switch (contextMenu.id) {
  158. case OPEN_IN_NEW_GROUP.id: {
  159. const { groups = [] } = molecule.editor.getState();
  160. const id = randomId();
  161. const initialTab = Object.assign({}, transformToEditorTab(treeNode!));
  162. const newGroup: IEditorGroup = Object.assign({})
  163. newGroup.data = [initialTab];
  164. newGroup.tab = initialTab;
  165. newGroup.activeTab = initialTab.id;
  166. newGroup.id = id;
  167. molecule.editor.setState({
  168. current: newGroup,
  169. groups: [...groups, newGroup]
  170. })
  171. break;
  172. }
  173. default:
  174. break;
  175. }
  176. })
  177. molecule.folderTree.setFolderContextMenu([])
  178. const folderTreeState = molecule.folderTree.getState()
  179. const folderTree = folderTreeState?.folderTree
  180. if (folderTree && folderTreeState && folderTree.contextMenu) folderTree.contextMenu = []
  181. }
  182. function initializeEntry() {
  183. molecule.folderTree.setEntry(
  184. <div className={classNames('mt-20px', 'text-center')}>
  185. {'Go to '}
  186. <Button
  187. type="link"
  188. onClick={() => jumpToCoder()}
  189. style={{padding: 0}}
  190. >
  191. Coder
  192. </Button>
  193. {' to select an exam'}
  194. </div>,
  195. );
  196. }
  197. function initLogin() {
  198. const userName = store.getState().loadUser.username
  199. updateAccountContext(
  200. userName?
  201. [
  202. {
  203. id: 'username',
  204. disabled: !!userName,
  205. icon: 'person',
  206. name: userName,
  207. },
  208. {
  209. id: 'divider',
  210. type: 'divider',
  211. },
  212. {
  213. id: 'logout',
  214. icon: 'log-out',
  215. name: 'Log Out',
  216. onClick: () => {
  217. store.dispatch(loadUser({}))
  218. deleteCookie('token')
  219. window.location.reload();
  220. }
  221. }]
  222. :[
  223. {
  224. id: 'login',
  225. name: 'Log In',
  226. icon: 'log-in',
  227. onClick: () => {
  228. goToLogin();
  229. },
  230. }
  231. ]
  232. );
  233. }