|
1 |
| -import { provideStyle, setNativeValue } from './utils' |
| 1 | +import { PropertiesUtils, escapeForRegExp, provideStyle, setNativeValue } from './utils' |
2 | 2 |
|
3 | 3 | import spareBlocksStyle from './css/spare_blocks.css?inline'
|
4 | 4 |
|
@@ -109,6 +109,88 @@ export function improveSearchFeature(toggle: boolean) {
|
109 | 109 | parent.document.removeEventListener('keydown', improveSearch_KeyDownListener, true)
|
110 | 110 | }
|
111 | 111 |
|
| 112 | +/** |
| 113 | + * Edit block on mouse click to reference with ALT/OPT pressed |
| 114 | + */ |
| 115 | +async function improveMouseRefClick_MouseUpListener(e: MouseEvent) { |
| 116 | + if (!e.target) |
| 117 | + return |
| 118 | + |
| 119 | + if (! (e.altKey && !e.shiftKey && !e.metaKey && !e.ctrlKey)) |
| 120 | + return |
| 121 | + |
| 122 | + const target = e.target as HTMLElement |
| 123 | + if (target.tagName !== 'A') |
| 124 | + return |
| 125 | + |
| 126 | + const isTag = target.classList.contains('tag') |
| 127 | + if (!target.classList.contains('page-ref') && !isTag) |
| 128 | + return |
| 129 | + |
| 130 | + const block = target.closest('.block-content') as HTMLElement |
| 131 | + // @ts-expect-error |
| 132 | + const uuid = block.attributes.blockid.value |
| 133 | + if (!uuid) |
| 134 | + return |
| 135 | + |
| 136 | + e.stopPropagation() |
| 137 | + |
| 138 | + let content = (await logseq.Editor.getBlock(uuid))!.content |
| 139 | + content = PropertiesUtils.deletePropertyFromString(content, 'id') |
| 140 | + |
| 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! |
| 150 | + } |
| 151 | + |
| 152 | + if (isTag) |
| 153 | + text = text.slice(1) |
| 154 | + |
| 155 | + // find ref position in block |
| 156 | + const escapedText = escapeForRegExp(text) |
| 157 | + const refText = `\\[\\[${escapedText}\\]\\]` |
| 158 | + |
| 159 | + let startRefPos = -1 |
| 160 | + if (isTag) { |
| 161 | + startRefPos = content.search(new RegExp(`#${escapedText}`, 'u')) |
| 162 | + if (startRefPos !== -1) |
| 163 | + startRefPos += 1 // for # |
| 164 | + |
| 165 | + if (startRefPos === -1) { |
| 166 | + startRefPos = content.search(new RegExp(`#${refText}`, 'u')) |
| 167 | + if (startRefPos !== -1) |
| 168 | + startRefPos += 3 // for #[[ |
| 169 | + } |
| 170 | + } else { |
| 171 | + startRefPos = content.search(new RegExp(refText, 'u')) |
| 172 | + if (startRefPos !== -1) |
| 173 | + startRefPos += 2 // for [[ |
| 174 | + } |
| 175 | + |
| 176 | + if (startRefPos === -1) |
| 177 | + startRefPos = 0 |
| 178 | + |
| 179 | + // find relative click position |
| 180 | + const textSize = (target.textContent ?? '').length || 1 |
| 181 | + const rect = target.getBoundingClientRect() |
| 182 | + const relativePos = e.x - rect.left |
| 183 | + const charPos = Math.round(textSize * relativePos / rect.width) |
| 184 | + |
| 185 | + await logseq.Editor.editBlock(uuid, {pos: startRefPos + charPos}) |
| 186 | +} |
| 187 | +export function improveMouseRefClick(toggle: boolean) { |
| 188 | + if (toggle) |
| 189 | + parent.document.addEventListener('mouseup', improveMouseRefClick_MouseUpListener, true) |
| 190 | + else |
| 191 | + parent.document.removeEventListener('mouseup', improveMouseRefClick_MouseUpListener, true) |
| 192 | +} |
| 193 | + |
112 | 194 | /**
|
113 | 195 | * CSS: Make spare space between 1-level blocks
|
114 | 196 | */
|
|
0 commit comments