Skip to content

Commit

Permalink
Merge pull request #5 from erindru/master
Browse files Browse the repository at this point in the history
Enable the ability to set word wrapping in code when the editor is created
  • Loading branch information
UndefinedOffset committed Jan 12, 2014
2 parents 5350a73 + fcedd35 commit 0da156c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class MyPage extends Page {
public function getCMSFields() {
$fields=parent::getCMSFields();

$fields->addFieldToTab("Root.Main", new MarkdownEditor('MarkdownContent', 'Page Content (Markdown)'));
$editor = new MarkdownEditor('MarkdownContent', 'Page Content (Markdown)');
$editor->setRows(15); //optional, set number of rows in CMS
$editor->setWrapMode(true); //optional, turn on word wrapping
$fields->addFieldToTab("Root.Main", $editor);

return $fields;
}
Expand Down
14 changes: 13 additions & 1 deletion code/forms/MarkdownEditor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<?php
class MarkdownEditor extends TextareaField {
protected $rows=30;

protected $wrap_mode=false;

/**
* Sets the "Wrap Mode" on the ACE editor markdown field.
* @param boolean $mode True if word wrap should be enabled, false if not
*/
public function setWrapMode($mode = false) {
$this->wrap_mode=$mode;
return $this;
}

/**
* Returns the field holder used by templates
Expand Down Expand Up @@ -29,8 +40,9 @@ public function getAttributes() {
parent::getAttributes(),
array(
'style'=>'width: 97%; max-width: 100%; height: '.($this->rows * 16).'px; resize: none;', // prevents horizontal scrollbars
'wrap-mode'=>($this->wrap_mode) ? "true" : "false"
)
);
}
}
}
?>
7 changes: 3 additions & 4 deletions javascript/MarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
TextArea: null,
Div: null,
Editor: null,
Frame: null,
WrapMode: false,
Frame: null,
SoftTabs: true,
onmatch: function() {
$(this).setFrame({
Expand All @@ -18,7 +17,7 @@

var div=$('<div id="'+$(this).attr('ID')+'_Editor" class="markdowneditor_editor"/>').css('height', $(this).getFrame().height).css('width', $(this).getFrame().width).text($(this).val());
div.insertAfter($(this));
$(this).setDiv(div);
$(this).setDiv(div);

var editor=ace.edit(div.get(0));
editor.getSession().setMode('ace/mode/markdown');
Expand All @@ -31,7 +30,7 @@
var code=$(this).val();
$(this).setUseSoftTabs($(this).usesSoftTabs(code));
$(this).setTabSize($(this).getSoftTabs() ? $(this).guessTabSize(code):8);
$(this).setUseWrapMode($(this).getWrapMode());
$(this).setUseWrapMode($(this).attr("wrap-mode") == "true");
$(this).setupFormBindings();
$(this).setupHacks();
},
Expand Down

0 comments on commit 0da156c

Please sign in to comment.