-
Notifications
You must be signed in to change notification settings - Fork 19
Add a @byContent selector for reparenting child content #5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
207c337
Initial support for @byContent decorator
cuberoot 96bf42d
Convert indentation to spaces
cuberoot 2c690f5
Add beginnings of a test
cuberoot d883017
Full test for @byContent decorator
cuberoot 858d6c5
Add documentation for the @byContent decorator
cuberoot d2bfee8
Change label in test
cuberoot 77d7044
Respond to code review for byContent decorator
cuberoot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
Copyright 2018 Adobe. All rights reserved. | ||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under | ||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint no-constant-condition: "off" */ | ||
|
||
const domNodeMutationOptions = { childList: true }; | ||
|
||
function handleDOMNodeMutation(mutations, observer) { | ||
const domNode = observer._domNode; | ||
if (!domNode.stolen) { | ||
return; | ||
} | ||
const node = domNode.node; | ||
const parentNode = node.parentNode; | ||
if (!parentNode) { | ||
domNode.applicationDidRemoveItem(); | ||
} | ||
} | ||
/** | ||
* This is a container around a HTMLElement which allows for the element to be removed from the DOM | ||
* replaced with a comment and then returned back to the DOM | ||
*/ | ||
export default class DOMNode { | ||
|
||
/** | ||
* Removes the node from the DOM and replaces with a comment | ||
* @returns {HTMLElement} - the HTML DOM node | ||
*/ | ||
stealNode() { | ||
if (this.stolen) { | ||
return null; | ||
} | ||
|
||
const node = this.node; | ||
this.returned = false; | ||
|
||
var placeholder = this.placeholder; | ||
if (!placeholder) { | ||
placeholder = this.placeholder = document.createComment('placeholder for ' + node.nodeName); | ||
placeholder._reactComponentDataNode = this; | ||
} | ||
|
||
node.parentNode.replaceChild(placeholder, node); | ||
this.stolen = true; | ||
return this.node; | ||
} | ||
|
||
/** | ||
* Returns the node to the DOM. It replaceses the comment with the node | ||
*/ | ||
returnNode() { | ||
if (!this.stolen) { | ||
return; | ||
} | ||
|
||
this.stolen = false; | ||
this.returned = true; | ||
this.stopObserving(); | ||
|
||
const placeholder = this.placeholder; | ||
const placeholderParent = placeholder.parentNode; | ||
if (placeholderParent) { | ||
placeholderParent.replaceChild(this.node, placeholder); | ||
} | ||
} | ||
|
||
/** | ||
* Observes the mutations of the parent node to check if the users are removing the node. | ||
*/ | ||
observe() { | ||
if (this.observer) { | ||
this.stopObserving(); | ||
} | ||
const observer = this.observer = new MutationObserver(handleDOMNodeMutation); | ||
observer._domNode = this; | ||
observer.observe(this.node.parentNode, domNodeMutationOptions); | ||
} | ||
|
||
/** | ||
* Stops the mutation observer | ||
*/ | ||
stopObserving() { | ||
const observer = this.observer; | ||
if (observer) { | ||
observer.disconnect(); | ||
this.observer = null; | ||
} | ||
} | ||
|
||
/** | ||
* Marks that the application removed the item | ||
*/ | ||
applicationDidRemoveItem() { | ||
this.stolen = false; | ||
this.returned = true; | ||
this.stopObserving(); | ||
this.remove(); | ||
} | ||
|
||
/** | ||
* Adds the node to the list of child elements. | ||
* It will look for the correct position in the list of the element. | ||
* @param {Array<Object>} - the list of the child element | ||
*/ | ||
add(list) { | ||
|
||
this.list = list; | ||
var target = this.span || this.node; | ||
do { | ||
target = target.previousSibling; | ||
if (!target) { | ||
// this is the first item, just insert it at the very top. | ||
list.splice(0, 0, this); | ||
return; | ||
} | ||
const data = target._reactComponentDataNode; | ||
if (data) { | ||
// If the element is stolen, then we need to use the placeholder and not | ||
// the actual element. Otherwise the element might actually be added to the same | ||
// list of elements and might use the wrong position. | ||
if (data.stolen && data.placeholder !== target) { | ||
// Continue until we find the right element. | ||
continue; | ||
} | ||
const index = list.indexOf(data); | ||
if (index !== -1) { | ||
list.splice(index + 1, 0, this); | ||
return; | ||
} | ||
} | ||
} while (true); | ||
} | ||
|
||
/** | ||
* Removes the node | ||
*/ | ||
remove() { | ||
const list = this.list; | ||
if (list) { | ||
list.removeItem(this); | ||
this.list = null; | ||
} | ||
const node = this.node; | ||
if (node._reactComponentDataNode === this) { | ||
node._reactComponentDataNode = null; | ||
} | ||
this.removePlaceholder(); | ||
} | ||
|
||
/** | ||
* Removes the placeholder of the node | ||
*/ | ||
removePlaceholder() { | ||
const placeholder = this.placeholder; | ||
if (placeholder) { | ||
const placeholderParent = placeholder.parentNode; | ||
if (placeholderParent) { | ||
placeholderParent.removeChild(placeholder); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2018 Adobe. All rights reserved. | ||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under | ||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
import React, {Component} from 'react'; | ||
|
||
export default class EmbedNode extends Component { | ||
|
||
render() { | ||
return <div ref={ (element) => this.element = element }/> | ||
} | ||
|
||
componentDidMount() { | ||
this.parent = this.element.parentElement; | ||
this.stolenNode = this.props.item.stealNode(); | ||
this.parent.replaceChild(this.stolenNode, this.element); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.parent.replaceChild(this.element, this.stolenNode); | ||
this.props.item.returnNode(); | ||
delete this.stolenNode; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
// Prevent this node from rendering after it is mounted | ||
return false; | ||
} | ||
|
||
get hasStolenNode() { | ||
return this.stolenNode != null | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I double checked this against the supported browsers.. The page was missing the info for Safari, Edge and IE. So, I checked all of those and
Node.COMMENT_NODE
was available.All that to say, that it's ok to use the constant.