Skip to content

Commit

Permalink
Merge pull request #15 from Firesphere/feature/settings-as-static-yml
Browse files Browse the repository at this point in the history
Feature/settings as static yml
  • Loading branch information
fonsekaean authored Nov 2, 2017
2 parents 6f9915f + 6846056 commit 6580109
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 120 deletions.
79 changes: 79 additions & 0 deletions _config/editorconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
Name: markdownconfig
---
SilverStripers\markdown\forms\MarkdownEditorConfig:
settings:
default:
-
name: heading
className: 'fa fa-header'
title: 'Heading HTML'
action: toggleHeadingSmaller
-
name: bold
className: 'fa fa-bold'
title: Bold
action: toggleBold
-
name: italic
className: 'fa fa-italic'
title: Italic
action: toggleItalic
-
name: strikethrough
className: 'fa fa-strikethrough'
title: 'Strike Through'
action: toggleStrikethrough
- '|'
-
name: quote
className: 'fa fa-quote-left'
title: Quote
action: toggleBlockquote
-
name: unordered-list
action: toggleUnorderedList
className: 'fa fa-list-ul'
title: 'Generic List'
-
name: ordered-list
action: toggleOrderedList
className: 'fa fa-list-ol'
title: 'Ordered List'
-
name: link
action: drawLink
className: 'fa fa-link'
title: 'Create Link'
-
name: embed
action: ssEmbed
className: 'fa fa-play'
title: 'Embed Media'
-
name: image
action: ssImage
className: 'fa fa-picture-o'
title: 'Insert Image'
- '|'
-
name: preview
action: togglePreview
className: 'fa fa-eye no-disable'
title: 'Toggle Preview'
-
name: side-by-side
action: toggleSideBySide
className: 'fa fa-columns no-disable no-mobile'
title: 'Toggle Side by Side'
-
name: fullscreen
action: toggleFullScreen
className: 'fa fa-arrows-alt no-disable no-mobile'
title: 'Toggle Fullscreen'
- '|'
-
name: guide
action: 'https://simplemde.com/markdown-guide'
className: 'fa fa-question-circle'
title: Help
2 changes: 1 addition & 1 deletion src/extensions/MarkdownSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MarkdownSiteTreeExtension extends DataExtension
public function updateCMSFields(FieldList $fields)
{
$injectorConfig = Config::inst()->get(Injector::class, 'HTMLText');
if ($injectorConfig && isset($injectorConfig['class']) && $injectorConfig['class'] == MarkdownText::class) {
if ($injectorConfig && isset($injectorConfig['class']) && $injectorConfig['class'] === MarkdownText::class) {
$fields->replaceField('Content', MarkdownEditorField::create('Content'));
}
}
Expand Down
199 changes: 82 additions & 117 deletions src/forms/MarkdownEditorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace SilverStripers\markdown\forms;

use LogicException;
use SilverStripe\Core\Config\Config_ForClass;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Convert;
Expand All @@ -21,99 +22,30 @@ class MarkdownEditorConfig
use Injectable;


/**
* Set of Editor configs
*
* @var array|MarkdownEditorConfig[]
*/
protected static $configs = [];

/**
* Current setting, if overridden, otherwise, default will be used
* @var string
*/
protected static $current;

/**
* Default identifier for config and settings
* @var string
*/
private static $default_config = 'default';

protected $settings = [
[
'name' => 'heading',
'className' => 'fa fa-header',
'title' => 'Heading HTML',
'action' => 'toggleHeadingSmaller'
],
[
'name' => 'bold',
'className' => 'fa fa-bold',
'title' => 'Bold',
'action' => 'toggleBold'
],
[
'name' => 'italic',
'className' => 'fa fa-italic',
'title' => 'Italic',
'action' => 'toggleItalic'
],
[
'name' => 'strikethrough',
'className' => 'fa fa-strikethrough',
'title' => 'Strike Through',
'action' => 'toggleStrikethrough'
],
'|',
[
'name' => 'quote',
'className' => 'fa fa-quote-left',
'title' => 'Quote',
'action' => 'toggleBlockquote'
],
[
'name' => 'unordered-list',
'action' => 'toggleUnorderedList',
'className' => 'fa fa-list-ul',
'title' => 'Generic List'
],
[
'name' => 'ordered-list',
'action' => 'toggleOrderedList',
'className' => 'fa fa-list-ol',
'title' => 'Ordered List'
],
[
'name' => 'link',
'action' => 'drawLink',
'className' => 'fa fa-link',
'title' => 'Create Link'
],
[
'name' => 'embed',
'action' => 'ssEmbed',
'className' => 'fa fa-play',
'title' => 'Embed Media'
],
[
'name' => 'image',
'action' => 'ssImage',
'className' => 'fa fa-picture-o',
'title' => 'Insert Image'
],
'|',
[
'name' => 'preview',
'action' => 'togglePreview',
'className' => 'fa fa-eye no-disable',
'title' => 'Toggle Preview'
],
[
'name' => 'side-by-side',
'action' => 'toggleSideBySide',
'className' => 'fa fa-columns no-disable no-mobile',
'title' => 'Toggle Side by Side'
],
[
'name' => 'fullscreen',
'action' => 'toggleFullScreen',
'className' => 'fa fa-arrows-alt no-disable no-mobile',
'title' => 'Toggle Fullscreen'
],
'|',
[
'name' => 'guide',
'action' => 'https://simplemde.com/markdown-guide',
'className' => 'fa fa-question-circle',
'title' => 'Help'
]
];
/**
* Settings for the MarkdownField
* @var array
*/
protected static $settings;

/**
* @param null $identifier
Expand All @@ -133,27 +65,34 @@ public static function get($identifier = null)
}

/**
* @return mixed
* @return string
*/
public static function get_active_identifier()
{
return static::config()->get('current') ?: static::config()->get('default_config');
}

/**
* @return mixed
* @return MarkdownEditorConfig
*/
public static function get_active()
{
return self::get(static::get_active_identifier());
$identifier = static::get_active_identifier();

return self::get($identifier);
}

/**
* @param MarkdownEditorConfig $config
* @param string $config
* @return Config_ForClass
* @throws LogicException
*/
public static function set_active(MarkdownEditorConfig $config)
public static function set_active($config)
{
if (!is_string($config)) {
throw new LogicException('String expected for config name');
}

return static::config()->update('current', $config);
}

Expand All @@ -176,38 +115,64 @@ public static function set_config($identifier, MarkdownEditorConfig $config = nu
/**
* @return array
*/
public function getConfig()
public function getSettings()
{
return $this->settings;
$settings = static::config()->get('settings');
$config = static::get_active_identifier();
if (isset($settings[$config])) {
return $settings[$config];
}
// Config not found, return default
$default = static::config()->get('default_config');

return $settings[$default];
}

/**
* @return MarkdownEditorConfig
*/
public function addSeparator()
{
array_push($this->settings, '|');
return $this;
}

/**
* @param $button
* @return MarkdownEditorConfig
*/
public function addButton($button)
{
array_push($this->settings, $button);
return $this;
}

/**
/**
* @return MarkdownEditorConfig
*/
public function addSeparator()
{
$settings = static::config()->get('settings');
$active = static::get_active_identifier();
if (is_array($settings[$active])) {
$settings[$active][] = '|';
} else {
$default = static::config()->get('default_config');
$settings[$default][] = '|';
}
static::config()->update('settings', $settings);

return $this;
}

/**
* @param $button
* @return MarkdownEditorConfig
*/
public function addButton($button)
{
$settings = static::config()->get('settings');
$active = static::get_active_identifier();
if (is_array($settings[$active])) {
$settings[$active][] = $button;
} else {
$default = static::config()->get('default_config');
$settings[$default][] = $button;
}
static::config()->update('settings', $settings);

return $this;
}

/**
* @return array
*/
public function getAttributes()
{
return [
'data-editor' => 'markDown',
'data-config' => Convert::array2json($this->getConfig()),
'data-config' => Convert::array2json($this->getSettings()),
];
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/forms/MarkdownEditorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ public function getEditorConfig()
*/
public function getAttributes()
{
$attributes = [];
$attributes = parent::getAttributes();
return array_merge(
$attributes,
parent::getAttributes(),
$this->getEditorConfig()->getAttributes()
);
}
Expand Down

0 comments on commit 6580109

Please sign in to comment.