Skip to content

Commit

Permalink
Meta search: Highlight keyword in single content view
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Apr 29, 2024
1 parent 2f24ad5 commit 3495d6e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ HumHub Changelog
- Fix #6967: Use same order for meta searching that is used for content page searching by default
- Enh #6968: Meta search: open external links in new window
- Fix #6970: MultiSelect loads wrong options (since #6768 in 1.16.0-beta.1)
- Enh #6971: Meta search: Highlight keyword in single content view

1.16.0-beta.2 (April 9, 2024)
-----------------------------
Expand Down
22 changes: 22 additions & 0 deletions protected/humhub/modules/content/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use yii\base\BaseObject;
use yii\console\Controller;
use yii\helpers\Console;
use yii\web\View;

/**
* Events provides callbacks to handle events.
Expand Down Expand Up @@ -143,6 +144,27 @@ public static function onCronBeforeAction($event): void
}
}

public static function onViewEndBody($event)
{
$highlight = Yii::$app->session->get('contentHighlight');
if ($highlight === null || $highlight === '') {
return;
}
Yii::$app->session->remove('contentHighlight');

/* @var View $view */
$view = $event->sender;

$index = time();
$view->registerJs('function contentHighlight' . $index . '() {
"' . str_replace('"', '\"', $highlight) . '".split(" ")
.forEach((keyword) => $(".layout-content-container").highlight(keyword))
}
$(document).ready(() => contentHighlight' . $index . '());
const wallStream = humhub.require("ui.widget").Widget.instance("[data-ui-widget=\'stream.wall.WallStream\']");
wallStream && wallStream.on("humhub:stream:afterAddEntries", () => contentHighlight' . $index . '());');
}


private static function canPublishScheduledContent(): bool
{
Expand Down
2 changes: 2 additions & 0 deletions protected/humhub/modules/content/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use humhub\modules\user\models\User;
use humhub\modules\space\models\Space;
use humhub\modules\content\components\ContentActiveRecord;
use yii\web\View;

return [
'id' => 'content',
Expand All @@ -22,6 +23,7 @@
['class' => ContentActiveRecord::class, 'event' => ContentActiveRecord::EVENT_AFTER_DELETE, 'callback' => [Events::class, 'onContentActiveRecordDelete']],
['class' => CronController::class, 'event' => CronController::EVENT_ON_DAILY_RUN, 'callback' => [Events::class, 'onCronDailyRun']],
['class' => CronController::class, 'event' => CronController::EVENT_BEFORE_ACTION, 'callback' => [Events::class, 'onCronBeforeAction']],
['class' => View::class, 'event' => View::EVENT_END_BODY, 'callback' => [Events::class, 'onViewEndBody']],
],
'consoleControllerMap' => [
'content-search' => '\humhub\modules\content\commands\SearchController',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
namespace humhub\modules\content\controllers;

use humhub\components\behaviors\AccessControl;
use Yii;
use humhub\components\Controller;
use humhub\modules\content\models\WallEntry;
use humhub\modules\content\models\Content;
use Yii;
use yii\web\HttpException;

/**
Expand Down Expand Up @@ -47,6 +46,10 @@ public function actionIndex()

$content = Content::findOne(['id' => $id]);
if ($content !== null) {
$highlight = Yii::$app->request->get('highlight');
if ($highlight !== null) {
Yii::$app->session->set('contentHighlight', $highlight);
}

if (method_exists($content->getPolymorphicRelation(), 'getUrl')) {
$url = $content->getPolymorphicRelation()->getUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getResults(int $maxResults): array

$results = [];
foreach ($resultSet->results as $content) {
$results[] = new SearchRecord($content);
$results[] = new SearchRecord($content, $this->getKeyword());
}

return [
Expand Down
7 changes: 5 additions & 2 deletions protected/humhub/modules/content/search/SearchRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use humhub\modules\ui\icon\widgets\Icon;
use humhub\modules\user\models\User;
use Yii;
use yii\helpers\Url;

/**
* Search Record for Content
Expand All @@ -24,10 +25,12 @@
class SearchRecord implements MetaSearchResultInterface
{
public ?Content $content = null;
public ?string $keyword = null;

public function __construct(Content $content)
public function __construct(Content $content, ?string $keyword = null)
{
$this->content = $content;
$this->keyword = $keyword;
}

/**
Expand Down Expand Up @@ -75,6 +78,6 @@ public function getDescription(): string
*/
public function getUrl(): string
{
return $this->content->getUrl();
return Url::to(['/content/perma', 'id' => $this->content->id, 'highlight' => $this->keyword]);
}
}
3 changes: 2 additions & 1 deletion static/js/humhub/humhub.ui.search.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ humhub.module('ui.search', function(module, require, $) {
provider.replaceWith(newProviderContent);
const records = newProviderContent.find(that.selectors.providerRecord);
if (records.length) {
records.find(that.selectors.providerRecordText).highlight(data.keyword);
const highlightedText = records.find(that.selectors.providerRecordText);
data.keyword.split(' ').forEach((keyword) => highlightedText.highlight(keyword));
} else if (newProviderContent.data('hide-on-empty') !== undefined) {
newProviderContent.hide();
}
Expand Down

0 comments on commit 3495d6e

Please sign in to comment.