Browse Source

Improve asset uploading

pull/261/head
Artur Arseniev 9 years ago
parent
commit
c55a59803d
  1. 5
      .eslintrc
  2. 6
      dist/grapes.min.js
  3. 9
      index.html
  4. 8
      package.json
  5. 6
      src/asset_manager/config/config.js
  6. 77
      src/asset_manager/view/FileUploader.js
  7. 2
      src/editor/index.js
  8. 6
      src/utils/fetch.js
  9. 3
      webpack.config.js

5
.eslintrc

@ -4,7 +4,10 @@
"node": true "node": true
}, },
"parserOptions": { "parserOptions": {
"sourceType": "module" "sourceType": "module",
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
}, },
"rules": { "rules": {
"strict": 0, "strict": 0,

6
dist/grapes.min.js

File diff suppressed because one or more lines are too long

9
index.html

@ -868,10 +868,11 @@
}, },
assetManager: { assetManager: {
storageType : '', upload: 'https://grapedrop.dev/asset-upload/testpage',
storeOnChange : true, params: {
storeAfterUpload : true, _token: 'pCYrSwjuiV0t5NVtZpQDY41Gn5lNUwo3it1FIkAj',
assets : [ },
assets: [
{ type: 'image', src : 'http://placehold.it/350x250/78c5d6/fff/image1.jpg', height:350, width:250}, { type: 'image', src : 'http://placehold.it/350x250/78c5d6/fff/image1.jpg', height:350, width:250},
{ type: 'image', src : 'http://placehold.it/350x250/459ba8/fff/image2.jpg', height:350, width:250}, { type: 'image', src : 'http://placehold.it/350x250/459ba8/fff/image2.jpg', height:350, width:250},
{ type: 'image', src : 'http://placehold.it/350x250/79c267/fff/image3.jpg', height:350, width:250}, { type: 'image', src : 'http://placehold.it/350x250/79c267/fff/image3.jpg', height:350, width:250},

8
package.json

@ -1,7 +1,7 @@
{ {
"name": "grapesjs", "name": "grapesjs",
"description": "Free and Open Source Web Builder Framework", "description": "Free and Open Source Web Builder Framework",
"version": "0.9.25", "version": "0.9.26",
"author": "Artur Arseniev", "author": "Artur Arseniev",
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"homepage": "http://grapesjs.com", "homepage": "http://grapesjs.com",
@ -11,6 +11,7 @@
"url": "https://github.com/artf/grapesjs.git" "url": "https://github.com/artf/grapesjs.git"
}, },
"dependencies": { "dependencies": {
"babel-preset-stage-3": "^6.24.1",
"backbone": "^1.3.3", "backbone": "^1.3.3",
"backbone-undo": "^0.2.5", "backbone-undo": "^0.2.5",
"codemirror": "^5.21.0", "codemirror": "^5.21.0",
@ -26,6 +27,7 @@
"babel-core": "^6.24.1", "babel-core": "^6.24.1",
"babel-loader": "^7.0.0", "babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1", "babel-preset-es2015": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
"chai": "^3.5.0", "chai": "^3.5.0",
"cross-env": "^5.0.4", "cross-env": "^5.0.4",
"documentation": "^4.0.0-beta2", "documentation": "^4.0.0-beta2",
@ -54,9 +56,7 @@
"editor" "editor"
], ],
"babel": { "babel": {
"presets": [ "presets": [ "es2015", "stage-3"]
"es2015"
]
}, },
"scripts": { "scripts": {
"lint": "eslint src", "lint": "eslint src",

6
src/asset_manager/config/config.js

@ -5,8 +5,10 @@ module.exports = {
// Style prefix // Style prefix
stylePrefix: 'am-', stylePrefix: 'am-',
// Url where uploads will be send, set false to disable upload // Upload endpoint, set `false` to disable upload
upload: 'http://localhost/assets/upload', // upload: 'https://endpoint/upload/assets',
// upload: false,
upload: 0,
// Custom headers to pass with the upload request // Custom headers to pass with the upload request
headers: {}, headers: {},

77
src/asset_manager/view/FileUploader.js

@ -31,38 +31,52 @@ module.exports = Backbone.View.extend({
this.delegateEvents(); this.delegateEvents();
}, },
/**
* Triggered before the upload is started
* @private
*/
onUploadStart() { onUploadStart() {
const em = this.config.em; const em = this.config.em;
em && em.trigger('asset:upload:start'); em && em.trigger('asset:upload:start');
}, },
onUploadEnd() { /**
* Triggered after the upload is ended
* @param {Object|string} res End result
* @private
*/
onUploadEnd(res) {
const em = this.config.em; const em = this.config.em;
em && em.trigger('asset:upload:end'); em && em.trigger('asset:upload:end', res);
}, },
/**
* Triggered on upload error
* @param {Object} err Error
* @private
*/
onUploadError(err) { onUploadError(err) {
console.error(err); console.error(err);
this.onUploadEnd(err); this.onUploadEnd(err);
}, },
onUploadResponse(res) { /**
* Triggered on upload response
* @param {string} text Response text
* @private
*/
onUploadResponse(text) {
const em = this.config.em; const em = this.config.em;
const config = this.config; const config = this.config;
const target = this.target; const target = this.target;
em && em.trigger('asset:upload:response', res); const json = JSON.parse(text);
em && em.trigger('asset:upload:response', json);
if (config.autoAdd && target) { if (config.autoAdd && target) {
if ((req.status/200|0) == 1) { target.add(json.data);
const json = JSON.parse(req.responseText);
target.add(json.data);
} else {
onUploadError(res);
return;
}
} }
this.onUploadEnd(res); this.onUploadEnd(text);
}, },
/** /**
@ -87,7 +101,6 @@ module.exports = Backbone.View.extend({
var target = this.target; var target = this.target;
const url = config.upload; const url = config.upload;
if (url) { if (url) {
this.onUploadStart(); this.onUploadStart();
return fetch(url, { return fetch(url, {
@ -95,39 +108,13 @@ module.exports = Backbone.View.extend({
credentials: 'include', credentials: 'include',
headers: config.headers, headers: config.headers,
body: formData, body: formData,
}).then(this.onUploadResponse) }).then(res => (res.status/200|0) == 1 ?
.catch(this.onUploadError); res.text() : res.text().then((text) =>
Promise.reject(text)
))
.then((text) => this.onUploadResponse(text))
.catch(err => this.onUploadError(err));
} }
//onStart upload:start
//onEnd upload:end
//onResponse upload:response
//autoAdd
/*
$.ajax({
url : this.config.upload,
type : 'POST',
data : formData,
beforeSend : this.config.beforeSend,
complete : this.config.onComplete,
xhrFields : {
onprogress(e) {
if (e.lengthComputable) {
var result = e.loaded / e.total * 100 + '%';
}
},
onload(e) {
//progress.value = 100;
}
},
cache: false, contentType: false, processData: false
}).done(data => {
target.add(data.data);
}).always(() => {
//turnOff loading
});
*/
}, },
/** /**

2
src/editor/index.js

@ -39,7 +39,7 @@
* * `component:styleUpdate:{propertyName}` - Listen for a specific style property change * * `component:styleUpdate:{propertyName}` - Listen for a specific style property change
* * `asset:upload:start` - Before the upload is started * * `asset:upload:start` - Before the upload is started
* * `asset:upload:end` - After the upload is ended * * `asset:upload:end` - After the upload is ended
* * `asset:upload:response` - On upload response, passes a response object as the argument * * `asset:upload:response` - On upload response, passes the result as an argument
* * `styleManager:change` - Triggered on style property change from new selected component, the view of the property is passed as an argument to the callback * * `styleManager:change` - Triggered on style property change from new selected component, the view of the property is passed as an argument to the callback
* * `styleManager:change:{propertyName}` - As above but for a specific style property * * `styleManager:change:{propertyName}` - As above but for a specific style property
* * `storage:load` - Triggered when something was loaded from the storage, loaded object passed as an argumnet * * `storage:load` - Triggered when something was loaded from the storage, loaded object passed as an argumnet

6
src/utils/fetch.js

@ -12,7 +12,11 @@ export default typeof fetch == 'function' ? fetch.bind() : (url, options) => {
req.setRequestHeader(k, options.headers[k]); req.setRequestHeader(k, options.headers[k]);
} }
req.onload = e => res(req); req.onload = e => res({
status: req.status,
statusText: req.statusText,
text: () => Promise.resolve(req.responseText)
});
req.onerror = rej; req.onerror = rej;
// Actually, fetch doesn't support onProgress feature // Actually, fetch doesn't support onProgress feature

3
webpack.config.js

@ -34,8 +34,7 @@ module.exports = {
test: /\.js$/, test: /\.js$/,
loader: 'babel-loader', loader: 'babel-loader',
include: /src/, include: /src/,
exclude: /node_modules/, exclude: /node_modules/
query: {presets: ['es2015']}
}], }],
}, },
resolve: { resolve: {

Loading…
Cancel
Save