Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monaco-editor support #299

Merged
merged 26 commits into from
Jun 6, 2018
Merged

Conversation

pranjaltale16
Copy link
Contributor

@pranjaltale16 pranjaltale16 commented May 24, 2018

Add support for monaco-editor.
I'm not sure, how do I've to update tests.
Also, I'm not sure about the detach thing. I tried to look for it, but unable to find anything for that.
Could someone help me with these 2 things?

@coveralls
Copy link

coveralls commented May 24, 2018

Coverage Status

Coverage decreased (-0.2%) to 72.543% when pulling 9b4880e on pranjaltale16:monaco into b678875 on firebase:master.

@pranjaltale16 pranjaltale16 changed the title Monaco Monaco-editor support May 24, 2018
@mikelehen
Copy link
Collaborator

Thanks! Would it be possible to add an examples/monaco.html file that demos this working? That'll also be helpful for anybody that tries to use this down the road.

@pranjaltale16
Copy link
Contributor Author

pranjaltale16 commented May 26, 2018

Done. I followed this to integrate the editor.
Since it is loaded with AMD modules so I've to keep the scripts locally.

@pranjaltale16
Copy link
Contributor Author

The other alternative could be to have monaco-editor in package.json. But in this case, one has to install node modules first. Otherwise, it won't work.

@pranjaltale16
Copy link
Contributor Author

Hey! What would be the better way to have the editor in examples?

@mikelehen
Copy link
Collaborator

Could you use this CDN version? https://cdnjs.com/libraries/monaco-editor

Else, having people install node modules first is probably fine.

@pranjaltale16
Copy link
Contributor Author

Hey! I updated that. Have a look.

@pranjaltale16
Copy link
Contributor Author

Copy link
Collaborator

@mikelehen mikelehen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woo! I was able to test this out and it seems to work quite well. Nice work!

I've left a few comments (though I didn't review the actual adapter too deeply). Please take a look and then we can likely get this merged. Thanks for putting this together!

package.json Outdated
"jsdom": ">3",
"firebase": "^3 || ^4"
"monaco-editor": "^0.13.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this a devDependency please? It's necessary to run the examples but we don't want to automatically depend on monaco-editor when you install firepad (since you may be using it with ace / codemirror)

test/index.html Outdated
@@ -15,6 +15,9 @@
<!-- CodeMirror -->
<script src="../bower_components/codemirror/lib/codemirror.js"></script>

<!-- Monaco -->
<script src="../bower_components/monaco-editor-official/monaco.d.ts"></script>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm... I don't see monaco-editor-official added to ~/bower.json and when I open this file, there's a 404. But the tests still pass because I don't think they depend on this. So maybe just remove this script include?

var firepadRef = getExampleRef();

//// Create Monaco and firepad.
require.config({ paths: {'vs': './../node_modules/monaco-editor/min/vs'}});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm... You're pulling in loader.js from the CDN but then loading from node_modules here... Ideally we'd use one or the other (and the CDN would be best so people can just copy/paste the example and have it work).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not that sure about the CDN thing, so added loader import from local only.


<!-- Firepad -->
<link rel="stylesheet" href="https://cdn.firebase.com/libs/firepad/1.4.0/firepad.css" />
<script src="https://cdn.firebase.com/libs/firepad/1.4.0/firepad.min.js"></script>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously this doesn't work yet. I can change it to 1.5.0 once it's released... but given that as-written, this depends on local files (node_modules/monaco-editor) you could also just change this to ../dist/firepad.min.js so that the example works as-is.

lib/firepad.js Outdated
if (!CodeMirror && !ace) {
throw new Error('Couldn\'t find CodeMirror or ACE. Did you forget to include codemirror.js or ace.js?');
if (!CodeMirror && !ace && !global.monaco) {
throw new Error('Couldn\'t find CodeMirror, ACE or Monaco. Did you forget to include codemirror.js/ace.js/monaco.d.ts?');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. I thought a .d.ts file only has type definitions and so it won't help to include it, right (it's not going to create global variables and whatnot). Can you fix this to be more correct, or just remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to remove this, but it is giving some error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, I just meant remove "/monaco.d.ts" from the error message...

lib/firepad.js Outdated
this.monaco_ = this.editor_ = place;
curValue = this.monaco_.getValue();
if (curValue !== '') {
throw new Error("Can't initialize Firepad with an Monaco instance that already contains text.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"an" => "a"

};

MonacoAdapter.prototype.detach = function() {
this.monaco.onDidChangeModelContent(this.onChange).dispose();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't dug into Monaco's implementation, but I kinda' suspect that this is attaching a new listener and then immediately removing it, and likely isn't removing the listener you attached in the constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! Even I'm not that sure about this. A better addition would be to have
this.onChange.dispose() this will call the dispose of for the actual attached onChange.

@pranjaltale16
Copy link
Contributor Author

Hey! I updated the changes. Have a look. :D

Copy link
Collaborator

@mikelehen mikelehen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry... One more question about the detach() code. :-)

this.onChange.dispose();
this.onBlur.dispose();
this.onFocus.dispose();
this.onCursorActivity.dispose();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm... Sorry to keep bugging you about this code, but have you tried calling firepad.dispose() to verify this actually runs okay? Unless calling this.monaco.onDidChangeModelContent(this.onChange) actually mutates your onChange function and adds a dispose property, I'd guess that this will not work correctly.

I would guess that what you need to do is something like:

var changeHandler = this.monaco.onDidChangeModelContent(this.onChange);
var didBlurHandler = this.monaco.onDidBlurEditor(this.onBlur);
...

this.detach = function() {
  changeHandler.dispose();
  blurHandler.dispose();
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, Thank you for helping me out with this. I'm clueless about the detach function.

@pranjaltale16
Copy link
Contributor Author

Is this okay? Do let me know if I've to make any other change. Actually, I'm not aware of the functioning of detach and how to handle attached listeners from monaco, so that's why this part is little confusing for me.
Again, Thanks for helping me.

@mikelehen mikelehen merged commit 251df18 into FirebaseExtended:master Jun 6, 2018
@mikelehen
Copy link
Collaborator

Thanks! It wasn't quite right, but I went ahead and merged it and then submitted a fix. :-)

@mikelehen
Copy link
Collaborator

I've pushed a new v1.5.0 npm module of firepad with the monaco support. Thanks for tackling this!

@pranjaltale16
Copy link
Contributor Author

Thank you so much for helping me out with this. :)

@mikelehen mikelehen mentioned this pull request Jul 3, 2018
mfont-gh pushed a commit to mfont-gh/firepad that referenced this pull request Jun 3, 2019
* New MonacoAdapter class added and plumbed into lib/firepad.js, etc.
* examples/monaco.html added to demonstrate it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants