pages: add render_title option for automatic title embedding

This commit is contained in:
E. S. 2024-11-05 03:08:14 +03:00
parent b01105d2b2
commit d99fb2a934
6 changed files with 54 additions and 21 deletions

View File

@ -338,10 +338,14 @@ class AdminHandler extends request_handler {
function POST_ajax_md_preview() {
ensure_xhr();
list($md, $title, $use_image_previews, $lang) = input('md, title, b:use_image_previews, lang');
list($md, $title, $use_image_previews, $lang, $is_page) = input('md, title, b:use_image_previews, lang, b:is_page');
$lang = PostLanguage::tryFrom($lang);
if (!$lang)
$lang = PostLanguage::getDefault();
if ($is_page) {
$md = '# '.$title."\n\n".$md;
$title = '';
}
$html = markup::markdownToHtml($md, $use_image_previews, $lang);
$ctx = skin('admin');
$html = $ctx->markdownPreview(
@ -436,6 +440,7 @@ class AdminHandler extends request_handler {
title: $page->title,
text: $page->md,
visible: $page->visible,
render_title: $page->renderTitle,
saved: $saved,
langs: PostLanguage::cases(),
js_text: $js_text);
@ -452,8 +457,8 @@ class AdminHandler extends request_handler {
csrf_check('editpage'.$page->shortName);
list($text, $title, $visible, $short_name)
= input('text, title, b:visible, new_short_name');
list($text, $title, $visible, $short_name, $render_title)
= input('text, title, b:visible, new_short_name, b:render_title');
$text = trim($text);
$title = trim($title);
@ -478,6 +483,7 @@ class AdminHandler extends request_handler {
'md' => $text,
'visible' => (bool)$visible,
'short_name' => $short_name,
'render_title' => (bool)$render_title
]);
admin_log(new \AdminActions\PageEdit($short_name, $new_short_name));

View File

@ -20,11 +20,13 @@ function AdminWriteEditForm(opts) {
this.form.text.addEventListener('input', this.onInput);
if (this.isPost())
this.form.keywords.addEventListener('input', this.onInput);
ge('toggle_wrap').addEventListener('click', this.onToggleWrapClick);
if (this.isPost()) {
if (this.isPost())
ge('toc_cb').addEventListener('change', this.onToCCheckboxChange);
}
else if (this.isPage() && this.isEditing())
ge('render_title_cb').addEventListener('change', this.onRenderTitleCheckboxChange);
var lang = 'en';
if (this.isPost()) {
@ -82,6 +84,16 @@ extend(AdminWriteEditForm.prototype, {
this.showPreview();
},
callShowPreview: function() {
if (this.previewTimeout !== null)
clearTimeout(this.previewTimeout);
this.previewTimeout = setTimeout(function() {
this.previewTimeout = null;
this.showPreview();
}.bind(this), 300);
},
showPreview: function() {
if (this.previewRequest !== null)
this.previewRequest.abort();
@ -93,6 +105,10 @@ extend(AdminWriteEditForm.prototype, {
params.title = this.form.elements.title.value;
params.lang = this.getCurrentLang();
}
if (this.isPage() && this.form.render_title.checked) {
params.title = this.form.elements.title.value;
params.is_page = 1
}
this.previewRequest = ajax.post('/admin/markdown-preview.ajax', params, function(err, response) {
if (err)
return console.error(err);
@ -150,6 +166,8 @@ extend(AdminWriteEditForm.prototype, {
// fd.append('lang', this.getCurrentLang())
if (this.isPost() || this.isEditing())
fd.append('visible', ge('visible_cb').checked ? 1 : 0);
if (this.isPage() && this.isEditing())
fd.append('render_title', ge('render_title_cb').checked ? 1 : 0);
// text-specific fields
var atLeastOneLangIsWritten = false;
@ -227,16 +245,10 @@ extend(AdminWriteEditForm.prototype, {
},
onInput: function(e) {
if (this.previewTimeout !== null)
clearTimeout(this.previewTimeout);
this.previewTimeout = setTimeout(function() {
this.previewTimeout = null;
this.showPreview();
var what = e.target.name;
this.draft.set(what, e.target.value);
}.bind(this), 300);
this.callShowPreview();
},
onLangChanged: function(e) {
@ -248,5 +260,9 @@ extend(AdminWriteEditForm.prototype, {
onToCCheckboxChange: function(e) {
this.tocByLang[this.getCurrentLang()] = e.target.checked;
},
onRenderTitleCheckboxChange: function(e) {
this.callShowPreview();
}
})

View File

@ -128,6 +128,9 @@
display: table-cell;
vertical-align: top;
}
.blog-write-form-preview .blog-post {
display: block;
}
.blog-post-toc {
display: table-cell;
vertical-align: top;
@ -311,11 +314,11 @@ body.wide .blog-post {
}
h1 {
margin: 40px 0 16px;
margin: 35px 0 16px;
font-weight: 600;
font-size: 30px;
font-size: 25px;
border-bottom: 1px $border-color solid;
padding-bottom: 8px;
padding-bottom: 6px;
}
h2 {

View File

@ -11,12 +11,17 @@ class Page extends model {
public int $ts;
public int $updateTs;
public bool $visible;
public bool $renderTitle;
public string $shortName;
function edit(array $fields) {
$fields['update_ts'] = time();
if ($fields['md'] != $this->md)
$fields['html'] = markup::markdownToHtml($fields['md']);
if ($fields['md'] != $this->md || $fields['render_title'] != $this->renderTitle) {
$md = $fields['md'];
if ($fields['render_title'])
$md = '# '.$fields['title']."\n\n".$md;
$fields['html'] = markup::markdownToHtml($md);
}
parent::edit($fields);
}

View File

@ -318,6 +318,7 @@ function pageForm($ctx,
bool $is_edit = false,
?bool $saved = null,
bool $visible = false,
bool $render_title = false,
?array $js_text = null): array {
$form_url = '/'.$short_name.'/'.($is_edit ? 'edit' : 'create').'/';
@ -354,7 +355,7 @@ $html = <<<HTML
</div>
{$ctx->if_then_else($is_edit,
fn() => $ctx->pageFormEditOptions($short_name, $visible),
fn() => $ctx->pageFormEditOptions($short_name, $visible, $render_title),
fn() => $ctx->pageFormAddOptions($short_name))}
</form>
@ -384,7 +385,7 @@ JS;
return [$html, $js];
}
function pageFormEditOptions($ctx, $short_name, $visible) {
function pageFormEditOptions($ctx, $short_name, $visible, $render_title) {
return <<<HTML
<div class="form-field-wrap clearfix">
<table class="blog-write-options-table">
@ -402,6 +403,7 @@ return <<<HTML
<div class="form-field-label">{$ctx->lang('pages_write_form_options')}</div>
<div class="form-field">
<label for="visible_cb"><input type="checkbox" id="visible_cb" name="visible"{$ctx->if_true($visible, ' checked="checked"')}> {$ctx->lang('pages_write_form_visible')}</label>
<label for="render_title_cb"><input type="checkbox" id="render_title_cb" name="render_title"{$ctx->if_true($render_title, ' checked="checked"')}> {$ctx->lang('pages_write_form_render_title')}</label>
</div>
</div>
</td>

View File

@ -103,6 +103,7 @@ pages_write_form_text: Text
pages_write_form_enter_text: Enter text..
pages_write_form_enter_title: Enter title..
pages_write_form_visible: Visible
pages_write_form_render_title: Title
pages_write_form_short_name: Short name
pages_write_form_toggle_wrap: Toggle wrap
pages_write_form_options: Options