Skip to content

Commit

Permalink
Resolve #16
Browse files Browse the repository at this point in the history
  • Loading branch information
taggon committed Jul 1, 2015
1 parent f3addd9 commit 4d09367
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 184 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1 +1,3 @@
.DS_Store
npm-debug.log
node_modules
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,2 +1,6 @@
## 0.1.0 - First Release
* Enable to turn on/off LiveReload server

## 0.3.0
* Works with Atom 1.0
* Supports some preferences
2 changes: 1 addition & 1 deletion LICENSE.md
@@ -1,4 +1,4 @@
Copyright (c) 2014 Taegon Kim
Copyright (c) 2014-2015 Taegon Kim

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@ An [Atom](https://atom.io/) package enables you to control built-in [LiveReload]
> *from LiveReload official site*
## Usage
1. Select main menu `Packages > LiveReload > Toggle Server` to turn the server on. Or, you can click `Toggle LiveReload` in the context menu to do same thing.
1. Select main menu `Packages > LiveReload > Toggle Server` to turn the server on. Or press `Ctrl+Shift+R`.
2. When the server started successfully, `LiveReload: PORT_NUMBER` text appears in the status bar(see the bottom right panel in the following screenshot).
![LiveReload status text](https://cloud.githubusercontent.com/assets/212034/3565696/c50f01ce-0aca-11e4-991e-4cb8475364c4.png)
3. Click it to get the url of `livereload.js` JavaScript file.
Expand All @@ -17,6 +17,7 @@ An [Atom](https://atom.io/) package enables you to control built-in [LiveReload]
## Features
* Reloads web pages when any file is saved.
* Applies changes without reloading when any CSS or image changed.
* Works well with [Chrome LiveReload extension](https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei?hl=en).

## Todo
* Support preprocessors.
Expand Down
14 changes: 0 additions & 14 deletions keymaps/livereload.cson

This file was deleted.

5 changes: 5 additions & 0 deletions keymaps/livereload.json
@@ -0,0 +1,5 @@
{
"atom-workspace": {
"ctrl-shift-r": "livereload:toggle"
}
}
91 changes: 0 additions & 91 deletions lib/livereload-view.coffee

This file was deleted.

140 changes: 140 additions & 0 deletions lib/livereload-view.js
@@ -0,0 +1,140 @@
"use babel";

import {CompositeDisposable} from 'atom';
import livereload from 'livereload';
import _ from 'lodash';

const DEFAULT_EXTS = 'html css js png gif jpg php php5 py rb erb coffee'.split(' ');
const DEFAULT_EXCLUSIONS = '.git/ .svn/ .hg/'.split(' ');

class LivereloadView extends HTMLDivElement {
server = null;
subscriptions = null;
tooltipText = 'hello';

initialize(state) {
// Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
this.subscriptions = new CompositeDisposable();

// add content
this.innerHTML = '<a href="#" data-url></a>';
this.firstChild.addEventListener('click', (event) => this.handleClick(event), false);

this.classList.add('livereload-status', 'inline-block');
}

loadConfig() {
let ret = {};

// port number
ret.port = atom.config.get('livereload.port');

// use HTTPS
ret.https = atom.config.get('livereload.useHTTPS') ? {} : null;

// applyJSLive and applyCSSLive
ret.applyJSLive = atom.config.get('livereload.applyJSLive');
ret.applyCSSLive = atom.config.get('livereload.applyCSSLive');

// exts
let exts = atom.config.get('livereload.exts').split(',').map( ext => ext.trim() );
exts = _.difference(exts, DEFAULT_EXTS);
exts = _.uniq(exts);
ret.exts = exts;

let exclusions = atom.config.get('livereload.exclusions').split(',').map( ex => ex.trim() );
exclusions = exclusions.concat(['.DS_Store', '.gitignore']);
exclusions = _.difference(exclusions, DEFAULT_EXCLUSIONS);
exclusions = _.uniq(exclusions);
ret.exclusions = exclusions;

return ret;
}

attach() {
// Register command that toggles this view
this.subscriptions.add(
atom.commands.add( 'atom-workspace', { 'livereload:toggle': this.toggle.bind(this) } )
);

// tooltip
this.subscriptions.add(
atom.tooltips.add( this, {title: () => this.tooltipText} )
);
}

detach() {
this.subscriptions.dispose();
}

serialize() {

}

destroy() {
try { this.detach() } catch(e){};

this.subscriptions = null;
this.remove();
}

toggle() {
if (this.server) {
this.closeServer();
} else {
this.startServer();
}
}

handleClick(event) {
event.preventDefault();
if (this.firstChild.dataset.url) {
atom.clipboard.write(this.firstChild.dataset.url, 'url');
}
}

startServer() {
this.firstChild.dataset.url = '';
this.firstChild.textContent = 'LiveReload: ...';

// load configurations
let config = this.loadConfig();

// create a server
this.server = livereload.createServer(config);

this.server.config.server
.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
this.tooltipText = `Trying port ${config.port+1}...`;
console.log(`LiveReload: port ${config.port} already in use. Trying port ${config.port+1}...`);
config.port++;

try { this.server.close(); } catch(e) {};
this.server = null;

setTimeout( this.startServer.bind(this), 1000 );
}
})
.on('listening', () => {
console.log(`LiveReload: listening on port ${config.port}.`);

this.firstChild.textContent = `LiveReload: ${config.port}`;
this.tooltipText = 'Click to copy the URL to clipboard';
this.firstChild.dataset.url = (config.useHTTPS ? 'https':'http') + `://localhost:${config.port}/livereload.js`;

let paths = atom.project.getPaths();
this.server.watch(paths);
});
}

closeServer() {
this.firstChild.textContent = 'LiveReload: Off';
this.server.config.server.close();
this.server = null;
}
}

