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 833
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9661 from matrix-org/feat/emoji-picker-rich-text-…
…mode Add emoji handling for rich text mode
- Loading branch information
Showing
13 changed files
with
249 additions
and
16 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
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
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
45 changes: 45 additions & 0 deletions
45
src/components/views/rooms/wysiwyg_composer/components/Emoji.tsx
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,45 @@ | ||
/* | ||
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 React from 'react'; | ||
|
||
import { AboveLeftOf } from "../../../../structures/ContextMenu"; | ||
import { EmojiButton } from "../../EmojiButton"; | ||
import dis from '../../../../../dispatcher/dispatcher'; | ||
import { ComposerInsertPayload } from "../../../../../dispatcher/payloads/ComposerInsertPayload"; | ||
import { Action } from "../../../../../dispatcher/actions"; | ||
import { useRoomContext } from "../../../../../contexts/RoomContext"; | ||
|
||
interface EmojiProps { | ||
selectPreviousSelection: () => void; | ||
menuPosition: AboveLeftOf; | ||
} | ||
|
||
export function Emoji({ selectPreviousSelection, menuPosition }: EmojiProps) { | ||
const roomContext = useRoomContext(); | ||
|
||
return <EmojiButton menuPosition={menuPosition} | ||
addEmoji={(emoji) => { | ||
selectPreviousSelection(); | ||
dis.dispatch<ComposerInsertPayload>({ | ||
action: Action.ComposerInsert, | ||
text: emoji, | ||
timelineRenderingType: roomContext.timelineRenderingType, | ||
}); | ||
return true; | ||
}} | ||
/>; | ||
} |
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
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
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
59 changes: 59 additions & 0 deletions
59
src/components/views/rooms/wysiwyg_composer/hooks/useSelection.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,59 @@ | ||
/* | ||
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 { useCallback, useEffect, useRef } from "react"; | ||
|
||
import useFocus from "../../../../../hooks/useFocus"; | ||
import { setSelection } from "../utils/selection"; | ||
|
||
type SubSelection = Pick<Selection, 'anchorNode' | 'anchorOffset' | 'focusNode' | 'focusOffset'>; | ||
|
||
export function useSelection() { | ||
const selectionRef = useRef<SubSelection>({ | ||
anchorNode: null, | ||
anchorOffset: 0, | ||
focusNode: null, | ||
focusOffset: 0, | ||
}); | ||
const [isFocused, focusProps] = useFocus(); | ||
|
||
useEffect(() => { | ||
function onSelectionChange() { | ||
const selection = document.getSelection(); | ||
|
||
if (selection) { | ||
selectionRef.current = { | ||
anchorNode: selection.anchorNode, | ||
anchorOffset: selection.anchorOffset, | ||
focusNode: selection.focusNode, | ||
focusOffset: selection.focusOffset, | ||
}; | ||
} | ||
} | ||
|
||
if (isFocused) { | ||
document.addEventListener('selectionchange', onSelectionChange); | ||
} | ||
|
||
return () => document.removeEventListener('selectionchange', onSelectionChange); | ||
}, [isFocused]); | ||
|
||
const selectPreviousSelection = useCallback(() => { | ||
setSelection(selectionRef.current); | ||
}, [selectionRef]); | ||
|
||
return { ...focusProps, selectPreviousSelection }; | ||
} |
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
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
29 changes: 29 additions & 0 deletions
29
src/components/views/rooms/wysiwyg_composer/utils/selection.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,29 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
export function setSelection(selection: | ||
Pick<Selection, 'anchorNode' | 'anchorOffset' | 'focusNode' | 'focusOffset'>, | ||
) { | ||
if (selection.anchorNode && selection.focusNode) { | ||
const range = new Range(); | ||
range.setStart(selection.anchorNode, selection.anchorOffset); | ||
range.setEnd(selection.focusNode, selection.focusOffset); | ||
|
||
document.getSelection()?.removeAllRanges(); | ||
document.getSelection()?.addRange(range); | ||
} | ||
} | ||
|
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
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