@@ -6,6 +6,12 @@ import { render as discourseOverlayRender } from "~/components/DiscourseContextO
66import { OnloadArgs } from "roamjs-components/types" ;
77import { renderSuggestive as renderSuggestiveOverlay } from "~/components/SuggestiveModeOverlay" ;
88
9+ const PAGE_REF_SELECTOR = "span.rm-page-ref" ;
10+ const DISCOURSE_OVERLAY_CLASS = "roamjs-discourse-context-overlay" ;
11+ const DISCOURSE_OVERLAY_ATTR = "data-roamjs-discourse-overlay" ;
12+ const SUGGESTIVE_OVERLAY_CLASS = "suggestive-mode-overlay" ;
13+ const SUGGESTIVE_OVERLAY_ATTR = "data-discourse-suggestive-overlay" ;
14+
915const pageRefObservers = new Set < ( s : HTMLSpanElement ) => void > ( ) ;
1016const pageRefObserverRef : { current ?: MutationObserver } = {
1117 current : undefined ,
@@ -38,27 +44,27 @@ export const overlayPageRefHandler = (
3844 const tag =
3945 s . getAttribute ( "data-tag" ) ||
4046 s . parentElement . getAttribute ( "data-link-title" ) ;
41- const hasOverlayAttribute = s . getAttribute ( "data-roamjs-discourse-overlay" ) ;
47+ const hasOverlayAttribute = s . getAttribute ( DISCOURSE_OVERLAY_ATTR ) ;
4248 const hasOverlayElement =
4349 ( s . hasAttribute ( "data-tag" ) &&
4450 Array . from ( s . children ) . some (
4551 ( child ) =>
4652 child instanceof HTMLSpanElement &&
47- child . querySelector ( ".roamjs-discourse-context-overlay" ) ,
53+ child . querySelector ( `. ${ DISCOURSE_OVERLAY_CLASS } ` ) ,
4854 ) ) ||
4955 ( s . parentElement &&
5056 Array . from ( s . parentElement . children ) . some (
5157 ( child ) =>
5258 child instanceof HTMLSpanElement &&
53- child . querySelector ( ".roamjs-discourse-context-overlay" ) ,
59+ child . querySelector ( `. ${ DISCOURSE_OVERLAY_CLASS } ` ) ,
5460 ) ) ;
5561 if (
5662 tag &&
5763 ! hasOverlayAttribute &&
5864 ! hasOverlayElement &&
5965 isDiscourseNode ( getPageUidByPageTitle ( tag ) )
6066 ) {
61- s . setAttribute ( "data-roamjs-discourse-overlay" , "true" ) ;
67+ s . setAttribute ( DISCOURSE_OVERLAY_ATTR , "true" ) ;
6268 const parent = document . createElement ( "span" ) ;
6369 discourseOverlayRender ( {
6470 parent,
@@ -84,10 +90,10 @@ export const suggestiveOverlayPageRefHandler = (
8490 s . parentElement . getAttribute ( "data-link-title" ) ;
8591 if (
8692 tag &&
87- ! s . getAttribute ( "data-discourse-suggestive-overlay" ) &&
93+ ! s . getAttribute ( SUGGESTIVE_OVERLAY_ATTR ) &&
8894 isDiscourseNode ( getPageUidByPageTitle ( tag ) )
8995 ) {
90- s . setAttribute ( "data-discourse-suggestive-overlay" , "true" ) ;
96+ s . setAttribute ( SUGGESTIVE_OVERLAY_ATTR , "true" ) ;
9197 const parent = document . createElement ( "span" ) ;
9298 renderSuggestiveOverlay ( {
9399 parent,
@@ -141,19 +147,15 @@ const applyHandlersToExistingPageRefs = (
141147 handler : ( s : HTMLSpanElement ) => void ,
142148) => {
143149 const existingPageRefs =
144- document . querySelectorAll < HTMLSpanElement > ( "span.rm-page-ref" ) ;
150+ document . querySelectorAll < HTMLSpanElement > ( PAGE_REF_SELECTOR ) ;
145151 existingPageRefs . forEach ( ( pageRef ) => {
146152 handler ( pageRef ) ;
147153 } ) ;
148154} ;
149155
150- const removeOverlayElements = (
151- pageRefSelector : string ,
152- overlayClass : string ,
153- attributeName : string ,
154- ) => {
156+ const removeOverlayElements = ( overlayClass : string , attributeName : string ) => {
155157 const allPageRefs =
156- document . querySelectorAll < HTMLSpanElement > ( pageRefSelector ) ;
158+ document . querySelectorAll < HTMLSpanElement > ( PAGE_REF_SELECTOR ) ;
157159 allPageRefs . forEach ( ( pageRef ) => {
158160 const directChildContainer = Array . from ( pageRef . children ) . find (
159161 ( child ) =>
@@ -184,18 +186,10 @@ const removeOverlayElements = (
184186
185187// Queries all page refs (not just attributed ones) to catch cases where attribute is missing
186188const removeOverlaysFromExistingPageRefs = ( ) =>
187- removeOverlayElements (
188- "span.rm-page-ref" ,
189- "roamjs-discourse-context-overlay" ,
190- "data-roamjs-discourse-overlay" ,
191- ) ;
189+ removeOverlayElements ( DISCOURSE_OVERLAY_CLASS , DISCOURSE_OVERLAY_ATTR ) ;
192190
193191const removeSuggestiveOverlaysFromExistingPageRefs = ( ) =>
194- removeOverlayElements (
195- "span.rm-page-ref" ,
196- "suggestive-mode-overlay" ,
197- "data-discourse-suggestive-overlay" ,
198- ) ;
192+ removeOverlayElements ( SUGGESTIVE_OVERLAY_CLASS , SUGGESTIVE_OVERLAY_ATTR ) ;
199193
200194export const onPageRefObserverChange =
201195 ( handler : ( s : HTMLSpanElement ) => void ) => ( b : boolean ) => {
0 commit comments