|
| 1 | +const reg = /^\-/; |
| 2 | + |
| 3 | +function getOffset(h, v, offset) { |
| 4 | + if (offset[0]) { |
| 5 | + h += reg.test(offset[0]) ? offset[0] : "+" + offset[0]; |
| 6 | + } |
| 7 | + |
| 8 | + if (offset[1]) { |
| 9 | + v += reg.test(offset[1]) ? offset[1] : "+" + offset[1]; |
| 10 | + } |
| 11 | + |
| 12 | + return [h, v].join(" "); |
| 13 | +} |
| 14 | + |
| 15 | +// at: point(at) |
| 16 | +// my: point(at) position of target |
| 17 | + |
| 18 | +type Placements = |
| 19 | + | "center" |
| 20 | + | "centerCenter" |
| 21 | + | "left" |
| 22 | + | "top" |
| 23 | + | "right" |
| 24 | + | "bottom" |
| 25 | + | "leftCenter" |
| 26 | + | "rightCenter" |
| 27 | + | "topCenter" |
| 28 | + | "bottomCenter" |
| 29 | + | "topLeft" |
| 30 | + | "leftTop" |
| 31 | + | "topRight" |
| 32 | + | "rightTop" |
| 33 | + | "bottomRight" |
| 34 | + | "rightBottom" |
| 35 | + | "bottomLeft" |
| 36 | + | "leftBottom"; |
| 37 | + |
| 38 | +type Offset = [number, number]; |
| 39 | + |
| 40 | +const placements = { |
| 41 | + center: function (offset: Offset) { |
| 42 | + return { |
| 43 | + at: getOffset("center", "center", offset), |
| 44 | + my: "center center", |
| 45 | + }; |
| 46 | + }, |
| 47 | + centerCenter: function (offset: Offset) { |
| 48 | + return { |
| 49 | + at: getOffset("center", "center", offset), |
| 50 | + my: "center center", |
| 51 | + }; |
| 52 | + }, |
| 53 | + left: function (offset: Offset) { |
| 54 | + return this.leftCenter(offset); |
| 55 | + }, |
| 56 | + top: function (offset: Offset) { |
| 57 | + return this.topCenter(offset); |
| 58 | + }, |
| 59 | + right: function (offset: Offset) { |
| 60 | + return this.rightCenter(offset); |
| 61 | + }, |
| 62 | + bottom: function (offset: Offset) { |
| 63 | + return this.bottomCenter(offset); |
| 64 | + }, |
| 65 | + leftCenter: function (offset: Offset) { |
| 66 | + return { |
| 67 | + at: getOffset("left", "center", offset), |
| 68 | + my: "right center", |
| 69 | + }; |
| 70 | + }, |
| 71 | + rightCenter: function (offset: Offset) { |
| 72 | + return { |
| 73 | + at: getOffset("right", "center", offset), |
| 74 | + my: "left center", |
| 75 | + }; |
| 76 | + }, |
| 77 | + topCenter: function (offset: Offset) { |
| 78 | + return { |
| 79 | + at: getOffset("center", "top", offset), |
| 80 | + my: "center bottom", |
| 81 | + }; |
| 82 | + }, |
| 83 | + bottomCenter: function (offset: Offset) { |
| 84 | + return { |
| 85 | + at: getOffset("center", "bottom", offset), |
| 86 | + my: "center top", |
| 87 | + }; |
| 88 | + }, |
| 89 | + topLeft: function (offset: Offset) { |
| 90 | + return { |
| 91 | + at: getOffset("left", "top", offset), |
| 92 | + my: "left bottom", |
| 93 | + }; |
| 94 | + }, |
| 95 | + leftTop: function (offset: Offset) { |
| 96 | + return { |
| 97 | + at: getOffset("left", "top", offset), |
| 98 | + my: "right top", |
| 99 | + }; |
| 100 | + }, |
| 101 | + topRight: function (offset: Offset) { |
| 102 | + return { |
| 103 | + at: getOffset("right", "top", offset), |
| 104 | + my: "right bottom", |
| 105 | + }; |
| 106 | + }, |
| 107 | + rightTop: function (offset: Offset) { |
| 108 | + return { |
| 109 | + at: getOffset("right", "top", offset), |
| 110 | + my: "left top", |
| 111 | + }; |
| 112 | + }, |
| 113 | + bottomRight: function (offset: Offset) { |
| 114 | + return { |
| 115 | + at: getOffset("right", "bottom", offset), |
| 116 | + my: "right top", |
| 117 | + }; |
| 118 | + }, |
| 119 | + rightBottom: function (offset: Offset) { |
| 120 | + return { |
| 121 | + at: getOffset("right", "bottom", offset), |
| 122 | + my: "left bottom", |
| 123 | + }; |
| 124 | + }, |
| 125 | + bottomLeft: function (offset: Offset) { |
| 126 | + return { |
| 127 | + at: getOffset("left", "bottom", offset), |
| 128 | + my: "left top", |
| 129 | + }; |
| 130 | + }, |
| 131 | + leftBottom: function (offset: Offset) { |
| 132 | + return { |
| 133 | + at: getOffset("left", "bottom", offset), |
| 134 | + my: "right bottom", |
| 135 | + }; |
| 136 | + }, |
| 137 | +}; |
| 138 | + |
| 139 | +export default function getPlacement(placement: Placements, offset?: [number, number]) { |
| 140 | + return placements[placement] ? placements[placement](offset || [0, 0]) : null; |
| 141 | +} |
0 commit comments