Skip to content

Instantly share code, notes, and snippets.

@pierogitus
Created July 19, 2016 19:05
Show Gist options
  • Save pierogitus/e5d68605c9f014e51dad0df47515b4d7 to your computer and use it in GitHub Desktop.
Save pierogitus/e5d68605c9f014e51dad0df47515b4d7 to your computer and use it in GitHub Desktop.
monaco editor with hacks
/*
Copyright (c) 2016 Alex Thompson
Licensed under the MIT License. Qme5EVPFLjWxs9joD9oJXxmXdKVGPMFMR45Yi16t5EoJfF
*/
declare var require
var d = React.DOM
declare var ace
export interface props {
codePane: HTMLElement
ref
}
export class view extends React.Component<props, any> {
editor: monaco.editor.ICodeEditor
change: () => void
changeCursor: () => void
switchModule: (modName: string) => void
initModel: monaco.editor.IModel
constructor(public props: props) {
super(props)
}
render() {
return null
}
componentDidMount() {
try {
let script = document.createElement("script");
script.src = System.paths['monacoRoot'] + '/loader.js'
script.async = true;
script.setAttribute('crossOrigin', 'anonymous')
script.onload = () => {
var _that = this
var oldRequire = require
function newRequire() {
if(arguments.length >= 2) {
if(arguments[0][0] == 'vs/editor/browser/standalone/simpleServices') {
var cb = arguments[1]
arguments[1] = function(m) {
m.SimpleEditorService.prototype.findModel = function(editor, data) {
var mod = monaco.editor.getModel(data.resource);
var s = (<any>new Error()).stack
if(s.indexOf('openEditor') >= 0) {
editor.setModel(mod)
_that.switchModule(mod.uri.toString())
}
return mod
};
if(cb) {
cb(m)
}
}
}
if(arguments[0][0] == 'vs/language/typescript/src/languageFeatures') {
var cb = arguments[1]
arguments[1] = function(m) {
m.FormatHelper._convertOptions = function(options) {
return {
ConvertTabsToSpaces: options.insertSpaces,
TabSize: options.tabSize,
IndentSize: options.tabSize,
IndentStyle: ts.IndentStyle.Smart,
NewLineCharacter: '\n',
InsertSpaceAfterCommaDelimiter: true,
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
InsertSpaceAfterKeywordsInControlFlowStatements: false,
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: true,
InsertSpaceAfterSemicolonInForStatements: false,
InsertSpaceBeforeAndAfterBinaryOperators: true,
PlaceOpenBraceOnNewLineForControlBlocks: false,
PlaceOpenBraceOnNewLineForFunctions: false
};
}
if(cb) {
cb(m)
}
}
}
if(arguments[0][0] == 'vs/language/typescript/src/workerManager') {
var cb = arguments[1]
arguments[1] = function(m) {
m.WorkerManager.prototype._checkIfIdle = function() { }
if(cb) {
cb(m)
}
}
}
if(arguments[0][0] == 'vs/editor/common/services/editorWorkerServiceImpl') {
var cb = arguments[1]
arguments[1] = function(m) {
var f = m.EditorWorkerClient.prototype._withSyncedResources
m.EditorWorkerClient.prototype._withSyncedResources = function(r) {
window['hackEditorWorker'] = this;
return f.apply(this, [r])
}
var fw = m.EditorWorkerServiceImpl.prototype.computeLinks
m.EditorWorkerServiceImpl.prototype.computeLinks = function(r) {
window['hackEditorWorkerService'] = this;
return fw.apply(this, [r])
}
if(cb) {
cb(m)
}
}
}
}
oldRequire.apply(this, arguments)
}
newRequire['config'] = require.config
newRequire['getConfig'] = require.getConfig
newRequire['reset'] = require.reset
newRequire['getBuildInfo'] = require.getBuildInfo
newRequire['getStats'] = require.getStats
require = newRequire
require.config({ paths: { 'vs': System.paths['monacoRoot'] } });
window['MonacoEnvironment'] = {
getWorkerUrl: function(workerId, label) {
return System.paths['monacoWorker'];
}
};
require(["vs/editor/editor.main"], this.create);
}
document.body.appendChild(script);
}
catch(e) {
console.log(e.message + e.stack)
}
}
create = () => {
require(['vs/language/typescript/src/mode'], () => {
require(['vs/editor/browser/standalone/simpleServices'], () => {
require(['vs/language/typescript/src/languageFeatures'], () => {
require(['vs/language/typescript/src/workerManager'], () => {
require(['vs/editor/common/services/editorWorkerServiceImpl'], () => {
var m = monaco.editor.createModel('', 'javascript', new monaco.Uri().with({ path: 'singleModule' }))
this.editor = monaco.editor.create(this.props.codePane, { model: m })
//this.editor.onDidChangeModelContent(this.didChangeModel)
if(this.change) {
this.editor.onDidChangeModelContent(this.change)
}
if(this.changeCursor) {
this.editor.onDidChangeCursorPosition(this.changeCursor)
}
if(this.initModel) {
this.editor.setModel(this.initModel)
}
setInterval(() => { window['hackEditorWorker']._withSyncedResources(monaco.editor.getModels().map(x => x.uri)); }, 10000)
setInterval(() => { window['hackEditorWorkerService']._workerManager.withWorker() }, 10000)
this.editor.layout()
this.editor.focus()
})
})
})
})
})
}
getText(): string {
return this.editor ? this.editor.getValue() : ''
}
setText(text: string) {
if(this.editor) {
this.editor.setValue(text)
}
}
setChange(cb: () => void) {
if(this.editor) {
this.editor.onDidChangeModelContent(cb)
}
this.change = cb
}
setChangeCursor(cb: () => void) {
if(this.editor) {
this.editor.onDidChangeCursorPosition(cb)
}
this.changeCursor = cb
}
setProject(infos: { [name: string]: string }) {
for(var k in infos) {
monaco.languages.typescript.typescriptDefaults.addExtraLib(infos[k], k)
}
for(var k in infos) {
monaco.editor.createModel(infos[k], 'typescript', new monaco.Uri().with({ path: k }))
}
}
setModule(modName: string) {
var m = monaco.editor.getModel(new monaco.Uri().with({ path: modName }))
if(this.editor) {
this.editor.setModel(m)
}
else {
this.initModel = m
}
}
resize = () => {
if(this.editor) {
this.editor.layout()
}
}
getOffset(): number {
if(this.editor) {
var p = this.editor.getPosition()
return this.editor.getModel().getOffsetAt(p)
}
return 0;
}
getPosition(offset: number): { column: number, lineNumber: number } {
if(this.editor) {
return this.editor.getModel().getPositionAt(offset)
}
return { column: 0, lineNumber: 0 }
}
}
@BeginnerGitHub
Copy link

Hi,

I would like to access your full app but the link you have given here: microsoft/monaco-editor#64 (comment) no longer works.

Could you please tell me where I could get your full app?
Thanks.

@pierogitus
Copy link
Author

@BeginnerGitHub I stopped work on this several years ago. I would assume the monaco editor has also had significant refactoring since then so these hacks would probably not work anymore.

@BeginnerGitHub
Copy link

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment