Code Monkey home page Code Monkey logo

Comments (4)

SimeonC avatar SimeonC commented on May 27, 2024

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.

from textangular.

nizsheanez avatar nizsheanez commented on May 27, 2024

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

from textangular.

nizsheanez avatar nizsheanez commented on May 27, 2024

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

loose raw text, it's not dom

from textangular.

SimeonC avatar SimeonC commented on May 27, 2024

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.

from textangular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.