mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
ui: scratchpad: allow reuse existing models
This commit is contained in:
1 parent
39e9b4a9cc
commit
c0550fc97c
4 files changed
+39
-22
No files matched your search
@@ -66,7 +66,7 @@ export async function load(features = ['markdown']) {
|
|||||||
}
|
}
|
||||||
await res.loadThemePromise;
|
await res.loadThemePromise;
|
||||||
resolve();
|
resolve();
|
||||||
return { monaco: res.default, registerAction: res.registerAction };
|
return { monaco: res.default, registerAction: res.registerAction, customOptions: {} };
|
||||||
}
|
}
|
||||||
|
|
||||||
export default load;
|
export default load;
|
||||||
|
|||||||
+25
-14
@@ -1,25 +1,36 @@
|
|||||||
|
import type * as monaco from 'monaco-editor';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import { load } from 'vj/components/monaco/loader';
|
import { load } from 'vj/components/monaco/loader';
|
||||||
|
|
||||||
const mapStateToProps = (state) => ({
|
interface ScratchpadOptions {
|
||||||
|
value?: string;
|
||||||
|
language?: string;
|
||||||
|
handleUpdateCode?: (str: string, event: monaco.editor.IModelContentChangedEvent) => void;
|
||||||
|
mainSize?: number;
|
||||||
|
recordSize?: number;
|
||||||
|
pretestSize?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default connect((state: any) => ({
|
||||||
value: state.editor.code,
|
value: state.editor.code,
|
||||||
language: window.LANGS[state.editor.lang]?.monaco,
|
language: window.LANGS[state.editor.lang]?.monaco,
|
||||||
mainSize: state.ui.main.size,
|
mainSize: state.ui.main.size,
|
||||||
pretestSize: state.ui.pretest.size,
|
pretestSize: state.ui.pretest.size,
|
||||||
recordSize: state.ui.records.size,
|
recordSize: state.ui.records.size,
|
||||||
});
|
}), (dispatch) => ({
|
||||||
const mapDispatchToProps = (dispatch) => ({
|
handleUpdateCode: (code: string) => {
|
||||||
handleUpdateCode: (code) => {
|
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'SCRATCHPAD_EDITOR_UPDATE_CODE',
|
type: 'SCRATCHPAD_EDITOR_UPDATE_CODE',
|
||||||
payload: code,
|
payload: code,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
}))(class MonacoEditor extends React.PureComponent<ScratchpadOptions> {
|
||||||
|
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(class MonacoEditor extends React.PureComponent {
|
|
||||||
disposable = [];
|
disposable = [];
|
||||||
|
__prevent_trigger_change_event = false;
|
||||||
|
model: monaco.editor.ITextModel;
|
||||||
|
editor: monaco.editor.IStandaloneCodeEditor;
|
||||||
|
containerElement: HTMLElement;
|
||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
const value = this.props.value || '';
|
const value = this.props.value || '';
|
||||||
@@ -28,12 +39,11 @@ export default connect(mapStateToProps, mapDispatchToProps)(class MonacoEditor e
|
|||||||
const uri = monaco.Uri.parse(`hydro://${UiContext.pdoc.pid || UiContext.pdoc.docId}.${language}`);
|
const uri = monaco.Uri.parse(`hydro://${UiContext.pdoc.pid || UiContext.pdoc.docId}.${language}`);
|
||||||
this.model = monaco.editor.createModel(value, language, uri);
|
this.model = monaco.editor.createModel(value, language, uri);
|
||||||
if (this.containerElement) {
|
if (this.containerElement) {
|
||||||
/** @type {monaco.editor.IStandaloneEditorConstructionOptions} */
|
const config: monaco.editor.IStandaloneEditorConstructionOptions = {
|
||||||
const config = {
|
|
||||||
theme: 'vs-light',
|
theme: 'vs-light',
|
||||||
...customOptions,
|
...customOptions,
|
||||||
fontFamily: UserContext.codeFontFamily,
|
fontFamily: UserContext.codeFontFamily,
|
||||||
lineNumbers: true,
|
lineNumbers: 'on',
|
||||||
glyphMargin: true,
|
glyphMargin: true,
|
||||||
lightbulb: { enabled: true },
|
lightbulb: { enabled: true },
|
||||||
model: this.model,
|
model: this.model,
|
||||||
@@ -43,12 +53,12 @@ export default connect(mapStateToProps, mapDispatchToProps)(class MonacoEditor e
|
|||||||
this.disposable.push(
|
this.disposable.push(
|
||||||
this.editor.onDidChangeModelContent((event) => {
|
this.editor.onDidChangeModelContent((event) => {
|
||||||
if (!this.__prevent_trigger_change_event) {
|
if (!this.__prevent_trigger_change_event) {
|
||||||
this.props.handleUpdateCode(this.editor.getValue({ lineEnding: '\n', preserveBOM: false }), event);
|
this.props.handleUpdateCode?.(this.editor.getValue({ lineEnding: '\n', preserveBOM: false }), event);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
window.editor = this.editor;
|
(window as any).editor = this.editor;
|
||||||
window.monaco = monaco;
|
(window as any).monaco = monaco;
|
||||||
window.Hydro.bus.emit('scratchpadEditorCreate', this.editor, monaco);
|
window.Hydro.bus.emit('scratchpadEditorCreate', this.editor, monaco);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,6 +81,7 @@ export default connect(mapStateToProps, mapDispatchToProps)(class MonacoEditor e
|
|||||||
text: value,
|
text: value,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
() => null,
|
||||||
);
|
);
|
||||||
editor.pushUndoStop();
|
editor.pushUndoStop();
|
||||||
this.__prevent_trigger_change_event = false;
|
this.__prevent_trigger_change_event = false;
|
||||||
@@ -79,7 +90,7 @@ export default connect(mapStateToProps, mapDispatchToProps)(class MonacoEditor e
|
|||||||
const val = model.getValue(LF, false);
|
const val = model.getValue(LF, false);
|
||||||
model.dispose();
|
model.dispose();
|
||||||
const uri = monaco.Uri.parse(`hydro://${UiContext.pdoc.pid || UiContext.pdoc.docId}.${language}`);
|
const uri = monaco.Uri.parse(`hydro://${UiContext.pdoc.pid || UiContext.pdoc.docId}.${language}`);
|
||||||
this.model = monaco.editor.createModel(val, language, uri);
|
this.model = monaco.editor.getModel(uri) || monaco.editor.createModel(val, language, uri);
|
||||||
editor.setModel(this.model);
|
editor.setModel(this.model);
|
||||||
}
|
}
|
||||||
if (editor) {
|
if (editor) {
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/ui-default",
|
"name": "@hydrooj/ui-default",
|
||||||
"version": "4.40.9",
|
"version": "4.40.10",
|
||||||
"author": "undefined <i@undefined.moe>",
|
"author": "undefined <i@undefined.moe>",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"main": "hydro.js",
|
"main": "hydro.js",
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
declare global {
|
||||||
declare let UserContext: any;
|
interface Window {
|
||||||
declare let UiContext: any;
|
node_modules: any;
|
||||||
// eslint-disable-next-line
|
LANGS: Record<string, any>;
|
||||||
declare let node_modules: any;
|
|
||||||
declare let LANGS: Record<string, any>;
|
}
|
||||||
|
|
||||||
|
let UserContext: Record<string, any>;
|
||||||
|
let UiContext: Record<string, any>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export { };
|
||||||
Reference in new issue
Block a user