Skip to content

Commit 469dc44

Browse files
silverwinddelvh
andauthored
Add monospace toggle button to textarea (#24034)
- Add new button to textarea to switch font. State is persisted in localStorage. - Change markdown-switch-easymde button from `<span>` to `<button>` - Slightly increased monospace font globally by 5% as I think it fits better. For hover effect on these buttons I'm deferring to #23896. ![](https://user-images.githubusercontent.com/115237/230948526-ecf8d730-0c69-4a8e-a1a5-1e5e079c754d.gif) --------- Co-authored-by: delvh <[email protected]>
1 parent 29194a9 commit 469dc44

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ buttons.list.task.tooltip = Add a list of tasks
133133
buttons.mention.tooltip = Mention a user or team
134134
buttons.ref.tooltip = Reference an issue or pull request
135135
buttons.switch_to_legacy.tooltip = Use the legacy editor instead
136+
buttons.enable_monospace_font = Enable monospace font
137+
buttons.disable_monospace_font = Disable monospace font
136138

137139
[filter]
138140
string.asc = A - Z

templates/shared/combomarkdowneditor.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ Template Attributes:
4040
<md-ref class="markdown-toolbar-button" data-tooltip-content="{{.locale.Tr "editor.buttons.ref.tooltip"}}">{{svg "octicon-cross-reference"}}</md-ref>
4141
</div>
4242
<div class="markdown-toolbar-group">
43+
<button class="markdown-toolbar-button markdown-switch-monospace" role="switch" data-enable-text="{{.locale.Tr "editor.buttons.enable_monospace_font"}}" data-disable-text="{{.locale.Tr "editor.buttons.disable_monospace_font"}}">{{svg "octicon-typography"}}</button>
4344
<button class="markdown-toolbar-button markdown-switch-easymde" data-tooltip-content="{{.locale.Tr "editor.buttons.switch_to_legacy.tooltip"}}">{{svg "octicon-arrow-switch"}}</button>
4445
</div>
4546
</markdown-toolbar>
4647
<text-expander keys=": @">
4748
<textarea class="markdown-text-editor js-quick-submit"{{if .TextareaName}} name="{{.TextareaName}}"{{end}}{{if .TextareaPlaceholder}} placeholder="{{.TextareaPlaceholder}}"{{end}}{{if .TextareaAriaLabel}} aria-label="{{.TextareaAriaLabel}}"{{end}}>{{.TextareaContent}}</textarea>
4849
</text-expander>
50+
<script>
51+
if (localStorage?.getItem('markdown-editor-monospace') === 'true') {
52+
document.querySelector('.markdown-text-editor').classList.add('gt-mono');
53+
}
54+
</script>
4955
</div>
5056
<div class="ui tab markup" data-tab-panel="markdown-previewer">
5157
{{.locale.Tr "loading"}}

web_src/css/editor-markdown.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
user-select: none;
2525
padding: 5px;
2626
cursor: pointer;
27+
color: var(--color-text);
2728
}
2829

2930
.combo-markdown-editor .markdown-toolbar-button:hover {

web_src/css/helpers.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
.gt-mono {
3131
font-family: var(--fonts-monospace) !important;
32-
font-size: .9em !important; /* compensate for monospace fonts being usually slightly larger */
32+
font-size: .95em !important; /* compensate for monospace fonts being usually slightly larger */
3333
}
3434

3535
.gt-bold { font-weight: 600 !important; }

web_src/js/features/comp/ComboMarkdownEditor.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,25 @@ class ComboMarkdownEditor {
7373
// upstream bug: The role code is never executed in base MarkdownButtonElement https://github.com/github/markdown-toolbar-element/issues/70
7474
el.setAttribute('role', 'button');
7575
}
76-
this.switchToEasyMDEButton = this.container.querySelector('.markdown-switch-easymde');
77-
this.switchToEasyMDEButton?.addEventListener('click', async (e) => {
76+
77+
const monospaceButton = this.container.querySelector('.markdown-switch-monospace');
78+
const monospaceEnabled = localStorage?.getItem('markdown-editor-monospace') === 'true';
79+
const monospaceText = monospaceButton.getAttribute(monospaceEnabled ? 'data-disable-text' : 'data-enable-text');
80+
monospaceButton.setAttribute('data-tooltip-content', monospaceText);
81+
monospaceButton.setAttribute('aria-checked', String(monospaceEnabled));
82+
83+
monospaceButton?.addEventListener('click', (e) => {
84+
e.preventDefault();
85+
const enabled = localStorage?.getItem('markdown-editor-monospace') !== 'true';
86+
localStorage.setItem('markdown-editor-monospace', String(enabled));
87+
this.textarea.classList.toggle('gt-mono', enabled);
88+
const text = monospaceButton.getAttribute(enabled ? 'data-disable-text' : 'data-enable-text');
89+
monospaceButton.setAttribute('data-tooltip-content', text);
90+
monospaceButton.setAttribute('aria-checked', String(enabled));
91+
});
92+
93+
const easymdeButton = this.container.querySelector('.markdown-switch-easymde');
94+
easymdeButton?.addEventListener('click', async (e) => {
7895
e.preventDefault();
7996
this.userPreferredEditor = 'easymde';
8097
await this.switchToEasyMDE();

0 commit comments

Comments
 (0)