'use strict'; jQuery(function ($) { $('[data-head-settings]').each(function () { const $details = $(this); const source = $details.find('[data-head-source]').get(0); const $limit = $details.find('[data-head-limit]'); const encoder = new TextEncoder(); const size = (value) => encoder.encode(value).length; function fits(current, removed, added) { const original = size(current); const next = original - size(removed) + size(added); $limit.prop('hidden', next <= source.maxLength); return next <= source.maxLength || next < original; } let editor; $details.on('toggle', () => { if (!this.open || !window.CodeMirror) return; if (!editor) { editor = window.CodeMirror.fromTextArea(source, { mode: 'htmlmixed', lineNumbers: true, lineWrapping: true, indentUnit: 2, tabSize: 2, }); editor.getInputField().setAttribute('aria-labelledby', 'head-source-label'); editor.on('beforeChange', (cm, change) => { if (!fits(cm.getValue(), cm.getRange(change.from, change.to), change.text.join('\n'))) change.cancel(); }); editor.on('change', () => editor.save()); } editor.refresh(); }); $details.on('click', '[data-head-asset]', function () { const mime = this.dataset.headAsset; $('#media-picker').trigger('studio:pick', [mime, (file) => { const tag = mime === 'text/css' ? $('', {rel: 'stylesheet', href: file.url}) : $('