summaryrefslogtreecommitdiff
path: root/source/features/fit-textareas.tsx
blob: 147e319e1cd61a781fb71c942291550033c76312 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import './fit-textareas.css';
import select from 'select-dom';
import delegate from 'delegate-it';
import fitTextarea from 'fit-textarea';
import * as pageDetect from 'github-url-detection';

import features from '.';
import onPrMergePanelOpen from '../github-events/on-pr-merge-panel-open';

function inputListener(event: Event): void {
	fitTextarea(event.target as HTMLTextAreaElement);
}

function watchTextarea(textarea: HTMLTextAreaElement): void {
	textarea.addEventListener('input', inputListener); // The user triggers `input` event
	textarea.addEventListener('change', inputListener); // File uploads trigger `change` events
	fitTextarea(textarea);

	// Disable constrained native feature
	textarea.classList.replace('js-size-to-fit', 'rgh-fit-textareas');
}

function focusListener({delegateTarget: textarea}: delegate.Event<Event, HTMLTextAreaElement>): void {
	watchTextarea(textarea);
}

function fitPrCommitMessageBox(): void {
	watchTextarea(select<HTMLTextAreaElement>('[name="commit_message"]')!);
}

function init(): void {
	// Exclude PR review box because it's in a `position:fixed` container; The scroll HAS to appear within the fixed element.
	delegate(document, 'textarea:not(#pull_request_review_body)', 'focusin', focusListener);

	select.all('textarea').forEach(watchTextarea);
}

void features.add({
	id: __filebasename,
	description: 'Auto-resizes comment fields to fit their content and no longer show scroll bars, rather than have a height limit like GitHub’s native "fit to content" behavior.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/54336211-66fd5e00-4666-11e9-9c5e-111fccab004d.gif'
}, {
	include: [
		pageDetect.hasRichTextEditor
	],
	init
}, {
	include: [
		pageDetect.isPRConversation
	],
	init() {
		onPrMergePanelOpen(fitPrCommitMessageBox);
	}
});