Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
Code tidy up and minor JavaScript bug fixes:
Browse files Browse the repository at this point in the history
* Trigger the preview to update when using shortcut buttons
* [#7]: Target the respective toolbar instead of using a class selector
* [#8]: Convert to start using PSR-2
  • Loading branch information
robbieaverill committed Apr 11, 2016
1 parent ef02b54 commit 653c997
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 233 deletions.
4 changes: 3 additions & 1 deletion _config.php
Expand Up @@ -4,8 +4,10 @@
$dir = basename(dirname(__FILE__));

if ($dir != 'markdowntextareafield') {
user_error(
user_error(
'Markdown: Directory name must be "markdowntextareafield" (currently "' . $dir . '")',
E_USER_ERROR
);
}

define('MARKDOWN_DIR', $dir);
152 changes: 79 additions & 73 deletions forms/MarkdownTextareaField.php
@@ -1,90 +1,96 @@
<?php
/**
* Configure the input field for markdown
*/
class MarkdownTextareaField extends TextareaField
{
/**
* Define the actions allowed on this field
* @var array
*/
private static $allowed_actions = array(
'preview',
'parse'
);

/**
* @var int Visible number of text lines.
* Default on TextareaField is too small
*/
protected $rows = 15;

class MarkdownTextareaField extends TextareaField {

/**
* Define the actions allowed on this field
* @var array
*/
private static $allowed_actions = array(
'preview',
'parse'
);

/**
* @var int Visible number of text lines.
* Default on TextareaField is too small
*/
protected $rows = 15;

/**
* Toggle rendering markdown with extra syntax enabled.
* @link http://michelf.ca/projects/php-markdown/extra
* @var boolean
*/
protected $enable_extra = false;
/**
* Toggle rendering markdown with extra syntax enabled.
* @link http://michelf.ca/projects/php-markdown/extra
* @var boolean
*/
protected $enable_extra = false;

/**
/**
* Returns the field holder used by templates
* @return string HTML to be used
*
* @param array $properties
* @return string HTML to be used
*/
public function FieldHolder($properties = array()) {
$this->extraClasses['stacked'] = 'stacked';
public function FieldHolder($properties = array())
{
$this->extraClasses['stacked'] = 'stacked';

Requirements::css('markdowntextareafield/templates/css/styles.css');
Requirements::css(MARKDOWN_DIR . '/templates/css/styles.css');

Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript('markdowntextareafield/thirdparty/textinputs_jquery.js');
Requirements::javascript('markdowntextareafield/templates/javascript/script.js');
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(MARKDOWN_DIR . '/thirdparty/textinputs_jquery.js');
Requirements::javascript(MARKDOWN_DIR . '/templates/javascript/script.js');

return parent::FieldHolder($properties);
return parent::FieldHolder($properties);
}

/**
* Turn on extra syntax support
* @return MarkdownTextareaField
*/
public function enableExtra() {
$this->enable_extra = true;
return $this;
}


/**
* Body for the preview iframe with just the typography styles included
* @return string html
*/
public function preview() {
Requirements::clear();
// Should contain text styles of the page by Silverstripe theme conventions.
Requirements::css('themes/' . Config::inst()->get('SSViewer', 'theme') . '/css/editor.css');
return $this->renderWith('PreviewFrame');
}
/**
* Turn on extra syntax support
* @return MarkdownTextareaField
*/
public function enableExtra()
{
$this->enable_extra = true;
return $this;
}

/**
* Parse markdown into html
* @return string html
*/
public function parse() {
$parser = new MarkdownParser($this->request['markdown']);
/**
* Body for the preview iframe with just the typography styles included
* @return string html
*/
public function preview()
{
Requirements::clear();
// Should contain text styles of the page by Silverstripe theme conventions.
Requirements::css('themes/' . Config::inst()->get('SSViewer', 'theme') . '/css/editor.css');
return $this->renderWith('PreviewFrame');
}

return ($this->enable_extra) ? $parser->parseExtra() : $parser->parse();
}
/**
* Parse markdown into html
* @return string html
*/
public function parse()
{
$parser = new MarkdownParser($this->request['markdown']);

/**
* Get buttons described in buttons.yml and wrap them in ViewableData
* @return ArrayList list of buttons and theyr configurations
*/
public function ToolbarButtons() {
$buttons = new ArrayList();
return ($this->enable_extra) ? $parser->parseExtra() : $parser->parse();
}

foreach($this->config()->get('buttons') as $button) {
$buttons->push(new ArrayData($button));
}
/**
* Get buttons described in buttons.yml and wrap them in ViewableData
* @return ArrayList list of buttons and theyr configurations
*/
public function ToolbarButtons()
{
$buttons = new ArrayList();

return $buttons;
}
foreach ($this->config()->get('buttons') as $button) {
$buttons->push(new ArrayData($button));
}

return $buttons;
}
}
89 changes: 46 additions & 43 deletions model/MarkdownText.php
Expand Up @@ -3,51 +3,54 @@
* Represents a large text field that contains HTML and Markdown content.
* Markdown gets processed automatically to HTML in templates
*/
class MarkdownText extends HTMLText {
class MarkdownText extends HTMLText
{
/**
* Define the casting for field names and types
* @var array
*/
public static $casting = array(
'MarkdownAsHTML' => 'MarkdownText',
'MarkdownExtraAsHTML' => 'MarkdownText',
);

/**
* Define the casting for field names and types
* @var array
*/
public static $casting = array(
'MarkdownAsHTML' => 'MarkdownText',
'MarkdownExtraAsHTML' => 'MarkdownText',
);
/**
* Returns Markdown content as HTML for templates
* @return string HTML
*/
public function forTemplate()
{
return $this->MarkdownAsHTML();
}

/**
* Returns Markdown content as HTML for templates
* @return string HTML
*/
public function forTemplate() {
return $this->MarkdownAsHTML();
}
/**
* Return Markdown content as HTML
* @return string HTML
*/
public function MarkdownAsHTML()
{
$parser = new MarkdownParser($this->value);
return $parser->parse();
}

/**
* Return Markdown content as HTML
* @return string HTML
*/
public function MarkdownAsHTML() {
$parser = new MarkdownParser($this->value);
return $parser->parse();
}

/**
* Return MarkdownExtra content as HTML
* @return string HTML
*/
public function MarkdownExtraAsHTML() {
$parser = new MarkdownParser($this->value);
return $parser->parseExtra();
}

/**
* Return an instance of the MarkdownTextareaField
* @param string $title
* @param array $params
* @return MarkdownTextareaField
*/
public function scaffoldFormField($title = null, $params = null) {
return new MarkdownTextareaField($this->name, $title);
}
/**
* Return MarkdownExtra content as HTML
* @return string HTML
*/
public function MarkdownExtraAsHTML()
{
$parser = new MarkdownParser($this->value);
return $parser->parseExtra();
}

/**
* Return an instance of the MarkdownTextareaField
* @param string $title
* @param array $params
* @return MarkdownTextareaField
*/
public function scaffoldFormField($title = null, $params = null)
{
return new MarkdownTextareaField($this->name, $title);
}
}
21 changes: 11 additions & 10 deletions model/MarkdownTextExtra.php
Expand Up @@ -2,25 +2,26 @@
/**
* Same as MarkdownText but Extra markup is enabled by default.
*/
class MarkdownTextExtra extends MarkdownText {

class MarkdownTextExtra extends MarkdownText
{
/**
* Return MarkdownExtra content as HTML for templates
* @return string HTML
*/
public function forTemplate() {
return $this->MarkdownExtraAsHTML();
}
public function forTemplate()
{
return $this->MarkdownExtraAsHTML();
}

/**
* Return an instance of the MarkdownTextareaField
* @param string $title
* @param array $params
* @return MarkdownTextareaField
*/
public function scaffoldFormField($title = null, $params = null) {
$field = new MarkdownTextareaField($this->name, $title);
return $field->enableExtra();
}

public function scaffoldFormField($title = null, $params = null)
{
$field = new MarkdownTextareaField($this->name, $title);
return $field->enableExtra();
}
}
19 changes: 10 additions & 9 deletions parsers/MarkdownParser.php
Expand Up @@ -2,22 +2,23 @@
/**
* Glue between Silverstripe TextParser interface and Michelf's markdown parser.
*/
class MarkdownParser extends TextParser {
class MarkdownParser extends TextParser
{
/**
* Parse Markdown content
* @return string
*/
function parse() {
return Michelf\Markdown::defaultTransform($this->content);
}
public function parse()
{
return Michelf\Markdown::defaultTransform($this->content);
}

/**
* Parse MarkdownExtra content
* @return string
*/
function parseExtra() {
return Michelf\MarkdownExtra::defaultTransform($this->content);
}

public function parseExtra()
{
return Michelf\MarkdownExtra::defaultTransform($this->content);
}
}
8 changes: 4 additions & 4 deletions templates/PreviewFrame.ss
@@ -1,10 +1,10 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Preview</title>
<meta charset="UTF-8">
<title>Preview</title>
</head>
<body>
</body>
</html>
</html>

0 comments on commit 653c997

Please sign in to comment.