Monaco Editor 'onChange' event?
I've just started investigating Monaco to be used as the editor for our internal code playground. And I'm unable to figure out how to create a handler for whenever the text in the editor window is changed, either by typing, pasting, or deleting. For context, using the CodeMirror editor, I simply did:
editor.on('change', function(editor, change) {
render();
});
Here is my JavaScript that creates the basic editor:
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
window.editor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
});
Thanks!
monaco-editor
add a comment |
I've just started investigating Monaco to be used as the editor for our internal code playground. And I'm unable to figure out how to create a handler for whenever the text in the editor window is changed, either by typing, pasting, or deleting. For context, using the CodeMirror editor, I simply did:
editor.on('change', function(editor, change) {
render();
});
Here is my JavaScript that creates the basic editor:
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
window.editor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
});
Thanks!
monaco-editor
add a comment |
I've just started investigating Monaco to be used as the editor for our internal code playground. And I'm unable to figure out how to create a handler for whenever the text in the editor window is changed, either by typing, pasting, or deleting. For context, using the CodeMirror editor, I simply did:
editor.on('change', function(editor, change) {
render();
});
Here is my JavaScript that creates the basic editor:
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
window.editor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
});
Thanks!
monaco-editor
I've just started investigating Monaco to be used as the editor for our internal code playground. And I'm unable to figure out how to create a handler for whenever the text in the editor window is changed, either by typing, pasting, or deleting. For context, using the CodeMirror editor, I simply did:
editor.on('change', function(editor, change) {
render();
});
Here is my JavaScript that creates the basic editor:
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
window.editor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
});
Thanks!
monaco-editor
monaco-editor
asked Feb 16 '18 at 14:06
eloraelora
515
515
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I found onDidChangeContent method the other day.
In your example you would attach the listener like this:
window.editor.model.onDidChangeContent((event) => {
render();
});
1
Thanks! I had finally found onDidChangeContentModel which worked. onDidChangeContent itself didn't work in my case.
– elora
Feb 16 '18 at 15:36
1
The difference is that one is a method of the editor and one of a model. Note in my example that the method is called on window.editor.model
– Gil
Feb 16 '18 at 20:08
add a comment |
After a lot of poking and experimenting, I did find something. I haven't figured out the difference between creating an editor and creating an editor using a model (not that I've looked), but the following works for me:
var monEditor;
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
monEditor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
monEditor.onDidChangeModelContent(function (e) {
render();
});
});
Using just onDidChangeContent did not work for me.
add a comment |
To expand on Gil's answer, there are two different methods, onDidChangeContent
and onDidChangeContentModel
.
onDidChangeContent
is attached to a model, and will only apply to that model
onDidChangeContentModel
is attached to the editor and will apply to all models
The nice thing is that you can use different onDidChangeContent
listeners on multiple models, switch them out with one another, and they'll all preserve their own onChange events. For example, you might have an editor with different models for HTML, CSS, and JS. If you wanted different onChange
listeners for each of those, this is easily achievable. At the same time, you can have listeners using onDidChangeContentModel
that will apply to all models.
To update his answer, as of the current release (0.15.6), the syntax editor.model
doesn't work. You have to use editor.getModel()
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48828538%2fmonaco-editor-onchange-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I found onDidChangeContent method the other day.
In your example you would attach the listener like this:
window.editor.model.onDidChangeContent((event) => {
render();
});
1
Thanks! I had finally found onDidChangeContentModel which worked. onDidChangeContent itself didn't work in my case.
– elora
Feb 16 '18 at 15:36
1
The difference is that one is a method of the editor and one of a model. Note in my example that the method is called on window.editor.model
– Gil
Feb 16 '18 at 20:08
add a comment |
I found onDidChangeContent method the other day.
In your example you would attach the listener like this:
window.editor.model.onDidChangeContent((event) => {
render();
});
1
Thanks! I had finally found onDidChangeContentModel which worked. onDidChangeContent itself didn't work in my case.
– elora
Feb 16 '18 at 15:36
1
The difference is that one is a method of the editor and one of a model. Note in my example that the method is called on window.editor.model
– Gil
Feb 16 '18 at 20:08
add a comment |
I found onDidChangeContent method the other day.
In your example you would attach the listener like this:
window.editor.model.onDidChangeContent((event) => {
render();
});
I found onDidChangeContent method the other day.
In your example you would attach the listener like this:
window.editor.model.onDidChangeContent((event) => {
render();
});
edited Sep 20 '18 at 2:20
Zuzanna Opała
184
184
answered Feb 16 '18 at 15:25
GilGil
766
766
1
Thanks! I had finally found onDidChangeContentModel which worked. onDidChangeContent itself didn't work in my case.
– elora
Feb 16 '18 at 15:36
1
The difference is that one is a method of the editor and one of a model. Note in my example that the method is called on window.editor.model
– Gil
Feb 16 '18 at 20:08
add a comment |
1
Thanks! I had finally found onDidChangeContentModel which worked. onDidChangeContent itself didn't work in my case.
– elora
Feb 16 '18 at 15:36
1
The difference is that one is a method of the editor and one of a model. Note in my example that the method is called on window.editor.model
– Gil
Feb 16 '18 at 20:08
1
1
Thanks! I had finally found onDidChangeContentModel which worked. onDidChangeContent itself didn't work in my case.
– elora
Feb 16 '18 at 15:36
Thanks! I had finally found onDidChangeContentModel which worked. onDidChangeContent itself didn't work in my case.
– elora
Feb 16 '18 at 15:36
1
1
The difference is that one is a method of the editor and one of a model. Note in my example that the method is called on window.editor.model
– Gil
Feb 16 '18 at 20:08
The difference is that one is a method of the editor and one of a model. Note in my example that the method is called on window.editor.model
– Gil
Feb 16 '18 at 20:08
add a comment |
After a lot of poking and experimenting, I did find something. I haven't figured out the difference between creating an editor and creating an editor using a model (not that I've looked), but the following works for me:
var monEditor;
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
monEditor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
monEditor.onDidChangeModelContent(function (e) {
render();
});
});
Using just onDidChangeContent did not work for me.
add a comment |
After a lot of poking and experimenting, I did find something. I haven't figured out the difference between creating an editor and creating an editor using a model (not that I've looked), but the following works for me:
var monEditor;
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
monEditor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
monEditor.onDidChangeModelContent(function (e) {
render();
});
});
Using just onDidChangeContent did not work for me.
add a comment |
After a lot of poking and experimenting, I did find something. I haven't figured out the difference between creating an editor and creating an editor using a model (not that I've looked), but the following works for me:
var monEditor;
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
monEditor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
monEditor.onDidChangeModelContent(function (e) {
render();
});
});
Using just onDidChangeContent did not work for me.
After a lot of poking and experimenting, I did find something. I haven't figured out the difference between creating an editor and creating an editor using a model (not that I've looked), but the following works for me:
var monEditor;
require.config({ paths: { 'vs': '../node_modules/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function()
{
monEditor = monaco.editor.create(document.getElementById('editor'),
{
value: [
'var canvas = document.getElementById("playground");',
'var ctx = canvas.getContext("2d");',
'ctx.fillStyle = "#FF00FF";',
'ctx.fillRect(0,0,150,75);',
].join('n'),
language: 'javascript'
});
monEditor.onDidChangeModelContent(function (e) {
render();
});
});
Using just onDidChangeContent did not work for me.
answered Feb 16 '18 at 15:35
eloraelora
515
515
add a comment |
add a comment |
To expand on Gil's answer, there are two different methods, onDidChangeContent
and onDidChangeContentModel
.
onDidChangeContent
is attached to a model, and will only apply to that model
onDidChangeContentModel
is attached to the editor and will apply to all models
The nice thing is that you can use different onDidChangeContent
listeners on multiple models, switch them out with one another, and they'll all preserve their own onChange events. For example, you might have an editor with different models for HTML, CSS, and JS. If you wanted different onChange
listeners for each of those, this is easily achievable. At the same time, you can have listeners using onDidChangeContentModel
that will apply to all models.
To update his answer, as of the current release (0.15.6), the syntax editor.model
doesn't work. You have to use editor.getModel()
.
add a comment |
To expand on Gil's answer, there are two different methods, onDidChangeContent
and onDidChangeContentModel
.
onDidChangeContent
is attached to a model, and will only apply to that model
onDidChangeContentModel
is attached to the editor and will apply to all models
The nice thing is that you can use different onDidChangeContent
listeners on multiple models, switch them out with one another, and they'll all preserve their own onChange events. For example, you might have an editor with different models for HTML, CSS, and JS. If you wanted different onChange
listeners for each of those, this is easily achievable. At the same time, you can have listeners using onDidChangeContentModel
that will apply to all models.
To update his answer, as of the current release (0.15.6), the syntax editor.model
doesn't work. You have to use editor.getModel()
.
add a comment |
To expand on Gil's answer, there are two different methods, onDidChangeContent
and onDidChangeContentModel
.
onDidChangeContent
is attached to a model, and will only apply to that model
onDidChangeContentModel
is attached to the editor and will apply to all models
The nice thing is that you can use different onDidChangeContent
listeners on multiple models, switch them out with one another, and they'll all preserve their own onChange events. For example, you might have an editor with different models for HTML, CSS, and JS. If you wanted different onChange
listeners for each of those, this is easily achievable. At the same time, you can have listeners using onDidChangeContentModel
that will apply to all models.
To update his answer, as of the current release (0.15.6), the syntax editor.model
doesn't work. You have to use editor.getModel()
.
To expand on Gil's answer, there are two different methods, onDidChangeContent
and onDidChangeContentModel
.
onDidChangeContent
is attached to a model, and will only apply to that model
onDidChangeContentModel
is attached to the editor and will apply to all models
The nice thing is that you can use different onDidChangeContent
listeners on multiple models, switch them out with one another, and they'll all preserve their own onChange events. For example, you might have an editor with different models for HTML, CSS, and JS. If you wanted different onChange
listeners for each of those, this is easily achievable. At the same time, you can have listeners using onDidChangeContentModel
that will apply to all models.
To update his answer, as of the current release (0.15.6), the syntax editor.model
doesn't work. You have to use editor.getModel()
.
answered Jan 18 at 23:26
Benny HinrichsBenny Hinrichs
425
425
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48828538%2fmonaco-editor-onchange-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown