diff options
Diffstat (limited to 'src/editor.mjs')
-rw-r--r-- | src/editor.mjs | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/editor.mjs b/src/editor.mjs index 2938745..82d93b0 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
@@ -3,6 +3,7 @@ | |||
3 | import { markdown2HTML, generateMaps } from './dumbymap' | 3 | import { markdown2HTML, generateMaps } from './dumbymap' |
4 | import { defaultAliases, parseConfigsFromYaml } from 'mapclay' | 4 | import { defaultAliases, parseConfigsFromYaml } from 'mapclay' |
5 | import * as menuItem from './MenuItem' | 5 | import * as menuItem from './MenuItem' |
6 | import { shiftByWindow } from './utils.mjs' | ||
6 | 7 | ||
7 | // Set up Containers {{{ | 8 | // Set up Containers {{{ |
8 | 9 | ||
@@ -382,7 +383,8 @@ const getSuggestionsForOptions = (optionTyped, validOptions) => { | |||
382 | o => | 383 | o => |
383 | new menuItem.Suggestion({ | 384 | new menuItem.Suggestion({ |
384 | text: `<span>${o.valueOf()}</span><span class='info' title="${o.desc ?? ''}">ⓘ</span>`, | 385 | text: `<span>${o.valueOf()}</span><span class='info' title="${o.desc ?? ''}">ⓘ</span>`, |
385 | replace: `${o.valueOf()}: ` | 386 | replace: `${o.valueOf()}: `, |
387 | cm | ||
386 | }) | 388 | }) |
387 | ) | 389 | ) |
388 | } | 390 | } |
@@ -397,7 +399,8 @@ const getSuggestionFromMapOption = option => { | |||
397 | 399 | ||
398 | return new menuItem.Suggestion({ | 400 | return new menuItem.Suggestion({ |
399 | text, | 401 | text, |
400 | replace: `${option.valueOf()}: ${option.example ?? ''}` | 402 | replace: `${option.valueOf()}: ${option.example ?? ''}`, |
403 | cm | ||
401 | }) | 404 | }) |
402 | } | 405 | } |
403 | // }}} | 406 | // }}} |
@@ -408,7 +411,8 @@ const getSuggestionsFromAliases = option => | |||
408 | const valueString = JSON.stringify(value).replaceAll('"', '') | 411 | const valueString = JSON.stringify(value).replaceAll('"', '') |
409 | return new menuItem.Suggestion({ | 412 | return new menuItem.Suggestion({ |
410 | text: `<span>${alias}</span><span class="truncate" style="color: gray">${valueString}</span>`, | 413 | text: `<span>${alias}</span><span class="truncate" style="color: gray">${valueString}</span>`, |
411 | replace: `${option.valueOf()}: ${valueString}` | 414 | replace: `${option.valueOf()}: ${valueString}`, |
415 | cm | ||
412 | }) | 416 | }) |
413 | }) ?? [] | 417 | }) ?? [] |
414 | // }}} | 418 | // }}} |
@@ -523,7 +527,8 @@ const getSuggestions = anchor => { | |||
523 | ([renderer, info]) => | 527 | ([renderer, info]) => |
524 | new menuItem.Suggestion({ | 528 | new menuItem.Suggestion({ |
525 | text: `<span>use: ${renderer}</span><span class='info' title="${info.desc}">ⓘ</span>`, | 529 | text: `<span>use: ${renderer}</span><span class='info' title="${info.desc}">ⓘ</span>`, |
526 | replace: `use: ${renderer}` | 530 | replace: `use: ${renderer}`, |
531 | cm | ||
527 | }) | 532 | }) |
528 | ) | 533 | ) |
529 | return rendererSuggestions.length > 0 ? rendererSuggestions : [] | 534 | return rendererSuggestions.length > 0 ? rendererSuggestions : [] |
@@ -542,7 +547,6 @@ const addSuggestions = (anchor, suggestions) => { | |||
542 | 547 | ||
543 | menu.innerHTML = '' | 548 | menu.innerHTML = '' |
544 | suggestions | 549 | suggestions |
545 | .map(s => s.createElement(cm)) | ||
546 | .forEach(option => menu.appendChild(option)) | 550 | .forEach(option => menu.appendChild(option)) |
547 | 551 | ||
548 | const widgetAnchor = document.createElement('div') | 552 | const widgetAnchor = document.createElement('div') |
@@ -550,8 +554,8 @@ const addSuggestions = (anchor, suggestions) => { | |||
550 | const rect = widgetAnchor.getBoundingClientRect() | 554 | const rect = widgetAnchor.getBoundingClientRect() |
551 | menu.style.left = `calc(${rect.left}px + 2rem)` | 555 | menu.style.left = `calc(${rect.left}px + 2rem)` |
552 | menu.style.top = `calc(${rect.bottom}px + 1rem)` | 556 | menu.style.top = `calc(${rect.bottom}px + 1rem)` |
553 | menu.style.maxWidth = `calc(${window.innerWidth}px - ${rect.x}px - 3rem)` | ||
554 | menu.style.display = 'block' | 557 | menu.style.display = 'block' |
558 | shiftByWindow(menu) | ||
555 | } | 559 | } |
556 | // }}} | 560 | // }}} |
557 | // EVENT: Suggests for current selection {{{ | 561 | // EVENT: Suggests for current selection {{{ |
@@ -583,7 +587,7 @@ cm.on('keydown', (_, e) => { | |||
583 | ) { return } | 587 | ) { return } |
584 | 588 | ||
585 | // Directly add a newline when no suggestion is selected | 589 | // Directly add a newline when no suggestion is selected |
586 | const currentSuggestion = menu.querySelector('.container__suggestion.focus') | 590 | const currentSuggestion = menu.querySelector('.menu-item.focus') |
587 | if (!currentSuggestion && e.key === 'Enter') return | 591 | if (!currentSuggestion && e.key === 'Enter') return |
588 | 592 | ||
589 | // Override default behavior | 593 | // Override default behavior |
@@ -592,10 +596,10 @@ cm.on('keydown', (_, e) => { | |||
592 | // Suggestion when pressing Tab or Shift + Tab | 596 | // Suggestion when pressing Tab or Shift + Tab |
593 | const nextSuggestion = | 597 | const nextSuggestion = |
594 | currentSuggestion?.nextSibling ?? | 598 | currentSuggestion?.nextSibling ?? |
595 | menu.querySelector('.container__suggestion:first-child') | 599 | menu.querySelector('.menu-item:first-child') |
596 | const previousSuggestion = | 600 | const previousSuggestion = |
597 | currentSuggestion?.previousSibling ?? | 601 | currentSuggestion?.previousSibling ?? |
598 | menu.querySelector('.container__suggestion:last-child') | 602 | menu.querySelector('.menu-item:last-child') |
599 | const focusSuggestion = e.shiftKey ? previousSuggestion : nextSuggestion | 603 | const focusSuggestion = e.shiftKey ? previousSuggestion : nextSuggestion |
600 | 604 | ||
601 | // Current editor selection state | 605 | // Current editor selection state |