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

Animated change transitions #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ Alternatively, instead of loading livereload.js from the LiveReload server, you
<script src="https://github.com/livereload/livereload-js/raw/master/dist/livereload.js?host=localhost"></script>
```

### Animated transitions

LiveReload can animate changes. To enable animated transitions pass `animate=true` to `livereload.js` when including in the script tag. The default transition duration is 280ms and is configurable via `animation_duration`. The value is integer in milliseconds.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to allow e.g. animate=200, animate=400 in addition to animate=true to optionally specify the duration?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. I'll update.



Issues & Limitations
--------------------
Expand Down
39 changes: 37 additions & 2 deletions dist/livereload.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,31 @@
LiveReload.prototype.performReload = function(message) {
var _ref, _ref1;
this.log("LiveReload received reload request: " + (JSON.stringify(message, null, 2)));
return this.reloader.reload(message.path, {
this.reloader.reload(message.path, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't send dist/livereload.js changes in pull requests; they tend to create an ungodly amount of merge conflicts.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood.

liveCSS: (_ref = message.liveCSS) != null ? _ref : true,
liveImg: (_ref1 = message.liveImg) != null ? _ref1 : true,
originalPath: message.originalPath || '',
overrideURL: message.overrideURL || '',
serverURL: "http://" + this.options.host + ":" + this.options.port
serverURL: "//" + this.options.host + ":" + this.options.port
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the most curious line. Why did you remove http: here? At least in LiveReload 2 app, SSL isn't handled by the built-in web server used for the Override URL feature (which is, of course, lame, but hey).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually an outdated build. I since undid that change in CoffeeScript file because I didn't want to introduce unrelated changes in this PR.

});
return this.performTransition();
};

LiveReload.prototype.performAlert = function(message) {
return alert(message.message);
};

LiveReload.prototype.performTransition = function() {
var existingHtmlClass, html, reloadedClass, _ref;
html = document.body.parentNode;
reloadedClass = ' livereload-reloaded ';
existingHtmlClass = (_ref = html.getAttribute('class')) != null ? _ref : '';
html.setAttribute('class', "" + (existingHtmlClass.replace(reloadedClass, '')) + " " + reloadedClass);
return setTimeout((function() {
return html.setAttribute('class', existingHtmlClass.replace(reloadedClass, ''));
}), parseInt(this.options.animation_duration, 10) + 20);
};

LiveReload.prototype.shutDown = function() {
var _base;
this.connector.disconnect();
Expand Down Expand Up @@ -450,6 +462,24 @@
});
};

LiveReload.prototype.setUpCSSTransitions = function() {
var cssText, head, prefixer, styleNode;
prefixer = function(declaration) {
return (['-webkit-', '-moz-', ''].map(function(item) {
return "" + item + declaration;
})).join(' ');
};
head = document.getElementsByTagName('head')[0];
styleNode = document.createElement("style");
cssText = ".livereload-reloaded * { " + (prefixer('transition: all ' + this.options.animation_duration + 'ms ease-out;')) + " }";
if (styleNode.styleSheet) {
styleNode.styleSheet.cssText = cssText;
} else {
styleNode.appendChild(document.createTextNode(cssText));
}
return head.appendChild(styleNode);
};

return LiveReload;

})();
Expand All @@ -470,6 +500,8 @@
this.mindelay = 1000;
this.maxdelay = 60000;
this.handshake_timeout = 5000;
this.animate = false;
this.animation_duration = 280;
}

Options.prototype.set = function(name, value) {
Expand Down Expand Up @@ -1095,6 +1127,9 @@
});

LiveReload.on('connect', function() {
if (!!/true|1$/.test(LiveReload.options.animate)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's cleaner to put all parsing in Options.set, and have sanitized data inside the fields. (The same can be said about my own code, though. Maybe it's a separate change.)

LiveReload.setUpCSSTransitions();
}
return CustomEvents.fire(document, 'LiveReloadConnect');
});

Expand Down
25 changes: 25 additions & 0 deletions src/livereload.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,19 @@ exports.LiveReload = class LiveReload
originalPath: message.originalPath || ''
overrideURL: message.overrideURL || ''
serverURL: "http://#{@options.host}:#{@options.port}"
@performTransition() if !!(/true|1$/).test(LiveReload.options.animate)

performAlert: (message) ->
alert message.message

performTransition: ->
html = document.body.parentNode
reloadedClass = ' livereload-reloaded '
existingHtmlClass = html.getAttribute('class') ? ''
html.setAttribute('class', "#{existingHtmlClass.replace(reloadedClass, '')} #{reloadedClass}")
setTimeout (-> html.setAttribute('class', existingHtmlClass.replace(reloadedClass, ''))),
parseInt(@options.animation_duration, 10) + 20

shutDown: ->
@connector.disconnect()
@log "LiveReload disconnected."
Expand Down Expand Up @@ -143,3 +152,19 @@ exports.LiveReload = class LiveReload

@connector.sendCommand { command: 'info', plugins: pluginsData, url: @window.location.href }
return

setUpCSSTransitions: ->
prefixer = (declaration) ->
(['-webkit-', '-moz-', ''].map (item) -> ("#{item}#{declaration}")).join(' ')

head = document.getElementsByTagName('head')[0]
styleNode = document.createElement("style")
cssText = ".livereload-reloaded * { #{prefixer('transition: all ' +
@options.animation_duration + 'ms ease-out;')} }"

if styleNode.styleSheet
styleNode.styleSheet.cssText = cssText
else
styleNode.appendChild(document.createTextNode(cssText))

head.appendChild(styleNode)
3 changes: 3 additions & 0 deletions src/options.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ exports.Options = class Options
@maxdelay = 60000
@handshake_timeout = 5000

@animate = false
@animation_duration = 280 # ms

set: (name, value) ->
if typeof value is 'undefined'
return
Expand Down
1 change: 1 addition & 0 deletions src/startup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LiveReload.addPlugin require('./less')

LiveReload.on 'shutdown', -> delete window.LiveReload
LiveReload.on 'connect', ->
LiveReload.setUpCSSTransitions() if !!(/true|1$/).test(LiveReload.options.animate)
CustomEvents.fire document, 'LiveReloadConnect'
LiveReload.on 'disconnect', ->
CustomEvents.fire document, 'LiveReloadDisconnect'
Expand Down
3 changes: 3 additions & 0 deletions test/html/animation/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: green;
}
11 changes: 11 additions & 0 deletions test/html/animation/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>LiveReload Test</title>
<link rel="stylesheet" href="test.css">
</head>
<body>
<h1>Test animated transitions</h1>
<script src="../../../dist/livereload.js?host=localhost&amp;animation=true" async defer></script>
</body>
</html>