@ -62,7 +62,7 @@ export async function createMonaco(textarea, filename, editorOpts) {
const monaco = await import ( /* webpackChunkName: "monaco" */ 'monaco-editor' ) ;
const monaco = await import ( /* webpackChunkName: "monaco" */ 'monaco-editor' ) ;
initLanguages ( monaco ) ;
initLanguages ( monaco ) ;
let { language , ... other } = editorOpts ;
let { language , eol , ... other } = editorOpts ;
if ( ! language ) language = getLanguage ( filename ) ;
if ( ! language ) language = getLanguage ( filename ) ;
const container = document . createElement ( 'div' ) ;
const container = document . createElement ( 'div' ) ;
@ -105,14 +105,28 @@ export async function createMonaco(textarea, filename, editorOpts) {
monaco . languages . register ( { id : 'vs.editor.nullLanguage' } ) ;
monaco . languages . register ( { id : 'vs.editor.nullLanguage' } ) ;
monaco . languages . setLanguageConfiguration ( 'vs.editor.nullLanguage' , { } ) ;
monaco . languages . setLanguageConfiguration ( 'vs.editor.nullLanguage' , { } ) ;
// We encode the initial value in JSON on the backend to prevent browsers from
// discarding the \r during HTML parsing:
// https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
const value = JSON . parse ( textarea . getAttribute ( 'data-initial-value' ) || '""' ) ;
textarea . value = value ;
textarea . removeAttribute ( 'data-initial-value' ) ;
const editor = monaco . editor . create ( container , {
const editor = monaco . editor . create ( container , {
value : textarea . value ,
value ,
theme : 'gitea' ,
theme : 'gitea' ,
language ,
language ,
... other ,
... other ,
} ) ;
} ) ;
const model = editor . getModel ( ) ;
const model = editor . getModel ( ) ;
// Monaco performs auto-detection of dominant EOL in the file, biased towards LF for
// empty files. If there is an editorconfig value, override this detected value.
if ( eol in monaco . editor . EndOfLineSequence ) {
model . setEOL ( monaco . editor . EndOfLineSequence [ eol ] ) ;
}
model . onDidChangeContent ( ( ) => {
model . onDidChangeContent ( ( ) => {
textarea . value = editor . getValue ( ) ;
textarea . value = editor . getValue ( ) ;
textarea . dispatchEvent ( new Event ( 'change' ) ) ; // seems to be needed for jquery-are-you-sure
textarea . dispatchEvent ( new Event ( 'change' ) ) ; // seems to be needed for jquery-are-you-sure
@ -187,5 +201,6 @@ function getEditorConfigOptions(ec) {
opts . trimAutoWhitespace = ec . trim _trailing _whitespace === true ;
opts . trimAutoWhitespace = ec . trim _trailing _whitespace === true ;
opts . insertSpaces = ec . indent _style === 'space' ;
opts . insertSpaces = ec . indent _style === 'space' ;
opts . useTabStops = ec . indent _style === 'tab' ;
opts . useTabStops = ec . indent _style === 'tab' ;
opts . eol = ec . end _of _line ? . toUpperCase ( ) ;
return opts ;
return opts ;
}
}