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

How to Insert dom element into editor? #51

Closed
nizsheanez opened this issue Dec 11, 2013 · 4 comments
Closed

How to Insert dom element into editor? #51

nizsheanez opened this issue Dec 11, 2013 · 4 comments
Assignees
Milestone

Comments

@nizsheanez
Copy link

I wan't to create button in toolbar, which will insert some angular directive in editor.
So, if i insert some directive, i must to $compile it:

checkbox: {
   display: "<button type='button' ng-click='action()' ><i class='fa fa-check-square-o'></i></button>",
   action: function () {
       var element = $compile("<eg-todo>")(this.$parent)
       //we have some dom element
       //how to insert it into editor?

    }
},
@SimeonC
Copy link
Collaborator

SimeonC commented Dec 11, 2013

Sounds like you have an interesting project! That isn't really something textAngular was designed to support natively as all our dom manipulation is done through document.execCommand which doesn't support what you want to do.
So you'd have to do some custom code but this should get you going:

action: function(){
  var element = $compile("<eg-todo>")(this.$parent);
  //figure out how to insert it here using something like Rangy to get the current cursor position/selection, then inserting your compiled html 
  this.$parent.displayElements.text[0].focus(); // refocus on the WYSIWYG editor after loosing it to the button
  this.$parent.updateSelectedStyles(); // you can omit this if you aren't implementing activeState or the active class
  this.$parent.updateTaBindtext(); // updates the model
}

I'd say you'd also need to change line 442 of textAngular from element.html(val); to element.html($compile(val)(scope.$parent)); and then inject $compile into the taBind directive.

No guarantees it will work, if it does we could consider making the ability to insert directives dynamically a feature in the future, which sounds cooler the more I think about it. But I have no idea what the implications are of allowing this on security and performance and I have a feeling these could be a tricky.

@SimeonC SimeonC closed this as completed Dec 11, 2013
@nizsheanez
Copy link
Author

If we living in directive world i think we must to support it.
I solved this problem using next way:

Checkbox button:

 checkbox: {
            display: "<button type='button' ng-click='action()' ><i class='fa fa-check-square-o'></i></button>",
            action: function () {

                function insertNodeAtCursor(node) {
                    var sel, range, html;
                    if (window.getSelection) {
                        sel = window.getSelection();
                        if (sel.getRangeAt && sel.rangeCount) {
                            sel.getRangeAt(0).insertNode(node);
                        }
                    } else if (document.selection && document.selection.createRange) {
                        range = document.selection.createRange();
                        html = (node.nodeType == 3) ? node.data : node.outerHTML;
                        range.pasteHTML(html);
                    }
                }
                function placeCaretAtEnd(el) {
                    el.focus();
                    if (typeof window.getSelection != "undefined"
                        && typeof document.createRange != "undefined") {
                        var range = document.createRange();
                        range.setStartAfter( el );
                        range.setEndAfter( el );
                        var sel = window.getSelection();
                        sel.removeAllRanges();
                        sel.addRange(range);
                    } else if (typeof document.body.createTextRange != "undefined") {
                        var textRange = document.body.createTextRange();
                        textRange.moveToElementText(el);
                        textRange.collapse(false);
                        textRange.select();
                    }
                }

                var element = $compile('<input type="checkbox" eg-todo />')(this.$parent);

                this.$parent.displayElements.text[0].focus();
                insertNodeAtCursor(element[0]);
                placeCaretAtEnd(element[0])
                return this.$parent.wrapSelection("insertHTML", "&nbsp;");
            }
        },

$compile before insert
nizsheanez@f588a0b
and add change event listener
nizsheanez@7e26d35

and example of eg-todo directive:


angular.module('eg.goal').directive('egTodo', function () {
    return {
        restrict: 'A',
        scope: false,
        link: function ($scope, element, attrs) {
            element.change(function() {
                element.attr('checked', element.is(':checked') ? 'checked' : false);
            });
        }
    };
});

i try to use it on page with 14 editors, and 200 checkboxes per editor.
Don't see problems with memory or cpu

@nizsheanez
Copy link
Author

first problem:
console.log($compile('<span>1</span>sdfasdfasdf')(scope.$parent));

loose raw text, it's not dom

@SimeonC
Copy link
Collaborator

SimeonC commented Dec 11, 2013

Cool, great that it's working for you and thanks for sharing your code. I'm not able to look into this properly till Feb next year due to Family commitments. I agree that it's a good idea to allow the use of directives in the wysiwyg, I just have some concerns about essentially allowing dynamic injecting of directives into your code. It opens up a nice big hole in your app if we use your approach as is. From what I can see any angular directives put into the raw HTML view will be activated and run using the WYSIWYG view which normally you can't do afaik.

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