aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/editor.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/editor.mjs')
-rw-r--r--src/editor.mjs18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/editor.mjs b/src/editor.mjs
index ed63f9c..4f500f5 100644
--- a/src/editor.mjs
+++ b/src/editor.mjs
@@ -19,13 +19,14 @@ const refLinkPattern = /\[([^\x5B\x5D]+)\]:\s+(.+)/
19let refLinks = [] 19let refLinks = []
20const validateAnchorName = anchorName => 20const validateAnchorName = anchorName =>
21 !refLinks.find(obj => obj.ref === anchorName) 21 !refLinks.find(obj => obj.ref === anchorName)
22const appendRefLink = ({ cm, ref, link }) => { 22const appendRefLink = (cm, refLink) => {
23 let refLinkString = `\n[${ref}]: ${link}` 23 const {ref, link, title} = refLink
24 let refLinkString = `\n[${ref}]: ${link} "${title ?? ''}"`
24 const lastLineIsRefLink = cm.getLine(cm.lastLine()).match(refLinkPattern) 25 const lastLineIsRefLink = cm.getLine(cm.lastLine()).match(refLinkPattern)
25 if (!lastLineIsRefLink) refLinkString = '\n' + refLinkString 26 if (!lastLineIsRefLink) refLinkString = '\n' + refLinkString
26 cm.replaceRange(refLinkString, { line: Infinity }) 27 cm.replaceRange(refLinkString, { line: Infinity })
27 28
28 refLinks.push({ ref, link }) 29 refLinks.push(refLink)
29} 30}
30/** 31/**
31 * Watch for changes of editing mode 32 * Watch for changes of editing mode
@@ -468,8 +469,8 @@ const menuForEditor = (event, menu) => {
468 const item = new menuItem.Item({ 469 const item = new menuItem.Item({
469 text: 'Add Anchor', 470 text: 'Add Anchor',
470 onclick: (event) => { 471 onclick: (event) => {
471 const { ref, link } = addAnchorByPoint({ point: event, map, validateAnchorName }) 472 const refLink = addAnchorByPoint({ point: event, map, validateAnchorName })
472 appendRefLink({ cm, ref, link }) 473 appendRefLink(cm, refLink)
473 }, 474 },
474 }) 475 })
475 menu.insertBefore(item, menu.firstChild) 476 menu.insertBefore(item, menu.firstChild)
@@ -1146,12 +1147,11 @@ dumbyContainer.onmousedown = (e) => {
1146 return 1147 return
1147 } 1148 }
1148 1149
1149 const { ref, link } = refLink 1150 appendRefLink(cm, refLink)
1150 appendRefLink({ cm, ref, link }) 1151 if (selection === refLink.ref) {
1151 if (selection === ref) {
1152 cm.replaceSelection(`[${selection}]`) 1152 cm.replaceSelection(`[${selection}]`)
1153 } else { 1153 } else {
1154 cm.replaceSelection(`[${selection}][${ref}]`) 1154 cm.replaceSelection(`[${selection}][${refLink.ref}]`)
1155 } 1155 }
1156 } 1156 }
1157} 1157}