|
36 | 36 | padding: 10px;
|
37 | 37 | tab-size: 4;
|
38 | 38 | }
|
| 39 | + |
| 40 | + .popup { |
| 41 | + background: #fff; |
| 42 | + border-radius: 4px; |
| 43 | + box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2); |
| 44 | + display: none; |
| 45 | + font-family: "Source Code Pro", monospace; |
| 46 | + font-size: 14px; |
| 47 | + font-weight: 400; |
| 48 | + letter-spacing: normal; |
| 49 | + line-height: 20px; |
| 50 | + max-width: 500px; |
| 51 | + overflow: hidden; |
| 52 | + padding: 5px; |
| 53 | + pointer-events: none; |
| 54 | + position: absolute; |
| 55 | + z-index: 2; |
| 56 | + } |
39 | 57 | </style>
|
40 | 58 | </head>
|
41 | 59 | <body>
|
42 | 60 | <main>
|
43 | 61 | <div class="editor language-js"></div>
|
| 62 | + <div class="popup"></div> |
44 | 63 | </main>
|
45 | 64 | <script type="module">
|
46 |
| - import {CodeJar} from "./codejar.js" |
47 |
| - import {withLineNumbers} from "./linenumbers.js" |
| 65 | + import {CodeJar} from './codejar.js' |
| 66 | + import {withLineNumbers} from './linenumbers.js' |
| 67 | + import {cursorPosition, selectedText, textBeforeCursor, textAfterCursor} from './cursor.js' |
| 68 | + |
| 69 | + const editor = document.querySelector('.editor') |
| 70 | + const popup = document.querySelector('.popup') |
| 71 | + |
| 72 | + const updatePopup = () => { |
| 73 | + const pos = cursorPosition() |
| 74 | + popup.style.top = pos.top |
| 75 | + popup.style.left = pos.left |
| 76 | + |
| 77 | + const before = textBeforeCursor(editor).match(/\b\w+$/) |
| 78 | + const after = textAfterCursor(editor).match(/^\w+\b/) |
| 79 | + let word = (before && before[0]) || '' |
| 80 | + word += selectedText() |
| 81 | + word += (after && after[0]) || '' |
| 82 | + if (word) { |
| 83 | + popup.style.display = 'block' |
| 84 | + popup.textContent = word |
| 85 | + } else popup.style.display = 'none' |
| 86 | + } |
48 | 87 |
|
49 |
| - const editor = document.querySelector(".editor") |
| 88 | + editor.addEventListener('click', updatePopup) |
| 89 | + editor.addEventListener('keyup', updatePopup) |
50 | 90 |
|
51 | 91 | const highlight = editor => {
|
52 | 92 | // highlight.js does not trim old tags,
|
|
55 | 95 | hljs.highlightBlock(editor)
|
56 | 96 | }
|
57 | 97 |
|
58 |
| - const jar = new CodeJar(editor, withLineNumbers(highlight), {tab: " "}) |
| 98 | + const jar = new CodeJar(editor, withLineNumbers(highlight), {tab: ' '}) |
59 | 99 |
|
60 |
| - jar.updateCode(localStorage.getItem("code")) |
| 100 | + jar.updateCode(localStorage.getItem('code')) |
61 | 101 | jar.onUpdate(code => {
|
62 |
| - localStorage.setItem("code", code) |
| 102 | + localStorage.setItem('code', code) |
63 | 103 | })
|
64 | 104 | </script>
|
65 | 105 | <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script>
|
|
0 commit comments