forked from laysense/laysite
57 lines
2.4 KiB
JavaScript
57 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
jQuery(function ($) {
|
|
$('[data-article-editor]').each(function () {
|
|
const Editor = window.toastui?.Editor;
|
|
if (!Editor) return;
|
|
const $field = $(this);
|
|
const $source = $field.find('textarea');
|
|
const $format = $field.find('input[name="body_format"]');
|
|
const $host = $field.find('[data-editor-host]');
|
|
const $status = $field.find('[data-article-editor-status]');
|
|
const originalFormat = $format.val();
|
|
const original = $source.val();
|
|
$host.prop('hidden', false);
|
|
let editor;
|
|
try {
|
|
editor = new Editor({
|
|
el: $host.get(0),
|
|
height: '34rem',
|
|
initialEditType: originalFormat === 'html' ? 'wysiwyg' : 'markdown',
|
|
initialValue: originalFormat === 'markdown' ? original : '',
|
|
previewStyle: 'vertical',
|
|
language: 'zh-CN',
|
|
usageStatistics: false,
|
|
autofocus: false,
|
|
hooks: {
|
|
addImageBlobHook() {
|
|
$status.text('请先上传图片到媒体库,再从媒体库插入。');
|
|
}
|
|
}
|
|
});
|
|
if (originalFormat === 'html') editor.setHTML(original);
|
|
} catch (error) {
|
|
$host.prop('hidden', true);
|
|
return;
|
|
}
|
|
const snapshot = originalFormat === 'html' ? editor.getHTML() : editor.getMarkdown();
|
|
$source.prop('hidden', true);
|
|
$field.find('[data-article-editor-label]').removeAttr('for');
|
|
$field.find('[data-insert-article-image]').prop('hidden', false).on('click', () => {
|
|
$('#media-picker').triggerHandler('studio:pick', ['image/', (file) => {
|
|
const alt = window.prompt('图片替代文本(装饰图可留空)', file.label);
|
|
if (alt === null) return;
|
|
$status.empty();
|
|
editor.focus();
|
|
editor.exec('addImage', {imageUrl: file.url, altText: alt});
|
|
}]);
|
|
});
|
|
$field.closest('form').on('studio:sync', function () {
|
|
const format = editor.isMarkdownMode() ? 'markdown' : 'html';
|
|
const content = format === 'html' ? editor.getHTML() : editor.getMarkdown();
|
|
$format.val(format);
|
|
$source.val(format === originalFormat && content === snapshot ? original : content);
|
|
});
|
|
});
|
|
});
|