37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
jQuery(function ($) {
|
|
$('[data-head-settings]').each(function () {
|
|
const $details = $(this);
|
|
const source = $details.find('[data-head-source]').get(0);
|
|
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('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'
|
|
? $('<link>', {rel: 'stylesheet', href: file.url})
|
|
: $('<script>', {src: file.url, defer: true});
|
|
const value = tag.prop('outerHTML') + '\n';
|
|
if (editor) {
|
|
editor.replaceSelection(value);
|
|
editor.focus();
|
|
} else {
|
|
source.setRangeText(value, source.selectionStart, source.selectionEnd, 'end');
|
|
source.focus();
|
|
}
|
|
}]);
|
|
});
|
|
});
|
|
});
|