var LivereloadViewTag = document.registerElement('livereload-status-bar', {prototype:LivereloadView.prototype});

export default LivereloadViewTag;
16 changes: 0 additions & 16 deletions lib/livereload.coffee

This file was deleted.

68 changes: 68 additions & 0 deletions lib/livereload.js
@@ -0,0 +1,68 @@
"use babel";

import LivereloadView from './livereload-view';
import {CompositeDisposable} from 'atom';

export default {
livereloadView: null,

config: {
port: {
title: 'Port Number',
type: 'integer',
default: 35729
},
exts : {
title: 'Additional Extensions',
description: 'The server will watch these comma-separated extensions as well as the defaults.',
type: 'string',
default: 'html, css, js, png, gif, jpg, php, php5, py, rb, erb, coffee'
},
exclusions: {
title: 'Additional Exclusions',
description: 'The server will ignore these path in addition to the defaults.',
type: 'string',
default: '.DS_Store, .gitignore, .git/, .svn/, .hg/'
},
applyJSLive: {
title: 'Apply JavaScript Live',
type: 'boolean',
description: 'If checked, LiveReload will reload JS files in the background instead of reloading the page.',
default: false
},
applyCSSLive: {
title: 'Apply CSS Live',
type: 'boolean',
description: 'If checked, LiveReload will reload CSS files in the background instead of refreshing the page.',
default: true
},
useHTTPS: {
title: 'Use HTTPS Protocol',
type: 'boolean',
default: false
}
},

activate(state) {
this.livereloadView = new LivereloadView();
this.livereloadView.initialize(state);
this.livereloadView.attach();
},

deactivate() {
if (this.statusBarTile) {
this.statusBarTile.destory();
}
this.statusBarTile = null;
this.livereloadView.detach();
this.livereloadView.destroy();
},

serialize() {
return { livereloadViewState: this.livereloadView.serialize() };
},

consumeStatusBar(statusBar) {
this.statusBarTile = statusBar.addRightTile({item:this.livereloadView, priority:100});
}
};
16 changes: 0 additions & 16 deletions menus/livereload.cson

This file was deleted.

0 comments on commit 4d09367

Please sign in to comment.