Skip to content

Commit 2a47ac1

Browse files
Chase-PercyKeavon
andauthored
Allow right clicking in text fields (#1207)
* Allow right clicking in text edit * Cleanup --------- Co-authored-by: Keavon Chambers <[email protected]>
1 parent a924cdd commit 2a47ac1

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

frontend/src/components/panels/Document.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@
276276
textInput.focus();
277277
textInput.click();
278278
279+
// Sends the text input element used for interactively editing with the text tool in a custom event
279280
window.dispatchEvent(new CustomEvent("modifyinputfield", { detail: textInput }));
280281
}
281282

frontend/src/io-managers/input.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ export function createInputManager(editor: Editor, dialog: DialogState, document
2121
app?.focus();
2222

2323
let viewportPointerInteractionOngoing = false;
24-
let textInput = undefined as undefined | HTMLDivElement;
24+
let textToolInteractiveInputElement = undefined as undefined | HTMLDivElement;
2525
let canvasFocused = true;
2626

27-
function blurApp(): void {
28-
canvasFocused = false;
29-
}
30-
3127
// Event listeners
3228

3329
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -43,7 +39,7 @@ export function createInputManager(editor: Editor, dialog: DialogState, document
4339
{ target: window, eventName: "wheel", action: (e: WheelEvent) => onWheelScroll(e), options: { passive: false } },
4440
{ target: window, eventName: "modifyinputfield", action: (e: CustomEvent) => onModifyInputField(e) },
4541
{ target: window, eventName: "focusout", action: () => (canvasFocused = false) },
46-
{ target: window.document, eventName: "contextmenu", action: (e: MouseEvent) => e.preventDefault() },
42+
{ target: window.document, eventName: "contextmenu", action: (e: MouseEvent) => onContextMenu(e) },
4743
{ target: window.document, eventName: "fullscreenchange", action: () => fullscreen.fullscreenModeChanged() },
4844
{ target: window.document.body, eventName: "paste", action: (e: ClipboardEvent) => onPaste(e) },
4945
];
@@ -154,7 +150,7 @@ export function createInputManager(editor: Editor, dialog: DialogState, document
154150
const { target } = e;
155151
const isTargetingCanvas = target instanceof Element && target.closest("[data-canvas]");
156152
const inDialog = target instanceof Element && target.closest("[data-dialog-modal] [data-floating-menu-content]");
157-
const inTextInput = target === textInput;
153+
const inTextInput = target === textToolInteractiveInputElement;
158154

159155
if (get(dialog).visible && !inDialog) {
160156
dialog.dismissDialog();
@@ -163,7 +159,7 @@ export function createInputManager(editor: Editor, dialog: DialogState, document
163159
}
164160

165161
if (!inTextInput) {
166-
if (textInput) editor.instance.onChangeText(textInputCleanup(textInput.innerText));
162+
if (textToolInteractiveInputElement) editor.instance.onChangeText(textInputCleanup(textToolInteractiveInputElement.innerText));
167163
else viewportPointerInteractionOngoing = isTargetingCanvas instanceof Element;
168164
}
169165

@@ -179,7 +175,7 @@ export function createInputManager(editor: Editor, dialog: DialogState, document
179175
function onPointerUp(e: PointerEvent): void {
180176
if (!e.buttons) viewportPointerInteractionOngoing = false;
181177

182-
if (!textInput) {
178+
if (!textToolInteractiveInputElement) {
183179
const modifiers = makeKeyboardModifiersBitfield(e);
184180
editor.instance.onMouseUp(e.clientX, e.clientY, e.buttons, modifiers);
185181
}
@@ -188,7 +184,7 @@ export function createInputManager(editor: Editor, dialog: DialogState, document
188184
function onDoubleClick(e: PointerEvent): void {
189185
if (!e.buttons) viewportPointerInteractionOngoing = false;
190186

191-
if (!textInput) {
187+
if (!textToolInteractiveInputElement) {
192188
const modifiers = makeKeyboardModifiersBitfield(e);
193189
editor.instance.onDoubleClick(e.clientX, e.clientY, e.buttons, modifiers);
194190
}
@@ -215,8 +211,16 @@ export function createInputManager(editor: Editor, dialog: DialogState, document
215211
}
216212
}
217213

214+
function onContextMenu(e: MouseEvent): void {
215+
if (!targetIsTextField(e.target || undefined) && e.target !== textToolInteractiveInputElement) {
216+
e.preventDefault();
217+
}
218+
}
219+
220+
// Receives a custom event dispatched when the user begins interactively editing with the text tool.
221+
// We keep a copy of the text input element to check against when it's active for text entry.
218222
function onModifyInputField(e: CustomEvent): void {
219-
textInput = e.detail;
223+
textToolInteractiveInputElement = e.detail;
220224
}
221225

222226
// Window events

0 commit comments

Comments
 (0)