viewports.js (4794B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 ADD_VIEWPORT, 9 CHANGE_DEVICE, 10 CHANGE_PIXEL_RATIO, 11 CHANGE_VIEWPORT_ANGLE, 12 EDIT_DEVICE, 13 REMOVE_DEVICE_ASSOCIATION, 14 RESIZE_VIEWPORT, 15 ROTATE_VIEWPORT, 16 ZOOM_VIEWPORT, 17 } = require("resource://devtools/client/responsive/actions/index.js"); 18 19 const VIEWPORT_WIDTH_PREF = "devtools.responsive.viewport.width"; 20 const VIEWPORT_HEIGHT_PREF = "devtools.responsive.viewport.height"; 21 const VIEWPORT_PIXEL_RATIO_PREF = "devtools.responsive.viewport.pixelRatio"; 22 const VIEWPORT_ANGLE_PREF = "devtools.responsive.viewport.angle"; 23 24 let nextViewportId = 0; 25 26 const INITIAL_VIEWPORTS = []; 27 const INITIAL_VIEWPORT = { 28 id: nextViewportId++, 29 angle: Services.prefs.getIntPref(VIEWPORT_ANGLE_PREF, 0), 30 device: "", 31 deviceType: "", 32 height: Services.prefs.getIntPref(VIEWPORT_HEIGHT_PREF, 480), 33 width: Services.prefs.getIntPref(VIEWPORT_WIDTH_PREF, 320), 34 pixelRatio: Services.prefs.getIntPref(VIEWPORT_PIXEL_RATIO_PREF, 0), 35 userContextId: 0, 36 zoom: 1, 37 }; 38 39 const reducers = { 40 [ADD_VIEWPORT](viewports, { userContextId }) { 41 // For the moment, there can be at most one viewport. 42 if (viewports.length === 1) { 43 return viewports; 44 } 45 46 return [ 47 ...viewports, 48 { 49 ...INITIAL_VIEWPORT, 50 userContextId, 51 }, 52 ]; 53 }, 54 55 [CHANGE_DEVICE](viewports, { id, device, deviceType }) { 56 return viewports.map(viewport => { 57 if (viewport.id !== id) { 58 return viewport; 59 } 60 61 return { 62 ...viewport, 63 device, 64 deviceType, 65 }; 66 }); 67 }, 68 69 [CHANGE_PIXEL_RATIO](viewports, { id, pixelRatio }) { 70 return viewports.map(viewport => { 71 if (viewport.id !== id) { 72 return viewport; 73 } 74 75 Services.prefs.setIntPref(VIEWPORT_PIXEL_RATIO_PREF, pixelRatio); 76 77 return { 78 ...viewport, 79 pixelRatio, 80 }; 81 }); 82 }, 83 84 [CHANGE_VIEWPORT_ANGLE](viewports, { id, angle }) { 85 return viewports.map(viewport => { 86 if (viewport.id !== id) { 87 return viewport; 88 } 89 90 Services.prefs.setIntPref(VIEWPORT_ANGLE_PREF, angle); 91 92 return { 93 ...viewport, 94 angle, 95 }; 96 }); 97 }, 98 99 [EDIT_DEVICE](viewports, { viewport, newDevice, deviceType }) { 100 if (!viewport) { 101 return viewports; 102 } 103 104 return viewports.map(v => { 105 if (v.id !== viewport.id) { 106 return viewport; 107 } 108 109 Services.prefs.setIntPref(VIEWPORT_WIDTH_PREF, newDevice.width); 110 Services.prefs.setIntPref(VIEWPORT_HEIGHT_PREF, newDevice.height); 111 Services.prefs.setIntPref( 112 VIEWPORT_PIXEL_RATIO_PREF, 113 newDevice.pixelRatio 114 ); 115 116 return { 117 ...viewport, 118 device: newDevice.name, 119 deviceType, 120 height: newDevice.height, 121 width: newDevice.width, 122 pixelRatio: newDevice.pixelRatio, 123 userAgent: newDevice.userAgent, 124 touch: newDevice.touch, 125 }; 126 }); 127 }, 128 129 [REMOVE_DEVICE_ASSOCIATION](viewports, { id }) { 130 return viewports.map(viewport => { 131 if (viewport.id !== id) { 132 return viewport; 133 } 134 135 return { 136 ...viewport, 137 device: "", 138 deviceType: "", 139 }; 140 }); 141 }, 142 143 [RESIZE_VIEWPORT](viewports, { id, width, height }) { 144 return viewports.map(viewport => { 145 if (viewport.id !== id) { 146 return viewport; 147 } 148 149 if (!height) { 150 height = viewport.height; 151 } 152 153 if (!width) { 154 width = viewport.width; 155 } 156 157 Services.prefs.setIntPref(VIEWPORT_WIDTH_PREF, width); 158 Services.prefs.setIntPref(VIEWPORT_HEIGHT_PREF, height); 159 160 return { 161 ...viewport, 162 height, 163 width, 164 }; 165 }); 166 }, 167 168 [ROTATE_VIEWPORT](viewports, { id }) { 169 return viewports.map(viewport => { 170 if (viewport.id !== id) { 171 return viewport; 172 } 173 174 const height = viewport.width; 175 const width = viewport.height; 176 177 Services.prefs.setIntPref(VIEWPORT_WIDTH_PREF, width); 178 Services.prefs.setIntPref(VIEWPORT_HEIGHT_PREF, height); 179 180 return { 181 ...viewport, 182 height, 183 width, 184 }; 185 }); 186 }, 187 188 [ZOOM_VIEWPORT](viewports, { id, zoom }) { 189 return viewports.map(viewport => { 190 if (viewport.id !== id) { 191 return viewport; 192 } 193 194 if (!zoom) { 195 zoom = viewport.zoom; 196 } 197 198 return { 199 ...viewport, 200 zoom, 201 }; 202 }); 203 }, 204 }; 205 206 module.exports = function (viewports = INITIAL_VIEWPORTS, action) { 207 const reducer = reducers[action.type]; 208 if (!reducer) { 209 return viewports; 210 } 211 return reducer(viewports, action); 212 };