summaryrefslogtreecommitdiff
path: root/source/features/upload-button.tsx
blob: 64f0f3d0c88e775885fb8d28f9572f1d55bd6b9d (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
import './upload-button.css';
import React from 'dom-chef';
import select from 'select-dom';
import delegate, {DelegateEvent} from 'delegate-it';
import features from '../libs/features';
import * as icons from '../libs/icons';

function addButtons(): void {
	for (const toolbar of select.all('form:not(.rgh-has-upload-field) markdown-toolbar')) {
		const form = toolbar.closest('form')!;
		if (!select.exists('.js-manual-file-chooser[type=file]', form)) {
			continue;
		}

		const toolbarGroup = select('.toolbar-group:last-child', toolbar);
		if (toolbarGroup) {
			toolbarGroup.append(
				<button type="button" className="toolbar-item rgh-upload-btn tooltipped tooltipped-nw" aria-label="Upload attachments" data-hotkey="u">
					{icons.cloudUpload()}
				</button>
			);
			form.classList.add('rgh-has-upload-field');
		}
	}
}

function triggerUploadUI({delegateTarget}: DelegateEvent<Event, HTMLButtonElement>): void {
	delegateTarget
		.form!
		.querySelector<HTMLElement>('.js-manual-file-chooser')! // Find <input [type=file]>
		.click(); // Open UI
}

function init(): void {
	addButtons();
	delegate('.rgh-upload-btn', 'click', triggerUploadUI);
}

features.add({
	id: 'upload-button',
	description: 'Add an upload button in comments for uploading attachments',
	include: [
		features.hasRichTextEditor
	],
	load: features.onAjaxedPages,
	init
});