diff src/bbcode/bbcode.js @ 46:289718f121e4

use bbcode editor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Nov 2022 18:43:38 -0700
parents 2d4f00755092
children 7ef9222474e2
line wrap: on
line diff
--- a/src/bbcode/bbcode.js	Sun Nov 13 22:07:18 2022 -0700
+++ b/src/bbcode/bbcode.js	Mon Nov 14 18:43:38 2022 -0700
@@ -39,7 +39,7 @@
 	request.send(formData);
 }
 
-function getBbcodeDiv(node) {
+function bb_getDiv(node) {
 	do {
 		//console.log(node);
 		if( node.getAttribute('bbcode') !== null )
@@ -47,16 +47,18 @@
 	} while( node = node.parentNode );
 }
 
+function bb_getTextarea(node) {
+	return bb_getDiv(node).querySelector('textarea');
+}
+
 function bb_uploaded(input,url,filename) {
-	let div = getBbcodeDiv(input);
-	let textarea = div.querySelector('textarea');
+	let textarea = bb_getTextarea(input);
 	textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select');
 	textarea.focus();
 }
 
 function bb_add(input,openTag,closeTag) {
-	let div = getBbcodeDiv(input);
-	let textarea = div.querySelector('textarea');
+	let textarea = bb_getTextarea(input);
 	let start = textarea.selectionStart;
 	let end = textarea.selectionEnd;
 	textarea.setRangeText(closeTag,end,end);
@@ -67,14 +69,17 @@
 }
 
 function bbcodeCreate(div,options) {
-	if( typeof(div) === 'string' )
+	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>
-			<textarea oninput="bb_fixTextarea(this)"></textarea>
+			<textarea name=bbcode oninput="bb_fixTextarea(this)">${content}</textarea>
 			<div buttons>
 				<button type=button onclick="bb_add(this,'[b]','[/b]')" title="Bold"><img src="/bbcode/icons/format_bold.svg"></button>
 				<button type=button onclick="bb_add(this,'[i]','[/i]')" title="Italic"><img src="/bbcode/icons/format_italic.svg"></button>
@@ -104,4 +109,6 @@
 		div.querySelector('button[save]').addEventListener('click',save);
 	if(cancel)
 		div.querySelector('button[cancel]').addEventListener('click',cancel);
+	let textarea = div.querySelector('textarea')
+	bb_fixTextarea(textarea);
 }