-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a @byContent selector for reparenting child content (#5)
* Initial support for @byContent decorator * Convert indentation to spaces * Add beginnings of a test * Full test for @byContent decorator * Add documentation for the @byContent decorator * Change label in test * Respond to code review for byContent decorator
- Loading branch information
Showing
5 changed files
with
414 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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.