@@ -16,6 +16,7 @@ const SELECTION_TARGET_DATA_ATTR = "[data-pinboard-selection-target]";
16
16
interface ButtonPosition {
17
17
top : number ;
18
18
left : number ;
19
+ unRoundedCorner : "bottom-left" | "top-right" | "top-left" | "bottom-right" ;
19
20
}
20
21
21
22
export const NewswiresIntegration = ( ) => {
@@ -49,14 +50,27 @@ export const NewswiresIntegration = () => {
49
50
) ;
50
51
const firstRect = selectionRects [ 0 ] ;
51
52
const lastRect = selectionRects [ selectionRects . length - 1 ] ;
52
- const newFirstButtonCoords = {
53
+ const firstButtonCoords = {
53
54
top : firstRect . y - parentRect . y ,
54
55
left : firstRect . x - parentRect . x ,
55
56
} ;
56
- const newLastButtonCoords = {
57
+ const firstButtonPosition : ButtonPosition = {
58
+ ...firstButtonCoords ,
59
+ unRoundedCorner : `bottom-${
60
+ firstButtonCoords . left > parentRect . width / 2 ? "right" : "left"
61
+ } `,
62
+ } ;
63
+ const lastButtonCoords = {
57
64
top : lastRect . y - parentRect . y + lastRect . height ,
58
65
left : lastRect . x - parentRect . x + lastRect . width - 1 ,
59
66
} ;
67
+ const lastButtonPosition : ButtonPosition = {
68
+ ...lastButtonCoords ,
69
+ unRoundedCorner : `top-${
70
+ lastButtonCoords . left > parentRect . width / 2 ? "right" : "left"
71
+ } `,
72
+ } ;
73
+
60
74
if ( maybeClonedTargetEl ) {
61
75
console . log (
62
76
"selection contains whole target element; contents:" ,
@@ -65,8 +79,8 @@ export const NewswiresIntegration = () => {
65
79
setState ( {
66
80
selectedHTML : maybeClonedTargetEl . innerHTML ,
67
81
containerElement : maybeOriginalTargetEl ,
68
- firstButtonPosition : newFirstButtonCoords ,
69
- lastButtonPosition : newLastButtonCoords ,
82
+ firstButtonPosition,
83
+ lastButtonPosition,
70
84
} ) ;
71
85
} else if (
72
86
maybeOriginalTargetEl ?. contains ( selection . anchorNode ) &&
@@ -81,10 +95,10 @@ export const NewswiresIntegration = () => {
81
95
setState ( {
82
96
selectedHTML : tempEl . innerHTML ,
83
97
containerElement : maybeOriginalTargetEl ,
84
- firstButtonPosition : newFirstButtonCoords ,
85
- lastButtonPosition : newLastButtonCoords ,
98
+ firstButtonPosition,
99
+ lastButtonPosition,
86
100
} ) ;
87
- //TODO might need to clean up tempEl
101
+ //TODO might need to clean up tempEl to avoid memory leak?
88
102
}
89
103
}
90
104
} ;
@@ -107,8 +121,8 @@ export const NewswiresIntegration = () => {
107
121
* [x] check parent node of selection is newswires body text el (maybe add data attribute to body text el)
108
122
* - (find first shared parent of anchorNode and focusNode, make sure we're not sharing bits of text outside of the target)
109
123
* [x] extract HTML from selection (see chat thread)
110
- * [ ] render button when there's a selection
111
- * [ ] do things with pinboard
124
+ * [x ] render button when there's a selection
125
+ * [x ] do things with pinboard
112
126
*/
113
127
return ( ) =>
114
128
document . removeEventListener (
@@ -157,10 +171,14 @@ export const NewswiresIntegration = () => {
157
171
left : ${ buttonCoords . left } px;
158
172
transform : translate (
159
173
${
160
- buttonCoords === state . firstButtonPosition
161
- ? "0, -100%"
162
- : "-100%, 0"
163
- }
174
+ buttonCoords . unRoundedCorner . includes ( "left" )
175
+ ? "0"
176
+ : "-100%"
177
+ } , ${
178
+ buttonCoords . unRoundedCorner . includes ( "bottom" )
179
+ ? "-100%"
180
+ : "0"
181
+ }
164
182
);
165
183
display : flex;
166
184
align-items : center;
@@ -169,11 +187,7 @@ export const NewswiresIntegration = () => {
169
187
box- shadow: ${ boxShadow } ;
170
188
bor der: none;
171
189
border-radius : 100px ;
172
- border-${
173
- buttonCoords === state . firstButtonPosition
174
- ? "bottom-left"
175
- : "top-right"
176
- } -radius: 0 ;
190
+ border-${ buttonCoords . unRoundedCorner } -radius : 0 ;
177
191
padding : 0 ${ space [ 2 ] } px 0 ${ space [ 3 ] } px;
178
192
line-height : 2 ;
179
193
cursor : pointer;
0 commit comments