| 589 | * @returns Edit result with new value/cursor/selection and optional action, or null if event not applicable |
| 590 | */ |
| 591 | export function applyInputEditEvent( |
| 592 | event: ZrevEvent, |
| 593 | ctx: Readonly<{ |
| 594 | id: string; |
| 595 | value: string; |
| 596 | cursor: number; |
| 597 | selectionStart?: number | null; |
| 598 | selectionEnd?: number | null; |
| 599 | multiline?: boolean; |
| 600 | }>, |
| 601 | ): InputEditResult | null { |
| 602 | const id = ctx.id; |
| 603 | const value = ctx.value; |
| 604 | const multiline = ctx.multiline === true; |
| 605 | const selection0 = normalizeInputSelection(value, ctx.selectionStart, ctx.selectionEnd); |
| 606 | const cursorBase = normalizeInputCursor(value, ctx.cursor); |
| 607 | const cursor0 = cursorBase; |
| 608 | const [selectionMin, selectionMax] = selection0 |
| 609 | ? normalizeSelectionRange(selection0) |
| 610 | : [cursor0, cursor0]; |
| 611 | |
| 612 | function result( |
| 613 | nextValue: string, |
| 614 | nextCursor: number, |
| 615 | nextSelectionStart: number | null, |
| 616 | nextSelectionEnd: number | null, |
| 617 | ): InputEditResult { |
| 618 | if (nextValue !== value) { |
| 619 | const action: InputEditAction = Object.freeze({ |
| 620 | id, |
| 621 | action: "input", |
| 622 | value: nextValue, |
| 623 | cursor: nextCursor, |
| 624 | }); |
| 625 | return Object.freeze({ nextValue, nextCursor, nextSelectionStart, nextSelectionEnd, action }); |
| 626 | } |
| 627 | return Object.freeze({ nextValue, nextCursor, nextSelectionStart, nextSelectionEnd }); |
| 628 | } |
| 629 | |
| 630 | if (event.kind === "key") { |
| 631 | if (event.action !== "down" && event.action !== "repeat") return null; |
| 632 | const hasShift = (event.mods & ZR_MOD_SHIFT) !== 0; |
| 633 | const hasCtrl = (event.mods & ZR_MOD_CTRL) !== 0; |
| 634 | |
| 635 | if (event.key === ZR_KEY_A && hasCtrl && !hasShift) { |
| 636 | if (value.length === 0) |
| 637 | return Object.freeze({ |
| 638 | nextValue: value, |
| 639 | nextCursor: 0, |
| 640 | nextSelectionStart: null, |
| 641 | nextSelectionEnd: null, |
| 642 | }); |
| 643 | return Object.freeze({ |
| 644 | nextValue: value, |
| 645 | nextCursor: value.length, |
| 646 | nextSelectionStart: 0, |
| 647 | nextSelectionEnd: value.length, |
| 648 | }); |