139 lines
4.2 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Backend\FormWidgets;
use Backend\Models\EditorPreferences;
2014-05-14 23:24:20 +10:00
use Backend\Classes\FormWidgetBase;
/**
* Code Editor
* Renders a code editor field.
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
class CodeEditor extends FormWidgetBase
{
/**
* {@inheritDoc}
*/
2015-02-28 12:43:53 +11:00
protected $defaultAlias = 'codeeditor';
2014-05-14 23:24:20 +10:00
/**
* @var string Code language to display (php, twig)
*/
public $language = 'php';
/**
* @var boolean Determines whether the gutter is visible.
2014-05-14 23:24:20 +10:00
*/
public $showGutter = true;
/**
* @var boolean Indicates whether the the word wrapping is enabled.
2014-05-14 23:24:20 +10:00
*/
2014-06-12 18:29:12 +10:00
public $wordWrap = true;
2014-05-14 23:24:20 +10:00
/**
* @var string Cold folding mode: manual, markbegin, markbeginend.
*/
public $codeFolding = 'manual';
/**
* @var boolean Automatically close tags and special characters,
* like quotation marks, parenthesis, or brackets.
*/
public $autoClosing = true;
/**
* @var boolean Indicates whether the the editor uses spaces for indentation.
*/
public $useSoftTabs = true;
/**
* @var boolean Sets the size of the indentation.
*/
public $tabSize = 4;
2014-05-14 23:24:20 +10:00
/**
* @var integer Sets the font size.
2014-05-14 23:24:20 +10:00
*/
public $fontSize = 12;
/**
* @var integer Sets the editor margin size.
2014-05-14 23:24:20 +10:00
*/
public $margin = 10;
/**
* @var $theme Ace Editor theme to use.
2014-05-14 23:24:20 +10:00
*/
public $theme = 'twilight';
/**
* {@inheritDoc}
*/
public function init()
{
// Load the editor system settings
$editorSettings = EditorPreferences::instance();
$this->fontSize = $this->getConfig('fontSize', $editorSettings->font_size);
2014-06-12 18:29:12 +10:00
$this->wordWrap = $this->getConfig('wordWrap', $editorSettings->word_wrap);
$this->codeFolding = $this->getConfig('codeFolding', $editorSettings->code_folding);
$this->autoClosing = $this->getConfig('autoClosing', $editorSettings->auto_closing);
$this->tabSize = $this->getConfig('tabSize', $editorSettings->tab_size);
2014-06-12 18:29:12 +10:00
$this->theme = $this->getConfig('theme', $editorSettings->theme);
$this->showInvisibles = $this->getConfig('showInvisibles', $editorSettings->show_invisibles);
$this->highlightActiveLine = $this->getConfig('highlightActiveLine', $editorSettings->highlight_active_line);
$this->useSoftTabs = $this->getConfig('useSoftTabs', !$editorSettings->use_hard_tabs);
2014-06-12 18:29:12 +10:00
$this->showGutter = $this->getConfig('showGutter', $editorSettings->show_gutter);
$this->language = $this->getConfig('language', 'php');
2014-05-14 23:24:20 +10:00
$this->margin = $this->getConfig('margin', 0);
}
/**
* {@inheritDoc}
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('codeeditor');
}
/**
* Prepares the widget data
2014-05-14 23:24:20 +10:00
*/
public function prepareVars()
{
$this->vars['fontSize'] = $this->fontSize;
2014-06-12 18:29:12 +10:00
$this->vars['wordWrap'] = $this->wordWrap;
$this->vars['codeFolding'] = $this->codeFolding;
$this->vars['autoClosing'] = $this->autoClosing;
$this->vars['tabSize'] = $this->tabSize;
2014-05-14 23:24:20 +10:00
$this->vars['theme'] = $this->theme;
2014-06-12 18:29:12 +10:00
$this->vars['showInvisibles'] = $this->showInvisibles;
$this->vars['highlightActiveLine'] = $this->highlightActiveLine;
$this->vars['useSoftTabs'] = $this->useSoftTabs;
$this->vars['showGutter'] = $this->showGutter;
$this->vars['language'] = $this->language;
$this->vars['margin'] = $this->margin;
$this->vars['stretch'] = $this->formField->stretch;
$this->vars['size'] = $this->formField->size;
2014-05-14 23:24:20 +10:00
$this->vars['name'] = $this->formField->getName();
// Double encode when escaping
$this->vars['value'] = htmlentities($this->getLoadValue(), ENT_QUOTES, 'UTF-8', true);
2014-05-14 23:24:20 +10:00
}
/**
* {@inheritDoc}
*/
public function loadAssets()
{
2014-05-24 16:57:38 +10:00
$this->addCss('css/codeeditor.css', 'core');
2015-02-18 20:54:36 +01:00
$this->addJs('vendor/emmet/emmet.js', 'core');
2014-05-24 16:57:38 +10:00
$this->addJs('vendor/ace/ace.js', 'core');
2015-02-18 20:54:36 +01:00
$this->addJs('vendor/ace/ext-emmet.js', 'core');
2014-05-24 16:57:38 +10:00
$this->addJs('js/codeeditor.js', 'core');
2014-05-14 23:24:20 +10:00
}
2014-10-10 23:50:05 +02:00
}