This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically focus the WYSIWYG composer when you enter a room (#9412)
Automatically focus the WYSIWYG composer when you enter a room
- Loading branch information
1 parent
13e9e14
commit e38c9e0
Showing
3 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/components/views/rooms/wysiwyg_composer/useWysiwygActionHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { useRef } from "react"; | ||
|
||
import defaultDispatcher from "../../../../dispatcher/dispatcher"; | ||
import { Action } from "../../../../dispatcher/actions"; | ||
import { ActionPayload } from "../../../../dispatcher/payloads"; | ||
import { IRoomState } from "../../../structures/RoomView"; | ||
import { TimelineRenderingType, useRoomContext } from "../../../../contexts/RoomContext"; | ||
import { useDispatcher } from "../../../../hooks/useDispatcher"; | ||
|
||
export function useWysiwygActionHandler( | ||
disabled: boolean, | ||
composerElement: React.MutableRefObject<HTMLElement>, | ||
) { | ||
const roomContext = useRoomContext(); | ||
const timeoutId = useRef<number>(); | ||
|
||
useDispatcher(defaultDispatcher, (payload: ActionPayload) => { | ||
// don't let the user into the composer if it is disabled - all of these branches lead | ||
// to the cursor being in the composer | ||
if (disabled) return; | ||
|
||
const context = payload.context ?? TimelineRenderingType.Room; | ||
|
||
switch (payload.action) { | ||
case "reply_to_event": | ||
case Action.FocusSendMessageComposer: | ||
focusComposer(composerElement, context, roomContext, timeoutId); | ||
break; | ||
// TODO: case Action.ComposerInsert: - see SendMessageComposer | ||
} | ||
}); | ||
} | ||
|
||
function focusComposer( | ||
composerElement: React.MutableRefObject<HTMLElement>, | ||
renderingType: TimelineRenderingType, | ||
roomContext: IRoomState, | ||
timeoutId: React.MutableRefObject<number>, | ||
) { | ||
if (renderingType === roomContext.timelineRenderingType) { | ||
// Immediately set the focus, so if you start typing it | ||
// will appear in the composer | ||
composerElement.current?.focus(); | ||
// If we call focus immediate, the focus _is_ in the right | ||
// place, but the cursor is invisible, presumably because | ||
// some other event is still processing. | ||
// The following line ensures that the cursor is actually | ||
// visible in composer. | ||
if (timeoutId.current) { | ||
clearTimeout(timeoutId.current); | ||
} | ||
timeoutId.current = setTimeout( | ||
() => composerElement.current?.focus(), | ||
200, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters