viewports.js (2345B)
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 asyncStorage = require("resource://devtools/shared/async-storage.js"); 8 9 const { 10 ADD_VIEWPORT, 11 CHANGE_DEVICE, 12 CHANGE_PIXEL_RATIO, 13 CHANGE_VIEWPORT_ANGLE, 14 REMOVE_DEVICE_ASSOCIATION, 15 RESIZE_VIEWPORT, 16 ROTATE_VIEWPORT, 17 ZOOM_VIEWPORT, 18 } = require("resource://devtools/client/responsive/actions/index.js"); 19 20 module.exports = { 21 /** 22 * Add an additional viewport to display the document. 23 */ 24 addViewport(userContextId = 0) { 25 return { 26 type: ADD_VIEWPORT, 27 userContextId, 28 }; 29 }, 30 31 /** 32 * Change the viewport device. 33 */ 34 changeDevice(id, device, deviceType) { 35 return async function ({ dispatch }) { 36 dispatch({ 37 type: CHANGE_DEVICE, 38 id, 39 device, 40 deviceType, 41 }); 42 43 try { 44 await asyncStorage.setItem("devtools.responsive.deviceState", { 45 id, 46 device, 47 deviceType, 48 }); 49 } catch (e) { 50 console.error(e); 51 } 52 }; 53 }, 54 55 /** 56 * Change the viewport pixel ratio. 57 */ 58 changePixelRatio(id, pixelRatio = 0) { 59 return { 60 type: CHANGE_PIXEL_RATIO, 61 id, 62 pixelRatio, 63 }; 64 }, 65 66 changeViewportAngle(id, angle) { 67 return { 68 type: CHANGE_VIEWPORT_ANGLE, 69 id, 70 angle, 71 }; 72 }, 73 74 /** 75 * Remove the viewport's device assocation. 76 */ 77 removeDeviceAssociation(id, { resetProfile } = {}) { 78 return async function ({ dispatch }) { 79 window.postMessage({ 80 type: "remove-device-association", 81 resetProfile, 82 }); 83 84 dispatch({ 85 type: REMOVE_DEVICE_ASSOCIATION, 86 id, 87 }); 88 89 await asyncStorage.removeItem("devtools.responsive.deviceState"); 90 }; 91 }, 92 93 /** 94 * Resize the viewport. 95 */ 96 resizeViewport(id, width, height) { 97 return { 98 type: RESIZE_VIEWPORT, 99 id, 100 width, 101 height, 102 }; 103 }, 104 105 /** 106 * Rotate the viewport. 107 */ 108 rotateViewport(id) { 109 return { 110 type: ROTATE_VIEWPORT, 111 id, 112 }; 113 }, 114 115 /** 116 * Zoom the viewport. 117 */ 118 zoomViewport(id, zoom) { 119 return { 120 type: ZOOM_VIEWPORT, 121 id, 122 zoom, 123 }; 124 }, 125 };