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

Loading ace editor into a widget #6

Open
psychemedia opened this issue Feb 25, 2020 · 4 comments
Open

Loading ace editor into a widget #6

psychemedia opened this issue Feb 25, 2020 · 4 comments

Comments

@psychemedia
Copy link

I'm seem to be going round in circles on this one.

What's the trick for loading something like the ace editor?

If I try the following I get a new error message: TypeError: define is not a function error, which suggests I should be loading things in using require?

html="""<h1>sshgdhsgd</h1>
<div id="editor"></div>
"""

test0 = jp_proxy_widget.JSProxyWidget()

e = test0.element

# Call some standard jQuery method on the widget element:
e.empty()
e.width("1000px")
e.height("1000px")


test0.load_js_files(["ace-src-min/ace.js"])
#test0.require_js("ace/ace", "ace-src-min/ace.js")

e.html(html)

sc="""
     var editor = ace.edit("editor");
"""
test0.js_init(sc)

display(test0)

If instead replace the script load with a require load, eg test0.require_js("ace/ace", "ace-src-min/ace.js"), then I get a new error message: ReferenceError: ace is not defined error?

It's probably something obvious but I don't see it?

@AaronWatters
Copy link
Owner

I got it working with some effort. I think you need to use require
to load ace

ace_test = jp_proxy_widget.JSProxyWidget()

def load_test():
    ace_test.js_init("""
        element.requirejs.config({
          paths: {
              d3: '//cdnjs.cloudflare.com/ajax/libs/d3/3.4.8/d3.min',
              ace: '//cdnjs.cloudflare.com/ajax/libs/ace/1.4.7/ace'
          }
        });
        element.requirejs(["ace"], function() { // no argument to load function!
            element.html("ace is loaded: " + ace)
            var e = $('<div id="editor"></div>').appendTo(element);
            e.width("500px");
            e.height("300px");
            var editor = ace.edit(e[0]);
        });
    """)
# You need to wrap the function in "uses_require" to make sure
# require is loaded properly
ace_test.uses_require(load_test)
ace_test

image

@psychemedia
Copy link
Author

psychemedia commented Feb 26, 2020 via email

@AaronWatters
Copy link
Owner

Actually, I got sporadic failures with the method given above.

This seems to work consistently in my tests:

ace_test = jp_proxy_widget.JSProxyWidget()

def load_test():
    ace_test.js_init("""
        // load ace from a CDN using require.js
        element.requirejs.config({
          paths: {
              //d3: '//cdnjs.cloudflare.com/ajax/libs/d3/3.4.8/d3.min',
              ace: '//pagecdn.io/lib/ace/1.4.8'
          }
        });
        // Use require.js to attach ace to the window object.
        element.requirejs(["ace/ace"], function(ace) {  // no argument to load function!
            element.html("ace is loaded: " + ace)
            var e = $('<div id="editor"></div>').appendTo(element);
            e.width("500px");
            e.height("300px");
            var editor = ace.edit(e[0]);
        });
    """)

# wrap the load operation in 'uses_require' to initialize require.js.
ace_test.uses_require(load_test)
ace_test

@psychemedia
Copy link
Author

Thanks... one thing I notice if I run this is that I get the Uninitialized Proxy Widget warning as if things were loaded twice?

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

No branches or pull requests

2 participants