Skip to content

Commit 7dae135

Browse files
committed
Lots of random changes...
- Many improvements to devtools, including dark-mode logo, tabs, and embedded gitter chat - Added componentWillMount callback - Make createTree([]) be treated as fragment with children
1 parent 2b9c8ea commit 7dae135

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+947
-558
lines changed

CONTRIBUTING.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ sed -i -e 's/1.0.0-beta.8/1.0.0-beta.9/g' **/lib/*.js
2222
# Update the root package version.
2323
sed -i -e 's/1.0.0-beta.8/1.0.0-beta.9/g' ./package.json
2424

25+
# Update the DevTools version. It cannot use letters or dashes.
26+
sed -i -e 's/1.0.0.8/1.0.0.9/g' ./packages/diffhtml-devtools/chrome-extension/manifest.json
27+
2528
# Make a descriptive commit.
2629
git commit -am"Update JavaScript and REAMDE files to 1.0.0-beta.9"
2730
```

docs/api.html

+67-46
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,45 @@ <h1>
300300

301301
<hr></hr>
302302

303-
<section id="content"><h1 id="core-api-a-classgithub-hrefhttpsgithubcomtbranyendiffhtmltreemasterpackagesdiffhtmli-classfa-fa-githubia">Core API <a class="github" href="https://github.com/tbranyen/diffhtml/tree/master/packages/diffhtml"><i class="fa fa-github"></i></a></h1>
304-
<p>This reference contains all stable API documentation which is up-to-date with
305-
the latest release. The core was designed to be minimal and familiar if you&#39;ve
306-
used browser DOM APIs such as <code>innerHTML</code> and <code>addEventListener</code>.</p>
303+
<section id="content"><h1 id="a-hrefapihtmlcore-apia-a-classgithub-hrefhttpsgithubcomtbranyendiffhtmltreemasterpackagesdiffhtmli-classfa-fa-githubia"><a href="/api.html">Core API</a> <a class="github" href="https://github.com/tbranyen/diffhtml/tree/master/packages/diffhtml"><i class="fa fa-github"></i></a></h1>
304+
<p>This documentation covers the core public API. This page stays up-to-date with
305+
the latest version. All of these methods work in the browser, with JSDOM, and
306+
directly in Node.</p>
307+
<pre><code class="language-sh">npm install diffhtml</code></pre>
308+
<p>Some diffHTML lingo to be aware of:</p>
309+
<ul>
310+
<li><p><strong>Transaction</strong>: A class structure which represents a set of changes to the
311+
DOM.</p>
312+
</li>
313+
<li><p><strong>VTree</strong>: Virtual Tree, represents the shape of a DOM node, Component,
314+
etc. These objects are created when diffHTML first loads and help regulate
315+
memory usage.</p>
316+
</li>
317+
<li><p><strong>Mount</strong>: Most commonly a DOM node that you want to update, but can also be
318+
VTrees and other types.</p>
319+
</li>
320+
</ul>
307321
<p><a name="inner-html"></a></p>
308322
<hr></hr>
309-
<h2 id="a-hrefinner-htmlinnerhtmla-domnode-markup-options"><a href="#inner-html">innerHTML</a> <strong><code>(domNode, markup, options)</code></strong></h2>
310-
<p>Replaces the contents of a DOM node with the passed in markup, but will only
311-
update changed content and structure. Works like the browser&#39;s <code>innerHTML</code> only
312-
changing the element&#39;s children, but not the containing element. If you want to
313-
control the entire tag, use
314-
<a href="#outer-html" title="null"><code>outerHTML</code></a>
315-
.</p>
323+
<h2 id="a-hrefinner-htmlinnerhtmla-mount-input-options"><a href="#inner-html">innerHTML</a> <strong><code>(mount, input, options)</code></strong></h2>
324+
<p>The <code>innerHTML</code> and <code>outerHTML</code> methods are the most common to use. They allow
325+
you to mimic the respective browser feature, where you replace the contents of
326+
a DOM node and in the case of <code>outerHTML</code> replace the top-level element
327+
attributes as well. You may call this once in a complex application where the
328+
individual components re-render themselves, or in a game you would call this on
329+
every render tick, if you&#39;re building something simple, you can call it
330+
whenever the state changes.</p>
331+
<p>These methods can work in the browser and browser-like environments like JSDOM.
332+
An interesting feature is that you can pass in more than just DOM nodes. This
333+
comes in handy for advanced use cases such as</p>
334+
<p>What&#39;s nice about these methods is that all renders go through the same
335+
scheduling pipeline and VTrees are shared across all other renders.</p>
336+
<p>By calling these methods you are signaling intent that you want the mount to
337+
reflect the incoming input.</p>
338+
<ul>
339+
<li>Creating and scheduling a render transaction</li>
340+
<li></li>
341+
</ul>
316342
<h3 id="arguments">Arguments</h3>
317343
<table>
318344
<thead>
@@ -322,27 +348,27 @@ <h3 id="arguments">Arguments</h3>
322348
</tr>
323349
</thead>
324350
<tbody><tr>
325-
<td><strong>domNode</strong></td>
326-
<td>The root DOM Node to change the child contents of, but not the element itself.</td>
351+
<td><strong>mount</strong></td>
352+
<td>The root DOM node to update children in, but not the node itself.</td>
327353
</tr>
328354
<tr>
329355
<td><strong>input</strong></td>
330-
<td>New markup to replace into <strong>domNode</strong>.</td>
356+
<td>New markup to replace into <strong>mount</strong>.</td>
331357
</tr>
332358
<tr>
333359
<td><strong>options</strong></td>
334-
<td><ul><li><strong>tasks:</strong> An array of tasks to run. Can swap these out completely to run custom logic instead.</li><li><strong>parser:</strong> Settings which influence the HTML parser. No parser settings available in the runtime build.</li></ul></td>
360+
<td><ul><li><strong>tasks:</strong> An array of tasks to run. Can swap these out to modify the render flow.</li><li><strong>parser:</strong> Settings which influence the HTML parser.</li></ul></td>
335361
</tr>
336362
</tbody></table>
337-
<h3 id="example">Example</h3>
363+
<h3 id="examples">Examples</h3>
338364
<pre><code class="language-js">import { innerHTML } from &#39;diffhtml&#39;;
339365

340366
innerHTML(document.body, `
341367
&lt;h1&gt;Hello world!&lt;/h1&gt;
342368
`);</code></pre>
343369
<p><a name="outer-html"></a></p>
344370
<hr></hr>
345-
<h2 id="a-hrefouter-htmlouterhtmla-domnode-markup-options"><a href="#outer-html">outerHTML</a> <strong><code>(domNode, markup, options)</code></strong></h2>
371+
<h2 id="a-hrefouter-htmlouterhtmla-mount-input-options"><a href="#outer-html">outerHTML</a> <strong><code>(mount, input, options)</code></strong></h2>
346372
<p>Replaces the contents of a DOM node with the passed in markup, only updates
347373
what has changed. Additionally updates the attributes of the parent. If the
348374
element you&#39;re modifying has a parent, you may also change the element type,
@@ -358,16 +384,16 @@ <h3 id="arguments-1">Arguments</h3>
358384
</tr>
359385
</thead>
360386
<tbody><tr>
361-
<td><strong>domNode</strong></td>
362-
<td>A DOM Node to change.</td>
387+
<td><strong>mount</strong></td>
388+
<td>The root DOM node to update including attributes and children.</td>
363389
</tr>
364390
<tr>
365-
<td><strong>markup</strong></td>
366-
<td>New markup to replace the entire <code>domNode</code> with.</td>
391+
<td><strong>input</strong></td>
392+
<td>New markup to replace into <strong>mount</strong>.</td>
367393
</tr>
368394
<tr>
369395
<td><strong>options</strong></td>
370-
<td><ul><li><strong>tasks:</strong> An array of tasks to run. Can swap these out completely to run custom logic instead.</li><li><strong>parser:</strong> Settings which influence the HTML parser, not available with the runtime build.</li></ul></td>
396+
<td><ul><li><strong>tasks:</strong> An array of tasks to run. Can swap these out to modify the render flow.</li><li><strong>parser:</strong> Settings which influence the HTML parser.</li></ul></td>
371397
</tr>
372398
</tbody></table>
373399
<p><a name="html"></a></p>
@@ -386,7 +412,7 @@ <h2 id="a-hrefhtmlhtmla-markup"><a href="#html">html</a> <strong><code>(markup)<
386412
after it, like the example below, it will be automatically trimmed. If you
387413
provide multiple elements, the whitespace becomes</p>
388414
<p>A simple example of its usage along with interpolation.</p>
389-
<pre><code class="language-js">html`
415+
<pre><code class="language-js">const vTree = html`
390416
&lt;body&gt;
391417
&lt;center style=${{ fontSize: &#39;11px&#39; }}&gt;Hello world&lt;/center&gt;
392418
&lt;/body&gt;
@@ -406,15 +432,13 @@ <h3 id="arguments-2">Arguments</h3>
406432
<p><a name="use"></a></p>
407433
<hr></hr>
408434
<h2 id="a-hrefuseusea-middlewarefunction-or-middlewareobject"><a href="#use">use</a> <strong><code>(middlewareFunction or middlewareObject)</code></strong></h2>
409-
<p>Can be used to mount pre-existing middleware or you can write your own.
410-
Middleware are effectively hooks that execute in various areas of the
411-
reconciler during a render call such as <code>innerHTML</code> or <code>outerHTML</code>.</p>
412-
<p>A function is useful when you want to follow the transactions (which are
413-
started and run a series of tasks), and passing an object can be cleaner when
414-
you want to modify the Virtual Tree or automatically add properties.</p>
435+
<p>This function is used to hook plugins into your render pipeline. These plugins
436+
are referred to as middleware. They are meant for advanced use cases such as
437+
observing the render flow, modifying attributes or elements, and more.</p>
438+
<p>Middleware can be enabled and disabled via code or the browser DevTools.</p>
415439
<p>
416-
<a href="/middleware.html#writing-middleware" title="null"><strong>Refer to the Middleware documentation for more in-depth
417-
information.</strong></a>
440+
<a href="/middleware.html" title="null"><strong>Refer to the Middleware documentation for documentation on writing your own
441+
and find existing plugins.</strong></a>
418442
</p>
419443
<h3 id="arguments-3">Arguments</h3>
420444
<table>
@@ -430,10 +454,10 @@ <h3 id="arguments-3">Arguments</h3>
430454
</tr>
431455
<tr>
432456
<td><strong>middlewareObject</strong></td>
433-
<td>Use this when you don&#39;t care about the transaction start/stop, and want a cleaner way to monitor the VTree lifecycle. <p><b>- createTreeHook</b></p><p><b>- syncTreeHook</b></p> <p><b>- releaseHook</b></p><p><b>- subscribe</b></p><p><b>- unsubscribe</b></p></td>
457+
<td>Use this when you don&#39;t care about the transaction start/stop, and want a cleaner way to monitor the VTree lifecycle. <ul><li>displayName</li><li>createNodeHook</li><li>createTreeHook</li><li>syncTreeHook</li><li>releaseHook</li><li>subscribe</li><li>unsubscribe</li></ul></td>
434458
</tr>
435459
</tbody></table>
436-
<h3 id="examples">Examples</h3>
460+
<h3 id="examples-1">Examples</h3>
437461
<h4 id="logging-the-start-of-a-render-transaction">Logging the start of a render transaction</h4>
438462
<pre><code class="language-js">function someTask(transaction) {
439463
console.log(&#39;Start of render transaction:&#39;, transaction);
@@ -579,9 +603,9 @@ <h3 id="arguments-6">Arguments</h3>
579603
<p><a name="create-tree"></a></p>
580604
<hr></hr>
581605
<h2 id="a-hrefcreate-treecreatetreea-nodename-attributes-childnodes"><a href="#create-tree">createTree</a> <strong><code>(nodeName, attributes, ...childNodes)</code></strong></h2>
582-
<p>Creates a Virtual Tree which can be interpolated and rendered. This has a
583-
similar purpose to hyperscript&#39;s <code>h()</code> and React&#39;s <code>createElement</code>.</p>
584-
<p>Example:</p>
606+
<p>Creates a Virtual Tree (VTree) which can be interpolated and rendered. This has
607+
a similar purpose to hyperscript&#39;s <code>h()</code> and React&#39;s <code>createElement</code>.</p>
608+
<p>Examples:</p>
585609
<pre><code class="language-js">const attributes = {
586610
id: &#39;test&#39;,
587611
style: &#39;color: red&#39;,
@@ -603,7 +627,7 @@ <h3 id="arguments-7">Arguments</h3>
603627
</thead>
604628
<tbody><tr>
605629
<td><strong>nodeName</strong></td>
606-
<td>A string for HTML, or if using components or middleware other types, like functions</td>
630+
<td>A string for HTML, or couuld be a component or other types like functions when using middleware</td>
607631
</tr>
608632
<tr>
609633
<td><strong>attributes</strong></td>
@@ -616,7 +640,7 @@ <h3 id="arguments-7">Arguments</h3>
616640
</tbody></table>
617641
<p><a name="release"></a></p>
618642
<hr></hr>
619-
<h2 id="a-hrefreleasereleasea-domnode"><a href="#release">release</a> <strong><code>(domNode)</code></strong></h2>
643+
<h2 id="a-hrefreleasereleasea-mount"><a href="#release">release</a> <strong><code>(mount)</code></strong></h2>
620644
<p>Use this method if you need to clean up memory allocations and anything else
621645
internal to diffHTML associated with your element. This is very useful for unit
622646
testing and general cleanup when you&#39;re done with an element. Applications will
@@ -630,18 +654,15 @@ <h3 id="arguments-8">Arguments</h3>
630654
</tr>
631655
</thead>
632656
<tbody><tr>
633-
<td><strong>domNode</strong></td>
634-
<td>The root DOM Node to release memory around</td>
657+
<td><strong>mount</strong></td>
658+
<td>Release memory and all internal references to the DOM node.</td>
635659
</tr>
636660
</tbody></table>
637-
<p>This method should fully release everything related to the rendering, but its
638-
possible an untested code path is hit, so you should track the internal memory
639-
allocations to verify that memory isn&#39;t causing a problem.</p>
640661
<h4 id="domnode">domNode</h4>
641662
<p><em>Reference element.</em></p>
642663
<p>This argument is overloaded. Can be one of many types:</p>
643664
<ul>
644-
<li>HTML Element / DOM Node (Used interchangeably)</li>
665+
<li>HTML Element / DOM node (Used interchangeably)</li>
645666
<li>Virtual Tree Element (produced from <code>diff.html</code>)</li>
646667
</ul>
647668
<p><a name="internals"></a></p>
@@ -669,7 +690,7 @@ <h3 id="a-namesync-tree-hook-cache-hrefsync-tree-hook-cachesynctreehookcachea"><
669690
<p>A JavaScript Set object that contains functions that trigger whenever Virtual
670691
Trees are compared.</p>
671692
<h3 id="a-namenode-cache-hrefnode-cachenodecachea"><a name="node-cache" href="#node-cache">NodeCache</a></h3>
672-
<p>A Map that can be used to get access to the DOM Node associated to a VTree.
693+
<p>A Map that can be used to get access to the DOM node associated to a VTree.
673694
This is comparable to <code>findDOMNode</code> in React. Basically if you encounter an
674695
object that the documentation says is a VTree and you want to convert to a DOM
675696
Node, you could write something like:</p>
@@ -731,7 +752,7 @@ <h3 id="a-nameinternals-version-hrefinternals-versionversiona"><a name="internal
731752
<hr></hr>
732753
<h2 id="a-hrefversionversiona"><a href="#version">VERSION</a></h2>
733754
<p>Property which indicates the current running version of diffHTML.</p>
734-
<h3 id="example-1">Example</h3>
755+
<h3 id="example">Example</h3>
735756
<pre><code class="language-js">console.log(VERSION);</code></pre>
736757
<hr></hr>
737758
</section>

docs/index.css

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ layer#main h2 {
197197
margin-top: 40px;
198198
}
199199

200+
layer#main h1 a,
200201
layer#main h2 a {
201202
text-decoration: none;
202203
}
@@ -366,6 +367,13 @@ h2 a.github,
366367
h1 a.github {
367368
margin-left: 15px;
368369
font-size: 20px;
370+
position: relative;
371+
top: -2px;
372+
}
373+
374+
h2 a.github:hover,
375+
h1 a.github:hover {
376+
font-weight: bold;
369377
}
370378

371379
.open-menu {

packages/diffhtml-components/lib/component.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,26 @@ class Component {
8888
*/
8989
const domNode = (childNodes[0]);
9090

