{% set version = "tinymce" %}
{% block label %}
	{% if field.label %}
		{% set hint = field.help ? 'data-hint="' ~ field.help|tu|raw ~ '"': '' %}
		<div class="form-label form-field hint--bottom" {{ hint }}>{{ field.label|tu|raw }}</div>
	{% endif %}
{% endblock %}
{% block field %}
	<div class="form-field {{ field.classes|default('') }}">
		<div class="form-data">
			<textarea
				class="tinymce"
				name="{{ (scope ~ field.name)|fieldName }}"
				{% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
				{% if field.placeholder %}placeholder="{{ field.placeholder|tu }}"{% endif %}
				{% if field.autofocus in ['on', 'true', 1] %}autofocus="autofocus"{% endif %}
				{% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
				{% if field.readonly in ['on', 'true', 1] %}readonly="readonly"{% endif %}
			>{{ value|join("\n")|e("html") }}</textarea>
		</div>
	</div>
	<script>
		tinymce.init({
			selector: "textarea.tinymce",
			convert_urls: false,
			document_base_url: "{{ (uri.rootUrl(true) ~ admin.page.rawRoute)|e("js") }}/",
			language: "{{ url('user://data/tinymce-editor/js/' ~ version ~ '/langs/' ~ grav.user.language ~ '.js')|default(url('user://data/tinymce-editor/js/' ~ version ~ '/langs/' ~ grav.user.language ~ '_' ~ (grav.user.language|upper) ~ '.js')|default(url('plugins://tinymce-editor/js/' ~ version ~ '/langs/' ~ grav.user.language ~ '.js')|default(url('plugins://tinymce-editor/js/' ~ version ~ '/langs/' ~ grav.user.language ~ '_' ~ (grav.user.language|upper) ~ '.js'))))|split('/')|last|split('.')|first }}",
			language_url: "{{ url('user://data/tinymce-editor/js/' ~ version ~ '/langs/' ~ grav.user.language ~ '.js')|default(url('user://data/tinymce-editor/js/' ~ version ~ '/langs/' ~ grav.user.language ~ '_' ~ (grav.user.language|upper) ~ '.js')|default(url('plugins://tinymce-editor/js/' ~ version ~ '/langs/' ~ grav.user.language ~ '.js')|default(url('plugins://tinymce-editor/js/' ~ version ~ '/langs/' ~ grav.user.language ~ '_' ~ (grav.user.language|upper) ~ '.js')))) }}",
			contextmenu: false,
			plugins: "{{ config.plugins["tinymce-editor"].plugins|join(" ")|e("js") }}",
			{% for key in config.plugins["tinymce-editor"].parameters|keys %}
				"{{ config.plugins["tinymce-editor"].parameters[key].name|e("js") }}": function(x) {
					try{return JSON.parse(x)}catch(e){return x}
				}("{{ config.plugins["tinymce-editor"].parameters[key].value|e("js") }}"),
			{% endfor %}
			menubar: {% if config.plugins["tinymce-editor"].menubar %}"{% for key in config.plugins["tinymce-editor"].menu|keys %}{{ config.plugins["tinymce-editor"].menu[key].title|lower|e("js") }} {% endfor %}"{% else %}false{% endif %},
			menu: {
				{% for key in config.plugins["tinymce-editor"].menu|keys %}
					"{{ config.plugins["tinymce-editor"].menu[key].title|lower|e("js") }}":{
						title: "{{ config.plugins["tinymce-editor"].menu[key].title|e("js") }}",
						items: "{{ config.plugins["tinymce-editor"].menu[key].items|e("js") }}"
					},
				{% endfor %}
			},
			toolbar: [
				{% for fields in config.plugins["tinymce-editor"].toolbar %}
					{% for row in fields %}
						'{{ row|e("js") }}',
					{% endfor %}
				{% endfor %}
			],
			branding: {% if config.plugins["tinymce-editor"].branding %}true{% else %}false{% endif %},
			statusbar: {% if config.plugins["tinymce-editor"].statusbar %}true{% else %}false{% endif %},
			setup: function(editor) {
				editor.on("init", (function() {
					function insert(dir, src) {
						var file = new Image();
						file.src = dir+src;
						file.onload = function() {
							tinymce.execCommand("mceImage");
							$("div.mce-window input.mce-textbox:first").val(src);
						};
						file.onerror = function() {
							file = new Audio();
							file.src = dir+src;
							file.ondurationchange = function() {
								tinymce.execCommand("mceMedia");
								$("div.mce-window input.mce-textbox:first").val(src);
							};
							file.onerror = function() {
								tinymce.execCommand("mceLink");
								$("div.mce-window input.mce-textbox:eq(1)").val(src);
								$("div.mce-window input.mce-textbox:first").val(src);
								return true;
							};
							return true;
						};
					}
					$("div.dropzone").on("click", "a.dz-insert", function() {
						insert($("#grav-dropzone").attr("data-media-path")+"/", encodeURIComponent($(this).parent().find("[data-dz-name]").text()));
					});
					var isMedia = 0;
					var wasMedia = 0;
					$("#grav-dropzone").on("dragstart", ".dz-preview", function(e) {
						isMedia = 1;
					});
					editor.on("drop", function(e) {
						if(isMedia) {
							e.preventDefault();
							e.stopPropagation();
							var txt = encodeURIComponent(decodeURIComponent(e.dataTransfer.getData("text").split("(").pop()));
							insert($("#grav-dropzone").attr("data-media-path")+"/", txt.substring(0, txt.length-1));
							wasMedia = 1;
						}
					});
					$("#grav-dropzone").on("dragend", ".dz-preview", function(e) {
						isMedia = 0;
					});
					editor.on("PastePreProcess", function(e) {
						if(wasMedia) {
							e.content = "";
							wasMedia = 0;
						}
					});
					if(!tinymce.settings.allow_html_data_urls && !tinymce.settings.allow_svg_data_urls) {
						editor.parser.addNodeFilter("embed,iframe,object", function(nodes) {
							nodes.forEach(function(node) {
								if(node.attributes) {
									node.attributes.forEach(function(attr) {
										if(/^data:/i.test(attr.value)) {
											node.attr(attr.name, "");
										}
									});
								}
							});
						});
					}
				}));
			},
			{{ config.plugins["tinymce-editor"].evals|raw }}
		});
	</script>
{% endblock %}
