diff --git a/assets/js/cell/markdown.js b/assets/js/cell/markdown.js index cbcab9893..37ead1a0c 100644 --- a/assets/js/cell/markdown.js +++ b/assets/js/cell/markdown.js @@ -1,6 +1,6 @@ import marked from "marked"; import morphdom from "morphdom"; -import DOMPurify from 'dompurify'; +import DOMPurify from "dompurify"; /** * Renders markdown content in the given container. diff --git a/assets/js/content_editable/index.js b/assets/js/content_editable/index.js index f8cbe552f..878e0922e 100644 --- a/assets/js/content_editable/index.js +++ b/assets/js/content_editable/index.js @@ -25,6 +25,13 @@ const ContentEditable = { const text = event.clipboardData.getData("text/plain"); document.execCommand("insertText", false, text); }); + + // Unfocus the element on Enter or Escape + this.el.addEventListener("keydown", (event) => { + if (event.key === "Enter" || event.key === "Escape") { + this.el.blur(); + } + }); }, updated() { diff --git a/assets/js/lib/utils.js b/assets/js/lib/utils.js index d16993a05..772507d10 100644 --- a/assets/js/lib/utils.js +++ b/assets/js/lib/utils.js @@ -1,3 +1,10 @@ export function isMacOS() { return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform); } + +export function isEditableElement(element) { + return ( + ["input", "textarea"].includes(element.tagName.toLowerCase()) || + element.contentEditable === "true" + ); +} diff --git a/assets/js/session/index.js b/assets/js/session/index.js index b8ee1da45..d6e722b6d 100644 --- a/assets/js/session/index.js +++ b/assets/js/session/index.js @@ -1,5 +1,5 @@ import { getAttributeOrThrow, parseBoolean } from "../lib/attribute"; -import { isMacOS } from "../lib/utils"; +import { isMacOS, isEditableElement } from "../lib/utils"; import KeyBuffer from "./key_buffer"; /** @@ -42,6 +42,11 @@ const Session = { this.pushEvent("queue_focused_cell_evaluation"); } } else { + if (isEditableElement(event.target)) { + keyBuffer.reset(); + return; + } + keyBuffer.push(event.key); if (keyBuffer.tryMatch(["d", "d"])) { diff --git a/lib/live_book_web/live/cell_component.ex b/lib/live_book_web/live/cell_component.ex index 25efa8d17..0205ee936 100644 --- a/lib/live_book_web/live/cell_component.ex +++ b/lib/live_book_web/live/cell_component.ex @@ -18,8 +18,13 @@ defmodule LiveBookWeb.CellComponent do def render_cell_content(%{cell: %{type: :markdown}} = assigns) do ~L""" <%= if @focused do %> -