foo123 / ace-grammar

Transform a JSON grammar into a syntax-highlight parser for ACE Editor
https://foo123.github.io/examples/ace-grammar/
36 stars 8 forks source link

Ace ``window`` (Global Object) is Not Defined When Using ``editor.session.setMode()`` #8

Open KiddoV opened 2 years ago

KiddoV commented 2 years ago

My App has a few grammar modes defined in global vars:

var ins_mode = AceGrammar.getMode({
    "RegExpID": "RE::",
        "Style": {
    //...
});

var ycd_mode = AceGrammar.getMode({
    "RegExpID": "RE::",
        "Style": {
    //...
});

var aoi3_mode = AceGrammar.getMode({
    "RegExpID": "RE::",
        "Style": {
    //...
});

var mmd_mode = AceGrammar.getMode({
    "RegExpID": "RE::",
        "Style": {
    //...
});

When I try to set the editor to a different mode, ace.js gave me this error:

let modeStr = getMode() //This return either "ins_mode", "ycd_mode" ... in string
myAceEditor.session.setMode(window[modeStr]);
ace.js?nocache=nonce_1_1657893450835_623:11570 

       Uncaught ReferenceError: window is not defined
    at ace.js?nocache=nonce_1_1657893450835_623:11570:5
    at ace.js?nocache=nonce_1_1657893450835_623:11585:3
(anonymous) @ ace.js?nocache=nonce_1_1657893450835_623:11570
(anonymous) @ ace.js?nocache=nonce_1_1657893450835_623:11585

Capture

I think it is because I pass global value to the editor.session.setMode(), and somehow it would mess with ace.js window global object. Is there anyway that I can set editor to a different AceGrammar mode without produce this error?

foo123 commented 2 years ago

Debugging other's code is outside the scope of github issues. I dont see any problem with AceGrammar. The error is that window is not defined, so it matters what is the scope and context of the function/closure that runs this code. Maybe you can setup a jsfiddle with only the necessary to reproduce the error.

KiddoV commented 2 years ago

I can't create JSFiddle. There is no ace_grammar.js on any CDN platforms. Can you provide a CDN link?

My JSFiddle so far: https://jsfiddle.net/KiddoV/vkfg9cos/20/

foo123 commented 2 years ago

You can include it manually or you can use the link from github.io https://foo123.github.io/examples/common/js/ace_grammar.min.js

KiddoV commented 2 years ago

I cannot re-produce the error. Now I got a different error when separate ace.js and ace_grammar.js :( At first, I put the entire library in the same file with ace.js. Now I got this error, when doing the same thing...

ace_grammar.min.js?nocache=nonce_1_1657909554027_629:1078 

       Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://wails.localhost/http://wails.localhost/assets/worker-json.js?nocache=nonce_1_1657909554027_629' failed to load.
    at ue (http://wails.localhost/assets/ace_grammar.min.js?nocache=nonce_1_1657909554027_629:1078:46)
ue @ ace_grammar.min.js?nocache=nonce_1_1657909554027_629:1078

This is so frustrating. Maybe I use too many libs? Do I really need worker-json.js for ace editor?

foo123 commented 2 years ago

Ace uses workers in order to handle syntax errors and Ace Grammar uses workers too, taken from Ace. If jsfiddle has problem with that, I dont know. Maybe create a simple example in your own server where this problem is not present.