|
| 1 | +--- |
| 2 | +to: ../../output/power-apps/<%= name %>/DB<%= h.capitalize(name) %>/utils.ts |
| 3 | +force: true |
| 4 | +--- |
| 5 | +const isDev = () => !!document.querySelector('[class="harness-root"]'); |
| 6 | +const isProdEditor = () => document.querySelector('[class="studio web"]'); |
| 7 | +const getControlSideBar = () => |
| 8 | + document.querySelector( |
| 9 | + isDev() ? '[class="io-pane"]' : '[id="control-sidebar"]' |
| 10 | + ); |
| 11 | + |
| 12 | +export const initMutationObserver = async () => { |
| 13 | + if (isDev() || isProdEditor()) { |
| 14 | + let initCanvasTries = 0; |
| 15 | + let element: Element | null = null; |
| 16 | + while (!element && initCanvasTries < 5) { |
| 17 | + element = getControlSideBar(); |
| 18 | + initCanvasTries++; |
| 19 | + if (element) { |
| 20 | + await new Promise((resolve) => setTimeout(resolve, 200)); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + if (element && !element.hasAttribute('mutation-observer')) { |
| 25 | + const config = { |
| 26 | + childList: true, |
| 27 | + subtree: true |
| 28 | + }; |
| 29 | + const callback = (mutationList: MutationRecord[]) => { |
| 30 | + // SideBar has a change try to find the canvasSize Elements |
| 31 | + startCustomControlProcess(); |
| 32 | + }; |
| 33 | + const observer = new MutationObserver(callback); |
| 34 | + observer.observe(element, config); |
| 35 | + element.setAttribute('mutation-observer', 'true'); |
| 36 | + startCustomControlProcess(); |
| 37 | + } |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +const isDBUIElementSelected = () => { |
| 42 | + const sideBarDBUIInputs = |
| 43 | + getControlSideBar()?.querySelectorAll('[id*="DBUI"]'); |
| 44 | + const currentSelectedControlContent = document.getElementById( |
| 45 | + 'currentSelectedControl' |
| 46 | + )?.textContent; |
| 47 | + |
| 48 | + if (sideBarDBUIInputs && currentSelectedControlContent) { |
| 49 | + return ( |
| 50 | + currentSelectedControlContent.includes('DBUI') && |
| 51 | + sideBarDBUIInputs.length > 0 |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + return false; |
| 56 | +}; |
| 57 | + |
| 58 | +export const startCustomControlProcess = () => { |
| 59 | + if (isDev() || isProdEditor()) { |
| 60 | + if (isDev() || isDBUIElementSelected()) { |
| 61 | + let customControl; |
| 62 | + |
| 63 | + if (isDev()) { |
| 64 | + customControl = document.querySelector('[class*="customControl"]'); |
| 65 | + } else { |
| 66 | + const selectedContainers = getSelectedContainers(); |
| 67 | + if (selectedContainers?.length >= 2) { |
| 68 | + customControl = selectedContainers[0].querySelector( |
| 69 | + '[class*="customControl"]' |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (customControl) { |
| 75 | + const canvasHeightState = customControl.getAttribute( |
| 76 | + 'data-canvas-height-state' |
| 77 | + ); |
| 78 | + const canvasWidthState = customControl.getAttribute( |
| 79 | + 'data-canvas-width-state' |
| 80 | + ); |
| 81 | + handleCanvasSize( |
| 82 | + customControl, |
| 83 | + canvasHeightState, |
| 84 | + canvasWidthState |
| 85 | + ); |
| 86 | + } |
| 87 | + } else { |
| 88 | + const canvasSizeElements = getCanvasSizeElements(); |
| 89 | + if (canvasSizeElements?.length === 2) { |
| 90 | + const canvasWidthElement = canvasSizeElements[0]; |
| 91 | + const canvasHeightElement = canvasSizeElements[1]; |
| 92 | + if (canvasHeightElement) { |
| 93 | + canvasHeightElement.disabled = false; |
| 94 | + } |
| 95 | + if (canvasWidthElement) { |
| 96 | + canvasWidthElement.disabled = false; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +const getReactEventHandlers = (element: any): any | undefined => { |
| 104 | + const keys = Object.keys(element); |
| 105 | + const foundEventHandlersKey = keys.find((key) => |
| 106 | + key.includes('__reactEventHandlers') |
| 107 | + ); |
| 108 | + if (foundEventHandlersKey) { |
| 109 | + return element[foundEventHandlersKey]; |
| 110 | + } |
| 111 | + |
| 112 | + return undefined; |
| 113 | +}; |
| 114 | + |
| 115 | +const getSelectedContainers = (): any => { |
| 116 | + const selectedContainer: any = document.querySelector( |
| 117 | + '[class*="selectedAdornersContainer"]' |
| 118 | + ); |
| 119 | + |
| 120 | + if (selectedContainer) { |
| 121 | + const foundEventHandlers = getReactEventHandlers(selectedContainer); |
| 122 | + if (foundEventHandlers) { |
| 123 | + const dataControlId = foundEventHandlers['data-control-id']; |
| 124 | + return document.querySelectorAll( |
| 125 | + `[data-control-id="${dataControlId}"]` |
| 126 | + ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return undefined; |
| 131 | +}; |
| 132 | + |
| 133 | +const changeCanvasSize = (element: any, size: number) => { |
| 134 | + const keys = Object.keys(element); |
| 135 | + if (keys) { |
| 136 | + if (isDev()) { |
| 137 | + const foundReactPropsKey = keys.find((key) => |
| 138 | + key.includes('__reactProps') |
| 139 | + ); |
| 140 | + if (foundReactPropsKey) { |
| 141 | + element[foundReactPropsKey].onChange({ |
| 142 | + target: { value: (size + 1).toString() } |
| 143 | + }); |
| 144 | + } |
| 145 | + } else { |
| 146 | + const foundEventHandlers = getReactEventHandlers(element); |
| 147 | + |
| 148 | + if (foundEventHandlers) { |
| 149 | + foundEventHandlers.onBlur({ |
| 150 | + currentTarget: { value: size.toString() } |
| 151 | + }); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +const getCanvasSizeElements = (): HTMLInputElement[] => { |
| 158 | + const devControlContainer = document.getElementById('control-dimensions'); |
| 159 | + let inputs; |
| 160 | + if (devControlContainer) { |
| 161 | + inputs = Array.from(devControlContainer.getElementsByTagName('input')); |
| 162 | + } else { |
| 163 | + inputs = document.querySelectorAll('input[id*="SizeGroup"]'); |
| 164 | + } |
| 165 | + |
| 166 | + return inputs as HTMLInputElement[]; |
| 167 | +}; |
| 168 | + |
| 169 | +const handleCanvasSize = ( |
| 170 | + customControl: any, |
| 171 | + canvasHeightState: string, |
| 172 | + canvasWidthState: string |
| 173 | +): void => { |
| 174 | + const canvasSizeElements = getCanvasSizeElements(); |
| 175 | + if (canvasSizeElements?.length === 2) { |
| 176 | + const canvasWidthElement = canvasSizeElements[0]; |
| 177 | + const canvasHeightElement = canvasSizeElements[1]; |
| 178 | + |
| 179 | + if (canvasHeightElement) { |
| 180 | + if (canvasHeightState !== 'fixed') { |
| 181 | + canvasHeightElement.disabled = false; |
| 182 | + canvasHeightElement.type = 'number'; |
| 183 | + // TODO: Add min size for this based on config |
| 184 | + customControl.style.height = '100%'; |
| 185 | + } else { |
| 186 | + customControl.style.height = 'fit-content'; |
| 187 | + changeCanvasSize( |
| 188 | + canvasHeightElement, |
| 189 | + customControl.offsetHeight |
| 190 | + ); |
| 191 | + canvasHeightElement.disabled = true; |
| 192 | + //hideResizers(true); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + if (canvasWidthElement) { |
| 197 | + const unchangeable = |
| 198 | + canvasWidthState === 'fixed' || |
| 199 | + (canvasWidthState === 'dynamic' && |
| 200 | + customControl.querySelector('[data-width="auto"]')); |
| 201 | + if (unchangeable) { |
| 202 | + customControl.style.width = 'fit-content'; |
| 203 | + changeCanvasSize(canvasWidthElement, customControl.offsetWidth); |
| 204 | + canvasWidthElement.disabled = true; |
| 205 | + //hideResizers(false); |
| 206 | + } else { |
| 207 | + canvasWidthElement.disabled = false; |
| 208 | + canvasWidthElement.type = 'number'; |
| 209 | + // TODO: Add min size for this based on config |
| 210 | + customControl.style.width = '100%'; |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | +}; |
| 215 | + |
| 216 | +/*const hideResizers = (isHeight: boolean) => { |
| 217 | + if (isProdEditor()) { |
| 218 | + const resizers = document.querySelectorAll("[class^='resizerCommon']"); |
| 219 | + |
| 220 | + if (resizers?.length === 8) { |
| 221 | + Array.from(resizers) |
| 222 | + .filter((_, index: number) => { |
| 223 | + if (isHeight) { |
| 224 | + return index !== 2 && index !== 3; |
| 225 | + } else { |
| 226 | + return index !== 0 && index !== 1; |
| 227 | + } |
| 228 | + }) |
| 229 | + .forEach((resizer) => resizer.setAttribute('hidden', '')); |
| 230 | + } |
| 231 | + } |
| 232 | +};*/ |
| 233 | + |
| 234 | +export default { initMutationObserver, startCustomControlProcess }; |
0 commit comments