view src/test/tiny.html @ 26:0837820b97fb

tinymce work
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 Jul 2022 15:47:18 -0600
parents fccbe5554fcc
children 6871eec2cf4c
line wrap: on
line source

<!doctype html>
<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<script src="http://tinymce.luan.software/tinymce.min.js" xreferrerpolicy="origin"></script>
		<style>
		</style>
		<script>
			var youtubePtn1 = new RegExp('https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?');
			var youtubePtn2 = new RegExp('https://www.youtube.com/watch\\?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?');
			var bitchutePtn = new RegExp('https://www.bitchute.com/video/([a-zA-Z0-9]+)/');
			var url2;

			function media_url_resolver(data,resolve,reject) {
				let html;
				let url = data.url;
				url2 = url;
				let result;
				if( result = url.match(youtubePtn1) || url.match(youtubePtn2) ) {
					html = '<iframe data-video="'+url+'" width="560" height="315" src="https://www.youtube.com/embed/' + result[1];
					if( result[2] )
						html += '?start=' + result[2]
					html += '" frameborder="0" allowfullscreen></iframe>';
				} else if( result = url.match(bitchutePtn) ) {
					html = '<iframe data-video="'+url+'" width="560" height="315" scrolling="no" frameborder="0" style="border: none;" src="https://www.bitchute.com/embed/' + result[1] + '/"></iframe>';
				} else {
					html = '<a data-video="'+url+'" href="'+url+'">'+url+'</a>';
				}
				resolve({ html: html });
			}


			function tinymceSetup(editor) {

				editor.ui.registry.addToggleButton('styleCode', {
					icon: 'sourcecode',
					tooltip: 'Code',
					onAction: function(api) {
						editor.execCommand('mceToggleFormat', false, 'code')
						//api.setActive( !api.isActive() );
					},
					onSetup: function(api) {
						api.setActive(editor.formatter.match('code'));
						let changed = editor.formatter.formatChanged('code', api.setActive);
						return function() { changed.unbind(); };
					}
				});

				editor.ui.registry.addMenuButton('styleText', {
					icon: 'format',
					tooltip: 'Text',
					fetch: function(callback) {
						callback([
							'fontsize',
							'forecolor',
						])
					}
				});

				editor.on( 'init', function(e) {editor.focus()} );
				//editor.focus();
			}

			tinymce.init({
				selector: 'textarea',
				setup: tinymceSetup,
				menubar: false,
				statusbar: false,
				toolbar: 'link image media | styleCode bold italic underline strikethrough superscript styleText | blockquote numlist bullist',
				//block_formats: 'Paragraph=div; Header 1=h1; Header 2=h2; Header 3=h3',
				plugins: ['link', 'image', 'media', 'lists', 'code', 'autoresize'],
				autoresize_bottom_margin: 0,
				link_target_list: false,
				link_title: false,
				image_description: false,
				image_dimensions: false,
				object_resizing: false,
				contextmenu: false,
				media_alt_source: false,
				media_dimensions: false,
				media_poster: false,
				media_url_resolver: media_url_resolver,
				text_patterns: false,
				content_style: 'img {max-width: 500px;} p {margin: 0}',
				//newline_behavior: 'linebreak',
				extended_valid_elements: 'b,i',
				formats: {
					bold: { inline: 'b' },
					italic: {inline: 'i'},
					underline: {inline: 'u'},
				},
			});

			function log() {
				console.log(tinymce.activeEditor.getContent());
			}
		</script>
	</head>
	<body>
		<p><a href="https://www.tiny.cloud/">TinyMCE</a></p>
		<textarea></textarea>
		<p><button onclick="log()">log</button></p>
	</body>
</html>