posts: make keywords language-specific

This commit is contained in:
E. S. 2024-09-13 22:53:33 +03:00
parent 392c3bc809
commit 679e9e2f5e
5 changed files with 41 additions and 21 deletions

View File

@ -494,6 +494,7 @@ class AdminHandler extends request_handler {
'title' => '',
'md' => '',
'toc' => false,
'keywords' => '',
];
}
@ -520,9 +521,9 @@ class AdminHandler extends request_handler {
$lang_data = [];
$at_least_one_lang_is_written = false;
foreach (PostLanguage::cases() as $lang) {
list($title, $text, $toc_enabled) = input("title:{$lang->value}, text:{$lang->value}, b:toc:{$lang->value}", ['trim' => true]);
list($title, $text, $keywords, $toc_enabled) = input("title:{$lang->value}, text:{$lang->value}, keywords:{$lang->value}, b:toc:{$lang->value}", ['trim' => true]);
if ($title !== '' && $text !== '') {
$lang_data[$lang->value] = [$title, $text, $toc_enabled];
$lang_data[$lang->value] = [$title, $text, $keywords, $toc_enabled];
$at_least_one_lang_is_written = true;
}
}
@ -549,11 +550,12 @@ class AdminHandler extends request_handler {
// add texts
$added_texts = []; // for admin actions logging, at the end
foreach ($lang_data as $lang => $data) {
list($title, $text, $toc_enabled) = $data;
list($title, $text, $keywords, $toc_enabled) = $data;
if (!($new_post_text = $post->addText(
lang: PostLanguage::from($lang),
title: $title,
md: $text,
keywords: $keywords,
toc: $toc_enabled))
) {
posts::delete($post);
@ -607,12 +609,14 @@ class AdminHandler extends request_handler {
'title' => $text->title,
'md' => $text->md,
'toc' => $text->toc,
'keywords' => $text->keywords,
];
} else {
$js_texts[$pl->value] = [
'title' => '',
'md' => '',
'toc' => false,
'keywords' => '',
];
}
}
@ -635,7 +639,7 @@ class AdminHandler extends request_handler {
saved: $saved,
short_name: $short_name,
source_url: $post->sourceUrl,
keywords: $post->keywords,
keywords: $text->keywords,
langs: PostLanguage::cases(),
lang: $text->lang->value,
js_texts: $js_texts
@ -645,7 +649,7 @@ class AdminHandler extends request_handler {
function POST_post_edit() {
ensure_xhr();
list($old_short_name, $short_name, $langs, $date, $keywords, $source_url) = input('short_name, new_short_name, langs, date, keywords, source_url');
list($old_short_name, $short_name, $langs, $date, $source_url) = input('short_name, new_short_name, langs, date, source_url');
$post = posts::getByName($old_short_name);
if (!$post)
@ -660,7 +664,7 @@ class AdminHandler extends request_handler {
foreach (explode(',', $langs) as $lang) {
$lang = PostLanguage::from($lang);
list($text, $title, $visible, $toc) = input("text:{$lang->value}, title:{$lang->value}, b:visible, b:toc:{$lang->value}");
list($text, $title, $visible, $toc, $keywords) = input("text:{$lang->value}, title:{$lang->value}, b:visible, b:toc:{$lang->value}, keywords:{$lang->value}");
$error_code = null;
if (!$title)
@ -676,6 +680,7 @@ class AdminHandler extends request_handler {
lang: $lang,
title: $title,
md: $text,
keywords: $keywords,
toc: $toc
);
if (!$pt)
@ -684,7 +689,8 @@ class AdminHandler extends request_handler {
$pt->edit([
'title' => $title,
'md' => $text,
'toc' => (int)$toc
'toc' => (int)$toc,
'keywords' => $keywords
]);
}
}
@ -692,7 +698,6 @@ class AdminHandler extends request_handler {
$post_data = [
'date' => $date,
'visible' => $visible,
'keywords' => $keywords,
'source_url' => $source_url
];
if ($post->shortName != $short_name)

View File

@ -98,8 +98,8 @@ class MainHandler extends request_handler {
'$url' => $config['domain'].$post->getUrl(),
'$description' => $pt->getDescriptionPreview(155)
];
if ($post->keywords)
$meta['$keywords'] = $post->keywords;
if ($pt->keywords)
$meta['$keywords'] = $pt->keywords;
add_meta($meta);
if (($img = $pt->getFirstImage()) !== null)

View File

@ -32,18 +32,26 @@ extend(Draft.prototype, {
},
get: function(what) {
return what === 'title' ? this.getTitle() : this.getText()
switch (what) {
case 'title': return this.getTitle();
case 'text': return this.getText();
case 'keywords': return this.getKeywords()
}
},
set: function(what, val) {
if (what === 'title')
this.setTitle(val)
else
this.setText(val)
switch (what) {
case 'title': return this.setTitle(val);
case 'text': return this.setText(val);
case 'keywords': return this.setKeywords(val);
}
},
getTitle: function() { return LS.getItem(this.key('title')) || '' },
getText: function() { return LS.getItem(this.key('text')) || '' },
getKeywords: function() { return LS.getItem(this.key('keywords')) || '' },
setTitle: function(val) { LS.setItem(this.key('title'), val) },
setText: function(val) { LS.setItem(this.key('text'), val) }
setText: function(val) { LS.setItem(this.key('text'), val) },
setKeywords: function(val) { LS.setItem(this.key('keywords'), val) }
});

View File

@ -18,6 +18,8 @@ function AdminWriteEditForm(opts) {
this.form.addEventListener('submit', this.onSubmit);
this.form.title.addEventListener('input', this.onInput);
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()) {
@ -44,6 +46,7 @@ function AdminWriteEditForm(opts) {
this.draft.setLang(l)
this.draft.setTitle(opts.texts[l].title)
this.draft.setText(opts.texts[l].md)
this.draft.setKeywords(opts.texts[l].keywords)
this.tocByLang[l] = opts.texts[l].toc
}
this.draft.setLang(lang)
@ -68,6 +71,8 @@ extend(AdminWriteEditForm.prototype, {
fillFromDraft: function(opts) {
opts = opts || {applyEventEmpty: false};
var whats = ['title', 'text'];
if (this.isPost())
whats.push('keywords');
for (var i = 0; i < whats.length; i++) {
var what = whats[i];
if (this.draft.get(what) !== '' || opts.applyEvenEmpty)
@ -150,15 +155,16 @@ extend(AdminWriteEditForm.prototype, {
var atLeastOneLangIsWritten = false;
var writtenLangs = [];
if (this.isPost()) {
fd.append('source_url', evt.target.source_url.value)
fd.append('keywords', evt.target.keywords.value)
fd.append('source_url', evt.target.source_url.value);
this.opts.langs.forEach(function(l) {
var title = this.draft.getForLang(l, 'title');
var text = this.draft.getForLang(l, 'text');
var keywords = this.draft.getForLang(l, 'keywords');
if (title !== '' && text !== '') {
atLeastOneLangIsWritten = true;
fd.append('title:' + l, title);
fd.append('text:' + l, text);
fd.append('keywords:' + l, keywords);
fd.append('toc:' + l, this.tocByLang[l] ? 1 : 0);
writtenLangs.push(l);
}
@ -228,7 +234,7 @@ extend(AdminWriteEditForm.prototype, {
this.previewTimeout = null;
this.showPreview();
var what = e.target.name === 'title' ? 'title' : 'text';
var what = e.target.name;
this.draft.set(what, e.target.value);
}.bind(this), 300);
},

View File

@ -19,7 +19,6 @@ class Post extends model {
public bool $visible;
public string $shortName;
public string $sourceUrl;
public string $keywords;
protected array $texts = [];
@ -28,7 +27,7 @@ class Post extends model {
parent::edit($fields);
}
public function addText(PostLanguage $lang, string $title, string $md, bool $toc): ?PostText {
public function addText(PostLanguage $lang, string $title, string $md, string $keywords, bool $toc): ?PostText {
$html = markup::markdownToHtml($md, lang: $lang);
$text = markup::htmlToText($html);
@ -40,6 +39,7 @@ class Post extends model {
'text' => $text,
'md' => $md,
'toc' => $toc,
'keywords' => $keywords,
];
$db = DB();
@ -133,6 +133,7 @@ class PostText extends model {
public string $text;
public bool $toc;
public string $tocHtml;
public string $keywords;
public function edit(array $fields) {
if ($fields['md'] != $this->md) {