Insert Ember component at cursor in contentEditable element

前端 未结 1 1118
我在风中等你
我在风中等你 2021-01-24 03:06

I have a contentEditable div where I want to allow users to type text, as well as insert input elements such as text boxes and dropdowns. The elements will be i

相关标签:
1条回答
  • 2021-01-24 03:37

    I think you could use a ember-cli plugin called ember-wormhole. What this component do is basically move the dom of you ember component to an html element that contains an id attribute.

    e.g.

    document.selection.createRange().pasteHTML('<div id="my-component-id"></div>');
    

    use my-component-id to ember-wormhole destinantion attribute:

    {{#ember-wormhole to="my-component-id"}}
        {{my-component...}}
    {{/ember-wormhole}}
    

    Regarding that, you could do something like:

    click() {
        let componentId = 'my-component-id';
    
        document.selection.createRange().pasteHTML(`<div id="${componentId}"></div>`);
        this.get('components').pushObject(componentId); // components being an array
    }
    

    in your handlebars template:

    {{#each components as |componentId|}}
        {{#ember-wormhole to=componentId}}
            {{my-component...}}
        {{/ember-wormhole}}
    {{/each}}
    
    0 讨论(0)
提交回复
热议问题