forked from laysense/laysense.cn
Fix CMS login and restore PAGEPAN feature icons
This commit is contained in:
+111
-40
@@ -1,30 +1,95 @@
|
||||
'use strict';
|
||||
|
||||
const formats = {
|
||||
markdown: {
|
||||
heading: ['## ', '', '小标题'],
|
||||
bold: ['**', '**', '粗体文字'],
|
||||
list: ['- ', '', '列表项'],
|
||||
link: ['[', '](https://)', '链接文字']
|
||||
},
|
||||
html: {
|
||||
heading: ['<h3>', '</h3>', '小标题'],
|
||||
bold: ['<strong>', '</strong>', '粗体文字'],
|
||||
list: ['<ul>\n <li>', '</li>\n</ul>', '列表项'],
|
||||
link: ['<a href="https://">', '</a>', '链接文字']
|
||||
jQuery(function ($) {
|
||||
const formats = {
|
||||
markdown: {
|
||||
heading: ['## ', '', '小标题'],
|
||||
bold: ['**', '**', '粗体文字'],
|
||||
list: ['- ', '', '列表项'],
|
||||
link: ['[', '](https://)', '链接文字']
|
||||
},
|
||||
html: {
|
||||
heading: ['<h3>', '</h3>', '小标题'],
|
||||
bold: ['<strong>', '</strong>', '粗体文字'],
|
||||
list: ['<ul>\n <li>', '</li>\n</ul>', '列表项'],
|
||||
link: ['<a href="https://">', '</a>', '链接文字']
|
||||
}
|
||||
};
|
||||
|
||||
function refreshToken(response) {
|
||||
if (response?.csrf) {
|
||||
$('input[name="csrf"]').val(response.csrf);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (const editor of document.querySelectorAll('[data-editor]')) {
|
||||
const area = editor.querySelector('textarea');
|
||||
const format = editor.dataset.format;
|
||||
const preview = editor.querySelector('.editor-preview');
|
||||
const previewButton = editor.querySelector('[data-preview-button]');
|
||||
const form = editor.closest('form');
|
||||
function formError($form, message, status) {
|
||||
let $feedback = $form.children('.ajax-feedback');
|
||||
if (!$feedback.length) {
|
||||
$feedback = $('<p>', {class: 'ajax-feedback feedback error', role: 'alert', tabindex: -1})
|
||||
.prependTo($form);
|
||||
}
|
||||
$feedback.text(message);
|
||||
if (status === 401) {
|
||||
$feedback.append(' ', $('<a>', {
|
||||
href: '/admin/login', target: '_blank', rel: 'noopener', text: '打开登录页'
|
||||
}));
|
||||
}
|
||||
$feedback.prop('hidden', false).trigger('focus');
|
||||
}
|
||||
|
||||
for (const button of editor.querySelectorAll('[data-insert]')) {
|
||||
button.addEventListener('click', () => {
|
||||
const [before, after, example] = formats[format][button.dataset.insert];
|
||||
$('form[method="post"]').on('submit', function (event) {
|
||||
event.preventDefault();
|
||||
const $form = $(this);
|
||||
if ($form.attr('aria-busy') === 'true') return;
|
||||
const action = $form.attr('action');
|
||||
const isLogin = action === '/admin/login';
|
||||
const $submit = $form.find(':submit');
|
||||
$form.attr('aria-busy', 'true').children('.ajax-feedback').prop('hidden', true);
|
||||
$submit.prop('disabled', true);
|
||||
|
||||
function finish() {
|
||||
$form.removeAttr('aria-busy');
|
||||
$submit.prop('disabled', false);
|
||||
}
|
||||
|
||||
function submit(canRetryLogin) {
|
||||
$.ajax({
|
||||
url: action,
|
||||
method: 'POST',
|
||||
data: $form.serialize(),
|
||||
dataType: 'json'
|
||||
}).done(function (response) {
|
||||
if (response.redirect) {
|
||||
window.location.assign(response.redirect);
|
||||
return;
|
||||
}
|
||||
formError($form, '未收到保存结果,请刷新页面确认。');
|
||||
finish();
|
||||
}).fail(function (request) {
|
||||
const response = request.responseJSON;
|
||||
refreshToken(response);
|
||||
if (canRetryLogin && request.status === 403 && response?.csrf) {
|
||||
submit(false);
|
||||
return;
|
||||
}
|
||||
formError($form, response?.message || '请求未完成,请稍后重试。', request.status);
|
||||
finish();
|
||||
});
|
||||
}
|
||||
|
||||
submit(isLogin);
|
||||
});
|
||||
|
||||
$('[data-editor]').each(function () {
|
||||
const $editor = $(this);
|
||||
const area = $editor.find('textarea').get(0);
|
||||
const format = $editor.data('format');
|
||||
const $preview = $editor.find('.editor-preview');
|
||||
const $form = $editor.closest('form');
|
||||
let pendingPreview;
|
||||
|
||||
$editor.on('click', '[data-insert]', function () {
|
||||
const [before, after, example] = formats[format][$(this).data('insert')];
|
||||
const start = area.selectionStart;
|
||||
const end = area.selectionEnd;
|
||||
const selection = area.value.slice(start, end) || example;
|
||||
@@ -32,24 +97,30 @@ for (const editor of document.querySelectorAll('[data-editor]')) {
|
||||
area.focus();
|
||||
area.setSelectionRange(start + before.length, start + before.length + selection.length);
|
||||
});
|
||||
}
|
||||
|
||||
previewButton.addEventListener('click', async () => {
|
||||
const show = preview.hidden;
|
||||
preview.hidden = !show;
|
||||
previewButton.textContent = show ? '收起预览' : '预览正文';
|
||||
previewButton.setAttribute('aria-expanded', String(show));
|
||||
if (!show) return;
|
||||
$editor.on('click', '[data-preview-button]', function () {
|
||||
const $button = $(this);
|
||||
const show = $preview.prop('hidden');
|
||||
$preview.prop('hidden', !show);
|
||||
$button.text(show ? '收起预览' : '预览正文').attr('aria-expanded', String(show));
|
||||
pendingPreview?.abort();
|
||||
if (!show) return;
|
||||
|
||||
preview.textContent = '正在加载预览…';
|
||||
try {
|
||||
const body = new URLSearchParams({body: area.value, format, csrf: form.elements.csrf.value});
|
||||
const response = await fetch('/admin/preview', {method: 'POST', body, redirect: 'error'});
|
||||
if (!response.ok) throw new Error('Preview could not be loaded');
|
||||
const markup = await response.text();
|
||||
preview.innerHTML = markup;
|
||||
} catch {
|
||||
preview.textContent = '预览无法加载,请刷新后重试。';
|
||||
}
|
||||
$preview.text('正在加载预览…');
|
||||
pendingPreview = $.ajax({
|
||||
url: '/admin/preview',
|
||||
method: 'POST',
|
||||
data: {body: area.value, format, csrf: $form.find('input[name="csrf"]').val()},
|
||||
dataType: 'text'
|
||||
}).done((markup) => $preview.html(markup)).fail((request, status) => {
|
||||
if (status === 'abort') return;
|
||||
let feedback = request.responseJSON;
|
||||
if (!feedback && request.getResponseHeader('Content-Type')?.startsWith('application/json')) {
|
||||
feedback = JSON.parse(request.responseText);
|
||||
}
|
||||
refreshToken(feedback);
|
||||
$preview.text(feedback?.message || '预览无法加载,请重试。');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user