ui: editor: always use LF as line ending

This commit is contained in:
undefined committed 2022-03-13 01:55:18 +08:00
1 parent 32bf5b9a7e
commit c4521d9ce3
4 files changed
+10 -10

No files matched your search

@@ -143,7 +143,7 @@ export default class Editor extends DOMAttachedObject {
}); });
} }
this.editor.onDidChangeModelContent(() => { this.editor.onDidChangeModelContent(() => {
const val = this.editor.getValue(); const val = this.editor.getValue({ lineEnding: '\n', preserveBOM: false });
$dom.val(val); $dom.val(val);
$dom.text(val); $dom.text(val);
if (onChange) onChange(val); if (onChange) onChange(val);
@@ -208,7 +208,7 @@ export default class Editor extends DOMAttachedObject {
value(val?: string) { value(val?: string) {
this.ensureValid(); this.ensureValid();
if (typeof val === 'string') return (this.editor || this.vditor).setValue(val); if (typeof val === 'string') return (this.editor || this.vditor).setValue(val);
return (this.editor || this.vditor).getValue(); return (this.editor || this.vditor).getValue({ lineEnding: '\n', preserveBOM: false });
} }
focus() { focus() {
@@ -134,7 +134,7 @@ function handlePasteEvent(editor: monaco.editor.IStandaloneCodeEditor) {
export function registerAction( export function registerAction(
editor: monaco.editor.IStandaloneCodeEditor, editor: monaco.editor.IStandaloneCodeEditor,
model: monaco.editor.IModel, model: monaco.editor.IModel,
element, element?,
) { ) {
if (element) { if (element) {
editor.addAction({ editor.addAction({
@@ -1,5 +1,6 @@
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';
const mapStateToProps = (state) => ({ const mapStateToProps = (state) => ({
value: state.editor.code, value: state.editor.code,
@@ -23,7 +24,6 @@ export default connect(mapStateToProps, mapDispatchToProps)(class MonacoEditor e
async componentDidMount() { async componentDidMount() {
const value = this.props.value || ''; const value = this.props.value || '';
const { language } = this.props; const { language } = this.props;
const { load } = await import('vj/components/monaco/loader');
const { monaco, registerAction, customOptions } = await load([language]); const { monaco, registerAction, customOptions } = await load([language]);
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);
@@ -42,7 +42,7 @@ 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(), event); this.props.handleUpdateCode(this.editor.getValue({ lineEnding: '\n', preserveBOM: false }), event);
} }
}), }),
); );
@@ -56,7 +56,9 @@ export default connect(mapStateToProps, mapDispatchToProps)(class MonacoEditor e
value, language, mainSize, recordSize, pretestSize, value, language, mainSize, recordSize, pretestSize,
} = this.props; } = this.props;
const { editor, model } = this; const { editor, model } = this;
if (this.props.value != null && this.props.value !== model.getValue()) { const { monaco } = await load([language]);
const { LF } = monaco.editor.EndOfLinePreference;
if (this.props.value != null && this.props.value !== model.getValue(LF, false)) {
this.__prevent_trigger_change_event = true; this.__prevent_trigger_change_event = true;
editor.pushUndoStop(); editor.pushUndoStop();
model.pushEditOperations( model.pushEditOperations(
@@ -71,10 +73,8 @@ export default connect(mapStateToProps, mapDispatchToProps)(class MonacoEditor e
editor.pushUndoStop(); editor.pushUndoStop();
this.__prevent_trigger_change_event = false; this.__prevent_trigger_change_event = false;
} }
const { load } = await import('vj/components/monaco/loader');
const { monaco } = await load([language]);
if (prevProps.language !== language) { if (prevProps.language !== language) {
const val = model.getValue(); 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.createModel(val, language, uri);
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/ui-default", "name": "@hydrooj/ui-default",
"version": "4.34.0", "version": "4.34.1",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"main": "hydro.js", "main": "hydro.js",