Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(attributes): port the detail popup keyboard shortcuts to Preact
Elian Dorande39a824f0e0
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsxindex b8bfe6663a..0dd57dab32 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx@@ -6,6 +6,7 @@ import type { Attribute } from "../../services/attribute_parser.js";import { isExperimentalFeatureEnabled } from "../../services/experimental_features.js";import { focusSavedElement, saveFocusedElement } from "../../services/focus.js";import { t } from "../../services/i18n.js";+import { isIMEComposing } from "../../services/shortcuts.js";import utils from "../../services/utils.js";import NoteContextAwareWidget from "../note_context_aware_widget.js";import Button from "../react/Button.jsx";@@ -143,6 +144,7 @@ interface AttributeDetailProps extends AttributeFormCallbacks {function AttributeDetail({ opts, showId, parentOffset, onDismiss, onCancel, ...formCallbacks }: AttributeDetailProps) {const popupRef = useRef<HTMLDivElement>(null);const shown = !!opts;+ const { onSaveAndClose } = formCallbacks;// Positioning needs the popup's rendered size, so it runs after the DOM is// built but before paint.@@ -185,7 +187,26 @@ function AttributeDetail({ opts, showId, parentOffset, onDismiss, onCancel, ...fconst attrType = getAttrType(opts.attribute);return (- <div ref={popupRef} class="attr-detail tn-tool-dialog">+ <div+ ref={popupRef}+ class="attr-detail tn-tool-dialog"+ // Handled here rather than through the shortcut service so the keys stay scoped+ // to the popup subtree and unbind with it.+ onKeyDown={(e) => {+ if (isIMEComposing(e)) {+ return;+ }++ if (e.key === "Escape") {+ e.stopPropagation();+ onCancel();+ } else if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {+ e.preventDefault();+ e.stopPropagation();+ onSaveAndClose();+ }+ }}+ ><div class="attr-detail-header"><h5 class="attr-detail-title">{attrType ? ATTR_TITLES[attrType] : ""}</h5>diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.tsindex 1e1de2ed66..0c9b153af8 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts@@ -1,14 +1,12 @@import appContext from "../../components/app_context.js";import attributeAutocompleteService from "../../services/attribute_autocomplete.js";import type { Attribute } from "../../services/attribute_parser.js";-import { focusSavedElement } from "../../services/focus.js";import froca from "../../services/froca.js";import { t } from "../../services/i18n.js";import linkService from "../../services/link.js";import noteAutocompleteService from "../../services/note_autocomplete.js";import promotedAttributeDefinitionParser from "../../services/promoted_attribute_definition_parser.js";import server from "../../services/server.js";-import shortcutService from "../../services/shortcuts.js";import SpacedUpdate from "../../services/spaced_update.js";import utils, { openInAppHelpFromUrl } from "../../services/utils.js";import NoteContextAwareWidget from "../note_context_aware_widget.js";@@ -147,9 +145,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {this.$widget = $(TPL);- shortcutService.bindElShortcut(this.$widget, "ctrl+return", () => this.saveAndClose());- shortcutService.bindElShortcut(this.$widget, "esc", () => this.cancelAndClose());-this.$inputName = this.$widget.find(".attr-input-name");this.$inputName.on("autocomplete:closed", () => this.userEditedAttribute());@@ -287,22 +282,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {this.toggleInt(true);}- async saveAndClose() {- await this.triggerCommand("saveAttributes");-- this.hide();-- focusSavedElement();- }-- async cancelAndClose() {- await this.triggerCommand("reloadAttributes");-- this.hide();-- focusSavedElement();- }-userEditedAttribute() {this.updateAttributeInEditor();this.updateHelp();