-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormElement.php
More file actions
118 lines (113 loc) · 5.84 KB
/
FormElement.php
File metadata and controls
118 lines (113 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Dfe\Markdown;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute as A;
use Magento\Framework\Data\Form\Element\Textarea;
/**
* 2016-01-06
* Наш класс замещает класс @see \Magento\Catalog\Block\Adminhtml\Helper\Form\Wysiwyg
* но отныне не наследуется от него.
* @final Unable to use the PHP «final» keyword here because of the M2 code generation.
* @method string|null getClass()
* @method string|null getTitle()
*/
class FormElement extends Textarea {
/**
* 2016-01-06
* Нам нужно сделать примерно то же, что делает метод
* @see \Magento\Framework\Data\Form\Element\Textarea::getElementHtml()
* https://github.com/magento/magento2/blob/c58d2d/lib/internal/Magento/Framework/Data/Form/Element/Textarea.php#L81-L90
* но без вызова @see \Dfe\Markdown\FormElement::getAfterElementHtml()
* https://github.com/magento/magento2/blob/c58d2d/lib/internal/Magento/Framework/Data/Form/Element/Textarea.php#L88
* В то же время мы учитываем, как работает
* @see \Magento\Framework\Data\Form\Element\Editor::getElementHtml()
* https://github.com/magento/magento2/blob/c58d2d/lib/internal/Magento/Framework/Data/Form/Element/Editor.php#L103-L121
* @used-by \Dfe\Markdown\Plugin\Ui\Component\Form\Element\Wysiwyg::beforePrepare()
*/
function componentHtml():string {return
df_tag('textarea',
['class' => ['textarea', $this->getClass()], 'title' => $this->getTitle()] + df_fe_attrs($this)
,$this->getEscapedValue()
)
. (!$this->enabled() ? '' : $this->css())
;}
/**
* 2016-01-06
* @used-by self::getAfterElementHtml()
* @used-by \Dfe\Markdown\Plugin\Ui\Component\Form\Element\Wysiwyg::beforePrepare()
* @return array(mixed => mixed)
*/
function config():array {return dfc($this, function():array {return [
# 2015-11-04
# Нужно нам для идентификации страницы.
# Идентификация страницы нужна нам для правильного кэширования содержимого редактора в Local Storage.
'action' => df_action_name()
,'core' => df_wysiwyg_config()->getConfig()->getData()
# 2015-10-26
# На странице товарного раздела
# textarea имеет идентификатор «group4_description»,
# где «description» — это $this['html_id'], а «group4_» — это префикс.
# Для инициализации редактора нам нужен полный идентификатор,
# а для стилизации — наоборот, краткий
# (который, кстати, совпадает с кратким именем: значением атрибута «name»).
,'cssClass' => $this['name']
,'id' => $this->getHtmlId()
/**
* 2015-10-30
* By analogy with @see \Magento\Cms\Helper\Wysiwyg\Images::getImageHtmlDeclaration()
* https://github.com/magento/magento2/blob/2.0.0/app/code/Magento/Cms/Helper/Wysiwyg/Images.php#L170
* https://mage2.pro/t/153
*/
,'mediaBaseURL' => df_media_path2url()
# 2015-11-02
# Суффикс скрытого элемента формы,
# который будет содержать результат компиляции из Markdown в HTML.
,'suffixForCompiled' => self::HTML_COMPILED
];});}
/**
* 2016-01-06
* @used-by \Dfe\Markdown\Plugin\Ui\Component\Form\Element\Wysiwyg::beforePrepare()
* @used-by \Dfe\Markdown\FormElement::getAfterElementHtml()
*/
function enabled():bool {return dfc($this, function():bool {return df_wysiwyg_config()->isEnabled() && (
/** @var A|null $a */!($a = $this['entity_attribute']) || $a['is_wysiwyg_enabled']
);});}
/**
* 2015-10-24
* 2015-10-25
* Наш скрипт надо загружать именно через «text/x-magento-init».
* Сначала я ошибочно загружал его через df_page()->addPageAsset('Dfe_Markdown::main.js');
* однако это приводило к тому, что содержимое редактора не показывалось до клика по нему:
* http://stackoverflow.com/questions/8349571
* http://stackoverflow.com/questions/17086538
* 2015-10-26
* Результат метода надо обязательно кэшировать!
* https://github.com/magento/magento2/issues/2189
* https://mage2.pro/t/144
* «Performance bug: getAfterElementHtml() method implementation
* in \Magento\Framework\Data\Form\Element\AbstractElement descendants
* is sometimes computation expensive
* but called by the core multiple times for the same for element without caching»
* @override
* @see \Magento\Framework\Data\Form\Element\Textarea::getAfterElementHtml()
* @used-by \Magento\Framework\Data\Form\Element\Textarea::getElementHtml()
*/
function getAfterElementHtml():string {return dfc($this, function():string {return parent::getAfterElementHtml() . (
!$this->enabled() ? '' : $this->css() . df_js(__CLASS__, '', $this->config())
);});}
/**
* 2016-01-08
* Порядок загрузки лучше сделать именно таким, чтобы наши правила CSS загружались позже и переопределяли стандартные.
*/
private function css():string {return df_cc_n(df_link_inline(
df_asset_third_party('HighlightJs/github.css')
,'Dfe_Markdown::lib/SimpleMDE/main.css'
,'Dfe_Markdown::main.css'
));}
/**
* 2015-11-02 Суффикс скрытого элемента формы, который будет содержать результат компиляции из Markdown в HTML.
* @used-by self::config()
* @used-by \Dfe\Markdown\Observer\Catalog\ControllerAction\Predispatch::suffix()
* @used-by \Dfe\Markdown\Observer\Cms\Predispatch::execute()
*/
const HTML_COMPILED = '_html_compiled';
}