This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 823
Automatically focus the WYSIWYG composer when you enter a room #9412
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e8f7be9
Automatically focus the WYSIWYG composer when you enter a room
andybalaam f3e6ce9
Cancel the composer focus timeout
andybalaam 8c24132
Extract wysiwyg dispatcher logic into a separate hook
andybalaam 596d0c9
Test that composer gets focus when the relevant action happens
andybalaam b501f44
Merge develop into auto-focus-wysiwyg (using imerge)
andybalaam bc0bb0b
Pass a dummy onChange method to customRender
andybalaam a6e399d
Remove unneeded .container
andybalaam b071289
Improve test coverage
andybalaam 07128a2
Merge branch 'develop' into auto-focus-wysiwyg
andybalaam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
74 changes: 74 additions & 0 deletions
74
src/components/views/rooms/wysiwyg_composer/useWysiwygActionHandler.ts
This file contains hidden or 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,74 @@ | ||
/* | ||
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 } from "../../../../contexts/RoomContext"; | ||
import { useDispatcher } from "../../../../hooks/useDispatcher"; | ||
import { useRoomContext } from "../../../../contexts/RoomContext"; | ||
|
||
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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.