-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Disable RTE formatting buttons when the content contains a slash command #30802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
langleyd
merged 5 commits into
develop
from
langleyd/disable-rte-formatting-buttons-for-slash-commands
Sep 17, 2025
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4fbecf
Add ability to disable all formatting buttons
langleyd 50b0d4d
Create hook to check if the content contains a slash command
langleyd 36188d5
Disable the formatting buttons if the message content contains a slas…
langleyd e7e95b0
lint
langleyd f7baa28
typo
langleyd 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
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
53 changes: 53 additions & 0 deletions
53
src/components/views/rooms/wysiwyg_composer/hooks/useContainsCommand.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,53 @@ | ||
| /* | ||
| Copyright 2025 New Vector Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import { type Room } from "matrix-js-sdk/src/matrix"; | ||
| import { useEffect, useState, useRef } from "react"; | ||
|
|
||
| import CommandProvider from "../../../../../autocomplete/CommandProvider"; | ||
|
|
||
| /** | ||
| * A hook which determines if the given content contains a slash command. | ||
| * @returns true if the content contains a slash command, false otherwise. | ||
| * @param content The content to check for commands. | ||
| * @param room The current room. | ||
| */ | ||
| export function useContainsCommand(content: string | null, room: Room | undefined): boolean { | ||
| const [contentContainsCommands, setContentContainsCommands] = useState(false); | ||
| const providerRef = useRef<CommandProvider | null>(null); | ||
| const currentRoomIdRef = useRef<string | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!room || !content) { | ||
| setContentContainsCommands(false); | ||
| return; | ||
| } | ||
|
|
||
| // Create or reuse CommandProvider for the current room | ||
| if (!providerRef.current || currentRoomIdRef.current !== room.roomId) { | ||
| providerRef.current = new CommandProvider(room); | ||
| currentRoomIdRef.current = room.roomId; | ||
| } | ||
|
|
||
| const provider = providerRef.current; | ||
| provider | ||
| .getCompletions(content, { start: 0, end: 0 }) | ||
| .then((results) => { | ||
| if (results.length > 0) { | ||
| setContentContainsCommands(true); | ||
| } else { | ||
| setContentContainsCommands(false); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| // If there's an error getting completions, assume no commands | ||
| setContentContainsCommands(false); | ||
| }); | ||
| }, [content, room]); | ||
|
|
||
| return contentContainsCommands; | ||
| } | ||
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
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
161 changes: 161 additions & 0 deletions
161
test/unit-tests/components/views/rooms/wysiwyg_composer/hooks/useContainsCommand-test.tsx
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,161 @@ | ||
| /* | ||
| Copyright 2025 New Vector Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import { renderHook, waitFor } from "jest-matrix-react"; | ||
| import { Room } from "matrix-js-sdk/src/matrix"; | ||
|
|
||
| import { useContainsCommand } from "../../../../../../../src/components/views/rooms/wysiwyg_composer/hooks/useContainsCommand"; | ||
| import { stubClient } from "../../../../../../test-utils"; | ||
|
|
||
| // Mock CommandProvider | ||
| const mockGetCompletions = jest.fn(); | ||
| jest.mock("../../../../../../../src/autocomplete/CommandProvider", () => { | ||
| return jest.fn().mockImplementation(() => ({ | ||
| getCompletions: mockGetCompletions, | ||
| })); | ||
| }); | ||
|
|
||
| describe("useContainsCommand", () => { | ||
| let room: Room; | ||
|
|
||
| beforeEach(() => { | ||
| const client = stubClient(); | ||
| room = new Room("!room:example.com", client, "@user:example.com"); | ||
| mockGetCompletions.mockClear(); | ||
| // Default mock to return empty promise | ||
| mockGetCompletions.mockResolvedValue([]); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("should return false when content is null", async () => { | ||
| mockGetCompletions.mockResolvedValue([]); | ||
|
|
||
| const { result } = renderHook(() => useContainsCommand(null, room)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(false); | ||
| }); | ||
| expect(mockGetCompletions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should return false when content is empty string", async () => { | ||
| mockGetCompletions.mockResolvedValue([]); | ||
|
|
||
| const { result } = renderHook(() => useContainsCommand("", room)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(false); | ||
| }); | ||
| expect(mockGetCompletions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should return true when content contains a valid command", async () => { | ||
| mockGetCompletions.mockResolvedValue([{ type: "command", completion: "/spoiler" }]); | ||
|
|
||
| const { result } = renderHook(() => useContainsCommand("/spoiler test message", room)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(true); | ||
| }); | ||
| expect(mockGetCompletions).toHaveBeenCalledWith("/spoiler test message", { start: 0, end: 0 }); | ||
| }); | ||
|
|
||
| it("should return false when content contains no valid commands", async () => { | ||
| mockGetCompletions.mockResolvedValue([]); | ||
|
|
||
| const { result } = renderHook(() => useContainsCommand("/invalidcommand", room)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(false); | ||
| }); | ||
| expect(mockGetCompletions).toHaveBeenCalledWith("/invalidcommand", { start: 0, end: 0 }); | ||
| }); | ||
|
|
||
| it("should return true for partial command matches", async () => { | ||
| mockGetCompletions.mockResolvedValue([ | ||
| { type: "command", completion: "/spoiler" }, | ||
| { type: "command", completion: "/shrug" }, | ||
| ]); | ||
|
|
||
| const { result } = renderHook(() => useContainsCommand("/sp", room)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(true); | ||
| }); | ||
| expect(mockGetCompletions).toHaveBeenCalledWith("/sp", { start: 0, end: 0 }); | ||
| }); | ||
|
|
||
| it("should update when content changes", async () => { | ||
| mockGetCompletions.mockResolvedValue([]); | ||
|
|
||
| const { result, rerender } = renderHook(({ content, room }) => useContainsCommand(content, room), { | ||
| initialProps: { content: "/invalid", room }, | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(false); | ||
| }); | ||
|
|
||
| // Change to valid command | ||
| mockGetCompletions.mockResolvedValue([{ type: "command", completion: "/spoiler" }]); | ||
|
|
||
| rerender({ content: "/spoiler", room }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(true); | ||
| }); | ||
| expect(mockGetCompletions).toHaveBeenCalledWith("/spoiler", { start: 0, end: 0 }); | ||
| }); | ||
|
|
||
| it("should handle CommandProvider errors gracefully", async () => { | ||
| mockGetCompletions.mockRejectedValueOnce(new Error("Provider error")); | ||
|
|
||
| const { result } = renderHook(() => useContainsCommand("/test", room)); | ||
|
|
||
| // Should remain false even if promise rejects | ||
| await waitFor(() => { | ||
| expect(result.current).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| it("should return false for non-command content", async () => { | ||
| mockGetCompletions.mockResolvedValue([]); // CommandProvider returns empty for non-commands | ||
|
|
||
| const { result } = renderHook(() => useContainsCommand("regular message", room)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(false); | ||
| }); | ||
| expect(mockGetCompletions).toHaveBeenCalledWith("regular message", { start: 0, end: 0 }); | ||
| }); | ||
|
|
||
| it("should reset to false when switching to null content", async () => { | ||
| mockGetCompletions.mockResolvedValue([{ type: "command", completion: "/spoiler" }]); | ||
|
|
||
| const { result, rerender } = renderHook( | ||
| ({ content, room }: { content: string | null; room: Room | undefined }) => | ||
| useContainsCommand(content, room), | ||
| { | ||
| initialProps: { content: "/spoiler" as string | null, room: room as Room | undefined }, | ||
| }, | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(true); | ||
| }); | ||
|
|
||
| // Switch to null content | ||
| rerender({ content: null, room }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.