@@ -119,7 +119,11 @@ async function improveMouseRefClick_MouseUpListener(e: MouseEvent) {
119
119
if ( ! ( e . altKey && ! e . shiftKey && ! e . metaKey && ! e . ctrlKey ) )
120
120
return
121
121
122
- const target = e . target as HTMLElement
122
+ let target = e . target as HTMLElement
123
+ if ( target . classList . contains ( 'awLi-icon' ) ) // Awesome Links: icon element
124
+ target = target . parentElement !
125
+ if ( target . classList . contains ( 'shml-anchor' ) ) // Shorten My Links: anchor element
126
+ target = target . parentElement !
123
127
if ( target . tagName !== 'A' )
124
128
return
125
129
@@ -135,22 +139,30 @@ async function improveMouseRefClick_MouseUpListener(e: MouseEvent) {
135
139
136
140
e . stopPropagation ( )
137
141
142
+ // get block content
138
143
let content = ( await logseq . Editor . getBlock ( uuid ) ) ! . content
139
144
content = PropertiesUtils . deletePropertyFromString ( content , 'id' )
145
+ //
140
146
141
- // get ref text
142
- const walker = document . createTreeWalker ( target , NodeFilter . SHOW_TEXT , null ) !
143
- let text = ''
144
- let node : Node | null
145
- while ( true ) {
146
- node = walker . nextNode ( )
147
- if ( ! node )
148
- break
149
- text = node . textContent !
147
+ // get ref text and its width per char
148
+ const textNode = target . childNodes [ target . childNodes . length - 1 ]
149
+ let text = textNode . textContent ?? ''
150
+
151
+ const rect = target . getBoundingClientRect ( )
152
+ let textWidth = rect . width
153
+ for ( const child of target . childNodes ) {
154
+ const e = child as HTMLElement
155
+ if ( child . nodeType !== document . TEXT_NODE ) {
156
+ textWidth -= e . getBoundingClientRect ( ) . width
157
+ if ( e . classList . contains ( 'awLi-icon' ) )
158
+ textWidth -= 5 // additional margin for icon
159
+ }
150
160
}
151
161
162
+ const widthPerChar = textWidth / ( text . length || 1 )
152
163
if ( isTag )
153
164
text = text . slice ( 1 )
165
+ //
154
166
155
167
// find ref position in block
156
168
const escapedText = escapeForRegExp ( text )
@@ -165,22 +177,34 @@ async function improveMouseRefClick_MouseUpListener(e: MouseEvent) {
165
177
if ( startRefPos !== - 1 )
166
178
startRefPos += 2 // for [[
167
179
}
180
+
181
+ if ( startRefPos !== - 1 )
182
+ startRefPos += 1 // for #
168
183
} else {
169
184
startRefPos = content . search ( new RegExp ( refText , 'u' ) )
170
185
if ( startRefPos !== - 1 )
171
186
startRefPos += 2 // for [[
172
187
}
173
188
189
+ if ( startRefPos === - 1 ) {
190
+ // Shorten My Links plugin integration
191
+ const shortenRefText = `/${ escapedText } \\]\\]`
192
+ startRefPos = content . search ( new RegExp ( shortenRefText , 'u' ) )
193
+ if ( startRefPos !== - 1 )
194
+ startRefPos += 1 // for /
195
+ }
196
+
174
197
if ( startRefPos === - 1 )
175
198
startRefPos = 0
199
+ //
176
200
177
201
// find relative click position
178
- const textSize = ( target . textContent ?? '' ) . length || 1
179
- const rect = target . getBoundingClientRect ( )
180
- const relativePos = e . x - rect . left
181
- const charPos = Math . round ( textSize * relativePos / rect . width )
202
+ const relativeToEndPos = rect . right - e . x
203
+ const charPosRight = Math . round ( relativeToEndPos / widthPerChar )
204
+ const charsOffset = Math . max ( 0 , text . length - charPosRight )
205
+ //
182
206
183
- await logseq . Editor . editBlock ( uuid , { pos : startRefPos + charPos } )
207
+ await logseq . Editor . editBlock ( uuid , { pos : startRefPos + charsOffset } )
184
208
}
185
209
export function improveMouseRefClick ( toggle : boolean ) {
186
210
if ( toggle )
0 commit comments