diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-06 20:05:50 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-07 16:15:56 +0800 |
| commit | 6d1d37d74d7cd46483684ca99cdf7e7bbf51fcd2 (patch) | |
| tree | 6b9c7ab727c65505d94ed8cf319c048f54eec271 /src | |
| parent | ee7f8fa6c770e6063a0dcedcaadd0e9635b0298e (diff) | |
feat: add menu item for adding map anchors
Diffstat (limited to 'src')
| -rw-r--r-- | src/editor.mjs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/editor.mjs b/src/editor.mjs index 1b31535..d7ae7fe 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
| @@ -237,6 +237,7 @@ if (contentFromHash) { | |||
| 237 | editor.cleanup() | 237 | editor.cleanup() |
| 238 | editor.value(contentFromHash) | 238 | editor.value(contentFromHash) |
| 239 | } | 239 | } |
| 240 | |||
| 240 | // }}} | 241 | // }}} |
| 241 | // Set up logic about editor content {{{ | 242 | // Set up logic about editor content {{{ |
| 242 | 243 | ||
| @@ -411,6 +412,32 @@ const menuForEditor = (event, menu) => { | |||
| 411 | menu.appendChild(switchToEditingMode) | 412 | menu.appendChild(switchToEditingMode) |
| 412 | } | 413 | } |
| 413 | 414 | ||
| 415 | const map = dumbyContainer.querySelector('#' + menu.dataset.map) | ||
| 416 | if (map) { | ||
| 417 | const item = new Item({ | ||
| 418 | text: 'Add Anchor for GeoLinks', | ||
| 419 | onclick: () => { | ||
| 420 | const rect = map.getBoundingClientRect() | ||
| 421 | const [x, y] = map.renderer | ||
| 422 | .unproject([event.x - rect.left, event.y - rect.top]) | ||
| 423 | .map(coord => coord.toFixed(7)) | ||
| 424 | |||
| 425 | let prompt | ||
| 426 | let anchorName | ||
| 427 | let link | ||
| 428 | do { | ||
| 429 | prompt = prompt ? 'Anchor name exists' : 'Name this anchor' | ||
| 430 | anchorName = window.prompt(prompt, `${x}, ${y}`) | ||
| 431 | link = `geo:${y},${x}?xy=${x},${y}&id=${map.id} "${anchorName}"` | ||
| 432 | } | ||
| 433 | while (refLinks.find(({ref}) => ref === anchorName)) | ||
| 434 | |||
| 435 | cm.replaceRange(`\n[${anchorName}]: ${link}`, { line: Infinity }) | ||
| 436 | } | ||
| 437 | }) | ||
| 438 | menu.insertBefore(item, menu.firstChild) | ||
| 439 | } | ||
| 440 | |||
| 414 | // Prevent menu appears outside of window | 441 | // Prevent menu appears outside of window |
| 415 | menu.style.transform = '' | 442 | menu.style.transform = '' |
| 416 | shiftByWindow(menu) | 443 | shiftByWindow(menu) |