Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit a63da74

Browse files
authored
Add code blocks to rich text editor (#9921)
* Applies small changes to code block display in timeline * Makes the composer code block look like the timeline display, but without line numbers * Adds a button to allow code blocks to be implemented * Adds tests for the new button
1 parent 422802e commit a63da74

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

res/css/views/rooms/_EventTile.pcss

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ $left-gutter: 64px;
548548
pre,
549549
code {
550550
font-family: $monospace-font-family !important;
551-
background-color: $codeblock-background-color;
551+
background-color: $system;
552552
}
553553

554554
code:not(pre *) {
@@ -578,6 +578,8 @@ $left-gutter: 64px;
578578
background: transparent;
579579
}
580580

581+
border: 1px solid $quinary-content;
582+
581583
code {
582584
white-space: pre; /* we want code blocks to be scrollable and not wrap */
583585

@@ -756,6 +758,8 @@ $left-gutter: 64px;
756758

757759
.mx_EventTile_collapsedCodeBlock {
758760
max-height: 30vh;
761+
padding-top: $spacing-12;
762+
padding-bottom: $spacing-12;
759763
}
760764

761765
/* Inserted adjacent to <pre> blocks, (See TextualBody) */

res/css/views/rooms/wysiwyg_composer/components/_Editor.pcss

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,23 @@ limitations under the License.
4646

4747
// model output always includes a linebreak but we do not want the user
4848
// to see it when writing input in lists
49-
:is(ol, ul) + br:last-of-type {
49+
:is(ol, ul, pre) + br:last-of-type {
5050
display: none;
5151
}
5252

53+
> pre {
54+
font-size: $font-15px;
55+
line-height: $font-24px;
56+
57+
margin-top: 0;
58+
margin-bottom: 0;
59+
padding: $spacing-8 $spacing-12;
60+
61+
background-color: $inlinecode-background-color;
62+
border: 1px solid $inlinecode-border-color;
63+
border-radius: 2px;
64+
}
65+
5366
code {
5467
font-family: $monospace-font-family !important;
5568
background-color: $inlinecode-background-color;
Lines changed: 3 additions & 0 deletions
Loading

src/components/views/rooms/wysiwyg_composer/components/FormattingButtons.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { Icon as InlineCodeIcon } from "../../../../../../res/img/element-icons/
2626
import { Icon as LinkIcon } from "../../../../../../res/img/element-icons/room/composer/link.svg";
2727
import { Icon as BulletedListIcon } from "../../../../../../res/img/element-icons/room/composer/bulleted_list.svg";
2828
import { Icon as NumberedListIcon } from "../../../../../../res/img/element-icons/room/composer/numbered_list.svg";
29+
import { Icon as CodeBlockIcon } from "../../../../../../res/img/element-icons/room/composer/code_block.svg";
2930
import AccessibleTooltipButton from "../../../elements/AccessibleTooltipButton";
3031
import { Alignment } from "../../../elements/Tooltip";
3132
import { KeyboardShortcut } from "../../../settings/KeyboardShortcut";
@@ -132,6 +133,12 @@ export function FormattingButtons({ composer, actionStates }: FormattingButtonsP
132133
onClick={() => composer.inlineCode()}
133134
icon={<InlineCodeIcon className="mx_FormattingButtons_Icon" />}
134135
/>
136+
<Button
137+
actionState={actionStates.codeBlock}
138+
label={_td("Code block")}
139+
onClick={() => composer.codeBlock()}
140+
icon={<CodeBlockIcon className="mx_FormattingButtons_Icon" />}
141+
/>
135142
<Button
136143
actionState={actionStates.link}
137144
label={_td("Link")}

test/components/views/rooms/wysiwyg_composer/components/FormattingButtons-test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const mockWysiwyg = {
2828
underline: jest.fn(),
2929
strikeThrough: jest.fn(),
3030
inlineCode: jest.fn(),
31+
codeBlock: jest.fn(),
3132
link: jest.fn(),
3233
orderedList: jest.fn(),
3334
unorderedList: jest.fn(),
@@ -36,14 +37,15 @@ const mockWysiwyg = {
3637
const openLinkModalSpy = jest.spyOn(LinkModal, "openLinkModal");
3738

3839
const testCases: Record<
39-
Exclude<ActionTypes, "undo" | "redo" | "clear" | "codeBlock">,
40+
Exclude<ActionTypes, "undo" | "redo" | "clear">,
4041
{ label: string; mockFormatFn: jest.Func | jest.SpyInstance }
4142
> = {
4243
bold: { label: "Bold", mockFormatFn: mockWysiwyg.bold },
4344
italic: { label: "Italic", mockFormatFn: mockWysiwyg.italic },
4445
underline: { label: "Underline", mockFormatFn: mockWysiwyg.underline },
4546
strikeThrough: { label: "Strikethrough", mockFormatFn: mockWysiwyg.strikeThrough },
4647
inlineCode: { label: "Code", mockFormatFn: mockWysiwyg.inlineCode },
48+
codeBlock: { label: "Code block", mockFormatFn: mockWysiwyg.inlineCode },
4749
link: { label: "Link", mockFormatFn: openLinkModalSpy },
4850
orderedList: { label: "Numbered list", mockFormatFn: mockWysiwyg.orderedList },
4951
unorderedList: { label: "Bulleted list", mockFormatFn: mockWysiwyg.unorderedList },

0 commit comments

Comments
 (0)