|
| 1 | +// Helper functions for patching SortableHOC taken from the folders addon by GarboMuffin |
| 2 | +import { |
| 3 | + getSortableHOCFromElement, |
| 4 | + verifySortableHOC, |
| 5 | + setReactInternalKey, |
| 6 | +} from "../../libraries/common/cs/patch-SortableHOC.js"; |
| 7 | + |
| 8 | +export default async function ({ addon, console }) { |
| 9 | + // Related to settings |
| 10 | + const SPEED_PRESETS = { |
| 11 | + none: 0, |
| 12 | + slow: 3, |
| 13 | + default: 6, |
| 14 | + fast: 12, |
| 15 | + }; |
| 16 | + let scrollSpeed = SPEED_PRESETS[addon.settings.get("scroll-speed")]; |
| 17 | + |
| 18 | + // indexForPositionOnList taken from https://github.com/scratchfoundation/scratch-gui/blob/develop/src/lib/drag-utils.js |
| 19 | + const indexForPositionOnList = ({ x, y }, boxes, isRtl) => { |
| 20 | + if (boxes.length === 0) return null; |
| 21 | + let index = null; |
| 22 | + const leftEdge = Math.min.apply( |
| 23 | + null, |
| 24 | + boxes.map((b) => b.left) |
| 25 | + ); |
| 26 | + const rightEdge = Math.max.apply( |
| 27 | + null, |
| 28 | + boxes.map((b) => b.right) |
| 29 | + ); |
| 30 | + const topEdge = Math.min.apply( |
| 31 | + null, |
| 32 | + boxes.map((b) => b.top) |
| 33 | + ); |
| 34 | + const bottomEdge = Math.max.apply( |
| 35 | + null, |
| 36 | + boxes.map((b) => b.bottom) |
| 37 | + ); |
| 38 | + for (let n = 0; n < boxes.length; n++) { |
| 39 | + const box = boxes[n]; |
| 40 | + // Construct an "extended" box for each, extending out to infinity if |
| 41 | + // the box is along a boundary. |
| 42 | + let minX = box.left === leftEdge ? -Infinity : box.left; |
| 43 | + let maxX = box.right === rightEdge ? Infinity : box.right; |
| 44 | + const minY = box.top === topEdge ? -Infinity : box.top; |
| 45 | + const maxY = box.bottom === bottomEdge ? Infinity : box.bottom; |
| 46 | + // The last item in the wrapped list gets a right edge at infinity, even |
| 47 | + // if it isn't the farthest right, in RTL mode. In LTR mode, it gets a |
| 48 | + // left edge at infinity. |
| 49 | + if (n === boxes.length - 1) { |
| 50 | + if (isRtl) { |
| 51 | + minX = -Infinity; |
| 52 | + } else { |
| 53 | + maxX = Infinity; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Check if the point is in the bounds. |
| 58 | + if (x >= minX && x <= maxX && y >= minY && y <= maxY) { |
| 59 | + index = n; |
| 60 | + break; // No need to keep looking. |
| 61 | + } |
| 62 | + } |
| 63 | + return index; |
| 64 | + }; |
| 65 | + |
| 66 | + // Here is the original: https://github.com/scratchfoundation/scratch-gui/blob/develop/src/lib/sortable-hoc.jsx |
| 67 | + const patchSortableHOC = (SortableHOC) => { |
| 68 | + // Save original functions |
| 69 | + const originalCWRP = SortableHOC.prototype.componentWillReceiveProps; |
| 70 | + const originalGetMouseOverIndex = SortableHOC.prototype.getMouseOverIndex; |
| 71 | + |
| 72 | + SortableHOC.prototype.componentWillReceiveProps = function (newProps) { |
| 73 | + originalCWRP.call(this, newProps); |
| 74 | + |
| 75 | + // Just call original function if disabled or is a sprite |
| 76 | + if (addon.self.disabled || this.props.dragInfo.dragType === "SPRITE") { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const scrollContainer = this.ref.querySelector('[class*="selector_list-area"]'); |
| 81 | + |
| 82 | + if (newProps.dragInfo.dragging && !this.props.dragInfo.dragging) { |
| 83 | + // When started dragging |
| 84 | + this.initialScrollTop = scrollContainer.scrollTop; |
| 85 | + |
| 86 | + // Add scroll listener |
| 87 | + const onScroll = () => { |
| 88 | + if (this.props.dragInfo.dragging) { |
| 89 | + this.forceUpdate(); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + scrollContainer.addEventListener("scroll", onScroll); |
| 94 | + |
| 95 | + // Save listener so it can be removed later |
| 96 | + this._scrollListener = onScroll; |
| 97 | + } else if (!newProps.dragInfo.dragging && this.props.dragInfo.dragging) { |
| 98 | + // When stopped dragging |
| 99 | + if (this._scrollListener) { |
| 100 | + scrollContainer.removeEventListener("scroll", this._scrollListener); |
| 101 | + this._scrollListener = null; |
| 102 | + } |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + // While dragging |
| 107 | + SortableHOC.prototype.getMouseOverIndex = function () { |
| 108 | + // Just call original function if disabled or is a sprite |
| 109 | + if (addon.self.disabled || this.props.dragInfo.dragType === "SPRITE") { |
| 110 | + return originalGetMouseOverIndex.call(this); |
| 111 | + } |
| 112 | + |
| 113 | + let index = null; |
| 114 | + if (this.props.dragInfo.currentOffset) { |
| 115 | + const scrollContainer = this.ref.querySelector('[class*="selector_list-area"]'); |
| 116 | + const containerRect = scrollContainer.getBoundingClientRect(); |
| 117 | + const x = this.props.dragInfo.currentOffset.x; // x isn't affected |
| 118 | + const y = |
| 119 | + this.props.dragInfo.currentOffset.y + |
| 120 | + scrollContainer.scrollTop - |
| 121 | + containerRect.top - |
| 122 | + this.initialScrollTop + |
| 123 | + 96; |
| 124 | + |
| 125 | + if (this.boxes.length === 0) { |
| 126 | + index = 0; |
| 127 | + } else { |
| 128 | + index = indexForPositionOnList({ x, y }, this.boxes, this.props.isRtl); |
| 129 | + } |
| 130 | + |
| 131 | + // Auto scroll |
| 132 | + const edgeSize = 30; // Distance from the top/bottom to trigger scroll |
| 133 | + if (this.props.dragInfo.currentOffset.y < containerRect.top + edgeSize) { |
| 134 | + scrollContainer.scrollTop -= scrollSpeed; |
| 135 | + } else if (this.props.dragInfo.currentOffset.y > containerRect.bottom - edgeSize) { |
| 136 | + scrollContainer.scrollTop += scrollSpeed; |
| 137 | + } |
| 138 | + } |
| 139 | + return index; |
| 140 | + }; |
| 141 | + }; |
| 142 | + |
| 143 | + // When settings changed |
| 144 | + addon.settings.addEventListener("change", function () { |
| 145 | + scrollSpeed = SPEED_PRESETS[addon.settings.get("scroll-speed")]; |
| 146 | + }); |
| 147 | + |
| 148 | + // Taken from folders addon by GarboMuffin |
| 149 | + const selectorListItem = await addon.tab.waitForElement("[class*='selector_list-area']", { |
| 150 | + reduxCondition: (state) => state.scratchGui.editorTab.activeTabIndex !== 0 && !state.scratchGui.mode.isPlayerOnly, |
| 151 | + }); |
| 152 | + setReactInternalKey(addon.tab.traps.getInternalKey(selectorListItem)); |
| 153 | + const sortableHOCInstance = getSortableHOCFromElement(selectorListItem); |
| 154 | + verifySortableHOC(sortableHOCInstance, true); |
| 155 | + patchSortableHOC(sortableHOCInstance.constructor); |
| 156 | +} |
0 commit comments