tinymce-rails-fileupload 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +86 -0
- data/app/assets/javascripts/tinymce/plugins/uploadfile/langs/en.js +12 -0
- data/app/assets/javascripts/tinymce/plugins/uploadfile/plugin.js +216 -0
- data/lib/tasks/tinymce-uploadfile-assets.rake +14 -0
- data/lib/tinymce/rails/fileupload.rb +3 -0
- data/lib/tinymce/rails/fileupload/rails.rb +11 -0
- data/lib/tinymce/rails/fileupload/version.rb +7 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cb2d2c1d7e222106fdede52b64f7ec931cc0d696
|
4
|
+
data.tar.gz: 72d5b7b3e1ba261ef43c1eb08624d1d8179fceb0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eecf1642028e247fc9f0cd95c77cf90cb26d5f14fab40afe22767d516bab15489069ddbf42840dbdef29a62b9787f38db447cb64f2af0f236ccd1af2ffd0560e
|
7
|
+
data.tar.gz: 56052c22e35bd0bc0d37e7b611768f018db8c56576951af4030b30d68b8ac1cc5ed15a4a02a465c2256dc60f7ec73d51f1e403f661e6c7aca86f902bd4202fff
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Dreyer Calitz
|
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,86 @@
|
|
1
|
+
# tinymce-rails-fileupload plugin
|
2
|
+
|
3
|
+
Simple plugin for TinyMCE4 that allows for uploading files from your computer and inserting a link to the uploaded document.
|
4
|
+
|
5
|
+
It makes no assumption on how you store the files, it simply POSTs data to a URL and expects a JSON response (see the Setup).
|
6
|
+
|
7
|
+
This plugin started as a copy of [tinymce-rails-imageupload](https://github.com/PerfectlyNormal/tinymce-rails-imageupload).
|
8
|
+
|
9
|
+
## Requirements
|
10
|
+
|
11
|
+
* Rails >= 4.0
|
12
|
+
* TinyMCE4 using the advanced theme
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'tinymce-rails-fileupload'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install tinymce-rails-fileupload
|
27
|
+
|
28
|
+
## Setup
|
29
|
+
|
30
|
+
### Set up TinyMCE as you would normally, but in the call to `.tinymce()`, add
|
31
|
+
|
32
|
+
plugins: "uploadfile"
|
33
|
+
# toolbar option must include "uploadfile" somewhere to have the button appear
|
34
|
+
|
35
|
+
and the rest should happen automatically.
|
36
|
+
|
37
|
+
### Set up upload URL and handler
|
38
|
+
|
39
|
+
The plugin defaults to POSTing to `/tinymce_assets`.
|
40
|
+
|
41
|
+
You may modify it by supplying the option `uploadfile_form_url` in the call to `.tinymce()`
|
42
|
+
|
43
|
+
Routing to your controller must be done manually.
|
44
|
+
Set it up using something similar in `routes.rb`:
|
45
|
+
|
46
|
+
post '/tinymce_assets' => 'tinymce_assets#create'
|
47
|
+
|
48
|
+
This action gets called with a file parameter creatively called `document[file]`,
|
49
|
+
and must respond with JSON, containing the URL and title of the document.
|
50
|
+
|
51
|
+
The JSON has to be returned with a content type of "text/html" to work.
|
52
|
+
|
53
|
+
Example:
|
54
|
+
|
55
|
+
class TinymceAssetsController < ApplicationController
|
56
|
+
respond_to :json
|
57
|
+
|
58
|
+
def create
|
59
|
+
document = Document.create(document_params)
|
60
|
+
|
61
|
+
render json: {
|
62
|
+
document: {
|
63
|
+
url: document.file.url,
|
64
|
+
title: document.title
|
65
|
+
},
|
66
|
+
target: params[:target].to_s
|
67
|
+
}, layout: false, content_type: "text/html"
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def document_params
|
73
|
+
params.require(:document).permit(:file, :title)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
Inserted HTML is just `<a href="..." title="...">Link text</a>`.
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
1. Fork it ( https://github.com/[my-github-username]/tinymce-rails-documentupload/fork )
|
83
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
84
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
85
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
86
|
+
5. Create a new Pull Request
|
@@ -0,0 +1,12 @@
|
|
1
|
+
tinyMCE.addI18n("en", {
|
2
|
+
"Insert a file from your computer": "Insert a file from your computer",
|
3
|
+
"Insert file": "Insert file",
|
4
|
+
"Choose a file": "Choose a file",
|
5
|
+
"Target": "Target",
|
6
|
+
"You must choose a file": "You must choose a file",
|
7
|
+
"Got a bad response from the server": "Got a bad response from the server",
|
8
|
+
"Did not get a response from the server": "Did not get a response from the server",
|
9
|
+
"Insert": "Insert",
|
10
|
+
"Cancel": "Cancel",
|
11
|
+
"Title": "Title/Link text",
|
12
|
+
});
|
@@ -0,0 +1,216 @@
|
|
1
|
+
(function() {
|
2
|
+
tinymce.PluginManager.requireLangPack('uploadfile');
|
3
|
+
|
4
|
+
tinymce.create('tinymce.plugins.UploadFile', {
|
5
|
+
UploadFile: function(ed, url) {
|
6
|
+
var form,
|
7
|
+
iframe,
|
8
|
+
win,
|
9
|
+
editor = ed;
|
10
|
+
|
11
|
+
function showDialog() {
|
12
|
+
win = editor.windowManager.open({
|
13
|
+
title: ed.translate('Insert a file from your computer'),
|
14
|
+
width: 500 + parseInt(editor.getLang('uploadfile.delta_width', 0), 10),
|
15
|
+
height: 180 + parseInt(editor.getLang('uploadfile.delta_height', 0), 10),
|
16
|
+
body: [
|
17
|
+
{type: 'iframe', url: 'javascript:void(0)'},
|
18
|
+
{type: 'textbox', name: 'document[file]', label: ed.translate('Choose a file'), subtype: 'file'},
|
19
|
+
{type: 'textbox', name: 'document[title]', label: ed.translate('Title')},
|
20
|
+
{type: 'container', classes: 'error', html: "<p style='color: #b94a48;'> </p>"}
|
21
|
+
],
|
22
|
+
buttons: [
|
23
|
+
{
|
24
|
+
text: ed.translate('Insert'),
|
25
|
+
onclick: insertFile,
|
26
|
+
subtype: 'primary'
|
27
|
+
},
|
28
|
+
{
|
29
|
+
text: ed.translate('Cancel'),
|
30
|
+
onclick: ed.windowManager.close
|
31
|
+
}
|
32
|
+
],
|
33
|
+
}, {
|
34
|
+
plugin_url: url
|
35
|
+
});
|
36
|
+
|
37
|
+
// TinyMCE likes pointless submit handlers
|
38
|
+
win.off('submit');
|
39
|
+
win.on('submit', insertFile);
|
40
|
+
|
41
|
+
iframe = win.find("iframe")[0];
|
42
|
+
form = createElement('form', {
|
43
|
+
action: ed.getParam("uploadfile_form_url", "/tinymce_assets"),
|
44
|
+
target: iframe._id,
|
45
|
+
method: "POST",
|
46
|
+
enctype: 'multipart/form-data',
|
47
|
+
accept_charset: "UTF-8",
|
48
|
+
});
|
49
|
+
|
50
|
+
// Might have several instances on the same page,
|
51
|
+
// so we TinyMCE create unique IDs and use those.
|
52
|
+
iframe.getEl().name = iframe._id;
|
53
|
+
|
54
|
+
// Create some needed hidden inputs
|
55
|
+
form.appendChild(createElement('input', {type: "hidden", name: "utf8", value: "✓"}));
|
56
|
+
form.appendChild(createElement('input', {type: 'hidden', name: 'authenticity_token', value: getMetaContents('csrf-token')}));
|
57
|
+
|
58
|
+
var el = win.getEl();
|
59
|
+
var body = document.getElementById(el.id + "-body");
|
60
|
+
|
61
|
+
// Copy everything TinyMCE made into our form
|
62
|
+
var containers = body.getElementsByClassName('mce-container');
|
63
|
+
for(var i = 0; i < containers.length; i++) {
|
64
|
+
form.appendChild(containers[i]);
|
65
|
+
}
|
66
|
+
|
67
|
+
// Fix inputs, since TinyMCE hates HTML and forms
|
68
|
+
var inputs = form.getElementsByTagName('input');
|
69
|
+
for(var i = 0; i < inputs.length; i++) {
|
70
|
+
var ctrl = inputs[i];
|
71
|
+
|
72
|
+
if(ctrl.tagName.toLowerCase() == 'input' && ctrl.type != "hidden") {
|
73
|
+
if(ctrl.type == "file") {
|
74
|
+
ctrl.name = "document[file]";
|
75
|
+
|
76
|
+
// Hack styles
|
77
|
+
var padding = '0';
|
78
|
+
if(window.navigator.userAgent.match(/(Chrome|Safari)/))
|
79
|
+
padding = '7px 0';
|
80
|
+
|
81
|
+
tinymce.DOM.setStyles(ctrl, {
|
82
|
+
'border': 0,
|
83
|
+
'boxShadow': 'none',
|
84
|
+
'webkitBoxShadow': 'none',
|
85
|
+
'padding': padding
|
86
|
+
});
|
87
|
+
} else if(ctrl.type == "text") {
|
88
|
+
ctrl.name = "document[title]";
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
body.appendChild(form);
|
94
|
+
}
|
95
|
+
|
96
|
+
function insertFile() {
|
97
|
+
if(getInputValue("document[file]") == "") {
|
98
|
+
return handleError('You must choose a file');
|
99
|
+
}
|
100
|
+
|
101
|
+
clearErrors();
|
102
|
+
|
103
|
+
/* Add event listeners.
|
104
|
+
* We remove the existing to avoid them being called twice in case
|
105
|
+
* of errors and re-submitting afterwards.
|
106
|
+
*/
|
107
|
+
var target = iframe.getEl();
|
108
|
+
if(target.attachEvent) {
|
109
|
+
target.detachEvent('onload', uploadDone);
|
110
|
+
target.attachEvent('onload', uploadDone);
|
111
|
+
} else {
|
112
|
+
target.removeEventListener('load', uploadDone);
|
113
|
+
target.addEventListener('load', uploadDone, false);
|
114
|
+
}
|
115
|
+
|
116
|
+
form.submit();
|
117
|
+
}
|
118
|
+
|
119
|
+
function uploadDone() {
|
120
|
+
var target = iframe.getEl();
|
121
|
+
if(target.document || target.contentDocument) {
|
122
|
+
var doc = target.contentDocument || target.contentWindow.document;
|
123
|
+
handleResponse(doc.getElementsByTagName("body")[0].innerHTML);
|
124
|
+
} else {
|
125
|
+
handleError("Did not get a response from the server");
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
function handleResponse(ret) {
|
130
|
+
try {
|
131
|
+
var json = tinymce.util.JSON.parse(ret);
|
132
|
+
|
133
|
+
if(json["error"]) {
|
134
|
+
handleError(json["error"]["message"]);
|
135
|
+
} else {
|
136
|
+
ed.execCommand('mceInsertContent', false, buildHTML(json));
|
137
|
+
ed.windowManager.close();
|
138
|
+
}
|
139
|
+
} catch(e) {
|
140
|
+
console.log(e);
|
141
|
+
handleError('Got a bad response from the server');
|
142
|
+
}
|
143
|
+
}
|
144
|
+
|
145
|
+
function clearErrors() {
|
146
|
+
var message = win.find(".error")[0].getEl();
|
147
|
+
|
148
|
+
if(message)
|
149
|
+
message.getElementsByTagName("p")[0].innerHTML = " ";
|
150
|
+
}
|
151
|
+
|
152
|
+
function handleError(error) {
|
153
|
+
var message = win.find(".error")[0].getEl();
|
154
|
+
|
155
|
+
if(message)
|
156
|
+
message.getElementsByTagName("p")[0].innerHTML = ed.translate(error);
|
157
|
+
}
|
158
|
+
|
159
|
+
function createElement(element, attributes) {
|
160
|
+
var el = document.createElement(element);
|
161
|
+
for(var property in attributes) {
|
162
|
+
if (!(attributes[property] instanceof Function)) {
|
163
|
+
el[property] = attributes[property];
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
return el;
|
168
|
+
}
|
169
|
+
|
170
|
+
function buildHTML(json) {
|
171
|
+
var url = json["document"]["url"];
|
172
|
+
var title = json["document"]["title"];
|
173
|
+
var link = '<a href="' + url + '" title="' + title + '">' + title + '</a>';
|
174
|
+
|
175
|
+
return link;
|
176
|
+
}
|
177
|
+
|
178
|
+
function getInputValue(name) {
|
179
|
+
var inputs = form.getElementsByTagName("input");
|
180
|
+
|
181
|
+
for(var i in inputs)
|
182
|
+
if(inputs[i].name == name)
|
183
|
+
return inputs[i].value;
|
184
|
+
|
185
|
+
return "";
|
186
|
+
}
|
187
|
+
|
188
|
+
function getMetaContents(mn) {
|
189
|
+
var m = document.getElementsByTagName('meta');
|
190
|
+
|
191
|
+
for(var i in m)
|
192
|
+
if(m[i].name == mn)
|
193
|
+
return m[i].content;
|
194
|
+
|
195
|
+
return null;
|
196
|
+
}
|
197
|
+
|
198
|
+
// Add a button that opens a window
|
199
|
+
editor.addButton('uploadfile', {
|
200
|
+
tooltip: ed.translate('Insert a file from your computer'),
|
201
|
+
icon : 'newdocument',
|
202
|
+
onclick: showDialog
|
203
|
+
});
|
204
|
+
|
205
|
+
// Adds a menu item to the tools menu
|
206
|
+
editor.addMenuItem('uploadfile', {
|
207
|
+
text: ed.translate('Insert a file from your computer'),
|
208
|
+
icon : 'newdocument',
|
209
|
+
context: 'insert',
|
210
|
+
onclick: showDialog
|
211
|
+
});
|
212
|
+
}
|
213
|
+
});
|
214
|
+
|
215
|
+
tinymce.PluginManager.add('uploadfile', tinymce.plugins.UploadFile);
|
216
|
+
})();
|
@@ -0,0 +1,14 @@
|
|
1
|
+
assets_task = Rake::Task.task_defined?('assets:precompile:primary') ? 'assets:precompile:primary' : 'assets:precompile'
|
2
|
+
|
3
|
+
Rake::Task[assets_task].enhance do
|
4
|
+
require "tinymce/rails/asset_installer"
|
5
|
+
|
6
|
+
config = Rails.application.config
|
7
|
+
target = File.join(Rails.public_path, config.assets.prefix)
|
8
|
+
manifest = config.assets.manifest
|
9
|
+
|
10
|
+
assets = Pathname.new(File.expand_path(File.join(File.dirname(__FILE__),
|
11
|
+
"../../app/assets/javascripts/tinymce/plugins/uploaddocument")))
|
12
|
+
|
13
|
+
TinyMCE::Rails::AssetInstaller.new(assets, target, manifest).install
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tinymce-rails-fileupload
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dreyer Calitz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: tinymce-rails
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '4.0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '4.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.6'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.6'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rails
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '4.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '4.0'
|
75
|
+
description: TinyMCE plugin for file uploads in Rails >= 4.0. Document storage is
|
76
|
+
handled manually, so works with everything.
|
77
|
+
email:
|
78
|
+
- fdcalitz@gmail.com
|
79
|
+
executables: []
|
80
|
+
extensions: []
|
81
|
+
extra_rdoc_files: []
|
82
|
+
files:
|
83
|
+
- CHANGELOG.md
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- app/assets/javascripts/tinymce/plugins/uploadfile/langs/en.js
|
87
|
+
- app/assets/javascripts/tinymce/plugins/uploadfile/plugin.js
|
88
|
+
- lib/tasks/tinymce-uploadfile-assets.rake
|
89
|
+
- lib/tinymce/rails/fileupload.rb
|
90
|
+
- lib/tinymce/rails/fileupload/rails.rb
|
91
|
+
- lib/tinymce/rails/fileupload/version.rb
|
92
|
+
homepage: ''
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.2.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: TinyMCE plugin for file uploads in Rails >= 4.0
|
116
|
+
test_files: []
|