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

returning htmlParentNode (or Parent) twice returns the parentnode's parent. #25

Open
mcbmcb0 opened this issue Dec 31, 2017 · 1 comment

Comments

@mcbmcb0
Copy link

mcbmcb0 commented Dec 31, 2017

when I repeatedly click my button I first get the actual htmlParentNode but on second and subsequent click I get a div that is the htmlParentNode's parent:

	extensions: {
		'save': new MediumButton({
			label: 'save',
			action: function (html, mark, htmlParentNode) {
				var c_id = jQuery(htmlParentNode).closest('div.editable').attr('id');
				var contents = htmlParentNode.innerHTML; // innerText
				
				alert(c_id + '\n'+ contents);
				return html
			}
		}), 

am i doing something wrong or is this a bug?
could it be as the parent is now of the menu button and not the editor div? ie this:
var sel = window.getSelection(); var parent = sel.anchorNode.parentElement;

I'm actually trying to get the id of the div that the editor instance is attached to as I have a page with many editors/ divs of editable class - is there an easy way to do that? (my goal is ajax with many editable divs).

thanks

----- edit - i'm pretty sure the parent of parent is being returned. here's a kludgy workaround

`		extensions: {
			'save': new MediumButton({
				label: 'save',
				action: function (html, mark, htmlParentNode) {
					var maybe = jQuery(htmlParentNode).find('div.editable'); //.length !== 0)
					if (maybe.length !== 0){
						htmlParentNode = maybe;
					}
					var c_id = jQuery(htmlParentNode).attr('id');
					var contents = htmlParentNode.innerHTML; // innerText
					
					alert(c_id + '\n'+ html);
					return html
				}
			}),`
@LoulergueC
Copy link

I had the same issue, and to easily fix it, I did this :

  1. Create a variable outside of the extension :
    var parentelm;

  2. Set the variable value with the parent if not declared :

action: function (html, mark, parent){
                                if (parentelm == undefined) {
                                    parentelm = parent;
                                };
  1. Now you can use the same parent multiple times by using parentelm without selecting parent's parent.

Note : you need to reset the `parentelm` value to `undefined` each time you switch to an other editable element. Otherwise, the parent will not change and the button will act to the first editable element's parent

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