likadan 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2bdf5b94276da8220ca5344b009e26e11e900afc
4
- data.tar.gz: 55145af24ef76484e796629f18a980f5c332ff38
3
+ metadata.gz: 957b1cda6eb70bcb989807e528334c7d41058326
4
+ data.tar.gz: 28610f653c625a8437d057d7cff5cc8f2a90b014
5
5
  SHA512:
6
- metadata.gz: 8109fd809d7d81f6c99779a972446e27402a7632b8cedc4a0288eecff586e7bbb3ee7e187694b7478de00a91c94c1672fdfdc6b956516370ef003144eb0bde2b
7
- data.tar.gz: 038492a9885d98a506d0279a35a20a22e6db256200e598fc7a3c9813ff707c11c637d5010c6bda27f738b34d6de313ffe0004e3a6c6ff8295adaf5fd063d3dff
6
+ metadata.gz: bce71e0320443c5ce1a036a4f0033a641201decb0d7e2b44bfd6dae70df88d550c586fe0074c259f1973f759c769a81c2c019f2868143d5c2fd9fbc1ad7ea910
7
+ data.tar.gz: 7515a142377ed7ec6c23e521ab54403d4a3755d7dda20e85dbd700493d553529c8c1dee44e0093421594654128a07b1a0c3bce1f8e9813285a6a89e5589f87c8
@@ -19,6 +19,15 @@ begin
19
19
 
20
20
  # Render the example
21
21
  rendered = driver.execute_script('return window.likadan.renderCurrent()')
22
+ if error = rendered['error']
23
+ puts <<-EOS
24
+ Error while rendering "#{current['name']}" @#{width}px:
25
+ #{rendered['error']}
26
+ Debug by pointing your browser to
27
+ #{LikadanUtils.construct_url('/', name: current['name'])}
28
+ EOS
29
+ next
30
+ end
22
31
  output_file = LikadanUtils.path_to(current['name'], width, 'candidate.png')
23
32
 
24
33
  # Create the folder structure if it doesn't already exist
@@ -23,7 +23,14 @@ class LikadanUtils
23
23
  )
24
24
  end
25
25
 
26
- def self.construct_url(absolute_path)
27
- return "http://localhost:#{config['port']}#{absolute_path}"
26
+ def self.construct_url(absolute_path, params = {})
27
+ params_str = params.map do |key, value|
28
+ "#{key}=#{URI.escape(value)}"
29
+ end.join('&')
30
+ unless params_str.empty?
31
+ params_str = "?#{params_str}"
32
+ end
33
+
34
+ return "http://localhost:#{config['port']}#{absolute_path}#{params_str}"
28
35
  end
29
36
  end
