positionToCode.js 1006 B

123456789101112131415161718192021222324252627282930313233343536
  1. export const initDom = () => {
  2. if (import.meta.env.MODE === 'development') {
  3. document.onmousedown = function(e) {
  4. if (e.shiftKey && e.altKey && e.button === 0) {
  5. e.preventDefault()
  6. sendRequestToOpenFileInEditor(getFilePath(e))
  7. }
  8. }
  9. }
  10. }
  11. const getFilePath = (e) => {
  12. let element = e
  13. if (e.target) {
  14. element = e.target
  15. }
  16. if (!element || !element.getAttribute) return null
  17. if (element.getAttribute('code-location')) {
  18. return element.getAttribute('code-location')
  19. }
  20. return getFilePath(element.parentNode)
  21. }
  22. const sendRequestToOpenFileInEditor = (filePath) => {
  23. const protocol = window.location.protocol
  24. ? window.location.protocol
  25. : 'http:'
  26. const hostname = window.location.hostname
  27. ? window.location.hostname
  28. : 'localhost'
  29. const port = window.location.port ? window.location.port : '80'
  30. fetch(`${protocol}//${hostname}:${port}/gvaPositionCode?filePath=${filePath}`)
  31. .catch((error) => {
  32. console.log(error)
  33. })
  34. }