Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show hint when you click on a global without Ctrl down #2449

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions frontend/components/CellInput/go_to_definition_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Facet, ViewPlugin, Decoration, EditorView } from "../../imports/Codemir
import { ctrl_or_cmd_name, has_ctrl_or_cmd_pressed } from "../../common/KeyboardShortcuts.js"
import _ from "../../imports/lodash.js"
import { ScopeStateField } from "./scopestate_statefield.js"
import { open_pluto_popup } from "../Popup.js"
import { html } from "../../imports/Preact.js"

/**
* @param {any} state
Expand Down Expand Up @@ -106,42 +108,50 @@ export const go_to_definition_plugin = ViewPlugin.fromClass(
decorations: (v) => v.decorations,

eventHandlers: {
pointerdown: (event, view) => {
if (has_ctrl_or_cmd_pressed(event) && event.button === 0 && event.target instanceof Element) {
let pluto_variable = event.target.closest("[data-pluto-variable]")
click: (event, view) => {
if (event.button === 0 && event.target instanceof HTMLElement) {
let pluto_variable = /** @type {HTMLElement? }*/ (event.target.closest("[data-pluto-variable]"))
if (pluto_variable) {
let variable = pluto_variable.getAttribute("data-pluto-variable")
if (variable == null) {
return false
}

event.preventDefault()
let scrollto_selector = `[id='${encodeURI(variable)}']`
document.querySelector(scrollto_selector)?.scrollIntoView({
behavior: "smooth",
block: "center",
})
if (has_ctrl_or_cmd_pressed(event)) {
event.preventDefault()
let scrollto_selector = `[id='${encodeURI(variable)}']`
document.querySelector(scrollto_selector)?.scrollIntoView({
behavior: "smooth",
block: "center",
})

// TODO Something fancy where it counts going to definition as a page in history,
// .... so pressing/swiping back will go back to where you clicked on the definition.
// window.history.replaceState({ scrollTop: document.documentElement.scrollTop }, null)
// window.history.pushState({ scrollTo: scrollto_selector }, null)
// TODO Something fancy where it counts going to definition as a page in history,
// .... so pressing/swiping back will go back to where you clicked on the definition.
// window.history.replaceState({ scrollTop: document.documentElement.scrollTop }, null)
// window.history.pushState({ scrollTo: scrollto_selector }, null)

let global_definitions = view.state.facet(GlobalDefinitionsFacet)
let global_definitions = view.state.facet(GlobalDefinitionsFacet)

// TODO Something fancy where we actually emit the identifier we are looking for,
// .... and the cell then selects exactly that definition (using lezer and cool stuff)
if (global_definitions[variable]) {
window.dispatchEvent(
new CustomEvent("cell_focus", {
detail: {
cell_id: global_definitions[variable],
line: 0, // 1-based to 0-based index
definition_of: variable,
},
})
)
return true
// TODO Something fancy where we actually emit the identifier we are looking for,
// .... and the cell then selects exactly that definition (using lezer and cool stuff)
if (global_definitions[variable]) {
window.dispatchEvent(
new CustomEvent("cell_focus", {
detail: {
cell_id: global_definitions[variable],
line: 0, // 1-based to 0-based index
definition_of: variable,
},
})
)
return true
}
} else {
open_pluto_popup({
source_element: pluto_variable.closest(`.cm-line`),
type: "info",
body: html`${ctrl_or_cmd_name}-Click to jump to the definition of <strong>${variable}</strong>.`,
})
}
}

Expand Down