|
| 1 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 2 | +// See end of file for extended copyright information. |
| 3 | + |
| 4 | +function showErrorMessageBox(mark, posCursorX) { |
| 5 | + const div = createErrorBox( |
| 6 | + mark, |
| 7 | + posCursorX, |
| 8 | + mark.attributes["data-message"].value, |
| 9 | + mark.attributes["data-code"].value, |
| 10 | + mark.attributes["data-severity"].value |
| 11 | + ); |
| 12 | + let body = document.querySelector("body"); |
| 13 | + body.appendChild(div); |
| 14 | +} |
| 15 | + |
| 16 | +function createErrorBox( |
| 17 | + markedElement, |
| 18 | + posCursorX, |
| 19 | + errorMessage, |
| 20 | + code, |
| 21 | + severity |
| 22 | +) { |
| 23 | + // TODO: Change background color based of the severity |
| 24 | + let div = document.createElement("div"); |
| 25 | + const { bottom } = markedElement.getBoundingClientRect(); |
| 26 | + div.setAttribute("id", "error-box"); |
| 27 | + div.innerText = `${code} - ${errorMessage}`; |
| 28 | + div.style.position = "fixed"; |
| 29 | + div.style.overflow = "auto"; |
| 30 | + div.style.top = `${Math.trunc(bottom)}px`; |
| 31 | + div.style.left = `${posCursorX}px`; |
| 32 | + return div; |
| 33 | +} |
| 34 | + |
| 35 | +function removeErrorMessageBox() { |
| 36 | + let errorBoxElement = document.querySelector("#error-box"); |
| 37 | + if (errorBoxElement !== null) { |
| 38 | + errorBoxElement.remove(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function showErrorMessage(event) { |
| 43 | + removeErrorMessageBox(); |
| 44 | + |
| 45 | + const shadowInput = document.querySelector("#shadow-code-input"); |
| 46 | + const marks = shadowInput.querySelectorAll("mark"); |
| 47 | + for (let mark of marks) { |
| 48 | + const markRect = mark.getBoundingClientRect(); |
| 49 | + if (cursorOverMark(event.clientX, event.clientY, markRect)) { |
| 50 | + showErrorMessageBox(mark, event.clientX); |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function cursorOverMark(cursorPosX, cursorPosY, markRect) { |
| 57 | + const topDownIn = markRect.bottom >= cursorPosY && cursorPosY >= markRect.top; |
| 58 | + const leftRightIn = |
| 59 | + cursorPosX >= markRect.left && cursorPosX <= markRect.left + markRect.width; |
| 60 | + return topDownIn && leftRightIn; |
| 61 | +} |
| 62 | + |
| 63 | +document.addEventListener("DOMContentLoaded", () => { |
| 64 | + const codeInput = document.querySelector("#code-input"); |
| 65 | + codeInput.addEventListener("mousemove", showErrorMessage); |
| 66 | + codeInput.addEventListener("input", removeErrorMessageBox); |
| 67 | + codeInput.addEventListener("click", removeErrorMessageBox); |
| 68 | + codeInput.addEventListener("mouseout", removeErrorMessageBox); |
| 69 | +}); |
| 70 | + |
| 71 | +// quick-lint-js finds bugs in JavaScript programs. |
| 72 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 73 | +// |
| 74 | +// This file is part of quick-lint-js. |
| 75 | +// |
| 76 | +// quick-lint-js is free software: you can redistribute it and/or modify |
| 77 | +// it under the terms of the GNU General Public License as published by |
| 78 | +// the Free Software Foundation, either version 3 of the License, or |
| 79 | +// (at your option) any later version. |
| 80 | +// |
| 81 | +// quick-lint-js is distributed in the hope that it will be useful, |
| 82 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 83 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 84 | +// GNU General Public License for more details. |
| 85 | +// |
| 86 | +// You should have received a copy of the GNU General Public License |
| 87 | +// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>. |
0 commit comments