2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Backend\FormWidgets;
|
|
|
|
|
2016-05-27 07:46:50 +10:00
|
|
|
use Backend\Models\Preference as BackendPreference;
|
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
|
|
|
|
{
|
2015-02-28 14:43:34 +11:00
|
|
|
//
|
|
|
|
// Configurable properties
|
|
|
|
//
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Code language to display (php, twig)
|
|
|
|
*/
|
|
|
|
public $language = 'php';
|
|
|
|
|
|
|
|
/**
|
2015-02-14 19:50:12 +11:00
|
|
|
* @var boolean Determines whether the gutter is visible.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public $showGutter = true;
|
|
|
|
|
|
|
|
/**
|
2015-02-14 19:50:12 +11:00
|
|
|
* @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
|
|
|
|
2014-06-10 19:30:06 +10:00
|
|
|
/**
|
2015-02-14 19:50:12 +11: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.
|
2014-06-10 19:30:06 +10:00
|
|
|
*/
|
|
|
|
public $useSoftTabs = true;
|
|
|
|
|
|
|
|
/**
|
2015-02-14 19:50:12 +11:00
|
|
|
* @var boolean Sets the size of the indentation.
|
2014-06-10 19:30:06 +10:00
|
|
|
*/
|
|
|
|
public $tabSize = 4;
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
2015-02-14 19:50:12 +11:00
|
|
|
* @var integer Sets the font size.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public $fontSize = 12;
|
|
|
|
|
|
|
|
/**
|
2015-02-14 19:50:12 +11:00
|
|
|
* @var integer Sets the editor margin size.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2015-02-28 14:43:34 +11:00
|
|
|
public $margin = 0;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
2016-05-29 08:54:22 +10:00
|
|
|
* @var string Ace Editor theme to use.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public $theme = 'twilight';
|
|
|
|
|
2016-05-29 08:54:22 +10:00
|
|
|
/**
|
|
|
|
* @var bool Show invisible characters.
|
|
|
|
*/
|
|
|
|
public $showInvisibles = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool Highlight the active line.
|
|
|
|
*/
|
|
|
|
public $highlightActiveLine = true;
|
|
|
|
|
2015-08-25 21:34:21 -07:00
|
|
|
/**
|
|
|
|
* @var boolean If true, the editor is set to read-only mode
|
|
|
|
*/
|
|
|
|
public $readOnly = false;
|
|
|
|
|
2016-04-19 08:36:45 +02:00
|
|
|
/**
|
2016-04-30 04:53:33 +10:00
|
|
|
* @var string Autocomplete mode: manual, basic, live.
|
2016-04-23 05:31:05 +10:00
|
|
|
*/
|
2016-04-30 04:53:33 +10:00
|
|
|
public $autocompletion = 'manual';
|
2016-04-23 05:31:05 +10:00
|
|
|
|
2016-04-19 08:36:45 +02:00
|
|
|
/**
|
|
|
|
* @var boolean If true, the editor activate use Snippets
|
2016-04-23 05:31:05 +10:00
|
|
|
*/
|
|
|
|
public $enableSnippets = true;
|
|
|
|
|
2016-04-19 08:36:45 +02:00
|
|
|
/**
|
|
|
|
* @var boolean If true, the editor show Indent Guides
|
2016-04-23 05:31:05 +10:00
|
|
|
*/
|
|
|
|
public $displayIndentGuides = true;
|
|
|
|
|
2016-04-19 08:36:45 +02:00
|
|
|
/**
|
|
|
|
* @var boolean If true, the editor show Print Margin
|
2016-04-23 05:31:05 +10:00
|
|
|
*/
|
|
|
|
public $showPrintMargin = false;
|
|
|
|
|
2015-02-28 14:43:34 +11:00
|
|
|
//
|
|
|
|
// Object properties
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-02-28 14:43:34 +11:00
|
|
|
*/
|
|
|
|
protected $defaultAlias = 'codeeditor';
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
2015-02-28 14:43:34 +11:00
|
|
|
$this->applyEditorPreferences();
|
|
|
|
|
2017-03-14 07:38:02 +11:00
|
|
|
if ($this->formField->disabled) {
|
|
|
|
$this->readOnly = true;
|
|
|
|
}
|
|
|
|
|
2015-02-28 14:43:34 +11:00
|
|
|
$this->fillFromConfig([
|
|
|
|
'language',
|
|
|
|
'showGutter',
|
|
|
|
'wordWrap',
|
|
|
|
'codeFolding',
|
|
|
|
'autoClosing',
|
|
|
|
'useSoftTabs',
|
|
|
|
'tabSize',
|
|
|
|
'fontSize',
|
|
|
|
'margin',
|
|
|
|
'theme',
|
2016-05-29 08:54:22 +10:00
|
|
|
'showInvisibles',
|
|
|
|
'highlightActiveLine',
|
2016-04-19 08:36:45 +02:00
|
|
|
'readOnly',
|
2016-04-30 04:53:33 +10:00
|
|
|
'autocompletion',
|
2016-04-23 05:31:05 +10:00
|
|
|
'enableSnippets',
|
|
|
|
'displayIndentGuides',
|
|
|
|
'showPrintMargin'
|
2015-02-28 14:43:34 +11:00
|
|
|
]);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$this->prepareVars();
|
|
|
|
return $this->makePartial('codeeditor');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-16 20:47:23 -07:00
|
|
|
* 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;
|
2015-02-14 19:50:12 +11:00
|
|
|
$this->vars['autoClosing'] = $this->autoClosing;
|
2014-06-10 19:30:06 +10:00
|
|
|
$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;
|
2015-08-25 21:34:21 -07:00
|
|
|
$this->vars['readOnly'] = $this->readOnly;
|
2016-04-30 04:53:33 +10:00
|
|
|
$this->vars['autocompletion'] = $this->autocompletion;
|
2016-04-23 05:31:05 +10:00
|
|
|
$this->vars['enableSnippets'] = $this->enableSnippets;
|
2016-04-19 08:36:45 +02:00
|
|
|
$this->vars['displayIndentGuides'] = $this->displayIndentGuides;
|
|
|
|
$this->vars['showPrintMargin'] = $this->showPrintMargin;
|
2015-02-28 10:38:04 +11:00
|
|
|
|
|
|
|
// Double encode when escaping
|
|
|
|
$this->vars['value'] = htmlentities($this->getLoadValue(), ENT_QUOTES, 'UTF-8', true);
|
2016-11-28 07:50:06 +11:00
|
|
|
$this->vars['name'] = $this->getFieldName();
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2015-08-04 19:32:51 +10:00
|
|
|
protected function loadAssets()
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-05-24 16:57:38 +10:00
|
|
|
$this->addCss('css/codeeditor.css', 'core');
|
2015-06-24 18:26:45 +10:00
|
|
|
$this->addJs('js/build-min.js', 'core');
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
2015-02-28 14:43:34 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks at the user preferences and overrides any set values.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function applyEditorPreferences()
|
|
|
|
{
|
|
|
|
// Load the editor system settings
|
2016-05-27 07:46:50 +10:00
|
|
|
$preferences = BackendPreference::instance();
|
2016-04-23 05:31:05 +10:00
|
|
|
|
|
|
|
$this->fontSize = $preferences->editor_font_size;
|
|
|
|
$this->wordWrap = $preferences->editor_word_wrap;
|
|
|
|
$this->codeFolding = $preferences->editor_code_folding;
|
|
|
|
$this->autoClosing = $preferences->editor_auto_closing;
|
|
|
|
$this->tabSize = $preferences->editor_tab_size;
|
|
|
|
$this->theme = $preferences->editor_theme;
|
|
|
|
$this->showInvisibles = $preferences->editor_show_invisibles;
|
|
|
|
$this->highlightActiveLine = $preferences->editor_highlight_active_line;
|
|
|
|
$this->useSoftTabs = !$preferences->editor_use_hard_tabs;
|
|
|
|
$this->showGutter = $preferences->editor_show_gutter;
|
2016-04-30 04:53:33 +10:00
|
|
|
$this->autocompletion = $preferences->editor_autocompletion;
|
2016-04-23 05:31:05 +10:00
|
|
|
$this->enableSnippets = $preferences->editor_enable_snippets;
|
|
|
|
$this->displayIndentGuides = $preferences->editor_display_indent_guides;
|
|
|
|
$this->showPrintMargin = $preferences->editor_show_print_margin;
|
2015-02-28 14:43:34 +11:00
|
|
|
}
|
2014-10-10 23:50:05 +02:00
|
|
|
}
|