124 lines
4.5 KiB
PHP
124 lines
4.5 KiB
PHP
<?php $attributes ??= new \Illuminate\View\ComponentAttributeBag;
|
|
|
|
$__newAttributes = [];
|
|
$__propNames = \Illuminate\View\ComponentAttributeBag::extractPropNames((['name' => 'body', 'value' => '']));
|
|
|
|
foreach ($attributes->all() as $__key => $__value) {
|
|
if (in_array($__key, $__propNames)) {
|
|
$$__key = $$__key ?? $__value;
|
|
} else {
|
|
$__newAttributes[$__key] = $__value;
|
|
}
|
|
}
|
|
|
|
$attributes = new \Illuminate\View\ComponentAttributeBag($__newAttributes);
|
|
|
|
unset($__propNames);
|
|
unset($__newAttributes);
|
|
|
|
foreach (array_filter((['name' => 'body', 'value' => '']), 'is_string', ARRAY_FILTER_USE_KEY) as $__key => $__value) {
|
|
$$__key = $$__key ?? $__value;
|
|
}
|
|
|
|
$__defined_vars = get_defined_vars();
|
|
|
|
foreach ($attributes->all() as $__key => $__value) {
|
|
if (array_key_exists($__key, $__defined_vars)) unset($$__key);
|
|
}
|
|
|
|
unset($__defined_vars, $__key, $__value); ?>
|
|
|
|
|
|
<link rel="stylesheet" href="<?php echo e(asset('vendor/quill/quill.snow.css')); ?>">
|
|
<div class="rich-editor">
|
|
<div id="quill-<?php echo e($name); ?>"></div>
|
|
</div>
|
|
<textarea name="<?php echo e($name); ?>" id="quill-input-<?php echo e($name); ?>" hidden><?php echo e($value); ?></textarea>
|
|
|
|
<script src="<?php echo e(asset('vendor/quill/quill.js')); ?>"></script>
|
|
<script>
|
|
(function () {
|
|
var FIELD = '<?php echo e($name); ?>';
|
|
var input = document.getElementById('quill-input-' + FIELD);
|
|
if (typeof Quill === 'undefined' || !input) return;
|
|
|
|
var quill = new Quill('#quill-' + FIELD, {
|
|
theme: 'snow',
|
|
placeholder: '在此撰写文章正文,支持标题、列表、引用、代码与插入图片…',
|
|
modules: {
|
|
toolbar: {
|
|
container: [
|
|
[{ header: [1, 2, 3, false] }],
|
|
['bold', 'italic', 'underline', 'strike'],
|
|
[{ list: 'ordered' }, { list: 'bullet' }],
|
|
['blockquote', 'code-block'],
|
|
['link', 'image'],
|
|
['clean']
|
|
],
|
|
handlers: { image: imageHandler }
|
|
}
|
|
}
|
|
});
|
|
|
|
quill.root.innerHTML = input.value || '';
|
|
|
|
var form = input.closest('form');
|
|
if (form) {
|
|
form.addEventListener('submit', function () {
|
|
input.value = quill.root.innerHTML;
|
|
});
|
|
}
|
|
|
|
function imageHandler() {
|
|
var fileInput = document.createElement('input');
|
|
fileInput.type = 'file';
|
|
fileInput.accept = 'image/*';
|
|
fileInput.click();
|
|
fileInput.onchange = function () {
|
|
if (!fileInput.files.length) return;
|
|
var fd = new FormData();
|
|
fd.append('file', fileInput.files[0]);
|
|
var token = (document.querySelector('input[name=_token]') || {}).value || '';
|
|
fetch('<?php echo e(route('upload')); ?>', {
|
|
method: 'POST',
|
|
headers: { 'X-CSRF-TOKEN': token },
|
|
body: fd
|
|
})
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (d) {
|
|
if (d.url) {
|
|
var range = quill.getSelection(true);
|
|
quill.insertEmbed(range.index, 'image', d.url, 'user');
|
|
} else if (d.errors && d.errors.file) {
|
|
alert(d.errors.file[0]);
|
|
} else {
|
|
alert('图片上传失败,请重试');
|
|
}
|
|
})
|
|
.catch(function () { alert('图片上传失败,请重试'); });
|
|
};
|
|
}
|
|
|
|
// 通用文件上传(封面图等),供表单内上传按钮 onclick="uploadFile('cover')" 调用
|
|
window.uploadFile = function (field) {
|
|
var fileInput = document.getElementById(field + '-file');
|
|
if (!fileInput || !fileInput.files.length) { alert('请先选择文件'); return; }
|
|
var fd = new FormData();
|
|
fd.append('file', fileInput.files[0]);
|
|
var token = (document.querySelector('input[name=_token]') || {}).value || '';
|
|
fetch('<?php echo e(route('upload')); ?>', {
|
|
method: 'POST',
|
|
headers: { 'X-CSRF-TOKEN': token },
|
|
body: fd
|
|
})
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (d) {
|
|
if (d.url) { document.getElementById(field + '-url').value = d.url; }
|
|
else if (d.errors && d.errors.file) { alert(d.errors.file[0]); }
|
|
else { alert('上传失败,请重试'); }
|
|
})
|
|
.catch(function () { alert('上传失败,请重试'); });
|
|
};
|
|
})();
|
|
</script>
|
|
<?php /**PATH /www/wwwroot/cloud-chip.cn/resources/views/components/rich-editor.blade.php ENDPATH**/ ?>
|