From 1f25f95620bb27bf7806a4aef5becadee8dc70f9 Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Tue, 16 Jan 2018 14:17:28 +0100 Subject: [PATCH] Minor RichText widgets improvements --- protected/humhub/widgets/RichText.php | 71 ++++++++++++++++----------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/protected/humhub/widgets/RichText.php b/protected/humhub/widgets/RichText.php index 470079d138..c1908c1446 100644 --- a/protected/humhub/widgets/RichText.php +++ b/protected/humhub/widgets/RichText.php @@ -26,6 +26,11 @@ class RichText extends JsWidget */ public $text = ""; + /** + * @var string original text before parsed + */ + public $originalText = ""; + /** * @var boolean */ @@ -63,38 +68,14 @@ class RichText extends JsWidget public function run() { + $this->originalText = $this->text; + if ($this->encode) { $this->text = Html::encode($this->text); } if (!$this->minimal && !$this->edit) { - $maxOembedCount = 3; // Maximum OEmbeds - $oembedCount = 0; // OEmbeds used - $that = $this; - - $pattern= <<[^\s()]+)|(?R))*\) - | # else match a link with title - (https?|ftp):\/\/(([^\s()]+)|(?R))+(?text = preg_replace_callback($pattern, function ($match) use (&$oembedCount, &$maxOembedCount, &$that) { - - // Try use oembed - if ($maxOembedCount > $oembedCount) { - $oembed = UrlOembed::GetOEmbed($match[0]); - if ($oembed) { - $oembedCount++; - return $oembed; - } - } - - $options = strpos($match[0], Yii::$app->settings->get('baseUrl')) === 0 ? [] : ['target' => '_blank', 'rel' => "noopener noreferrer"]; - - // The markdown parser will parse the links by itself - return ($this->markdown) ? $match[0] : Html::a($match[0], Html::decode($match[0]), $options); - }, $this->text); + $this->translateOmbed(); // mark emails $this->text = preg_replace_callback('/[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})/', function ($match) { @@ -130,6 +111,40 @@ REGEXP; return trim($output); } + /** + * Translates Ombed links + */ + protected function translateOmbed() + { + $maxOembedCount = 3; // Maximum OEmbeds + $oembedCount = 0; // OEmbeds used + $that = $this; + + $pattern = <<[^\s()]+)|(?R))*\) + | # else match a link with title + (https?|ftp):\/\/(([^\s()]+)|(?R))+(?text = preg_replace_callback($pattern, function ($match) use (&$oembedCount, &$maxOembedCount, &$that) { + + // Try use oembed + if ($maxOembedCount > $oembedCount) { + $oembed = UrlOembed::GetOEmbed($match[0]); + if ($oembed) { + $oembedCount++; + return $oembed; + } + } + + $options = strpos($match[0], Yii::$app->settings->get('baseUrl')) === 0 ? [] : ['target' => '_blank', 'rel' => "noopener noreferrer"]; + + // The markdown parser will parse the links by itself + return ($this->markdown) ? $match[0] : Html::a($match[0], Html::decode($match[0]), $options); + }, $this->text); + } + /** * Replace emojis from text to img tag * @@ -154,7 +169,7 @@ REGEXP; return preg_replace_callback('@;(\w*?);@', function($hit) use(&$show, &$emojis) { if (in_array($hit[1], $emojis)) { if ($show) { - return Html::img(Yii::getAlias("@web-static/img/emoji/" . $hit[1] . ".svg"), array('data-emoji-name' => $hit[0], 'data-richtext-feature' => '', 'data-guid' => "@-emoji".$hit[0], 'class' => 'atwho-emoji', 'width' => '18', 'height' => '18', 'alt' => $hit[1])); + return Html::img(Yii::getAlias("@web-static/img/emoji/" . $hit[1] . ".svg"), array('data-emoji-name' => $hit[0], 'data-richtext-feature' => '', 'data-guid' => "@-emoji" . $hit[0], 'class' => 'atwho-emoji', 'width' => '18', 'height' => '18', 'alt' => $hit[1])); } return ''; }