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

View File

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

View File

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

View File

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