question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to programmatically add elements to a region.

See original GitHub issue

I noticed that if I append an element to a region programmatically using jquery’s prepend() that element is not focusable or editable. I assume this is because ContentTools is unaware of the new DOM elements. How should I be alerting ContentTools about the additions to the DOM? Is there a better way to append/prepend?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
anthonyjbcommented, May 10, 2016

@bcreeves sorry completely missed this, not sure how. So yes what you’d ideally do is add a ContentEdit element to the ContentEdit region rather than a DOM element. For example:

var p = new ContentEdit.Text('p', {}, 'My new paragraph');
var region = ContentTools.EditorApp.get().regions()['name_of_my_region'];
region.attach(p);
0reactions
anthonyjbcommented, May 10, 2016

You’re mixing DOM elements and ContentEdit elements here, so region is a ContentEdit element (or collection) that has child ContentEdit elements (like Text, Image, Video, etc). It doesn’t know how to remove the DOM element you’re selecting, you’d need to pass the detatch method the ContentEdit element.

If you want to find an element in the region using the associated DOM element you could do something like:

findElementUsingSelector(region, selector) {
    // Find a ContentEdit element within the region using a DOM selector
    var targetDOM = region.domElement().querySelector(selector);
    for (var i = 0; i < region.children.length; i++) {
        var child = region.children[i];
        if (child === targetDOM) {
            return child;
        }
}

region.detatch(findElementUsingSelector(region, '#my-element'));

Moving back to your previous question, you can’t add DOM elements into the DOM element representing the region, you can however create a DOM element, parse it using the correct class in ContentEdit, and add it to the region:


// Create an DOM element using innerHTML
var wrapper = document.createElement('div');
wrapper.innerHTML = '<p>My new paragraph</p>';

// Convert the DOM element to a ContentEdit Text element
var p = ContentEdit.Text.fromDOMElement(wrapper.querySelector('p'));

// Add the paragraph to the region
region.attach(p);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add programmatically region's class when region had ...
I have one row three column region. How can I add custom class on each region when one of region is not work?...
Read more >
Dynamically creating regions with Prism
I have done this in my application. Here is the code I came up with: string regionName = "MyRegionName"; ContentPresenter RegionContentControl = new ......
Read more >
Create region programmatically
P/s: When i try to insert region in Java : RichRegion region = new RichRegion(); region.setVisible(true); region.setRendered(true); ValueExpression value ...
Read more >
Creating Regions Dynamically
You can dynamically create regions in your application code and automatically instantiate them on members of a cluster.
Read more >
Creating Regions Dynamically | Geode Docs
DynamicRegionFactory class to dynamically create regions, or you can create them using the <dynamic-region-factory> element in the cache.xml file that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found