11const regex = / # \( \d + \) ! / g;
22
3- const plus_icon = '<path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0Zm1.062 4.312a1 1 0 1 0-2 0v2.75h-2.75a1 1 0 0 0 0 2h2.75v2.75a1 1 0 1 0 2 0v-2.75h2.75a1 1 0 1 0 0-2h-2.75Z"></path>' ;
4- const cross_icon = '<path d="M2.343 13.657A8 8 0 1 1 13.658 2.343 8 8 0 0 1 2.343 13.657ZM6.03 4.97a.751.751 0 0 0-1.042.018.751.751 0 0 0-.018 1.042L6.94 8 4.97 9.97a.749.749 0 0 0 .326 1.275.749.749 0 0 0 .734-.215L8 9.06l1.97 1.97a.749.749 0 0 0 1.275-.326.749.749 0 0 0-.215-.734L9.06 8l1.97-1.97a.749.749 0 0 0-.326-1.275.749.749 0 0 0-.734.215L8 6.94Z"></path>' ;
3+ const plusIcon = '<path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0Zm1.062 4.312a1 1 0 1 0-2 0v2.75h-2.75a1 1 0 0 0 0 2h2.75v2.75a1 1 0 1 0 2 0v-2.75h2.75a1 1 0 1 0 0-2h-2.75Z"></path>' ;
4+ const crossIcon = '<path d="M2.343 13.657A8 8 0 1 1 13.658 2.343 8 8 0 0 1 2.343 13.657ZM6.03 4.97a.751.751 0 0 0-1.042.018.751.751 0 0 0-.018 1.042L6.94 8 4.97 9.97a.749.749 0 0 0 .326 1.275.749.749 0 0 0 .734-.215L8 9.06l1.97 1.97a.749.749 0 0 0 1.275-.326.749.749 0 0 0-.215-.734L9.06 8l1.97-1.97a.749.749 0 0 0-.326-1.275.749.749 0 0 0-.734.215L8 6.94Z"></path>' ;
55
6- const icon_elem = '<svg version="1.1" width="1.0em" height="1.0em" class="sd-octicon sd-octicon-feed-plus sd-text-info code-annotation-icon" viewBox="0 0 16 16" aria-hidden="true">' + plus_icon + '</svg>' ;
6+ const iconTag = '<svg version="1.1" width="1.0em" height="1.0em" class="sd-octicon sd-octicon-feed-plus sd-text-info code-annotation-icon" viewBox="0 0 16 16" aria-hidden="true">' + plusIcon + '</svg>' ;
77
88var selectedAnnotation = null ;
99
10- var selected_icon = null ;
11- var selected_text = null ;
12-
1310/**
1411 * Returns true if an annotation is selected, else false.
1512 */
@@ -40,7 +37,7 @@ function hideSelectedAnnotation() {
4037 */
4138function hideAnnotation ( annotation ) {
4239 const { icon, text} = annotation ;
43- icon . innerHTML = plus_icon ;
40+ icon . innerHTML = plusIcon ;
4441 icon . classList . remove ( "sd-octicon-feed-plus" ) ;
4542 icon . classList . add ( "sd-octicon-x-circle-fill" ) ;
4643 icon . style . zIndex = "0" ;
@@ -51,27 +48,38 @@ function hideAnnotation(annotation) {
5148/**
5249 * Show a code annotation.
5350 */
54- function show_annotation ( annotation ) {
51+ function showAnnotation ( annotation ) {
5552 hideSelectedAnnotation ( ) ;
5653 const { icon, text} = annotation ;
5754
5855 icon . classList . remove ( "sd-octicon-feed-plus" ) ;
5956 icon . classList . add ( "sd-octicon-x-circle-fill" ) ;
6057 icon . style . zIndex = "2" ;
61- icon . innerHTML = cross_icon ;
58+ icon . innerHTML = crossIcon ;
6259
6360 text . style . display = "block" ;
6461 adjustTextPosition ( icon , text ) ;
62+
63+ const pre = icon . closest ( "pre" ) ;
64+ pre . addEventListener ( "scroll" , function ( ) {
65+ adjustTextPosition ( icon , text ) ;
66+ } ) ;
6567
6668 selectAnnotation ( icon , text ) ;
6769}
6870
6971function adjustTextPosition ( icon , text ) {
7072 const iconRect = icon . getBoundingClientRect ( ) ;
71- const offsetX = iconRect . left + 'px' ;
72- const offsetY = ( iconRect . top + window . scrollY ) + 'px' ;
73- text . style . left = 'calc(' + offsetX + ' + var(--code-annotation-offset))' ;
74- text . style . top = 'calc(' + offsetY + ' + var(--code-annotation-offset))' ;
73+ const textRect = text . getBoundingClientRect ( ) ;
74+ let offsetX = iconRect . left ;
75+ let offsetY = ( iconRect . top + window . scrollY ) ;
76+
77+ if ( iconRect . left + textRect . width > window . innerWidth ) {
78+ offsetX = iconRect . left - ( iconRect . left + textRect . width - window . innerWidth + 50 ) ;
79+ }
80+
81+ text . style . left = 'calc(' + offsetX + 'px + var(--code-annotation-offset))' ;
82+ text . style . top = 'calc(' + offsetY + 'px + var(--code-annotation-offset))' ;
7583}
7684
7785function findAssociatedList ( block ) {
@@ -96,7 +104,7 @@ function createAnnotation(id, text) {
96104 icon . setAttribute ( 'viewBox' , '0 0 16 16' ) ;
97105 icon . setAttribute ( 'aria-hidden' , 'true' ) ;
98106 icon . setAttributeNS ( "http://www.w3.org/2000/xmlns/" , "xmlns:xlink" , "http://www.w3.org/1999/xlink" ) ;
99- icon . innerHTML = plus_icon ;
107+ icon . innerHTML = plusIcon ;
100108 icon . id = 'code-annotation-icon-' + id ;
101109
102110 annotation . appendChild ( icon ) ;
@@ -124,7 +132,7 @@ function onClickAnnotation(id) {
124132 const text = document . getElementById ( 'code-annotation-text-' + id ) ;
125133
126134 if ( text . style . display == "none" ) {
127- show_annotation ( { icon, text} ) ;
135+ showAnnotation ( { icon, text} ) ;
128136 } else {
129137 hideAnnotation ( { icon, text} ) ;
130138 }
@@ -155,7 +163,8 @@ document.addEventListener("DOMContentLoaded", function() {
155163} ) ;
156164
157165window . addEventListener ( 'resize' , function ( ) {
158- if ( selected_icon != null && selected_text != null ) {
159- adjustTextPosition ( selected_icon , selected_text ) ;
166+ if ( areSelectedAnnotation ( ) ) {
167+ const { icon, text} = selectedAnnotation ;
168+ adjustTextPosition ( icon , text ) ;
160169 }
161170} ) ;
0 commit comments