Small refactor preparing for branch creating/editing. Also closes #58

This commit is contained in:
Cássio Zen 2016-08-29 17:09:04 -03:00
parent 2b70893e89
commit 6eec0feb72
12 changed files with 70 additions and 19 deletions

View File

@ -3,6 +3,7 @@ backend:
delay: 0.1
media_folder: "assets/uploads"
publish_mode: branch
collections: # A list of collections the CMS should be able to edit
- name: "posts" # Used in routes, ie.: /admin/collections/:slug/edit

View File

@ -62,7 +62,6 @@ export function loadConfig(config) {
function parseConfig(data) {
const config = yaml.safeLoad(data);
if (typeof CMS_ENV === 'string' && config[CMS_ENV]) {
for (var key in config[CMS_ENV]) {
if (config[CMS_ENV].hasOwnProperty(key)) {
@ -70,5 +69,14 @@ function parseConfig(data) {
}
}
}
if ('media_folder' in config && typeof config.media_folder === 'string') {
// Parse source & public paths for media folder.
config.media_folder = {
path: config.media_folder,
public_path: config.media_folder
};
}
return config;
}

View File

@ -175,9 +175,8 @@ export function persistEntry(collection, entry) {
const state = getState();
const backend = currentBackend(state.config);
const MediaProxies = entry.get('mediaFiles').map(path => getMedia(state, path));
dispatch(entryPersisting(collection, entry));
backend.persistEntry(collection, entry, MediaProxies.toJS()).then(
backend.persistEntry(state.config, collection, entry, MediaProxies.toJS()).then(
() => {
dispatch(entryPersisted(collection, entry));
},

View File

@ -1,6 +1,7 @@
import TestRepoBackend from './test-repo/implementation';
import GitHubBackend from './github/implementation';
import { resolveFormat } from '../formats/formats';
import { randomStr } from '../lib/randomGenerator';
import { createEntry } from '../valueObjects/Entry';
class LocalStorageAuthStore {
@ -91,11 +92,21 @@ class Backend {
});
}
persistEntry(collection, entryDraft, MediaFiles) {
const newEntry = entryDraft.getIn(['entry', 'newRecord']) || false;
const entryData = entryDraft.getIn(['entry', 'data']).toObject();
let entryObj;
getPublishMode(config) {
const publish_modes = ['simple', 'branch'];
const mode = config.get('publish_mode');
if (publish_modes.indexOf(mode) !== -1) {
return mode;
} else {
return 'simple';
}
}
persistEntry(config, collection, entryDraft, MediaFiles) {
const mode = this.getPublishMode(config);
const newEntry = entryDraft.getIn(['entry', 'newRecord']) || false;
const entryData = entryDraft.getIn(['entry', 'data']).toJS();
let entryObj;
if (newEntry) {
const slug = this.slugFormatter(collection.get('slug'), entryDraft.get('entry'));
entryObj = {
@ -115,7 +126,7 @@ class Backend {
collection.get('label') + ' “' +
entryDraft.getIn(['entry', 'data', 'title']) + '”';
return this.implementation.persistEntry(collection, entryObj, MediaFiles, { commitMessage }, newEntry);
return this.implementation.persistEntry(entryObj, MediaFiles, { newEntry, commitMessage, mode });
}
entryToRaw(collection, entry) {

View File

@ -43,11 +43,10 @@ class API {
});
}
persistFiles(collection, entry, mediaFiles, options) {
persistFiles(entry, mediaFiles, options) {
let filename, part, parts, subtree;
const fileTree = {};
const files = [];
mediaFiles.concat(entry).forEach((file) => {
if (file.uploaded) { return; }
files.push(this.uploadBlob(file));
@ -226,7 +225,7 @@ export default class GitHub {
));
}
persistEntry(collection, entry, mediaFiles = [], options = {}) {
return this.api.persistFiles(collection, entry, mediaFiles, options);
persistEntry(entry, mediaFiles = [], options = {}) {
return this.api.persistFiles(entry, mediaFiles, options);
}
}

View File

@ -45,7 +45,8 @@ export default class TestRepo {
));
}
persistEntry(collection, entry, mediaFiles = [], newEntry = false) {
persistEntry(entry, mediaFiles = [], options) {
const newEntry = options.newEntry || false;
const folder = entry.path.substring(0, entry.path.lastIndexOf('/'));
const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1);
if (newEntry) {

View File

@ -53,7 +53,7 @@ export default class ImageControl extends React.Component {
if (file) {
const mediaProxy = new MediaProxy(file.name, file);
this.props.onAddMedia(mediaProxy);
this.props.onChange(mediaProxy.path);
this.props.onChange(mediaProxy.public_path);
} else {
this.props.onChange(null);
}

View File

@ -253,7 +253,7 @@ class VisualEditor extends React.Component {
.insertInline({
type: 'mediaproxy',
isVoid: true,
data: { src: mediaProxy.path }
data: { src: mediaProxy.public_path }
})
.collapseToEnd()
.insertBlock(DEFAULT_NODE)

View File

@ -0,0 +1,31 @@
/*
* Random number generator
*/
let rng;
if (window.crypto && crypto.getRandomValues) {
// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
// Moderately fast, high quality
const _rnds32 = new Uint32Array(1);
rng = function whatwgRNG() {
crypto.getRandomValues(_rnds32);
return _rnds32[0];
};
}
if (!rng) {
// Math.random()-based (RNG)
// If no Crypto available, use Math.random().
rng = function() {
const r = Math.random() * 0x100000000;
const _rnds = r >>> 0;
return _rnds;
};
}
export function randomStr() {
return rng().toString(36);
}
export default rng;

View File

@ -26,7 +26,7 @@ const entryDraft = (state = Map(), action) => {
return state.set('entry', action.payload);
case ADD_MEDIA:
return state.update('mediaFiles', (list) => list.push(action.payload.path));
return state.update('mediaFiles', (list) => list.push(action.payload.public_path));
case REMOVE_MEDIA:
return state.update('mediaFiles', (list) => list.filterNot((path) => path === action.payload));

View File

@ -5,7 +5,7 @@ import MediaProxy from '../valueObjects/MediaProxy';
const medias = (state = Map(), action) => {
switch (action.type) {
case ADD_MEDIA:
return state.set(action.payload.path, action.payload);
return state.set(action.payload.public_path, action.payload);
case REMOVE_MEDIA:
return state.delete(action.payload);

View File

@ -8,11 +8,12 @@ export default function MediaProxy(value, file, uploaded = false) {
this.file = file;
this.uploaded = uploaded;
this.sha = null;
this.path = config.media_folder && !uploaded ? config.media_folder + '/' + value : value;
this.path = config.media_folder && !uploaded ? config.media_folder.path + '/' + value : value;
this.public_path = config.media_folder && !uploaded ? config.media_folder.public_path + '/' + value : value;
}
MediaProxy.prototype.toString = function() {
return this.uploaded ? this.path : window.URL.createObjectURL(this.file, { oneTimeOnly: true });
return this.uploaded ? this.public_path : window.URL.createObjectURL(this.file, { oneTimeOnly: true });
};
MediaProxy.prototype.toBase64 = function() {