Skip to content

Commit c84160b

Browse files
committed
feat(features): mouse ref click: plugins integration — shorten my links & awesome links
1 parent 61b065a commit c84160b

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

src/features.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ async function improveMouseRefClick_MouseUpListener(e: MouseEvent) {
119119
if (! (e.altKey && !e.shiftKey && !e.metaKey && !e.ctrlKey))
120120
return
121121

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!
123127
if (target.tagName !== 'A')
124128
return
125129

@@ -135,22 +139,30 @@ async function improveMouseRefClick_MouseUpListener(e: MouseEvent) {
135139

136140
e.stopPropagation()
137141

142+
// get block content
138143
let content = (await logseq.Editor.getBlock(uuid))!.content
139144
content = PropertiesUtils.deletePropertyFromString(content, 'id')
145+
//
140146

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+
}
150160
}
151161

162+
const widthPerChar = textWidth / (text.length || 1)
152163
if (isTag)
153164
text = text.slice(1)
165+
//
154166

155167
// find ref position in block
156168
const escapedText = escapeForRegExp(text)
@@ -165,22 +177,34 @@ async function improveMouseRefClick_MouseUpListener(e: MouseEvent) {
165177
if (startRefPos !== -1)
166178
startRefPos += 2 // for [[
167179
}
180+
181+
if (startRefPos !== -1)
182+
startRefPos += 1 // for #
168183
} else {
169184
startRefPos = content.search(new RegExp(refText, 'u'))
170185
if (startRefPos !== -1)
171186
startRefPos += 2 // for [[
172187
}
173188

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+
174197
if (startRefPos === -1)
175198
startRefPos = 0
199+
//
176200

177201
// 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+
//
182206

183-
await logseq.Editor.editBlock(uuid, {pos: startRefPos + charPos})
207+
await logseq.Editor.editBlock(uuid, {pos: startRefPos + charsOffset})
184208
}
185209
export function improveMouseRefClick(toggle: boolean) {
186210
if (toggle)

0 commit comments

Comments
 (0)