91-
// If there is no DOM Node association then error out.
92-
if (process.env.NODE_ENV !== 'production') {
93-
if (!domNode) {
94-
throw new Error('Missing DOM Node association to this component');
95-
}
96-
97-
// Throw an error if we are not connected, cannot use stateful components
98-
// if they are rendered shallow.
99-
if (!domNode.parentNode) {
100-
throw new Error('Cannot use stateful features when shallow rendered');
101-
}
91+
// Do not attempt to re-render if we do not have prior children.
92+
// TODO This is cheating until there is a better way to determine if a
93+
// component has rendered or not.
94+
if (!domNode || !domNode.parentNode) {
95+
return;
10296
}
10397

98+
// If there is no DOM Node association then error out.
99+
//if (process.env.NODE_ENV !== 'production') {
100+
// if (!domNode) {
101+
// throw new Error('Missing DOM Node association to this component');
102+
// }
103+
104+
// // Throw an error if we are not connected, cannot use stateful components
105+
// // if they are rendered shallow.
106+
// if (!domNode.parentNode) {
107+
// throw new Error('Cannot use stateful features when shallow rendered');
108+
// }
109+
//}
110+
104111
const { parentNode } = domNode;
105112

106113
// Render directly from the Component.

packages/diffhtml-components/lib/shared/render-component.js

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export default function renderComponent(vTree, context) {
6363
// Now that the instance is associated, we can look up context.
6464
context = getContext(vTree);
6565

66+
// Signal the component is about to mount.
67+
if (instance.componentWillMount) {
68+
instance.componentWillMount();
69+
}
70+
6671
// Initial render of the class component.
6772
renderTree = createTree(instance.render(props, instance.state, context));
6873

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { $$render } from '../util/symbols';
1+
import { $$vTree, $$render } from '../util/symbols';
2+
import { InstanceCache } from '../util/caches';
23

34
const Debounce = new WeakMap();
45
const { assign } = Object;
@@ -7,6 +8,7 @@ export default function setState(state) {
78
this.state = assign({}, this.state, state);
89

910
if (!Debounce.has(this) && this.shouldComponentUpdate()) {
11+
// Only render if we have an instance (has rendered before).
1012
this[$$render]();
1113
}
12-
}
14+
}

packages/diffhtml-devtools/build/tasks/browserify.coffee

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = ->
44
@config 'browserify',
55
'bridge':
66
files:
7-
'chrome-extension/dist/extension/js/bridge.js': ['shared/scripts/bridge.js']
7+
'chrome-extension/dist/extension/js/bridge.js': ['lib/scripts/bridge.js']
88

99
options:
1010
transform: ['babelify', 'brfs']
@@ -15,10 +15,10 @@ module.exports = ->
1515

1616
'chrome-extension':
1717
files:
18-
'chrome-extension/dist/extension/js/index.js': ['shared/scripts/index.js']
19-
'chrome-extension/dist/extension/js/contentscript.js': ['shared/scripts/contentscript.js']
20-
'chrome-extension/dist/extension/js/devtools.js': ['shared/scripts/devtools.js']
21-
'chrome-extension/dist/extension/js/background.js': ['shared/scripts/background.js']
18+
'chrome-extension/dist/extension/js/index.js': ['lib/scripts/index.js']
19+
'chrome-extension/dist/extension/js/contentscript.js': ['lib/scripts/contentscript.js']
20+
'chrome-extension/dist/extension/js/devtools.js': ['lib/scripts/devtools.js']
21+
'chrome-extension/dist/extension/js/background.js': ['lib/scripts/background.js']
2222

2323
options:
2424
transform: [

packages/diffhtml-devtools/build/tasks/compress.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = ->
88
mode: 'zip'
99

1010
files: [
11-
{ src: ['**/*'], expand: true, cwd: 'shared' }
11+
{ src: ['**/*'], expand: true, cwd: 'lib' }
1212
{
1313
src: [
1414
'key.pem'

packages/diffhtml-devtools/build/tasks/copy.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = ->
3535
'!_assets/**'
3636
]
3737
expand: true
38-
cwd: 'shared'
38+
cwd: 'lib'
3939
dest: chromeDest
4040
}
4141
{

packages/diffhtml-devtools/build/tasks/jshint.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ module.exports = ->
77
jshintrc: '.jshintrc'
88

99
files: [
10-
'shared/**/*.js'
10+
'lib/**/*.js'
1111
]

packages/diffhtml-devtools/build/tasks/mocha.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = ->
1313
'test/integration/tests/**/*.js'
1414
]
1515

16-
'shared':
16+
'lib':
1717
src: [
1818
'test/unit/runner.js'
1919
'test/unit/tests/**/*.js'

packages/diffhtml-devtools/build/tasks/watch.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = ->
66
files: [
77
'chrome-extension/**/*'
88
'!chrome-extension/dist/**/*'
9-
'shared/**/*'
9+
'lib/**/*'
1010
]
1111

1212
tasks: [
Loading
Loading
Loading

0 commit comments

Comments
 (0)