Skip to content

Commit

Permalink
Added "newline" support (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
simialbi authored Apr 27, 2020
1 parent 58d4783 commit 6d35cf9
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 15 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
"npm-asset/highlight.js": "^9.18",
"npm-asset/katex": "^0.11",
"npm-asset/quill": "^1.3",
"npm-asset/quill-smart-break": ">= 0.1.1 <1.0.0",
"phpunit/phpunit": "^7.5",
"roave/security-advisories": "dev-master"
},
"suggest": {
"npm-asset/highlight.js": "For local highlight.js assets",
"npm-asset/katex": "For local KaTeX assets",
"npm-asset/quill": "For local Quill assets"
"npm-asset/quill": "For local Quill assets",
"npm-asset/quill-smart-break": "For 'SHIFT + Enter' newline support"
},
"autoload": {
"psr-4": {
Expand Down
64 changes: 50 additions & 14 deletions src/Quill.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use bizley\quill\assets\KatexLocalAsset;
use bizley\quill\assets\QuillAsset;
use bizley\quill\assets\QuillLocalAsset;
use bizley\quill\assets\SmartBreakLocalAsset;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
Expand Down Expand Up @@ -295,6 +296,10 @@ public function addModule(string $name, $config): void
if ($name === 'syntax') {
$this->setHighlightJs(true);
}

if ($name === 'smart-breaker') {
$this->setSmartBreak(true);
}
}

/**
Expand All @@ -307,6 +312,29 @@ public function setConfig(array $config): void
$this->_quillConfiguration = $config;
}

/** @var bool */
private $_smartBreak = false;

/**
* Checks whether the Smart break needs to be added.
* @return bool
* @since 3.1.0
*/
public function isSmartBreak(): bool
{
return $this->_smartBreak;
}

/**
* Sets Smart break flag.
* @param bool $smartBreak
* @since 3.1.0
*/
public function setSmartBreak(bool $smartBreak): void
{
$this->_smartBreak = $smartBreak;
}

/** @var bool */
private $_katex = false;

Expand Down Expand Up @@ -369,6 +397,10 @@ protected function prepareOptions(): void
$this->setHighlightJs(true);
}

if (isset($this->configuration['modules']['smart-breaker'])) {
$this->setSmartBreak(true);
}