@@ -2,6 +2,7 @@ window.likadan = {
2
2
  defined: [],
3
3
  currentIndex: 0,
4
4
  currentExample: undefined,
5
+ currentRenderedElement: undefined,
5
6
 
6
7
  define: function(name, func, viewportWidths, options) {
7
8
  this.defined.push({
@@ -15,6 +16,13 @@ window.likadan = {
15
16
  },
16
17
 
17
18
  next: function() {
19
+ if (this.currentRenderedElement) {
20
+ if (React) {
21
+ React.unmountComponentAtNode(document.body.lastChild);
22
+ } else {
23
+ this.currentRenderedElement.parentNode.removeChild(this.currentRenderedElement);
24
+ }
25
+ }
18
26
  this.currentExample = this.defined[this.currentIndex];
19
27
  if (!this.currentExample) {
20
28
  return;
@@ -23,17 +31,20 @@ window.likadan = {
23
31
  return this.currentExample;
24
32
  },
25
33
 
26
- getOutputElement: function() {
27
- return document.getElementById('likadan-output');
34
+ setCurrent: function(exampleName) {
35
+ this.defined.forEach(function(example, index) {
36
+ if (example.name === exampleName) {
37
+ this.currentExample = example;
38
+ }
39
+ }.bind(this));
40
+ if (!this.currentExample) {
41
+ throw 'No example found with name "' + exampleName + '"';
42
+ }
28
43
  },
29
44
 
30
45
  clearVisibleElements: function() {
31
- var out = this.getOutputElement();
32
46
  var allElements = Array.prototype.slice.call(document.querySelectorAll('body > *'));
33
47
  allElements.forEach(function(element) {
34
- if (element === out) {
35
- return;
36
- }
37
48
  var style = window.getComputedStyle(element);
38
49
  if (style.display !== 'none') {
39
50
  element.parentNode.removeChild(element);
@@ -42,32 +53,59 @@ window.likadan = {
42
53
  },
43
54
 
44
55
  renderCurrent: function() {
45
- var out = this.getOutputElement();
46
- out.innerHTML = '';
47
-
48
- // We need to clear out anything currently visible. There's no guarantee
49
- // that examples only render inside the .likadan-output container.
50
- this.clearVisibleElements();
56
+ try {
57
+ this.clearVisibleElements();
58
+ var elem = this.currentExample.func();
59
+ if (elem.getDOMNode) {
60
+ // Soft-dependency to React here. If the thing returned has a
61
+ // `getDOMNode` method, call it to get the real DOM node.
62
+ elem = elem.getDOMNode();
63
+ }
51
64
 
52
- var elem = this.currentExample.func();
53
- if (elem.getDOMNode) {
54
- // Soft-dependency to React here. If the thing returned has a
55
- // `getDOMNode` method, call it to get the real DOM node.
56
- elem = elem.getDOMNode();
57
- }
58
- out.appendChild(elem);
65
+ this.currentRenderedElement = elem;
59
66
 
60
- var width = elem.offsetWidth;
61
- var height = elem.offsetHeight;
67
+ var width = elem.offsetWidth;
68
+ var height = elem.offsetHeight;
62
69
 
63
- if (this.currentExample.options.snapshotEntireScreen) {
64
- width = window.innerWidth;
65
- height = window.innerHeight;
70
+ if (this.currentExample.options.snapshotEntireScreen) {
71
+ width = window.innerWidth;
72
+ height = window.innerHeight;
73
+ }
74
+ return {
75
+ name: this.currentExample.name,
76
+ width: width,
77
+ height: height,
78
+ };
79
+ } catch (error) {
80
+ console.error(error);
81
+ return {
82
+ name: this.currentExample.name,
83
+ error: error.message
84
+ };
66
85
  }
67
- return {
68
- name: this.currentExample.name,
69
- width: width,
70
- height: height
71
- };
72
86
  }
73
87
  };
88
+
89
+ window.addEventListener('load', function() {
90
+ var matches = window.location.search.match(/name=([^&]*)/);
91
+ if (!matches) {
92
+ return;
93
+ }
94
+ var example = decodeURIComponent(matches[1]);
95
+ window.likadan.setCurrent(example);
96
+ window.likadan.renderCurrent();
97
+ });
98
+
99
+ // We need to redefine a few global functions that halt execution. Without this,
100
+ // there's a chance that the Ruby code can't communicate with the browser.
101
+ window.alert = function(message) {
102
+ console.log('`window.alert` called', message);
103
+ };
104
+ window.confirm = function(message) {
105
+ console.log('`window.confirm` called', message);
106
+ return true;
107
+ };
108
+ window.prompt = function(message, value) {
109
+ console.log('`window.prompt` called', message, value);
110
+ return null;
111
+ };
@@ -18,7 +18,6 @@
18
18
  <% end %>
19
19
  </head>
20
20
  <body style="margin: 0; background-color: #fff;">
21
- <div id="likadan-output"/>
22
21
  <script src="/likadan-runner.js"></script>
23
22
  <% @config['source_files'].each do |source_file| %>
24
23
  <script src="/resource?file=<%= ERB::Util.url_encode(source_file) %>"></script>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: likadan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henric Trotzig