Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
EditStyleAction.php
1<?php
2
4
6use \Bitrix\Main\Web\DOM;
7
9{
10 protected const JS_COMMAND = 'updateStyle';
11
12 public function execute(bool $undo = true): bool
13 {
14 $block = new Block((int)$this->params['block']);
15 $selector = $this->params['selector'];
16 $position = $this->params['position'];
17 $isWrapper = $this->params['isWrapper'];
18 $value = $undo ? $this->params['valueBefore'] : $this->params['valueAfter'];
19
20 if ($selector)
21 {
22 if ($position >= 0 && !$isWrapper)
23 {
24 $selector = $selector . '@' . $position;
25 }
26 $data = [
27 $selector => [
28 'classList' => explode(' ', $value['className']),
29 'style' => $value['style'],
30 'affect' => $this->params['affect'],
31 ],
32 ];
33
34 return $block->setClasses($data) && $block->save();
35 }
36
37 return false;
38 }
39
40 public static function enrichParams(array $params): array
41 {
45 $block = $params['block'];
46
47 $getValue = static function($content) {
48 $doc = new DOM\Document();
49 $doc->loadHTML($content);
50 $children = $doc->getChildNodesArray();
51 $node = array_pop($children);
52
53 return $node
54 ? [
55 'className' => $node->getClassName() ?: '',
56 'style' => DOM\StyleInliner::getStyle($node, true),
57 'styleString' => $node->getAttribute('style') ?: '',
58 ]
59 : [];
60 };
61
62 return [
63 'block' => $block->getId(),
64 'selector' => $params['selector'] ?: '',
65 'isWrapper' => $params['isWrapper'] ?? false,
66 'position' => $params['position'] ?? -1,
67 'affect' => $params['affect'] ?: [],
68 'lid' => $block->getLandingId(),
69 'valueBefore' => $getValue($params['contentBefore']),
70 'valueAfter' => $getValue($params['contentAfter']),
71 ];
72 }
73
74 public function isNeedPush(): bool
75 {
76 // todo: move to another actions
77 return
78 parent::isNeedPush()
79 && $this->params['valueBefore'] !== $this->params['valueAfter'];
80 }
81
86 public function getJsCommand(bool $undo = true): array
87 {
88 $params = parent::getJsCommand($undo);
89
90 $params['params']['value'] =
91 $undo
92 ? $params['params']['valueBefore']
93 : $params['params']['valueAfter'];
94 $params['params']['value']['style'] = $params['params']['value']['styleString'];
95
96 unset(
97 $params['params']['valueAfter'],
98 $params['params']['valueBefore'],
99 $params['params']['value']['styleString'],
100 );
101
102 return $params;
103 }
104
111 public static function compareParams(array $oldParams, array $newParams): bool
112 {
113 unset($oldParams['valueBefore'], $newParams['valueBefore']);
114
115 return $oldParams === $newParams;
116 }
117}
static compareParams(array $oldParams, array $newParams)