File size: 4,422 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
import {
IMAGE_EDITOR_CROP,
IMAGE_EDITOR_COMPUTED_CROP,
IMAGE_EDITOR_ROTATE_COUNTERCLOCKWISE,
IMAGE_EDITOR_FLIP,
IMAGE_EDITOR_SET_ASPECT_RATIO,
IMAGE_EDITOR_SET_DEFAULT_ASPECT_RATIO,
IMAGE_EDITOR_SET_CROP_BOUNDS,
IMAGE_EDITOR_SET_FILE_INFO,
IMAGE_EDITOR_STATE_RESET,
IMAGE_EDITOR_STATE_RESET_ALL,
IMAGE_EDITOR_IMAGE_HAS_LOADED,
} from 'calypso/state/action-types';
import { combineReducers } from 'calypso/state/utils';
import { AspectRatios } from './constants';
export const defaultTransform = {
degrees: 0,
scaleX: 1,
scaleY: 1,
};
export const defaultFileInfo = {
src: '',
fileName: 'default',
mimeType: 'image/png',
title: 'default',
};
export const defaultCropBounds = {
topBound: 0,
leftBound: 0,
bottomBound: 100,
rightBound: 100,
};
export const defaultCrop = {
topRatio: 0,
leftRatio: 0,
widthRatio: 1,
heightRatio: 1,
};
export function hasChanges( state = false, action ) {
switch ( action.type ) {
case IMAGE_EDITOR_SET_ASPECT_RATIO:
case IMAGE_EDITOR_CROP:
case IMAGE_EDITOR_ROTATE_COUNTERCLOCKWISE:
case IMAGE_EDITOR_FLIP:
return true;
case IMAGE_EDITOR_STATE_RESET:
case IMAGE_EDITOR_STATE_RESET_ALL:
return false;
case IMAGE_EDITOR_SET_DEFAULT_ASPECT_RATIO:
case IMAGE_EDITOR_COMPUTED_CROP:
return state;
}
return state;
}
export const originalAspectRatio = ( state = null, action ) => {
switch ( action.type ) {
case IMAGE_EDITOR_IMAGE_HAS_LOADED: {
const { width, height } = action;
return { width, height };
}
case IMAGE_EDITOR_STATE_RESET_ALL:
return null;
}
return state;
};
export function imageIsLoading( state = true, action ) {
switch ( action.type ) {
case IMAGE_EDITOR_IMAGE_HAS_LOADED:
return false;
case IMAGE_EDITOR_STATE_RESET_ALL:
return true;
}
return state;
}
export function fileInfo( state = defaultFileInfo, action ) {
switch ( action.type ) {
case IMAGE_EDITOR_SET_FILE_INFO: {
const { src, fileName, mimeType, title } = action;
return { ...state, src, fileName, mimeType, title };
}
case IMAGE_EDITOR_STATE_RESET_ALL:
return {
...state,
...defaultFileInfo,
};
}
return state;
}
export function transform( state = defaultTransform, action ) {
switch ( action.type ) {
case IMAGE_EDITOR_ROTATE_COUNTERCLOCKWISE:
return Object.assign( {}, state, { degrees: ( state.degrees - 90 ) % 360 } );
case IMAGE_EDITOR_FLIP:
return Object.assign( {}, state, { scaleX: -state.scaleX } );
case IMAGE_EDITOR_STATE_RESET:
case IMAGE_EDITOR_STATE_RESET_ALL:
return Object.assign( {}, defaultTransform );
}
return state;
}
export function cropBounds( state = defaultCropBounds, action ) {
switch ( action.type ) {
case IMAGE_EDITOR_SET_CROP_BOUNDS:
return Object.assign( {}, state, {
topBound: action.topBound,
leftBound: action.leftBound,
bottomBound: action.bottomBound,
rightBound: action.rightBound,
} );
}
return state;
}
export function crop( state = defaultCrop, action ) {
switch ( action.type ) {
case IMAGE_EDITOR_CROP:
case IMAGE_EDITOR_COMPUTED_CROP:
return Object.assign( {}, state, {
topRatio: action.topRatio,
leftRatio: action.leftRatio,
widthRatio: action.widthRatio,
heightRatio: action.heightRatio,
} );
case IMAGE_EDITOR_ROTATE_COUNTERCLOCKWISE:
return {
topRatio: 1 - state.widthRatio - state.leftRatio,
leftRatio: state.topRatio,
widthRatio: state.heightRatio,
heightRatio: state.widthRatio,
};
case IMAGE_EDITOR_FLIP:
return Object.assign( {}, state, {
leftRatio: 1 - state.widthRatio - state.leftRatio,
} );
case IMAGE_EDITOR_STATE_RESET:
case IMAGE_EDITOR_STATE_RESET_ALL:
return Object.assign( {}, defaultCrop );
}
return state;
}
export function aspectRatio( state = AspectRatios.FREE, action ) {
switch ( action.type ) {
case IMAGE_EDITOR_SET_ASPECT_RATIO:
case IMAGE_EDITOR_SET_DEFAULT_ASPECT_RATIO:
return action.ratio;
case IMAGE_EDITOR_STATE_RESET:
case IMAGE_EDITOR_STATE_RESET_ALL: {
const { additionalData = {} } = action;
const { aspectRatio: payloadAspectRatio } = additionalData;
if ( payloadAspectRatio && AspectRatios[ payloadAspectRatio ] ) {
return payloadAspectRatio;
}
return AspectRatios.FREE;
}
}
return state;
}
export default combineReducers( {
hasChanges,
fileInfo,
transform,
cropBounds,
crop,
aspectRatio,
originalAspectRatio,
imageIsLoading,
} );
|