view src/bbcode/bbcode.js @ 50:0d99cec5142c

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 17 Nov 2022 22:00:59 -0700
parents f225e82b2bf8
children 78b2d6995244
line wrap: on
line source

// https://fonts.google.com/icons

function upload(input,callback) {
	let file = input.files[0];
	input.value = null;
	let request = new XMLHttpRequest();
	let url = 'https://upload.uploadcare.com/base/';
	request.open( 'POST', url );
	request.onload = function() {
		if( request.status !== 200 ) {
			let err = 'ajax failed: ' + request.status;
			if( request.responseText ) {
				err += '\n' + request.responseText;
				document.write('<pre>'+request.responseText+'</pre>');
			}
			console.log(err);
			ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
			return;
		}
		let response = JSON.parse(request.responseText);
		let filename = file.name;
		let url = 'https://ucarecdn.com/' + response.file + '/' + filename;
		callback(input,url,filename);
	};
	let formData = new FormData();
	formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' );
	formData.append( 'file', file );
	request.send(formData);
}

function bbcodeCreate(div,options) {
	if( typeof(div) === 'string' ) {
		div = document.querySelector(div);
		if(!div) throw 'selector not found';
	}
	options = options || {};
	let content = options.content || '';
	let save = options.save;
	let cancel = options.cancel;
	let html = `\
		<div bbcode_editor>
			<textarea name=bbcode>${content}</textarea>
			<div buttons>
				<button type=button bold title="Bold"><img src="/bbcode/icons/format_bold.svg"></button>
				<button type=button italic title="Italic"><img src="/bbcode/icons/format_italic.svg"></button>
				<button type=button underline title="Underline"><img src="/bbcode/icons/format_underlined.svg"></button>
				<button type=button strikethrough title="Strikethrough"><img src="/bbcode/icons/format_strikethrough.svg"></button>
				<button type=button sub title="Subscript"><img src="/bbcode/icons/subscript.svg"></button>
				<button type=button sup title="Superscript"><img src="/bbcode/icons/superscript.svg"></button>
				<button type=button ul title="Bullet list"><img src="/bbcode/icons/format_list_bulleted.svg"></button>
				<button type=button ol title="Numbered list"><img src="/bbcode/icons/format_list_numbered.svg"></button>
				<button type=button code_block title="Code block"><img src="/bbcode/icons/code_blocks.svg"></button>
				<button type=button code_inline title="Inline code"><img src="/bbcode/icons/code.svg"></button>
				<button type=button more checked title="More..."><img src="/bbcode/icons/more_horiz.svg"></button>
				<input type=file>
				<button type=button upload title="Upload File"><img src="/bbcode/icons/file_upload.svg"></button>
`	;
	if(save) {
		html += `\
				<button type=button save title="Send message"><img src="/bbcode/icons/send.svg"></button>
`		;
	}
	if(cancel) {
		html += `\
				<button type=button cancel title="Cancel"><img src="/bbcode/icons/cancel.svg"></button>
`		;
	}
	html += `\
			</div>
		</div>
`	;
	div.innerHTML = html;

	if(save)
		div.querySelector('button[save]').onclick = save;
	if(cancel)
		div.querySelector('button[cancel]').onclick = cancel;

	let textarea = div.querySelector('textarea');
	function fixTextarea() {
		let height = textarea.scrollHeight;
		if( height > textarea.clientHeight ) {
			textarea.style.height = (height+2) + "px";
		}
	};
	textarea.oninput = fixTextarea;
	fixTextarea();

	let moreButton = div.querySelector('button[more]');
	function more() {
		let button = moreButton;
		let checked = button.getAttribute('checked') !== null;
		let buttons = button.parentNode.querySelectorAll('button');
		if( checked ) {
			button.removeAttribute('checked');
			for( let b of buttons ) {
				if( b === button )
					break;
				b.setAttribute('hidden','');
			}
		} else {
			button.setAttribute('checked','');
			for( let b of buttons ) {
				if( b === button )
					break;
				b.removeAttribute('hidden');
			}
		}
	};
	moreButton.onclick = more;
	more();

	function add(tag,openTag,closeTag) {
		let button = div.querySelector('button['+tag+']');
		button.onclick = function() {
			let start = textarea.selectionStart;
			let end = textarea.selectionEnd;
			textarea.setRangeText(closeTag,end,end);
			textarea.setRangeText(openTag,start,start);
			let len = openTag.length;
			textarea.setSelectionRange(start+len,end+len);
			fixTextarea();
			textarea.focus();
		};
	}
	add('bold','[b]','[/b]');
	add('italic','[i]','[/i]');
	add('underline','[u]','[/u]');
	add('strikethrough','[s]','[/s]');
	add('sub','[sub]','[/sub]');
	add('sup','[sup]','[/sup]');
	add('ul','[list]\n[item]','[/item]\n[/list]');
	add('ol','[list=1]\n[item]','[/item]\n[/list]');
	add('code_block','[code]','[/code]');
	add('code_inline','[code=inline]','[/code]');

	let fileInput = div.querySelector('input[type="file"]');
	div.querySelector('button[upload]').onclick = function(){ fileInput.click(); };
	function uploaded(input,url,filename) {
		textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select');
		textarea.focus();
	}
	fileInput.onchange = function() { upload(fileInput,uploaded); };
}