grandejs_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grandejs_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Guy Israeli
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # GrandejsRails
2
+
3
+ Javascript library that implements features from Medium's editing experience.
4
+
5
+ see demo here http://mattduvall.com/grande.js/
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'grandejs_rails'
12
+
13
+ add to js manifest
14
+
15
+ //= require grande
16
+
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install grandejs_rails
25
+
26
+ ## Usage
27
+
28
+ as said in the source - https://github.com/mduvall/grande.js
29
+
30
+ To get up and running simply...
31
+
32
+ Include an <article> with contenteditable
33
+ Include the grande.js file at the bottom of your <body>
34
+ Bind the events on the document with grande.init()
35
+ You are set!
36
+
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,445 @@
1
+ // grande.js
2
+ // version: 0.0.1
3
+ // Matt DuVall <matt@mattduvall.com>
4
+ // Open source implementation of Medium's UI features
5
+ // "url": "https://github.com/mduvall/grande.js"
6
+ //
7
+
8
+
9
+ (function() {
10
+ var root = this, // Root object, this is going to be the window for now
11
+ document = this.document, // Safely store a document here for us to use
12
+ editableNodes = document.querySelectorAll(".g-body article"),
13
+ isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1,
14
+ textMenu,
15
+ optionsNode,
16
+ urlInput,
17
+ previouslySelectedText,
18
+
19
+ grande = {
20
+ bind: function(bindableNodes) {
21
+ if (bindableNodes) {
22
+ editableNodes = bindableNodes;
23
+ }
24
+
25
+ attachToolbarTemplate();
26
+ bindTextSelectionEvents();
27
+ bindTextStylingEvents();
28
+ },
29
+ select: function() {
30
+ triggerTextSelection();
31
+ }
32
+ },
33
+
34
+ tagClassMap = {
35
+ "b": "bold",
36
+ "i": "italic",
37
+ "h1": "header1",
38
+ "h2": "header2",
39
+ "a": "url",
40
+ "blockquote": "quote"
41
+ };
42
+
43
+ function attachToolbarTemplate() {
44
+ var div = document.createElement("div"),
45
+ toolbarTemplate = "<div class='options'> \
46
+ <span class='no-overflow'> \
47
+ <span class='ui-inputs'> \
48
+ <button class='bold'>B</button> \
49
+ <button class='italic'>i</button> \
50
+ <button class='header1'>h1</button> \
51
+ <button class='header2'>h2</button> \
52
+ <button class='quote'>&rdquo;</button> \
53
+ <button class='url useicons'>&#xe001;</button> \
54
+ <input class='url-input' type='text' placeholder='Paste or type a link'/> \
55
+ </span> \
56
+ </span> \
57
+ </div>";
58
+
59
+ div.className = "text-menu hide";
60
+ div.innerHTML = toolbarTemplate;
61
+
62
+ if (document.querySelectorAll(".text-menu").length === 0) {
63
+ document.body.appendChild(div);
64
+ }
65
+
66
+ textMenu = document.querySelectorAll(".text-menu")[0];
67
+ optionsNode = document.querySelectorAll(".text-menu .options")[0];
68
+ urlInput = document.querySelectorAll(".text-menu .url-input")[0];
69
+ }
70
+
71
+ function bindTextSelectionEvents() {
72
+ var i,
73
+ len,
74
+ node;
75
+
76
+ // Trigger on both mousedown and mouseup so that the click on the menu
77
+ // feels more instantaneously active
78
+ document.onmousedown = triggerTextSelection;
79
+ document.onmouseup = function(event) {
80
+ setTimeout(function() {
81
+ triggerTextSelection(event);
82
+ }, 1);
83
+ };
84
+
85
+ document.onkeydown = preprocessKeyDown;
86
+
87
+ document.onkeyup = function(event){
88
+ var sel = window.getSelection();
89
+
90
+ // FF will return sel.anchorNode to be the parentNode when the triggered keyCode is 13
91
+ if (sel.anchorNode && sel.anchorNode.nodeName !== "ARTICLE") {
92
+ triggerNodeAnalysis(event);
93
+
94
+ if (sel.isCollapsed) {
95
+ triggerTextParse(event);
96
+ }
97
+ }
98
+ };
99
+
100
+ // Handle window resize events
101
+ root.onresize = triggerTextSelection;
102
+
103
+ urlInput.onblur = triggerUrlBlur;
104
+ urlInput.onkeydown = triggerUrlSet;
105
+
106
+ for (i = 0, len = editableNodes.length; i < len; i++) {
107
+ node = editableNodes[i];
108
+ node.contentEditable = true;
109
+ node.onmousedown = node.onkeyup = node.onmouseup = triggerTextSelection;
110
+ }
111
+ }
112
+
113
+ function iterateTextMenuButtons(callback) {
114
+ var textMenuButtons = document.querySelectorAll(".text-menu button"),
115
+ i,
116
+ len,
117
+ node;
118
+
119
+ for (i = 0, len = textMenuButtons.length; i < len; i++) {
120
+ node = textMenuButtons[i];
121
+
122
+ (function(n) {
123
+ callback(n);
124
+ })(node);
125
+ }
126
+ }
127
+
128
+ function bindTextStylingEvents() {
129
+ iterateTextMenuButtons(function(node) {
130
+ node.onmousedown = function(event) {
131
+ triggerTextStyling(node);
132
+ };
133
+ });
134
+ }
135
+
136
+ function getFocusNode() {
137
+ return root.getSelection().focusNode;
138
+ }
139
+
140
+ function reloadMenuState() {
141
+ var className,
142
+ focusNode = getFocusNode(),
143
+ tagClass,
144
+ reTag;
145
+
146
+ iterateTextMenuButtons(function(node) {
147
+ className = node.className;
148
+
149
+ for (var tag in tagClassMap) {
150
+ tagClass = tagClassMap[tag];
151
+ reTag = new RegExp(tagClass);
152
+
153
+ if (reTag.test(className)) {
154
+ if (hasParentWithTag(focusNode, tag)) {
155
+ node.className = tagClass + " active";
156
+ } else {
157
+ node.className = tagClass;
158
+ }
159
+
160
+ break;
161
+ }
162
+ }
163
+ });
164
+ }
165
+
166
+ function preprocessKeyDown(event) {
167
+ var sel = window.getSelection(),
168
+ parentParagraph = getParentWithTag(sel.anchorNode, "p"),
169
+ p,
170
+ isHr;
171
+
172
+ if (event.keyCode === 13 && parentParagraph) {
173
+ prevSibling = parentParagraph.previousSibling;
174
+ isHr = prevSibling && prevSibling.nodeName === "HR" &&
175
+ !parentParagraph.textContent.length;
176
+
177
+ // Stop enters from creating another <p> after a <hr> on enter
178
+ if (isHr) {
179
+ event.preventDefault();
180
+ }
181
+ }
182
+ }
183
+
184
+ function triggerNodeAnalysis(event) {
185
+ var sel = window.getSelection(),
186
+ anchorNode,
187
+ parentParagraph;
188
+
189
+ if (event.keyCode === 13) {
190
+
191
+ // Enters should replace it's parent <div> with a <p>
192
+ if (sel.anchorNode.nodeName === "DIV") {
193
+ toggleFormatBlock("p");
194
+ }
195
+
196
+ parentParagraph = getParentWithTag(sel.anchorNode, "p");
197
+
198
+ if (parentParagraph) {
199
+ insertHorizontalRule(parentParagraph);
200
+ }
201
+ }
202
+ }
203
+
204
+ function insertHorizontalRule(parentParagraph) {
205
+ var prevSibling,
206
+ prevPrevSibling,
207
+ hr;
208
+
209
+ prevSibling = parentParagraph.previousSibling;
210
+ prevPrevSibling = prevSibling;
211
+
212
+ while(prevPrevSibling = prevPrevSibling.previousSibling){
213
+ if (prevPrevSibling.nodeType != Node.TEXT_NODE) break;
214
+ }
215
+
216
+ if (prevSibling.nodeName === "P" && !prevSibling.textContent.length && prevPrevSibling.nodeName !== "HR") {
217
+ hr = document.createElement("hr");
218
+ hr.contentEditable = false;
219
+ parentParagraph.parentNode.replaceChild(hr, prevSibling);
220
+ }
221
+ }
222
+
223
+ function getTextProp(el) {
224
+ var textProp;
225
+
226
+ if (el.nodeType === Node.TEXT_NODE) {
227
+ textProp = "data";
228
+ } else if (isFirefox) {
229
+ textProp = "textContent";
230
+ } else {
231
+ textProp = "innerText";
232
+ }
233
+
234
+ return textProp;
235
+ }
236
+
237
+ function insertListOnSelection(sel, textProp, listType) {
238
+ var execListCommand = listType === "ol" ? "insertOrderedList" : "insertUnorderedList",
239
+ nodeOffset = listType === "ol" ? 3 : 2;
240
+
241
+ document.execCommand(execListCommand);
242
+ sel.anchorNode[textProp] = sel.anchorNode[textProp].substring(nodeOffset);
243
+
244
+ return getParentWithTag(sel.anchorNode, listType);
245
+ }
246
+
247
+ function triggerTextParse(event) {
248
+ var sel = window.getSelection(),
249
+ textProp,
250
+ subject,
251
+ insertedNode,
252
+ unwrap,
253
+ node,
254
+ parent,
255
+ range;
256
+
257
+ // FF will return sel.anchorNode to be the parentNode when the triggered keyCode is 13
258
+ if (!sel.isCollapsed || !sel.anchorNode || sel.anchorNode.nodeName === "ARTICLE") {
259
+ return;
260
+ }
261
+
262
+ textProp = getTextProp(sel.anchorNode);
263
+ subject = sel.anchorNode[textProp];
264
+
265
+ if (subject.match(/^-\s/) && sel.anchorNode.parentNode.nodeName !== "LI") {
266
+ insertedNode = insertListOnSelection(sel, textProp, "ul");
267
+ }
268
+
269
+ if (subject.match(/^1\.\s/) && sel.anchorNode.parentNode.nodeName !== "LI") {
270
+ insertedNode = insertListOnSelection(sel, textProp, "ol");
271
+ }
272
+
273
+ unwrap = insertedNode &&
274
+ ["ul", "ol"].indexOf(insertedNode.nodeName.toLocaleLowerCase()) >= 0 &&
275
+ ["p", "div"].indexOf(insertedNode.parentNode.nodeName.toLocaleLowerCase()) >= 0;
276
+
277
+ if (unwrap) {
278
+ node = sel.anchorNode;
279
+ parent = insertedNode.parentNode;
280
+ parent.parentNode.insertBefore(insertedNode, parent);
281
+ parent.parentNode.removeChild(parent);
282
+ moveCursorToBeginningOfSelection(sel, node);
283
+ }
284
+ }
285
+
286
+ function moveCursorToBeginningOfSelection(selection, node) {
287
+ range = document.createRange();
288
+ range.setStart(node, 0);
289
+ range.setEnd(node, 0);
290
+ selection.removeAllRanges();
291
+ selection.addRange(range);
292
+ }
293
+
294
+ function triggerTextStyling(node) {
295
+ var className = node.className,
296
+ sel = window.getSelection(),
297
+ selNode = sel.anchorNode,
298
+ tagClass,
299
+ reTag;
300
+
301
+ for (var tag in tagClassMap) {
302
+ tagClass = tagClassMap[tag];
303
+ reTag = new RegExp(tagClass);
304
+
305
+ if (reTag.test(className)) {
306
+ switch(tag) {
307
+ case "b":
308
+ if (selNode && !hasParentWithTag(selNode, "h1") && !hasParentWithTag(selNode, "h2")) {
309
+ document.execCommand(tagClass, false);
310
+ }
311
+ return;
312
+ case "i":
313
+ document.execCommand(tagClass, false);
314
+ return;
315
+
316
+ case "h1":
317
+ case "h2":
318
+ case "h3":
319
+ case "blockquote":
320
+ toggleFormatBlock(tag);
321
+ return;
322
+
323
+ case "a":
324
+ toggleUrlInput();
325
+ optionsNode.className = "options url-mode";
326
+ return;
327
+ }
328
+ }
329
+ }
330
+
331
+ triggerTextSelection();
332
+ }
333
+
334
+ function triggerUrlBlur(event) {
335
+ var url = urlInput.value;
336
+
337
+ optionsNode.className = "options";
338
+ window.getSelection().addRange(previouslySelectedText);
339
+
340
+ document.execCommand("unlink", false);
341
+
342
+ if (url === "") {
343
+ return false;
344
+ }
345
+
346
+ if (!url.match("^(http://|https://|mailto:)")) {
347
+ url = "http://" + url;
348
+ }
349
+
350
+ document.execCommand("createLink", false, url);
351
+
352
+ urlInput.value = "";
353
+ }
354
+
355
+ function triggerUrlSet(event) {
356
+ if (event.keyCode === 13) {
357
+ event.preventDefault();
358
+ event.stopPropagation();
359
+
360
+ urlInput.blur();
361
+ }
362
+ }
363
+
364
+ function toggleFormatBlock(tag) {
365
+ if (hasParentWithTag(getFocusNode(), tag)) {
366
+ document.execCommand("formatBlock", false, "p");
367
+ document.execCommand("outdent");
368
+ } else {
369
+ document.execCommand("formatBlock", false, tag);
370
+ }
371
+ }
372
+
373
+ function toggleUrlInput() {
374
+ setTimeout(function() {
375
+ var url = getParentHref(getFocusNode());
376
+
377
+ if (typeof url !== "undefined") {
378
+ urlInput.value = url;
379
+ } else {
380
+ document.execCommand("createLink", false, "/");
381
+ }
382
+
383
+ previouslySelectedText = window.getSelection().getRangeAt(0);
384
+
385
+ urlInput.focus();
386
+ }, 150);
387
+ }
388
+
389
+ function getParent(node, condition, returnCallback) {
390
+ while (node.parentNode) {
391
+ if (condition(node)) {
392
+ return returnCallback(node);
393
+ }
394
+
395
+ node = node.parentNode;
396
+ }
397
+ }
398
+
399
+ function getParentWithTag(node, nodeType) {
400
+ var checkNodeType = function(node) { return node.nodeName.toLowerCase() === nodeType; },
401
+ returnNode = function(node) { return node; };
402
+
403
+ return getParent(node, checkNodeType, returnNode);
404
+ }
405
+
406
+ function hasParentWithTag(node, nodeType) {
407
+ return !!getParentWithTag(node, nodeType);
408
+ }
409
+
410
+ function getParentHref(node) {
411
+ var checkHref = function(node) { return typeof node.href !== "undefined"; },
412
+ returnHref = function(node) { return node.href; };
413
+
414
+ return getParent(node, checkHref, returnHref);
415
+ }
416
+
417
+ function triggerTextSelection() {
418
+ var selectedText = root.getSelection(),
419
+ range,
420
+ clientRectBounds;
421
+
422
+ // The selected text is collapsed, push the menu out of the way
423
+ if (selectedText.isCollapsed) {
424
+ setTextMenuPosition(-999, -999);
425
+ } else {
426
+ range = selectedText.getRangeAt(0);
427
+ clientRectBounds = range.getBoundingClientRect();
428
+
429
+ // Every time we show the menu, reload the state
430
+ reloadMenuState();
431
+ setTextMenuPosition(
432
+ clientRectBounds.top - 5 + root.pageYOffset,
433
+ (clientRectBounds.left + clientRectBounds.right) / 2
434
+ );
435
+ }
436
+ }
437
+
438
+ function setTextMenuPosition(top, left) {
439
+ textMenu.style.top = top + "px";
440
+ textMenu.style.left = left + "px";
441
+ }
442
+
443
+ root.grande = grande;
444
+
445
+ }).call(this);
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grandejs_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "grandejs_rails"
8
+ spec.version = GrandejsRails::VERSION
9
+ spec.authors = ["Guy Israeli"]
10
+ spec.description = %q{Grande.js For Rails 3.1+ }
11
+ spec.summary = %q{Grande.js is a great javascript library that implements features from Medium's editing experience.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_dependency "railties", ">=3.1"
23
+ end
@@ -0,0 +1,8 @@
1
+ require "grandejs_rails/version"
2
+
3
+ module GrandejsRails
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module GrandejsRails
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grandejs_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guy Israeli
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: railties
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3.1'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ description: ! 'Grande.js For Rails 3.1+ '
63
+ email:
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - app/assets/javascripts/grande.js
74
+ - grandejs_rails.gemspec
75
+ - lib/grandejs_rails.rb
76
+ - lib/grandejs_rails/version.rb
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.24
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Grande.js is a great javascript library that implements features from Medium's
102
+ editing experience.
103
+ test_files: []