Browse Source

Add camelCase option

up-style-manager
Artur Arseniev 5 years ago
parent
commit
63961419a1
  1. 12
      src/style_manager/model/Layer.js
  2. 13
      src/style_manager/model/PropertyStack.js
  3. 42
      src/utils/mixins.js

12
src/style_manager/model/Layer.js

@ -1,5 +1,6 @@
import Backbone from 'backbone'; import Backbone from 'backbone';
import Properties from './Properties'; import Properties from './Properties';
import { camelCase } from 'utils/mixins';
export default Backbone.Model.extend({ export default Backbone.Model.extend({
defaults: { defaults: {
@ -33,8 +34,15 @@ export default Backbone.Model.extend({
} }
}, },
getValues() { getValues(opts = {}) {
return this.get('values'); const values = this.get('values');
return opts.camelCase
? Object.keys(values).reduce((res, key) => {
res[camelCase(key)] = values[key];
return res;
}, {})
: values;
}, },
upValues(props = {}) { upValues(props = {}) {

13
src/style_manager/model/PropertyStack.js

@ -1,6 +1,7 @@
import { keys, isUndefined } from 'underscore'; import { keys, isUndefined } from 'underscore';
import Property from './PropertyComposite'; import Property from './PropertyComposite';
import Layers from './Layers'; import Layers from './Layers';
import { camelCase } from 'utils/mixins';
const VALUES_REG = /,(?![^\(]*\))/; const VALUES_REG = /,(?![^\(]*\))/;
const PARTS_REG = /\s(?![^(]*\))/; const PARTS_REG = /\s(?![^(]*\))/;
@ -125,7 +126,7 @@ export default Property.extend({
* Get style object from layer values * Get style object from layer values
* @param {[Layer]} layer * @param {[Layer]} layer
*/ */
getStyleFromLayer(layer) { getStyleFromLayer(layer, opts = {}) {
const sep = this.get('separator'); const sep = this.get('separator');
const values = layer.getValues(); const values = layer.getValues();
const result = this.getProperties().map(prop => { const result = this.getProperties().map(prop => {
@ -134,8 +135,7 @@ export default Property.extend({
const value = isUndefined(val) ? prop.getDefaultValue() : val; const value = isUndefined(val) ? prop.getDefaultValue() : val;
return { name, value }; return { name, value };
}); });
const style = this.get('detached')
return this.get('detached')
? result.reduce((acc, item) => { ? result.reduce((acc, item) => {
acc[item.name] = item.value; acc[item.name] = item.value;
return acc; return acc;
@ -143,6 +143,13 @@ export default Property.extend({
: { : {
[this.getName()]: result.map(r => r.value).join(sep), [this.getName()]: result.map(r => r.value).join(sep),
}; };
return opts.camelCase
? Object.keys(style).reduce((res, key) => {
res[camelCase(key)] = style[key];
return res;
}, {})
: style;
}, },
__getFullValue() { __getFullValue() {

42
src/utils/mixins.js

@ -5,20 +5,12 @@ export const isDef = value => typeof value !== 'undefined';
export const hasWin = () => typeof window !== 'undefined'; export const hasWin = () => typeof window !== 'undefined';
export const getGlobal = () => export const getGlobal = () =>
typeof globalThis !== 'undefined' typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : global;
? globalThis
: typeof window !== 'undefined'
? window
: global;
export const toLowerCase = str => (str || '').toLowerCase(); export const toLowerCase = str => (str || '').toLowerCase();
const elProt = hasWin() ? window.Element.prototype : {}; const elProt = hasWin() ? window.Element.prototype : {};
const matches = const matches = elProt.matches || elProt.webkitMatchesSelector || elProt.mozMatchesSelector || elProt.msMatchesSelector;
elProt.matches ||
elProt.webkitMatchesSelector ||
elProt.mozMatchesSelector ||
elProt.msMatchesSelector;
export const getUiClass = (em, defCls) => { export const getUiClass = (em, defCls) => {
const { stylePrefix, customUI } = em.getConfig(); const { stylePrefix, customUI } = em.getConfig();
@ -35,10 +27,7 @@ const appendStyles = (styles, opts = {}) => {
if (stls.length) { if (stls.length) {
const href = stls.shift(); const href = stls.shift();
if ( if (href && (!opts.unique || !document.querySelector(`link[href="${href}"]`))) {
href &&
(!opts.unique || !document.querySelector(`link[href="${href}"]`))
) {
const { head } = document; const { head } = document;
const link = document.createElement('link'); const link = document.createElement('link');
link.href = href; link.href = href;
@ -121,8 +110,7 @@ const getUnitFromValue = value => {
const upFirst = value => value[0].toUpperCase() + value.toLowerCase().slice(1); const upFirst = value => value[0].toUpperCase() + value.toLowerCase().slice(1);
const camelCase = value => { const camelCase = value => {
const values = value.split('-').filter(String); return value.replace(/-./g, x => x[1].toUpperCase());
return values[0].toLowerCase() + values.slice(1).map(upFirst);
}; };
const normalizeFloat = (value, step = 1, valueDef = 0) => { const normalizeFloat = (value, step = 1, valueDef = 0) => {
@ -139,10 +127,7 @@ const normalizeFloat = (value, step = 1, valueDef = 0) => {
}; };
const hasDnd = em => { const hasDnd = em => {
return ( return 'draggable' in document.createElement('i') && (em ? em.get('Config').nativeDnD : 1);
'draggable' in document.createElement('i') &&
(em ? em.get('Config').nativeDnD : 1)
);
}; };
/** /**
@ -211,7 +196,7 @@ const getElRect = el => {
top: 0, top: 0,
left: 0, left: 0,
width: 0, width: 0,
height: 0 height: 0,
}; };
if (!el) return def; if (!el) return def;
let rectText; let rectText;
@ -223,9 +208,7 @@ const getElRect = el => {
range.detach(); range.detach();
} }
return ( return rectText || (el.getBoundingClientRect ? el.getBoundingClientRect() : def);
rectText || (el.getBoundingClientRect ? el.getBoundingClientRect() : def)
);
}; };
/** /**
@ -233,8 +216,7 @@ const getElRect = el => {
* @param {Event} ev * @param {Event} ev
* @return {Event} * @return {Event}
*/ */
const getPointerEvent = ev => const getPointerEvent = ev => (ev.touches && ev.touches[0] ? ev.touches[0] : ev);
ev.touches && ev.touches[0] ? ev.touches[0] : ev;
/** /**
* Get cross-browser keycode * Get cross-browser keycode
@ -245,8 +227,7 @@ const getKeyCode = ev => ev.which || ev.keyCode;
const getKeyChar = ev => String.fromCharCode(getKeyCode(ev)); const getKeyChar = ev => String.fromCharCode(getKeyCode(ev));
const isEscKey = ev => getKeyCode(ev) === 27; const isEscKey = ev => getKeyCode(ev) === 27;
const isEnterKey = ev => getKeyCode(ev) === 13; const isEnterKey = ev => getKeyCode(ev) === 13;
const isObject = val => const isObject = val => val !== null && !Array.isArray(val) && typeof val === 'object';
val !== null && !Array.isArray(val) && typeof val === 'object';
const isEmptyObj = val => Object.keys(val).length <= 0; const isEmptyObj = val => Object.keys(val).length <= 0;
const capitalize = str => str && str.charAt(0).toUpperCase() + str.substring(1); const capitalize = str => str && str.charAt(0).toUpperCase() + str.substring(1);
@ -260,8 +241,7 @@ const setViewEl = (el, view) => {
const createId = (length = 16) => { const createId = (length = 16) => {
let result = ''; let result = '';
const chars = const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const len = chars.length; const len = chars.length;
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * len)); result += chars.charAt(Math.floor(Math.random() * len));
@ -296,5 +276,5 @@ export {
isEmptyObj, isEmptyObj,
isComponent, isComponent,
createId, createId,
isRule isRule,
}; };

Loading…
Cancel
Save