$this->setConfig($this->configuration);
} else {
if (!empty($this->theme)) {
Expand All @@ -391,7 +423,7 @@ protected function prepareOptions(): void
$this->addConfig('formats', $this->formats);
}

if ($this->readOnly !== null && (bool) $this->readOnly) {
if ($this->readOnly !== null && (bool)$this->readOnly) {
$this->addConfig('readOnly', true);
}

Expand Down Expand Up @@ -515,29 +547,33 @@ public function registerClientScript(): void
{
$view = $this->view;

if ($this->isKatex()) {
if ($this->localAssets) {
if ($this->localAssets) {
if ($this->isKatex()) {
KatexLocalAsset::register($view);
} else {
$katexAsset = KatexAsset::register($view);
$katexAsset->version = $this->katexVersion;
}
}

if ($this->isHighlightJs()) {
if ($this->localAssets) {
if ($this->isHighlightJs()) {
$highlightAsset = HighlightLocalAsset::register($view);
$highlightAsset->style = $this->highlightStyle;
} else {
}

$asset = QuillLocalAsset::register($view);

if ($this->isSmartBreak()) {
SmartBreakLocalAsset::register($view);
}
} else {
if ($this->isKatex()) {
$katexAsset = KatexAsset::register($view);
$katexAsset->version = $this->katexVersion;
}

if ($this->isHighlightJs()) {
$highlightAsset = HighlightAsset::register($view);
$highlightAsset->version = $this->highlightVersion;
$highlightAsset->style = $this->highlightStyle;
}
}

if ($this->localAssets) {
$asset = QuillLocalAsset::register($view);
} else {
$asset = QuillAsset::register($view);
$asset->version = $this->quillVersion;
}
Expand Down
23 changes: 23 additions & 0 deletions src/assets/SmartBreakLocalAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace bizley\quill\assets;

use yii\web\AssetBundle;

/**
* Smart break assets.
*
* smart-breaker.js can be found at
* https://github.com/simialbi/quill-smart-break
* @author Simon Karlen <simi.albi@outlook.com>
*/
class SmartBreakLocalAsset extends AssetBundle
{
/** {@inheritdoc} */
public $sourcePath = '@npm/quill-smart-break/dist';

/** {@inheritdoc} */
public $js = ['smart-breaker.min.js'];
}
87 changes: 87 additions & 0 deletions tests/QuillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use bizley\quill\assets\KatexLocalAsset;
use bizley\quill\assets\QuillAsset;
use bizley\quill\assets\QuillLocalAsset;
use bizley\quill\assets\SmartBreakLocalAsset;
use bizley\quill\Quill;
use PHPUnit\Framework\TestCase;
use Yii;
Expand Down Expand Up @@ -673,6 +674,46 @@ public function shouldRegisterLocalQuillAndKatex(): void
Yii::$app = null;
}

/**
* @test
* @throws InvalidConfigException
*/
public function shouldRegisterLocalQuillAndSmartBreak()
{
static::setApp();

$this->assertSame(
'<input type="hidden" id="quill-0" name="test"><div id="editor-quill-0" style="min-height:150px;"></div>',
(
new Quill(
[
'name' => 'test',
'localAssets' => true,
'modules' => ['smart-breaker' => true]
]
)
)->run()
);
$this->assertSame(
[
QuillLocalAsset::class,
SmartBreakLocalAsset::class
],
array_keys(Yii::$app->view->assetBundles)
);
$this->assertSame(['smart-breaker.min.js'], Yii::$app->view->assetBundles[SmartBreakLocalAsset::class]->js);
$this->assertSame(
[
'var q_quill_0=new Quill("#editor-quill-0",'
. '{"theme":"snow","modules":{"smart-breaker":true,"toolbar":true}});'
. 'q_quill_0.on(\'text-change\',function(){'
. 'document.getElementById("quill-0").value=q_quill_0.root.innerHTML;});'
],
array_values(Yii::$app->view->js[View::POS_END])
);
Yii::$app = null;
}

/**
* @test
* @throws InvalidConfigException
Expand Down Expand Up @@ -850,6 +891,52 @@ public function shouldRegisterLocalQuillKatexAndHighlightJs(): void
Yii::$app = null;
}

/**
* @test
* @throws InvalidConfigException
*/
public function shouldRegisterLocalQuillKatexAndHighlightJsAndSmartBreak(): void
{
static::setApp();

$this->assertSame(
'<input type="hidden" id="quill-0" name="test"><div id="editor-quill-0" style="min-height:150px;"></div>',
(
new Quill(
[
'name' => 'test',
'localAssets' => true,
'modules' => [
'syntax' => true,
'formula' => true,
'smart-breaker' => true
],
'toolbarOptions' => [['code-block', 'formula']],
]
)
)->run()
);
$this->assertSame(
[
KatexLocalAsset::class,
HighlightLocalAsset::class,
QuillLocalAsset::class,
SmartBreakLocalAsset::class
],
array_keys(Yii::$app->view->assetBundles)
);
$this->assertSame(
[
'var q_quill_0=new Quill("#editor-quill-0",'
. '{"theme":"snow","modules":{"syntax":true,"formula":true,"smart-breaker":true,"toolbar":[["code-block","formula"]]}});'
. 'q_quill_0.on(\'text-change\',function(){'
. 'document.getElementById("quill-0").value=q_quill_0.root.innerHTML;});'
],
array_values(Yii::$app->view->js[View::POS_END])
);
Yii::$app = null;
}

/**
* @test
* @throws InvalidConfigException
Expand Down

0 comments on commit 6d35cf9

Please sign in to comment.