Skip to content

Commit d59e81f

Browse files
author
Igor Chepurnoy
committed
added editor and assets
1 parent 27a185b commit d59e81f

File tree

8 files changed

+271
-2
lines changed

8 files changed

+271
-2
lines changed

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ignore all test and documentation for archive
2+
/.gitattributes export-ignore
3+
/.gitignore export-ignore
4+
/.scrutinizer.yml export-ignore
5+
/.travis.yml export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/tests export-ignore
8+
/docs export-ignore

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
/composer.lock
19+
20+
# composer itself is not needed
21+
composer.phar
22+
23+
# Mac DS_Store Files
24+
.DS_Store
25+
26+
# phpunit itself is not needed
27+
phpunit.phar
28+
# local phpunit config
29+
/phpunit.xml
30+
31+
# local tests configuration
32+
/tests/data/config.local.php
33+
34+
# runtime cache
35+
/tests/runtime

.php_cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->exclude('vendor')
5+
->in([__DIR__]);
6+
7+
$config = PhpCsFixer\Config::create()
8+
->setUsingCache(false)
9+
->setRules([
10+
'@Symfony' => true,
11+
'phpdoc_align' => false,
12+
'phpdoc_summary' => false,
13+
'phpdoc_inline_tag' => false,
14+
'pre_increment' => false,
15+
'heredoc_to_nowdoc' => false,
16+
'cast_spaces' => false,
17+
'include' => false,
18+
'phpdoc_no_package' => false,
19+
'concat_space' => ['spacing' => 'one'],
20+
'ordered_imports' => true,
21+
'array_syntax' => ['syntax' => 'short'],
22+
])
23+
->setFinder($finder);
24+
25+
return $config;

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
6+
# faster builds on new travis setup not using sudo
7+
sudo: false
8+
9+
# cache vendor dirs
10+
cache:
11+
directories:
12+
- $HOME/.composer/cache
13+
14+
install:
15+
- travis_retry composer self-update && composer --version
16+
- travis_retry composer global require "fxp/composer-asset-plugin:^1.2.0"
17+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
18+
- travis_retry composer install --prefer-dist --no-interaction
19+
20+
script:
21+
- vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --dry-run --diff

MarkdownEditor.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace yii2mod\markdown;
4+
5+
use yii\helpers\Html;
6+
use yii\helpers\Inflector;
7+
use yii\helpers\Json;
8+
use yii\web\JsExpression;
9+
use yii\widgets\InputWidget;
10+
11+
/**
12+
* Class MarkdownEditor
13+
*
14+
* @package yii2mod\markdown
15+
*/
16+
class MarkdownEditor extends InputWidget
17+
{
18+
/**
19+
* @var array markdown options
20+
*/
21+
public $editorOptions = [];
22+
23+
/**
24+
* Renders the widget.
25+
*/
26+
public function run()
27+
{
28+
if ($this->hasModel()) {
29+
echo Html::activeTextarea($this->model, $this->attribute, $this->options);
30+
} else {
31+
echo Html::textarea($this->attribute, $this->value, $this->options);
32+
}
33+
34+
$this->registerAssets();
35+
}
36+
37+
/**
38+
* Register client assets
39+
*/
40+
protected function registerAssets()
41+
{
42+
$view = $this->getView();
43+
MarkdownEditorAsset::register($view);
44+
$varName = Inflector::variablize('editor_' . $this->id);
45+
$script = "var {$varName} = new SimpleMDE(" . $this->getEditorOptions() . ');';
46+
$view->registerJs($script);
47+
}
48+
49+
/**
50+
* Return editor options in json format
51+
*
52+
* @return string
53+
*/
54+
protected function getEditorOptions()
55+
{
56+
$this->editorOptions['element'] = new JsExpression('$("#' . $this->id . '")[0]');
57+
58+
return Json::encode($this->editorOptions);
59+
}
60+
}

MarkdownEditorAsset.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace yii2mod\markdown;
4+
5+
use yii\web\AssetBundle;
6+
7+
/**
8+
* Class MarkdownEditorAsset
9+
*
10+
* @package yii2mod\markdown
11+
*/
12+
class MarkdownEditorAsset extends AssetBundle
13+
{
14+
/**
15+
* @var string
16+
*/
17+
public $sourcePath = '@bower/simplemde';
18+
19+
/**
20+
* @var array
21+
*/
22+
public $css = [
23+
'dist/simplemde.min.css',
24+
];
25+
26+
/**
27+
* @var array
28+
*/
29+
public $js = [
30+
'dist/simplemde.min.js',
31+
];
32+
33+
/**
34+
* @var array
35+
*/
36+
public $depends = [
37+
'yii\web\YiiAsset',
38+
];
39+
}

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
1-
# yii2-markdown
2-
Markdown widget for Yii2
1+
Markdown Widget for Yii 2
2+
=========
3+
Widget based on [simplemde-markdown-editor](https://github.com/NextStepWebs/simplemde-markdown-editor)
4+
5+
Installation
6+
------------
7+
8+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
9+
10+
Either run
11+
12+
```
13+
php composer.phar require --prefer-dist yii2mod/yii2-markdown "*"
14+
```
15+
16+
or add
17+
18+
```json
19+
"yii2mod/yii2-markdown": "*"
20+
```
21+
22+
to the require section of your composer.json.
23+
24+
Usage
25+
------------
26+
Once the extension is installed, simply add widget to your page as follows:
27+
28+
1) Usage with ActiveForm and model
29+
```php
30+
<?php echo $form->field($model, 'content')->widget(\yii2mod\markdown\MarkdownEditor::class, [
31+
'editorOptions' => [
32+
'showIcons' => ["code", "table"],
33+
],
34+
]); ?>
35+
```
36+
2) Usage without ActiveForm and model
37+
```php
38+
<?php echo \yii2mod\markdown\MarkdownEditor::widget([
39+
'name' => 'markdown-editor',
40+
'editorOptions' => [
41+
'showIcons' => ["code", "table"],
42+
],
43+
]);
44+
?>
45+
```
46+
47+
Markdown Editor Options
48+
----------------
49+
You can find them on the [options page](https://github.com/NextStepWebs/simplemde-markdown-editor#configuration)

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "yii2mod/yii2-markdown",
3+
"description": "Markdown Widget for Yii2",
4+
"type": "yii2-extension",
5+
"keywords": [
6+
"yii2",
7+
"yii2 markdown editor",
8+
"yii2 markdown widget"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Igor Chepurnoy",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"require": {
18+
"yiisoft/yii2": "*",
19+
"bower-asset/simplemde": "*"
20+
},
21+
"require-dev": {
22+
"friendsofphp/php-cs-fixer": "~2.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"yii2mod\\markdown\\": ""
27+
}
28+
},
29+
"extra": {
30+
"asset-installer-paths": {
31+
"bower-asset-library": "vendor/bower"
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)