Skip to content

Commit 39590dc

Browse files
committed
Add monospace toggle button to textarea
1 parent 58b36cc commit 39590dc

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,23 @@ 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+
const monospaceEnabled = localStorage?.getItem('editor-monospace') === 'true';
52+
const btn = document.querySelector('.markdown-switch-monospace');
53+
const text = btn.getAttribute(monospaceEnabled ? 'data-disable-text' : 'data-enable-text');
54+
btn.setAttribute('data-tooltip-content', text);
55+
btn.setAttribute('aria-checked', String(monospaceEnabled));
56+
if (monospaceEnabled) {
57+
document.querySelector('.markdown-text-editor').classList.add('gt-mono');
58+
}
59+
</script>
4960
</div>
5061
<div class="ui tab markup" data-tab-panel="markdown-previewer">
5162
{{.locale.Tr "loading"}}

web_src/css/editor-markdown.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
user-select: none;
2525
padding: 5px;
2626
cursor: pointer;
27+
border: none;
28+
background: none;
29+
color: var(--color-text);
2730
}
2831

2932
.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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,20 @@ 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+
monospaceButton?.addEventListener('click', (e) => {
79+
e.preventDefault();
80+
const enabled = localStorage?.getItem('editor-monospace') !== 'true';
81+
localStorage.setItem('editor-monospace', String(enabled));
82+
this.textarea.classList[enabled ? 'add' : 'remove']('gt-mono');
83+
const text = monospaceButton.getAttribute(enabled ? 'data-disable-text' : 'data-enable-text');
84+
monospaceButton.setAttribute('data-tooltip-content', text);
85+
monospaceButton.setAttribute('aria-checked', String(enabled));
86+
});
87+
88+
const easymdeButton = this.container.querySelector('.markdown-switch-easymde');
89+
easymdeButton?.addEventListener('click', async (e) => {
7890
e.preventDefault();
7991
this.userPreferredEditor = 'easymde';
8092
await this.switchToEasyMDE();

0 commit comments

Comments
 (0)