aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/editor.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/editor.mjs')
-rw-r--r--src/editor.mjs53
1 files changed, 23 insertions, 30 deletions
diff --git a/src/editor.mjs b/src/editor.mjs
index 499d42b..1c444eb 100644
--- a/src/editor.mjs
+++ b/src/editor.mjs
@@ -4,6 +4,7 @@ import { markdown2HTML, generateMaps } from './dumbymap'
4import { defaultAliases, parseConfigsFromYaml } from 'mapclay' 4import { defaultAliases, parseConfigsFromYaml } from 'mapclay'
5import * as menuItem from './MenuItem' 5import * as menuItem from './MenuItem'
6import { shiftByWindow } from './utils.mjs' 6import { shiftByWindow } from './utils.mjs'
7import { addAnchorByEvent } from './dumbyUtils.mjs'
7 8
8// Set up Containers {{{ 9// Set up Containers {{{
9 10
@@ -47,7 +48,7 @@ const toggleEditing = () => {
47// Content values for editor 48// Content values for editor
48 49
49const defaultContent = 50const defaultContent =
50`<br> 51 `<br>
51 52
52> <big>Hello My Friend! This is DumbyMap!</big> 53> <big>Hello My Friend! This is DumbyMap!</big>
53 54
@@ -441,30 +442,21 @@ const menuForEditor = (event, menu) => {
441 if (map) { 442 if (map) {
442 const item = new menuItem.Item({ 443 const item = new menuItem.Item({
443 text: 'Add Anchor', 444 text: 'Add Anchor',
444 onclick: () => { 445 onclick: (event) => {
445 const rect = map.getBoundingClientRect() 446 const validateAnchorName = anchorName =>
446 const [x, y] = map.renderer 447 !refLinks.find(obj => obj.ref === anchorName)
447 .unproject([event.x - rect.left, event.y - rect.top]) 448 const { ref, link } = addAnchorByEvent({
448 .map(coord => coord.toFixed(7)) 449 event,
449 450 map,
450 let prompt 451 validateAnchorName
451 let anchorName 452 })
452
453 do {
454 prompt = prompt ? 'Anchor name exists' : 'Name this anchor'
455 anchorName = window.prompt(prompt, `${x}, ${y}`)
456 }
457 while (anchorName !== null && refLinks.find(({ ref }) => ref === anchorName))
458 if (anchorName === null) return
459 453
460 const link = `geo:${y},${x}?xy=${x},${y}&id=${map.id} "${anchorName}"` 454 let refLinkString = `\n[${ref}]: ${link}`
461 const lastLineIsRefLink = cm.getLine(cm.lastLine()).match(refLinkPattern) 455 const lastLineIsRefLink = cm.getLine(cm.lastLine()).match(refLinkPattern)
462 cm.replaceRange( 456 if (lastLineIsRefLink) refLinkString = '\n' + refLinkString
463 `${lastLineIsRefLink ? '' : '\n'}\n[${anchorName}]: ${link}`, 457 cm.replaceRange(refLinkString, { line: Infinity })
464 { line: Infinity } 458
465 ) 459 refLinks.push({ ref, link })
466 refLinks = getRefLinks()
467 map.renderer.addMarker({ xy: [Number(x), Number(y)], title: `${map.id}@${x},${y}`, type: 'circle' })
468 } 460 }
469 }) 461 })
470 menu.insertBefore(item, menu.firstChild) 462 menu.insertBefore(item, menu.firstChild)
@@ -802,13 +794,13 @@ const getSuggestions = anchor => {
802 return rendererSuggestions.length === 0 794 return rendererSuggestions.length === 0
803 ? [] 795 ? []
804 : [ 796 : [
805 ...rendererSuggestions, 797 ...rendererSuggestions,
806 new menuItem.Item({ 798 new menuItem.Item({
807 innerHTML: '<a href="https://github.com/outdoorsafetylab/mapclay#renderer" class="external" style="display: block;">More...</a>', 799 innerHTML: '<a href="https://github.com/outdoorsafetylab/mapclay#renderer" class="external" style="display: block;">More...</a>',
808 className: ['suggestion'], 800 className: ['suggestion'],
809 onclick: () => window.open('https://github.com/outdoorsafetylab/mapclay#renderer', '_blank') 801 onclick: () => window.open('https://github.com/outdoorsafetylab/mapclay#renderer', '_blank')
810 }) 802 })
811 ] 803 ]
812 } 804 }
813 return [] 805 return []
814} 806}
@@ -1076,4 +1068,5 @@ document.addEventListener('selectionchange', () => {
1076 cm.setSelection({ ...anchor, ch: anchor.ch - content.length }, anchor) 1068 cm.setSelection({ ...anchor, ch: anchor.ch - content.length }, anchor)
1077 } 1069 }
1078}) 1070})
1071
1079// vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}} 1072// vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}}